blob: 8f5fbe656e13bf03d8cfc739a07efdc2c4fb0b89 [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;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000188
189private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000190 /// ProtocolPtrTy - LLVM type for external protocol handles
191 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000192 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000193
194public:
195 llvm::Type *getExternalProtocolPtrTy() {
196 if (!ExternalProtocolPtrTy) {
197 // FIXME: It would be nice to unify this with the opaque type, so that the
198 // IR comes out a bit cleaner.
199 CodeGen::CodeGenTypes &Types = CGM.getTypes();
200 ASTContext &Ctx = CGM.getContext();
201 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
202 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
203 }
204
205 return ExternalProtocolPtrTy;
206 }
207
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000208 // SuperCTy - clang type for struct objc_super.
209 QualType SuperCTy;
210 // SuperPtrCTy - clang type for struct objc_super *.
211 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000213 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000214 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000215 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000216 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000217
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000218 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
219 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000220 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000221
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222 /// PropertyListTy - LLVM type for struct objc_property_list
223 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000224 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000225 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000226 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000228 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000230
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000231 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000232 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000233 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000234 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000235
Chris Lattner72db6c32009-04-22 02:44:54 +0000236 llvm::Constant *getGetPropertyFn() {
237 CodeGen::CodeGenTypes &Types = CGM.getTypes();
238 ASTContext &Ctx = CGM.getContext();
239 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000240 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000241 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
242 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000243 Params.push_back(IdType);
244 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000245 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000246 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000247 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000248 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000249 FunctionType::ExtInfo()),
250 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000251 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
252 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000253
Chris Lattner72db6c32009-04-22 02:44:54 +0000254 llvm::Constant *getSetPropertyFn() {
255 CodeGen::CodeGenTypes &Types = CGM.getTypes();
256 ASTContext &Ctx = CGM.getContext();
257 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000259 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
260 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000263 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000264 Params.push_back(IdType);
265 Params.push_back(Ctx.BoolTy);
266 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000267 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000268 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000269 FunctionType::ExtInfo()),
270 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000271 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
272 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000273
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000274
275 llvm::Constant *getCopyStructFn() {
276 CodeGen::CodeGenTypes &Types = CGM.getTypes();
277 ASTContext &Ctx = CGM.getContext();
278 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000280 Params.push_back(Ctx.VoidPtrTy);
281 Params.push_back(Ctx.VoidPtrTy);
282 Params.push_back(Ctx.LongTy);
283 Params.push_back(Ctx.BoolTy);
284 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000285 llvm::FunctionType *FTy =
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000286 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
287 FunctionType::ExtInfo()),
288 false);
289 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
290 }
291
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000292 /// This routine declares and returns address of:
293 /// void objc_copyCppObjectAtomic(
294 /// void *dest, const void *src,
295 /// void (*copyHelper) (void *dest, const void *source));
296 llvm::Constant *getCppAtomicObjectFunction() {
297 CodeGen::CodeGenTypes &Types = CGM.getTypes();
298 ASTContext &Ctx = CGM.getContext();
299 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
300 SmallVector<CanQualType,3> Params;
301 Params.push_back(Ctx.VoidPtrTy);
302 Params.push_back(Ctx.VoidPtrTy);
303 Params.push_back(Ctx.VoidPtrTy);
304 llvm::FunctionType *FTy =
305 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
306 FunctionType::ExtInfo()),
307 false);
308 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
309 }
310
Chris Lattner72db6c32009-04-22 02:44:54 +0000311 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000312 CodeGen::CodeGenTypes &Types = CGM.getTypes();
313 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000314 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000315 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000316 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000318 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000319 FunctionType::ExtInfo()),
320 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
322 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000323
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000324 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000325 llvm::Constant *getGcReadWeakFn() {
326 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000327 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000328 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000329 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000331 }
332
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000333 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000334 llvm::Constant *getGcAssignWeakFn() {
335 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000336 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000337 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000338 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
340 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000341
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000342 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000343 llvm::Constant *getGcAssignGlobalFn() {
344 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000345 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000346 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000347 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000348 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
349 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000350
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000351 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
352 llvm::Constant *getGcAssignThreadLocalFn() {
353 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000354 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000355 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000356 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000357 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
358 }
359
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000360 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000361 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000362 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000363 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
364 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000365 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000366 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
368 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000369
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000370 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
371 llvm::Constant *GcMemmoveCollectableFn() {
372 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000373 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000374 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000375 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000377
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000378 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000379 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000380 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000381 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000382 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000383 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000384 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
385 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000386
387 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000388 llvm::Constant *getExceptionThrowFn() {
389 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000390 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000391 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000392 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000395
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000396 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
397 llvm::Constant *getExceptionRethrowFn() {
398 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000399 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
401 }
402
Daniel Dunbar1c566672009-02-24 01:43:46 +0000403 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000404 llvm::Constant *getSyncEnterFn() {
405 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000406 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000407 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000408 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000411
Daniel Dunbar1c566672009-02-24 01:43:46 +0000412 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000413 llvm::Constant *getSyncExitFn() {
414 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000415 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000417 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
419 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000420
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
423 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000424
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
427 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000428
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000429 llvm::Constant *getSendStretFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
431 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000432
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000433 llvm::Constant *getSendStretFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
435 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000436
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000437 llvm::Constant *getSendFpretFn(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
439 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000440
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000441 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
443 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000444
Anders Carlssoneea64802011-10-31 16:27:11 +0000445 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
446 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
447 }
448
449 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
450 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
451 }
452
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000453 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
454 ~ObjCCommonTypesHelper(){}
455};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000456
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000457/// ObjCTypesHelper - Helper class that encapsulates lazy
458/// construction of varies types used during ObjC generation.
459class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000460public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000461 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000462 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000463 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000464 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000465 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000466 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000467
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000468 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000469 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000471 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000472 /// ProtocolExtensionTy - LLVM type for struct
473 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000474 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000475 /// ProtocolExtensionTy - LLVM type for struct
476 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000477 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000478 /// MethodDescriptionTy - LLVM type for struct
479 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000480 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000481 /// MethodDescriptionListTy - LLVM type for struct
482 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000483 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000484 /// MethodDescriptionListPtrTy - LLVM type for struct
485 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000486 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000487 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000488 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000489 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000490 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000491 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000499 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000500 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000501 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000503 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000504 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000505 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000507 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000508 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000509 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000510 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000511
Anders Carlsson124526b2008-09-09 10:10:21 +0000512 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000513 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000514
Anders Carlsson124526b2008-09-09 10:10:21 +0000515 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000516 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000517 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000518 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000519 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000520 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000521 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000522
523 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000524 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000525 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000526 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000527 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000528 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000529 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000530
531 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000532 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000533 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000534 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000535 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000536 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000537 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000538
Anders Carlsson124526b2008-09-09 10:10:21 +0000539 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000540 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000541 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000542 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000543 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000544 "objc_exception_match");
545
Chris Lattner34b02a12009-04-22 02:26:14 +0000546 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000547
Anders Carlsson124526b2008-09-09 10:10:21 +0000548 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000549 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000550 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000551 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000552 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
553 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000554 "_setjmp",
555 llvm::Attribute::ReturnsTwice);
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000557
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000558public:
559 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000560 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000561};
562
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000563/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000564/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000565class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000566public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000568 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000569 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000571 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000572 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000573
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000574 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000575 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576
Daniel Dunbar948e2582009-02-15 07:36:20 +0000577 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000578 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000579
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000580 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000581 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000582
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000583 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000584 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000586 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000587 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000588
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000589 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000590 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000591
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000592 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000593 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000594
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000595 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000596 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000597
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000598 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000599 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000601 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000604 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000605 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000607 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000608 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000610 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000612 // MessageRefTy - LLVM for:
613 // struct _message_ref_t {
614 // IMP messenger;
615 // SEL name;
616 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000617 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000618 // MessageRefCTy - clang type for struct _message_ref_t
619 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000621 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000622 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000623 // MessageRefCPtrTy - clang type for struct _message_ref_t*
624 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Fariborz Jahanianef163782009-02-05 01:13:09 +0000626 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000627 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000629 // SuperMessageRefTy - LLVM for:
630 // struct _super_message_ref_t {
631 // SUPER_IMP messenger;
632 // SEL name;
633 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000634 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000635
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000636 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000637 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000638
Chris Lattner1c02f862009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendFixupFn() {
640 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000641 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000642 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000643 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000644 "objc_msgSend_fixup");
645 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Chris Lattner1c02f862009-04-22 02:53:24 +0000647 llvm::Constant *getMessageSendFpretFixupFn() {
648 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000649 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000650 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000651 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000652 "objc_msgSend_fpret_fixup");
653 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000654
Chris Lattner1c02f862009-04-22 02:53:24 +0000655 llvm::Constant *getMessageSendStretFixupFn() {
656 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000657 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000658 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000659 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000660 "objc_msgSend_stret_fixup");
661 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Chris Lattner1c02f862009-04-22 02:53:24 +0000663 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000665 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000666 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000667 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000668 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000669 "objc_msgSendSuper2_fixup");
670 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000671
Chris Lattner1c02f862009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000673 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000674 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000675 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000676 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000677 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000678 "objc_msgSendSuper2_stret_fixup");
679 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000680
Chris Lattner8a569112009-04-22 02:15:23 +0000681 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000682 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000683 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Chris Lattner8a569112009-04-22 02:15:23 +0000685 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000686
Chris Lattner8a569112009-04-22 02:15:23 +0000687 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000688 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000689 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000690 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000691 "objc_begin_catch");
692 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000693
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000694 llvm::StructType *EHTypeTy;
695 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000696
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000697 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
698 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000699};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000700
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000701class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000702public:
703 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000704 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000705 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000706 unsigned ivar_bytepos;
707 unsigned ivar_size;
708 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000709 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000710
711 // Allow sorting based on byte pos.
712 bool operator<(const GC_IVAR &b) const {
713 return ivar_bytepos < b.ivar_bytepos;
714 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000715 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000716
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000717 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000718 public:
719 unsigned skip;
720 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000722 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000723 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000724
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000725protected:
726 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000727 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000728 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000729 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000730
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000731 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000732 SmallVector<GC_IVAR, 16> SkipIvars;
733 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000734
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000735 /// LazySymbols - Symbols to generate a lazy reference for. See
736 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000737 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000738
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000739 /// DefinedSymbols - External symbols which are defined by this
740 /// module. The symbols in this list and LazySymbols are used to add
741 /// special linker symbols which ensure that Objective-C modules are
742 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000743 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000744
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000745 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000746 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000747
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000748 /// MethodVarNames - uniqued method variable names.
749 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000750
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000751 /// DefinedCategoryNames - list of category names in form Class_Category.
752 llvm::SetVector<std::string> DefinedCategoryNames;
753
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000754 /// MethodVarTypes - uniqued method type signatures. We have to use
755 /// a StringMap here because have no other unique reference.
756 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000758 /// MethodDefinitions - map of methods which have been defined in
759 /// this translation unit.
760 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000762 /// PropertyNames - uniqued method variable names.
763 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000765 /// ClassReferences - uniqued class references.
766 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000767
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000768 /// SelectorReferences - uniqued selector references.
769 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000771 /// Protocols - Protocols for which an objc_protocol structure has
772 /// been emitted. Forward declarations are handled by creating an
773 /// empty structure whose initializer is filled in when/if defined.
774 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000775
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000776 /// DefinedProtocols - Protocols which have actually been
777 /// defined. We should not need this, see FIXME in GenerateProtocol.
778 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000780 /// DefinedClasses - List of defined classes.
781 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000782
783 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
784 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000785
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000786 /// DefinedCategories - List of defined categories.
787 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000788
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000789 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
790 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000792 /// GetNameForMethod - Return a name for the given method.
793 /// \param[out] NameOut - The return value.
794 void GetNameForMethod(const ObjCMethodDecl *OMD,
795 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000796 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000797
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000798 /// GetMethodVarName - Return a unique constant for the given
799 /// selector's name. The return value has type char *.
800 llvm::Constant *GetMethodVarName(Selector Sel);
801 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000803 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000804 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000805
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000806 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000807 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
808 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000809 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000810
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000811 /// GetPropertyName - Return a unique constant for the given
812 /// name. The return value has type char *.
813 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000814
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000815 // FIXME: This can be dropped once string functions are unified.
816 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
817 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000818
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000819 /// GetClassName - Return a unique constant for the given selector's
820 /// name. The return value has type char *.
821 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000822
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000823 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
824
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000825 /// BuildIvarLayout - Builds ivar layout bitmap for the class
826 /// implementation for the __strong or __weak case.
827 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000828 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
829 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000830
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000831 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000833 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000834 unsigned int BytePos, bool ForStrongLayout,
835 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000836 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000837 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000838 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000839 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000840 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000841 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000842
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000843 /// GetIvarLayoutName - Returns a unique constant for the given
844 /// ivar layout bitmap.
845 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
846 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000848 /// EmitPropertyList - Emit the given property list. The return
849 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000850 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000852 const ObjCContainerDecl *OCD,
853 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Bob Wilsondc8dab62011-11-30 01:57:58 +0000855 /// EmitProtocolMethodTypes - Generate the array of extended method type
856 /// strings. The return value has type Int8PtrPtrTy.
857 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
858 const ConstantVector &MethodTypes,
859 const ObjCCommonTypesHelper &ObjCTypes);
860
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000861 /// PushProtocolProperties - Push protocol's property on the input stack.
862 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
863 std::vector<llvm::Constant*> &Properties,
864 const Decl *Container,
865 const ObjCProtocolDecl *PROTO,
866 const ObjCCommonTypesHelper &ObjCTypes);
867
Fariborz Jahanianda320092009-01-29 19:24:30 +0000868 /// GetProtocolRef - Return a reference to the internal protocol
869 /// description, creating an empty one if it has not been
870 /// defined. The return value has type ProtocolPtrTy.
871 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000872
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000873 /// CreateMetadataVar - Create a global variable with internal
874 /// linkage for use by the Objective-C runtime.
875 ///
876 /// This is a convenience wrapper which not only creates the
877 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000878 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000879 ///
880 /// \param Name - The variable name.
881 /// \param Init - The variable initializer; this is also used to
882 /// define the type of the variable.
883 /// \param Section - The section the variable should go into, or 0.
884 /// \param Align - The alignment for the variable, or 0.
885 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000886 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000887 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000888 llvm::Constant *Init,
889 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000890 unsigned Align,
891 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000892
John McCall944c8432011-05-14 03:10:52 +0000893 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
894 ReturnValueSlot Return,
895 QualType ResultType,
896 llvm::Value *Sel,
897 llvm::Value *Arg0,
898 QualType Arg0Ty,
899 bool IsSuper,
900 const CallArgList &CallArgs,
901 const ObjCMethodDecl *OMD,
902 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000903
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000904 /// EmitImageInfo - Emit the image info marker used to encode some module
905 /// level information.
906 void EmitImageInfo();
907
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000908public:
Owen Anderson69243822009-07-13 04:10:07 +0000909 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000910 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000911
David Chisnall0d13f6f2010-01-23 02:40:42 +0000912 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000914 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
915 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000916
Fariborz Jahanianda320092009-01-29 19:24:30 +0000917 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000918
Fariborz Jahanianda320092009-01-29 19:24:30 +0000919 /// GetOrEmitProtocol - Get the protocol object for the given
920 /// declaration, emitting it if necessary. The return value has type
921 /// ProtocolPtrTy.
922 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanianda320092009-01-29 19:24:30 +0000924 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
925 /// object for the given declaration, emitting it if needed. These
926 /// forward references will be filled in with empty bodies if no
927 /// definition is seen. The return value has type ProtocolPtrTy.
928 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000929 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
930 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000931
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000932};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000933
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000934class CGObjCMac : public CGObjCCommonMac {
935private:
936 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000937
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000938 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000939 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000940 void EmitModuleInfo();
941
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000942 /// EmitModuleSymols - Emit module symbols, the list of defined
943 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000944 llvm::Constant *EmitModuleSymbols();
945
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000946 /// FinishModule - Write out global data structures at the end of
947 /// processing a translation unit.
948 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000949
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000950 /// EmitClassExtension - Generate the class extension structure used
951 /// to store the weak ivar layout and properties. The return value
952 /// has type ClassExtensionPtrTy.
953 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
954
955 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
956 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000957 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000959
John McCallf85e1932011-06-15 23:02:42 +0000960 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
961 IdentifierInfo *II);
962
963 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
964
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000965 /// EmitSuperClassRef - Emits reference to class's main metadata class.
966 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000967
968 /// EmitIvarList - Emit the ivar list for the given
969 /// implementation. If ForClass is true the list of class ivars
970 /// (i.e. metaclass ivars) is emitted, otherwise the list of
971 /// interface ivars will be emitted. The return value has type
972 /// IvarListPtrTy.
973 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000974 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000975
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000976 /// EmitMetaClass - Emit a forward reference to the class structure
977 /// for the metaclass of the given interface. The return value has
978 /// type ClassPtrTy.
979 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
980
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000981 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000982 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000983 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
984 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000985 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000986
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000987 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000988
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000989 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000990
991 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000992 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000993 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000994 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000995 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000996
997 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000998 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999 /// - TypeName: The name for the type containing the methods.
1000 /// - IsProtocol: True iff these methods are for a protocol.
1001 /// - ClassMethds: True iff these are class methods.
1002 /// - Required: When true, only "required" methods are
1003 /// listed. Similarly, when false only "optional" methods are
1004 /// listed. For classes this should always be true.
1005 /// - begin, end: The method list to output.
1006 ///
1007 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001008 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001009 const char *Section,
1010 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001011
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001012 /// GetOrEmitProtocol - Get the protocol object for the given
1013 /// declaration, emitting it if necessary. The return value has type
1014 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001015 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001016
1017 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1018 /// object for the given declaration, emitting it if needed. These
1019 /// forward references will be filled in with empty bodies if no
1020 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001021 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001022
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023 /// EmitProtocolExtension - Generate the protocol extension
1024 /// structure used to store optional instance and class methods, and
1025 /// protocol properties. The return value has type
1026 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001027 llvm::Constant *
1028 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1029 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001030 const ConstantVector &OptClassMethods,
1031 const ConstantVector &MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032
1033 /// EmitProtocolList - Generate the list of referenced
1034 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001035 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001036 ObjCProtocolDecl::protocol_iterator begin,
1037 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001038
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001039 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1040 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001041 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1042 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001043
1044public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001045 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001046
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001047 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001048
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001049 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001050 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001051 QualType ResultType,
1052 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001053 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001054 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001055 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001056 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001057
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001058 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001059 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001060 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001061 QualType ResultType,
1062 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001063 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001064 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001065 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001066 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001067 const CallArgList &CallArgs,
1068 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001069
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001070 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001071 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001072
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001073 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1074 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001075
1076 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1077 /// untyped one.
1078 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1079 const ObjCMethodDecl *Method);
1080
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001081 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001082
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001083 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001084
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001085 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001086
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001087 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001088 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001089
Chris Lattner74391b42009-03-22 21:03:39 +00001090 virtual llvm::Constant *GetPropertyGetFunction();
1091 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001092 virtual llvm::Constant *GetGetStructFunction();
1093 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001094 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001095 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001096
John McCallf1549f62010-07-06 01:34:17 +00001097 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1098 const ObjCAtTryStmt &S);
1099 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1100 const ObjCAtSynchronizedStmt &S);
1101 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001102 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1103 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001104 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001105 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001106 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001107 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001108 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001109 llvm::Value *src, llvm::Value *dest,
1110 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001111 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001112 llvm::Value *src, llvm::Value *dest,
1113 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001114 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1115 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001116 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1117 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001118 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001119
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001120 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1121 QualType ObjectTy,
1122 llvm::Value *BaseValue,
1123 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001124 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001125 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001126 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001127 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001128
1129 /// GetClassGlobal - Return the global variable for the Objective-C
1130 /// class of the given name.
1131 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001132 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001133 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001134};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001135
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001136class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001137private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001138 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001139 llvm::GlobalVariable* ObjCEmptyCacheVar;
1140 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001141
Daniel Dunbar11394522009-04-18 08:51:00 +00001142 /// SuperClassReferences - uniqued super class references.
1143 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001144
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001145 /// MetaClassReferences - uniqued meta class references.
1146 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001147
1148 /// EHTypeReferences - uniqued class ehtype references.
1149 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001150
John McCall944c8432011-05-14 03:10:52 +00001151 /// VTableDispatchMethods - List of methods for which we generate
1152 /// vtable-based message dispatch.
1153 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001154
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001155 /// DefinedMetaClasses - List of defined meta-classes.
1156 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1157
John McCall944c8432011-05-14 03:10:52 +00001158 /// isVTableDispatchedSelector - Returns true if SEL is a
1159 /// vtable-based selector.
1160 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001161
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001162 /// FinishNonFragileABIModule - Write out global data structures at the end of
1163 /// processing a translation unit.
1164 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001165
Daniel Dunbar463b8762009-05-15 21:48:48 +00001166 /// AddModuleClassList - Add the given list of class pointers to the
1167 /// module with the provided symbol and section names.
1168 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1169 const char *SymbolName,
1170 const char *SectionName);
1171
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001172 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1173 unsigned InstanceStart,
1174 unsigned InstanceSize,
1175 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001176 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001177 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001178 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001179 llvm::Constant *ClassRoGV,
1180 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001182 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001183
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001184 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001185
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001186 /// EmitMethodList - Emit the method list for the given
1187 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001188 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001189 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001190 const ConstantVector &Methods);
1191 /// EmitIvarList - Emit the ivar list for the given
1192 /// implementation. If ForClass is true the list of class ivars
1193 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1194 /// interface ivars will be emitted. The return value has type
1195 /// IvarListnfABIPtrTy.
1196 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001198 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001199 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001200 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001201
Fariborz Jahanianda320092009-01-29 19:24:30 +00001202 /// GetOrEmitProtocol - Get the protocol object for the given
1203 /// declaration, emitting it if necessary. The return value has type
1204 /// ProtocolPtrTy.
1205 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001206
Fariborz Jahanianda320092009-01-29 19:24:30 +00001207 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1208 /// object for the given declaration, emitting it if needed. These
1209 /// forward references will be filled in with empty bodies if no
1210 /// definition is seen. The return value has type ProtocolPtrTy.
1211 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001212
Fariborz Jahanianda320092009-01-29 19:24:30 +00001213 /// EmitProtocolList - Generate the list of referenced
1214 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001215 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001216 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001217 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218
John McCall944c8432011-05-14 03:10:52 +00001219 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1220 ReturnValueSlot Return,
1221 QualType ResultType,
1222 Selector Sel,
1223 llvm::Value *Receiver,
1224 QualType Arg0Ty,
1225 bool IsSuper,
1226 const CallArgList &CallArgs,
1227 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001228
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001229 /// GetClassGlobal - Return the global variable for the Objective-C
1230 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001231 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001232
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001233 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001234 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001235 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001236 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001237
1238 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1239 IdentifierInfo *II);
1240
1241 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
Daniel Dunbar11394522009-04-18 08:51:00 +00001243 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1244 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1246 const ObjCInterfaceDecl *ID);
1247
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001248 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1249 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001250 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001251 const ObjCInterfaceDecl *ID);
1252
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001253 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1254 /// the given ivar.
1255 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001256 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257 const ObjCInterfaceDecl *ID,
1258 const ObjCIvarDecl *Ivar);
1259
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001260 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1261 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001262 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1263 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001264
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001265 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001266 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001267 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001268 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001269
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001270 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001271 return "OBJC_METACLASS_$_";
1272 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001274 const char *getClassSymbolPrefix() const {
1275 return "OBJC_CLASS_$_";
1276 }
1277
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001278 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001279 uint32_t &InstanceStart,
1280 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001282 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001283 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001284 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1285 return CGM.getContext().Selectors.getSelector(0, &II);
1286 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001288 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001289 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1290 return CGM.getContext().Selectors.getSelector(1, &II);
1291 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001292
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001293 /// ImplementationIsNonLazy - Check whether the given category or
1294 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001295 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001296
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001297public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001298 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001299 // FIXME. All stubs for now!
1300 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001303 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001304 QualType ResultType,
1305 Selector Sel,
1306 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001307 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001308 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001309 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
1311 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001312 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001313 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001314 QualType ResultType,
1315 Selector Sel,
1316 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001317 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001318 llvm::Value *Receiver,
1319 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001320 const CallArgList &CallArgs,
1321 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001322
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001323 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001324 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001325
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001326 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1327 bool lvalue = false)
1328 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001329
1330 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1331 /// untyped one.
1332 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1333 const ObjCMethodDecl *Method)
1334 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001335
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001336 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001337
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001338 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001339 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001340 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001341
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001342 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001343
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001344 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001345 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001346 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001347 virtual llvm::Constant *GetPropertySetFunction() {
1348 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001349 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001350
David Chisnall8fac25d2010-12-26 22:13:16 +00001351 virtual llvm::Constant *GetSetStructFunction() {
1352 return ObjCTypes.getCopyStructFn();
1353 }
1354 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001355 return ObjCTypes.getCopyStructFn();
1356 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001357 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1358 return ObjCTypes.getCppAtomicObjectFunction();
1359 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001360
Chris Lattner74391b42009-03-22 21:03:39 +00001361 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001362 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001363 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001364
John McCallf1549f62010-07-06 01:34:17 +00001365 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1366 const ObjCAtTryStmt &S);
1367 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1368 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001369 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001370 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001371 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001372 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001373 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001374 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001375 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001376 llvm::Value *src, llvm::Value *dest,
1377 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001378 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001379 llvm::Value *src, llvm::Value *dest,
1380 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001381 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001382 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001383 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1384 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001385 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001386 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1387 QualType ObjectTy,
1388 llvm::Value *BaseValue,
1389 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001390 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001391 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001392 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001393 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001394};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001395
1396/// A helper class for performing the null-initialization of a return
1397/// value.
1398struct NullReturnState {
1399 llvm::BasicBlock *NullBB;
1400 llvm::BasicBlock *callBB;
1401 NullReturnState() : NullBB(0), callBB(0) {}
1402
1403 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1404 // Make blocks for the null-init and call edges.
1405 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1406 callBB = CGF.createBasicBlock("msgSend.call");
1407
1408 // Check for a null receiver and, if there is one, jump to the
1409 // null-init test.
1410 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1411 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1412
1413 // Otherwise, start performing the call.
1414 CGF.EmitBlock(callBB);
1415 }
1416
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001417 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1418 const CallArgList &CallArgs,
1419 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001420 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001421
1422 llvm::Value *NullInitPtr = 0;
1423 if (result.isScalar() && !resultType->isVoidType()) {
1424 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1425 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1426 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001427
1428 // Finish the call path.
1429 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1430 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1431
1432 // Emit the null-init block and perform the null-initialization there.
1433 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001434
1435 // Release consumed arguments along the null-receiver path.
1436 if (Method) {
1437 CallArgList::const_iterator I = CallArgs.begin();
1438 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1439 e = Method->param_end(); i != e; ++i, ++I) {
1440 const ParmVarDecl *ParamDecl = (*i);
1441 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1442 RValue RV = I->RV;
1443 assert(RV.isScalar() &&
1444 "NullReturnState::complete - arg not on object");
1445 CGF.EmitARCRelease(RV.getScalarVal(), true);
1446 }
1447 }
1448 }
1449
1450 if (result.isScalar()) {
1451 if (NullInitPtr)
1452 CGF.EmitNullInitialization(NullInitPtr, resultType);
1453 // Jump to the continuation block.
1454 CGF.EmitBlock(contBB);
1455 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1456 : result;
1457 }
1458
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001459 if (!resultType->isAnyComplexType()) {
1460 assert(result.isAggregate() && "null init of non-aggregate result?");
1461 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1462 // Jump to the continuation block.
1463 CGF.EmitBlock(contBB);
1464 return result;
1465 }
1466
1467 // _Complex type
1468 // FIXME. Now easy to handle any other scalar type whose result is returned
1469 // in memory due to ABI limitations.
1470 CGF.EmitBlock(contBB);
1471 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1472 llvm::Type *MemberType = CallCV.first->getType();
1473 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1474 // Create phi instruction for scalar complex value.
1475 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1476 PHIReal->addIncoming(ZeroCV, NullBB);
1477 PHIReal->addIncoming(CallCV.first, callBB);
1478 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1479 PHIImag->addIncoming(ZeroCV, NullBB);
1480 PHIImag->addIncoming(CallCV.second, callBB);
1481 return RValue::getComplex(PHIReal, PHIImag);
1482 }
1483};
1484
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001485} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001486
1487/* *** Helper Functions *** */
1488
1489/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001490static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001491 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001492 unsigned idx0,
1493 unsigned idx1) {
1494 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001495 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1496 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001497 };
Jay Foada5c04342011-07-21 14:31:17 +00001498 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001499}
1500
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001501/// hasObjCExceptionAttribute - Return true if this class or any super
1502/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001503static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001504 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001505 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001506 return true;
1507 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001508 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001509 return false;
1510}
1511
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001512/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001513
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001514CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001515 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001516 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001517 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001518}
1519
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001520/// GetClass - Return a reference to the class for the given interface
1521/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001522llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001523 const ObjCInterfaceDecl *ID) {
1524 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001525}
1526
1527/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001528llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1529 bool lval) {
1530 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001531}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001532llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001533 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001534 return EmitSelector(Builder, Method->getSelector());
1535}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001536
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001537llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001538 if (T->isObjCIdType() ||
1539 T->isObjCQualifiedIdType()) {
1540 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001541 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001542 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001543 if (T->isObjCClassType() ||
1544 T->isObjCQualifiedClassType()) {
1545 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001546 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001547 }
1548 if (T->isObjCObjectPointerType())
1549 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1550
John McCall5a180392010-07-24 00:37:23 +00001551 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001552}
1553
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001554/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001555/*
1556 struct __builtin_CFString {
1557 const int *isa; // point to __CFConstantStringClassReference
1558 int flags;
1559 const char *str;
1560 long length;
1561 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001562*/
1563
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001564/// or Generate a constant NSString object.
1565/*
1566 struct __builtin_NSString {
1567 const int *isa; // point to __NSConstantStringClassReference
1568 const char *str;
1569 unsigned int length;
1570 };
1571*/
1572
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001573llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001574 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001575 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1576 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001577 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001578}
1579
1580/// Generates a message send where the super is the receiver. This is
1581/// a message send to self with special delivery semantics indicating
1582/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001583CodeGen::RValue
1584CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001585 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001586 QualType ResultType,
1587 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001588 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001589 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001590 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001591 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001592 const CodeGen::CallArgList &CallArgs,
1593 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001594 // Create and init a super structure; this is a (receiver, class)
1595 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001596 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001597 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001598 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001599 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001600 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001601 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001602
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001603 // If this is a class message the metaclass is passed as the target.
1604 llvm::Value *Target;
1605 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001606 if (isCategoryImpl) {
1607 // Message sent to 'super' in a class method defined in a category
1608 // implementation requires an odd treatment.
1609 // If we are in a class method, we must retrieve the
1610 // _metaclass_ for the current class, pointed at by
1611 // the class's "isa" pointer. The following assumes that
1612 // isa" is the first ivar in a class (which it must be).
1613 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1614 Target = CGF.Builder.CreateStructGEP(Target, 0);
1615 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001616 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001617 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1618 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1619 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1620 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001621 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001622 }
1623 else if (isCategoryImpl)
1624 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1625 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001626 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1627 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1628 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001629 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001630 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1631 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001632 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001633 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001634 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001635 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001636 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001637 return EmitMessageSend(CGF, Return, ResultType,
1638 EmitSelector(CGF.Builder, Sel),
1639 ObjCSuper, ObjCTypes.SuperPtrCTy,
1640 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001641}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001642
1643/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001644CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001645 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001646 QualType ResultType,
1647 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001648 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001649 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001650 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001651 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001652 return EmitMessageSend(CGF, Return, ResultType,
1653 EmitSelector(CGF.Builder, Sel),
1654 Receiver, CGF.getContext().getObjCIdType(),
1655 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001656}
1657
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001658CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001659CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1660 ReturnValueSlot Return,
1661 QualType ResultType,
1662 llvm::Value *Sel,
1663 llvm::Value *Arg0,
1664 QualType Arg0Ty,
1665 bool IsSuper,
1666 const CallArgList &CallArgs,
1667 const ObjCMethodDecl *Method,
1668 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001669 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001670 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001671 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001672 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1673 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001674 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001675
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001676 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001677 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001678 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001679 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001680 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001681
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001682 if (Method)
1683 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1684 CGM.getContext().getCanonicalType(ResultType) &&
1685 "Result type mismatch!");
1686
John McCallcba681a2011-05-14 21:12:11 +00001687 NullReturnState nullReturn;
1688
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001689 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001690 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001691 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001692 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001693 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001694 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1695 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1696 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001697 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1698 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1699 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001700 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001701 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001702 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001703 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001704
1705 bool requiresnullCheck = false;
1706 if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1707 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1708 e = Method->param_end(); i != e; ++i) {
1709 const ParmVarDecl *ParamDecl = (*i);
1710 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1711 if (!nullReturn.NullBB)
1712 nullReturn.init(CGF, Arg0);
1713 requiresnullCheck = true;
1714 break;
1715 }
1716 }
1717
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001718 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001719 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001720 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1721 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001722}
1723
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001724static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1725 if (FQT.isObjCGCStrong())
1726 return Qualifiers::Strong;
1727
John McCallf85e1932011-06-15 23:02:42 +00001728 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001729 return Qualifiers::Weak;
1730
1731 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1732 return Qualifiers::Strong;
1733
1734 if (const PointerType *PT = FQT->getAs<PointerType>())
1735 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1736
1737 return Qualifiers::GCNone;
1738}
1739
John McCall6b5a61b2011-02-07 10:33:21 +00001740llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1741 const CGBlockInfo &blockInfo) {
1742 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001743 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001744
Douglas Gregore289d812011-09-13 17:21:33 +00001745 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001746 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001747 return nullPtr;
1748
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001749 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001750 SkipIvars.clear();
1751 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001752 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1753 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001754
Fariborz Jahanian81979822010-09-09 00:21:45 +00001755 // __isa is the first field in block descriptor and must assume by runtime's
1756 // convention that it is GC'able.
1757 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001758
1759 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1760
1761 // Calculate the basic layout of the block structure.
1762 const llvm::StructLayout *layout =
1763 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1764
1765 // Ignore the optional 'this' capture: C++ objects are not assumed
1766 // to be GC'ed.
1767
1768 // Walk the captured variables.
1769 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1770 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1771 const VarDecl *variable = ci->getVariable();
1772 QualType type = variable->getType();
1773
1774 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1775
1776 // Ignore constant captures.
1777 if (capture.isConstant()) continue;
1778
1779 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1780
1781 // __block variables are passed by their descriptor address.
1782 if (ci->isByRef()) {
1783 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001784 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001785 }
1786
1787 assert(!type->isArrayType() && "array variable should not be caught");
1788 if (const RecordType *record = type->getAs<RecordType>()) {
1789 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001790 continue;
1791 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001792
John McCall6b5a61b2011-02-07 10:33:21 +00001793 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1794 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1795
1796 if (GCAttr == Qualifiers::Strong)
1797 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1798 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001799 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001800 SkipIvars.push_back(GC_IVAR(fieldOffset,
1801 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001802 }
1803
1804 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001805 return nullPtr;
1806
1807 // Sort on byte position; captures might not be allocated in order,
1808 // and unions can do funny things.
1809 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1810 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001811
1812 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001813 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001814 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1815 printf("\n block variable layout for block: ");
1816 const unsigned char *s = (unsigned char*)BitMap.c_str();
1817 for (unsigned i = 0; i < BitMap.size(); i++)
1818 if (!(s[i] & 0xf0))
1819 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1820 else
1821 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1822 printf("\n");
1823 }
1824
1825 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001826}
1827
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001828llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001829 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001830 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001831 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001832 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1833
Owen Anderson3c4972d2009-07-29 18:54:39 +00001834 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001835 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001836}
1837
Fariborz Jahanianda320092009-01-29 19:24:30 +00001838void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001839 // FIXME: We shouldn't need this, the protocol decl should contain enough
1840 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001841 DefinedProtocols.insert(PD->getIdentifier());
1842
1843 // If we have generated a forward reference to this protocol, emit
1844 // it now. Otherwise do nothing, the protocol objects are lazily
1845 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001847 GetOrEmitProtocol(PD);
1848}
1849
Fariborz Jahanianda320092009-01-29 19:24:30 +00001850llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001851 if (DefinedProtocols.count(PD->getIdentifier()))
1852 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001853
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001854 return GetOrEmitProtocolRef(PD);
1855}
1856
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001857/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001858// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1859struct _objc_protocol {
1860struct _objc_protocol_extension *isa;
1861char *protocol_name;
1862struct _objc_protocol_list *protocol_list;
1863struct _objc__method_prototype_list *instance_methods;
1864struct _objc__method_prototype_list *class_methods
1865};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001866
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001867See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001868*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001869llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1870 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1871
1872 // Early exit if a defining object has already been generated.
1873 if (Entry && Entry->hasInitializer())
1874 return Entry;
1875
Douglas Gregor1d784b22012-01-01 19:51:50 +00001876 // Use the protocol definition, if there is one.
1877 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1878 PD = Def;
1879
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001880 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001881 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001882 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1883
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001884 // Construct method lists.
1885 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1886 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001887 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001889 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001890 ObjCMethodDecl *MD = *i;
1891 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001892 if (!C)
1893 return GetOrEmitProtocolRef(PD);
1894
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001895 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1896 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001897 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001898 } else {
1899 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001900 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001901 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001902 }
1903
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001904 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001905 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001906 ObjCMethodDecl *MD = *i;
1907 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001908 if (!C)
1909 return GetOrEmitProtocolRef(PD);
1910
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001911 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1912 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001913 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001914 } else {
1915 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001916 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001917 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001918 }
1919
Bob Wilsondc8dab62011-11-30 01:57:58 +00001920 MethodTypesExt.insert(MethodTypesExt.end(),
1921 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1922
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001923 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001924 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1925 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001926 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001927 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001928 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001929 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001930 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001931 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001932 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001933 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001934 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001935 ClassMethods)
1936 };
Owen Anderson08e25242009-07-27 22:29:56 +00001937 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001938 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001939
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001940 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001941 // Already created, fix the linkage and update the initializer.
1942 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001943 Entry->setInitializer(Init);
1944 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001945 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001946 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001947 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001948 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001949 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001950 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001951 // FIXME: Is this necessary? Why only for protocol?
1952 Entry->setAlignment(4);
1953 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001954 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001955
1956 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001957}
1958
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001959llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001960 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1961
1962 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001963 // We use the initializer as a marker of whether this is a forward
1964 // reference or not. At module finalization we add the empty
1965 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001966 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001967 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001968 llvm::GlobalValue::ExternalLinkage,
1969 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001970 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001971 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972 // FIXME: Is this necessary? Why only for protocol?
1973 Entry->setAlignment(4);
1974 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001975
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001976 return Entry;
1977}
1978
1979/*
1980 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001981 uint32_t size;
1982 struct objc_method_description_list *optional_instance_methods;
1983 struct objc_method_description_list *optional_class_methods;
1984 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001985 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001986 };
1987*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001988llvm::Constant *
1989CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1990 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001991 const ConstantVector &OptClassMethods,
1992 const ConstantVector &MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001993 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001994 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001995 llvm::Constant *Values[] = {
1996 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001997 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001998 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001999 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002000 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002001 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002002 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002003 OptClassMethods),
2004 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002005 ObjCTypes),
2006 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2007 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002008 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002009
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002010 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002011 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002012 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002013 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002014
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002015 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002016 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002017
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002018 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002019 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002020 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002021 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022}
2023
2024/*
2025 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002026 struct objc_protocol_list *next;
2027 long count;
2028 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002029 };
2030*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00002031llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002032CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00002033 ObjCProtocolDecl::protocol_iterator begin,
2034 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002035 std::vector<llvm::Constant*> ProtocolRefs;
2036
Daniel Dunbardbc933702008-08-21 21:57:41 +00002037 for (; begin != end; ++begin)
2038 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002039
2040 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002041 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002042 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002043
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002044 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002045 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002046
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002047 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002048 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002049 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002050 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002051 ProtocolRefs.size() - 1);
2052 Values[2] =
2053 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2054 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002055 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002056
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002057 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002058 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002059 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002060 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002061 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062}
2063
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002064void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
2065 std::vector<llvm::Constant*> &Properties,
2066 const Decl *Container,
2067 const ObjCProtocolDecl *PROTO,
2068 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002069 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2070 E = PROTO->protocol_end(); P != E; ++P)
2071 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2072 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2073 E = PROTO->prop_end(); I != E; ++I) {
2074 const ObjCPropertyDecl *PD = *I;
2075 if (!PropertySet.insert(PD->getIdentifier()))
2076 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002077 llvm::Constant *Prop[] = {
2078 GetPropertyName(PD->getIdentifier()),
2079 GetPropertyTypeString(PD, Container)
2080 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002081 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2082 }
2083}
2084
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002085/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002086 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002087 const char * const name;
2088 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002089 };
2090
2091 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002092 uint32_t entsize; // sizeof (struct _objc_property)
2093 uint32_t prop_count;
2094 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002095 };
2096*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002097llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002098 const Decl *Container,
2099 const ObjCContainerDecl *OCD,
2100 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002101 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002102 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2104 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002105 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002106 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002107 llvm::Constant *Prop[] = {
2108 GetPropertyName(PD->getIdentifier()),
2109 GetPropertyTypeString(PD, Container)
2110 };
Owen Anderson08e25242009-07-27 22:29:56 +00002111 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002112 Prop));
2113 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002114 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002115 for (ObjCInterfaceDecl::all_protocol_iterator
2116 P = OID->all_referenced_protocol_begin(),
2117 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002118 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2119 ObjCTypes);
2120 }
2121 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2122 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2123 E = CD->protocol_end(); P != E; ++P)
2124 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2125 ObjCTypes);
2126 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002127
2128 // Return null for empty list.
2129 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002130 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002131
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002132 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002133 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002134 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002135 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2136 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002137 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002138 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002139 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002140 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002141
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002142 llvm::GlobalVariable *GV =
2143 CreateMetadataVar(Name, Init,
2144 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002145 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002146 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002147 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002148 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002149}
2150
Bob Wilsondc8dab62011-11-30 01:57:58 +00002151llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2152 const ConstantVector &MethodTypes,
2153 const ObjCCommonTypesHelper &ObjCTypes) {
2154 // Return null for empty list.
2155 if (MethodTypes.empty())
2156 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2157
2158 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2159 MethodTypes.size());
2160 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2161
2162 llvm::GlobalVariable *GV =
2163 CreateMetadataVar(Name, Init,
2164 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2165 (ObjCABI == 2) ? 8 : 4,
2166 true);
2167 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2168}
2169
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002170/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002171 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002172 int count;
2173 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002174 };
2175*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002176llvm::Constant *
2177CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002178 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002179 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002180 ObjCTypes.SelectorPtrTy),
2181 GetMethodVarType(MD)
2182 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002183 if (!Desc[1])
2184 return 0;
2185
Owen Anderson08e25242009-07-27 22:29:56 +00002186 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002187 Desc);
2188}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002189
Chris Lattner5f9e2722011-07-23 10:55:15 +00002190llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002191 const char *Section,
2192 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002193 // Return null for empty list.
2194 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002195 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002196
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002197 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002198 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002199 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002200 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002201 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002202 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002203
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002204 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002205 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002206 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002207}
2208
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002209/*
2210 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002211 char *category_name;
2212 char *class_name;
2213 struct _objc_method_list *instance_methods;
2214 struct _objc_method_list *class_methods;
2215 struct _objc_protocol_list *protocols;
2216 uint32_t size; // <rdar://4585769>
2217 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002218 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002219*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002220void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002221 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002222
Mike Stumpf5408fe2009-05-16 07:57:57 +00002223 // FIXME: This is poor design, the OCD should have a pointer to the category
2224 // decl. Additionally, note that Category can be null for the @implementation
2225 // w/o an @interface case. Sema should just create one for us as it does for
2226 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002227 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002228 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002229 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002230
2231 llvm::SmallString<256> ExtName;
2232 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2233 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002234
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002235 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002237 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002238 // Instance methods should always be defined.
2239 InstanceMethods.push_back(GetMethodConstant(*i));
2240 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002241 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002242 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002243 // Class methods should always be defined.
2244 ClassMethods.push_back(GetMethodConstant(*i));
2245 }
2246
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002247 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002248 Values[0] = GetClassName(OCD->getIdentifier());
2249 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002250 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002251 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002252 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002253 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002254 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002255 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002256 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002257 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002258 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002259 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002260 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002261 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002262 Category->protocol_begin(),
2263 Category->protocol_end());
2264 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002265 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002266 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002267 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002268
2269 // If there is no category @interface then there can be no properties.
2270 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002271 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002272 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002273 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002274 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002275 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002276
Owen Anderson08e25242009-07-27 22:29:56 +00002277 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002278 Values);
2279
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002280 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002281 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002282 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002283 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002284 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002285 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002286 // method definition entries must be clear for next implementation.
2287 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002288}
2289
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290// FIXME: Get from somewhere?
2291enum ClassFlags {
2292 eClassFlags_Factory = 0x00001,
2293 eClassFlags_Meta = 0x00002,
2294 // <rdr://5142207>
2295 eClassFlags_HasCXXStructors = 0x02000,
2296 eClassFlags_Hidden = 0x20000,
2297 eClassFlags_ABI2_Hidden = 0x00010,
2298 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2299};
2300
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002301/*
2302 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002303 Class isa;
2304 Class super_class;
2305 const char *name;
2306 long version;
2307 long info;
2308 long instance_size;
2309 struct _objc_ivar_list *ivars;
2310 struct _objc_method_list *methods;
2311 struct _objc_cache *cache;
2312 struct _objc_protocol_list *protocols;
2313 // Objective-C 1.0 extensions (<rdr://4585769>)
2314 const char *ivar_layout;
2315 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316 };
2317
2318 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002319*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002320void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002321 DefinedSymbols.insert(ID->getIdentifier());
2322
Chris Lattner8ec03f52008-11-24 03:54:41 +00002323 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002324 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002325 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002327 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002328 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002329 Interface->all_referenced_protocol_begin(),
2330 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002331 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002332 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002333 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002334 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002335 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336
2337 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002338 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 Flags |= eClassFlags_Hidden;
2340
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002341 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002342 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002343 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002344 // Instance methods should always be defined.
2345 InstanceMethods.push_back(GetMethodConstant(*i));
2346 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002347 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002348 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002349 // Class methods should always be defined.
2350 ClassMethods.push_back(GetMethodConstant(*i));
2351 }
2352
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002353 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002354 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002355 ObjCPropertyImplDecl *PID = *i;
2356
2357 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2358 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2359
2360 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2361 if (llvm::Constant *C = GetMethodConstant(MD))
2362 InstanceMethods.push_back(C);
2363 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2364 if (llvm::Constant *C = GetMethodConstant(MD))
2365 InstanceMethods.push_back(C);
2366 }
2367 }
2368
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002369 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002370 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002371 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002372 // Record a reference to the super class.
2373 LazySymbols.insert(Super->getIdentifier());
2374
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002375 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002376 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002377 ObjCTypes.ClassPtrTy);
2378 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002379 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002380 }
2381 Values[ 2] = GetClassName(ID->getIdentifier());
2382 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002383 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2384 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2385 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002386 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002387 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002388 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002389 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002390 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002392 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002394 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002396 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002397 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002398 std::string Name("\01L_OBJC_CLASS_");
2399 Name += ClassName;
2400 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2401 // Check for a forward reference.
2402 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2403 if (GV) {
2404 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2405 "Forward metaclass reference has incorrect type.");
2406 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2407 GV->setInitializer(Init);
2408 GV->setSection(Section);
2409 GV->setAlignment(4);
2410 CGM.AddUsedGlobal(GV);
2411 }
2412 else
2413 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002414 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002415 // method definition entries must be clear for next implementation.
2416 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417}
2418
2419llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2420 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002421 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002423 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002424
John McCall1fb0caa2010-10-22 21:05:15 +00002425 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002427
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002428 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 // The isa for the metaclass is the root of the hierarchy.
2430 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2431 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2432 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002434 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002435 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002436 // The super class for the metaclass is emitted as the name of the
2437 // super class. The runtime fixes this up to point to the
2438 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002439 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002441 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442 ObjCTypes.ClassPtrTy);
2443 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002444 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445 }
2446 Values[ 2] = GetClassName(ID->getIdentifier());
2447 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002448 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2449 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2450 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002451 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002452 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002453 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002454 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002455 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002456 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002457 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 Values[ 9] = Protocols;
2459 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002460 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002461 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002462 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002463 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002464 Values);
2465
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002466 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002467 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002468
2469 // Check for a forward reference.
2470 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2471 if (GV) {
2472 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2473 "Forward metaclass reference has incorrect type.");
2474 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2475 GV->setInitializer(Init);
2476 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002477 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002478 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002479 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002480 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002482 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002483 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
2485 return GV;
2486}
2487
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002488llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002489 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002490
Mike Stumpf5408fe2009-05-16 07:57:57 +00002491 // FIXME: Should we look these up somewhere other than the module. Its a bit
2492 // silly since we only generate these while processing an implementation, so
2493 // exactly one pointer would work if know when we entered/exitted an
2494 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002495
2496 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002497 // Previously, metaclass with internal linkage may have been defined.
2498 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002499 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2500 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002501 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2502 "Forward metaclass reference has incorrect type.");
2503 return GV;
2504 } else {
2505 // Generate as an external reference to keep a consistent
2506 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002507 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002508 llvm::GlobalValue::ExternalLinkage,
2509 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002510 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002511 }
2512}
2513
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002514llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2515 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2516
2517 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2518 true)) {
2519 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2520 "Forward class metadata reference has incorrect type.");
2521 return GV;
2522 } else {
2523 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2524 llvm::GlobalValue::ExternalLinkage,
2525 0,
2526 Name);
2527 }
2528}
2529
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530/*
2531 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002532 uint32_t size;
2533 const char *weak_ivar_layout;
2534 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002535 };
2536*/
2537llvm::Constant *
2538CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002539 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002540 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002541
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002542 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002543 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002544 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002545 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002546 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002547
2548 // Return null if no extension bits are used.
2549 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002550 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002551
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002552 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002553 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002554 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002555 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002556 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002557}
2558
2559/*
2560 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002561 char *ivar_name;
2562 char *ivar_type;
2563 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002564 };
2565
2566 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002567 int ivar_count;
2568 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002569 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002570*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002571llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002572 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002573 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002574
2575 // When emitting the root class GCC emits ivar entries for the
2576 // actual class structure. It is not clear if we need to follow this
2577 // behavior; for now lets try and get away with not doing it. If so,
2578 // the cleanest solution would be to make up an ObjCInterfaceDecl
2579 // for the class.
2580 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002581 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002582
Jordy Rosedb8264e2011-07-22 02:08:32 +00002583 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002584
Jordy Rosedb8264e2011-07-22 02:08:32 +00002585 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002586 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002587 // Ignore unnamed bit-fields.
2588 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002589 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002590 llvm::Constant *Ivar[] = {
2591 GetMethodVarName(IVD->getIdentifier()),
2592 GetMethodVarType(IVD),
2593 llvm::ConstantInt::get(ObjCTypes.IntTy,
2594 ComputeIvarBaseOffset(CGM, OID, IVD))
2595 };
Owen Anderson08e25242009-07-27 22:29:56 +00002596 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002597 }
2598
2599 // Return null for empty list.
2600 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002601 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002602
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002603 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002604 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002605 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002606 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002607 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002608 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002609
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002610 llvm::GlobalVariable *GV;
2611 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002612 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002613 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002614 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002615 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002616 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002617 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002618 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002619 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002620}
2621
2622/*
2623 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002624 SEL method_name;
2625 char *method_types;
2626 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002627 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002628
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002629 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002630 struct objc_method_list *obsolete;
2631 int count;
2632 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002633 };
2634*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002635
2636/// GetMethodConstant - Return a struct objc_method constant for the
2637/// given method if it has been defined. The result is null if the
2638/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002639llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002640 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002641 if (!Fn)
2642 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002643
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002644 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002645 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002646 ObjCTypes.SelectorPtrTy),
2647 GetMethodVarType(MD),
2648 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2649 };
Owen Anderson08e25242009-07-27 22:29:56 +00002650 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002651}
2652
Chris Lattner5f9e2722011-07-23 10:55:15 +00002653llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002654 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002655 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002656 // Return null for empty list.
2657 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002658 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002659
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002660 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002661 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002662 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002663 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002664 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002665 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002666 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002667
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002668 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002669 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002670}
2671
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002672llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002673 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002674 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002675 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002676
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002677 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002678 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002679 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002680 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002681 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002682 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002683 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002684 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002685 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002686
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002687 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002688}
2689
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002690llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002691CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002692 llvm::Constant *Init,
2693 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002694 unsigned Align,
2695 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002696 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002697 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002698 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002699 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002700 if (Section)
2701 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002702 if (Align)
2703 GV->setAlignment(Align);
2704 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002705 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002706 return GV;
2707}
2708
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002709llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002710 // Abuse this interface function as a place to finalize.
2711 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002712 return NULL;
2713}
2714
Chris Lattner74391b42009-03-22 21:03:39 +00002715llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002716 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002717}
2718
Chris Lattner74391b42009-03-22 21:03:39 +00002719llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002720 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002721}
2722
David Chisnall8fac25d2010-12-26 22:13:16 +00002723llvm::Constant *CGObjCMac::GetGetStructFunction() {
2724 return ObjCTypes.getCopyStructFn();
2725}
2726llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002727 return ObjCTypes.getCopyStructFn();
2728}
2729
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002730llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2731 return ObjCTypes.getCppAtomicObjectFunction();
2732}
2733
Chris Lattner74391b42009-03-22 21:03:39 +00002734llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002735 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002736}
2737
John McCallf1549f62010-07-06 01:34:17 +00002738void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2739 return EmitTryOrSynchronizedStmt(CGF, S);
2740}
2741
2742void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2743 const ObjCAtSynchronizedStmt &S) {
2744 return EmitTryOrSynchronizedStmt(CGF, S);
2745}
2746
John McCallcc505292010-07-21 06:59:36 +00002747namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002748 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002749 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002750 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002751 llvm::Value *CallTryExitVar;
2752 llvm::Value *ExceptionData;
2753 ObjCTypesHelper &ObjCTypes;
2754 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002755 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002756 llvm::Value *CallTryExitVar,
2757 llvm::Value *ExceptionData,
2758 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002759 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002760 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2761
John McCallad346f42011-07-12 20:27:29 +00002762 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002763 // Check whether we need to call objc_exception_try_exit.
2764 // In optimized code, this branch will always be folded.
2765 llvm::BasicBlock *FinallyCallExit =
2766 CGF.createBasicBlock("finally.call_exit");
2767 llvm::BasicBlock *FinallyNoCallExit =
2768 CGF.createBasicBlock("finally.no_call_exit");
2769 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2770 FinallyCallExit, FinallyNoCallExit);
2771
2772 CGF.EmitBlock(FinallyCallExit);
2773 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2774 ->setDoesNotThrow();
2775
2776 CGF.EmitBlock(FinallyNoCallExit);
2777
2778 if (isa<ObjCAtTryStmt>(S)) {
2779 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002780 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2781 // Save the current cleanup destination in case there's
2782 // control flow inside the finally statement.
2783 llvm::Value *CurCleanupDest =
2784 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2785
John McCallcc505292010-07-21 06:59:36 +00002786 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2787
John McCalld96a8e72010-08-11 00:16:14 +00002788 if (CGF.HaveInsertPoint()) {
2789 CGF.Builder.CreateStore(CurCleanupDest,
2790 CGF.getNormalCleanupDestSlot());
2791 } else {
2792 // Currently, the end of the cleanup must always exist.
2793 CGF.EnsureInsertPoint();
2794 }
2795 }
John McCallcc505292010-07-21 06:59:36 +00002796 } else {
2797 // Emit objc_sync_exit(expr); as finally's sole statement for
2798 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002799 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002800 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2801 ->setDoesNotThrow();
2802 }
2803 }
2804 };
John McCall87bb5822010-07-31 23:20:56 +00002805
2806 class FragileHazards {
2807 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002808 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002809 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2810
2811 llvm::InlineAsm *ReadHazard;
2812 llvm::InlineAsm *WriteHazard;
2813
2814 llvm::FunctionType *GetAsmFnType();
2815
2816 void collectLocals();
2817 void emitReadHazard(CGBuilderTy &Builder);
2818
2819 public:
2820 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002821
John McCall87bb5822010-07-31 23:20:56 +00002822 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002823 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002824 };
2825}
2826
2827/// Create the fragile-ABI read and write hazards based on the current
2828/// state of the function, which is presumed to be immediately prior
2829/// to a @try block. These hazards are used to maintain correct
2830/// semantics in the face of optimization and the fragile ABI's
2831/// cavalier use of setjmp/longjmp.
2832FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2833 collectLocals();
2834
2835 if (Locals.empty()) return;
2836
2837 // Collect all the blocks in the function.
2838 for (llvm::Function::iterator
2839 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2840 BlocksBeforeTry.insert(&*I);
2841
2842 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2843
2844 // Create a read hazard for the allocas. This inhibits dead-store
2845 // optimizations and forces the values to memory. This hazard is
2846 // inserted before any 'throwing' calls in the protected scope to
2847 // reflect the possibility that the variables might be read from the
2848 // catch block if the call throws.
2849 {
2850 std::string Constraint;
2851 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2852 if (I) Constraint += ',';
2853 Constraint += "*m";
2854 }
2855
2856 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2857 }
2858
2859 // Create a write hazard for the allocas. This inhibits folding
2860 // loads across the hazard. This hazard is inserted at the
2861 // beginning of the catch path to reflect the possibility that the
2862 // variables might have been written within the protected scope.
2863 {
2864 std::string Constraint;
2865 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2866 if (I) Constraint += ',';
2867 Constraint += "=*m";
2868 }
2869
2870 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2871 }
2872}
2873
2874/// Emit a write hazard at the current location.
2875void FragileHazards::emitWriteHazard() {
2876 if (Locals.empty()) return;
2877
Jay Foad4c7d9f12011-07-15 08:37:34 +00002878 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002879}
2880
John McCall87bb5822010-07-31 23:20:56 +00002881void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2882 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002883 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002884}
2885
2886/// Emit read hazards in all the protected blocks, i.e. all the blocks
2887/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002888void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002889 if (Locals.empty()) return;
2890
2891 CGBuilderTy Builder(CGF.getLLVMContext());
2892
2893 // Iterate through all blocks, skipping those prior to the try.
2894 for (llvm::Function::iterator
2895 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2896 llvm::BasicBlock &BB = *FI;
2897 if (BlocksBeforeTry.count(&BB)) continue;
2898
2899 // Walk through all the calls in the block.
2900 for (llvm::BasicBlock::iterator
2901 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2902 llvm::Instruction &I = *BI;
2903
2904 // Ignore instructions that aren't non-intrinsic calls.
2905 // These are the only calls that can possibly call longjmp.
2906 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2907 if (isa<llvm::IntrinsicInst>(I))
2908 continue;
2909
2910 // Ignore call sites marked nounwind. This may be questionable,
2911 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2912 llvm::CallSite CS(&I);
2913 if (CS.doesNotThrow()) continue;
2914
John McCall0b251722010-08-04 05:59:32 +00002915 // Insert a read hazard before the call. This will ensure that
2916 // any writes to the locals are performed before making the
2917 // call. If the call throws, then this is sufficient to
2918 // guarantee correctness as long as it doesn't also write to any
2919 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002920 Builder.SetInsertPoint(&BB, BI);
2921 emitReadHazard(Builder);
2922 }
2923 }
2924}
2925
2926static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2927 if (V) S.insert(V);
2928}
2929
2930void FragileHazards::collectLocals() {
2931 // Compute a set of allocas to ignore.
2932 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2933 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2934 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002935
2936 // Collect all the allocas currently in the function. This is
2937 // probably way too aggressive.
2938 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2939 for (llvm::BasicBlock::iterator
2940 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2941 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2942 Locals.push_back(&*I);
2943}
2944
2945llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002946 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002947 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2948 tys[i] = Locals[i]->getType();
2949 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002950}
2951
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002952/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002953
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002954 Objective-C setjmp-longjmp (sjlj) Exception Handling
2955 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002956
John McCallf1549f62010-07-06 01:34:17 +00002957 A catch buffer is a setjmp buffer plus:
2958 - a pointer to the exception that was caught
2959 - a pointer to the previous exception data buffer
2960 - two pointers of reserved storage
2961 Therefore catch buffers form a stack, with a pointer to the top
2962 of the stack kept in thread-local storage.
2963
2964 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2965 objc_exception_try_exit pops the given catch buffer, which is
2966 required to be the top of the EH stack.
2967 objc_exception_throw pops the top of the EH stack, writes the
2968 thrown exception into the appropriate field, and longjmps
2969 to the setjmp buffer. It crashes the process (with a printf
2970 and an abort()) if there are no catch buffers on the stack.
2971 objc_exception_extract just reads the exception pointer out of the
2972 catch buffer.
2973
2974 There's no reason an implementation couldn't use a light-weight
2975 setjmp here --- something like __builtin_setjmp, but API-compatible
2976 with the heavyweight setjmp. This will be more important if we ever
2977 want to implement correct ObjC/C++ exception interactions for the
2978 fragile ABI.
2979
2980 Note that for this use of setjmp/longjmp to be correct, we may need
2981 to mark some local variables volatile: if a non-volatile local
2982 variable is modified between the setjmp and the longjmp, it has
2983 indeterminate value. For the purposes of LLVM IR, it may be
2984 sufficient to make loads and stores within the @try (to variables
2985 declared outside the @try) volatile. This is necessary for
2986 optimized correctness, but is not currently being done; this is
2987 being tracked as rdar://problem/8160285
2988
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002989 The basic framework for a @try-catch-finally is as follows:
2990 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002991 objc_exception_data d;
2992 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002993 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002994
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002995 objc_exception_try_enter(&d);
2996 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002997 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002998 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002999 // exception path
3000 id _caught = objc_exception_extract(&d);
3001
3002 // enter new try scope for handlers
3003 if (!setjmp(d.jmp_buf)) {
3004 ... match exception and execute catch blocks ...
3005
3006 // fell off end, rethrow.
3007 _rethrow = _caught;
3008 ... jump-through-finally to finally_rethrow ...
3009 } else {
3010 // exception in catch block
3011 _rethrow = objc_exception_extract(&d);
3012 _call_try_exit = false;
3013 ... jump-through-finally to finally_rethrow ...
3014 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003015 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003016 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003017
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003018 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003019 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003020 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003021
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003022 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003023 ... dispatch to finally destination ...
3024
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003025 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003026 objc_exception_throw(_rethrow);
3027
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003028 finally_end:
3029 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003030
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003031 This framework differs slightly from the one gcc uses, in that gcc
3032 uses _rethrow to determine if objc_exception_try_exit should be called
3033 and if the object should be rethrown. This breaks in the face of
3034 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003035
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003036 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003037
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003038 - If there are no catch blocks, then we avoid emitting the second
3039 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003040
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003041 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3042 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003043
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003044 - FIXME: If there is no @finally block we can do a few more
3045 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003046
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003047 Rethrows and Jumps-Through-Finally
3048 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003049
John McCallf1549f62010-07-06 01:34:17 +00003050 '@throw;' is supported by pushing the currently-caught exception
3051 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003052
John McCallf1549f62010-07-06 01:34:17 +00003053 Branches through the @finally block are handled with an ordinary
3054 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3055 exceptions are not compatible with C++ exceptions, and this is
3056 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003057
John McCallf1549f62010-07-06 01:34:17 +00003058 @synchronized(expr) { stmt; } is emitted as if it were:
3059 id synch_value = expr;
3060 objc_sync_enter(synch_value);
3061 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003062*/
3063
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003064void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3065 const Stmt &S) {
3066 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003067
3068 // A destination for the fall-through edges of the catch handlers to
3069 // jump to.
3070 CodeGenFunction::JumpDest FinallyEnd =
3071 CGF.getJumpDestInCurrentScope("finally.end");
3072
3073 // A destination for the rethrow edge of the catch handlers to jump
3074 // to.
3075 CodeGenFunction::JumpDest FinallyRethrow =
3076 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003077
Daniel Dunbar1c566672009-02-24 01:43:46 +00003078 // For @synchronized, call objc_sync_enter(sync.expr). The
3079 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003080 // @synchronized. We can't avoid a temp here because we need the
3081 // value to be preserved. If the backend ever does liveness
3082 // correctly after setjmp, this will be unnecessary.
3083 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003084 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003085 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003086 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3087 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003088 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3089 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003090
3091 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3092 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003093 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003094
John McCall0b251722010-08-04 05:59:32 +00003095 // Allocate memory for the setjmp buffer. This needs to be kept
3096 // live throughout the try and catch blocks.
3097 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3098 "exceptiondata.ptr");
3099
John McCall87bb5822010-07-31 23:20:56 +00003100 // Create the fragile hazards. Note that this will not capture any
3101 // of the allocas required for exception processing, but will
3102 // capture the current basic block (which extends all the way to the
3103 // setjmp call) as "before the @try".
3104 FragileHazards Hazards(CGF);
3105
John McCallf1549f62010-07-06 01:34:17 +00003106 // Create a flag indicating whether the cleanup needs to call
3107 // objc_exception_try_exit. This is true except when
3108 // - no catches match and we're branching through the cleanup
3109 // just to rethrow the exception, or
3110 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003111 // The setjmp-safety rule here is that we should always store to this
3112 // variable in a place that dominates the branch through the cleanup
3113 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003114 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003115 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003116
John McCall9e2213d2010-10-04 23:42:51 +00003117 // A slot containing the exception to rethrow. Only needed when we
3118 // have both a @catch and a @finally.
3119 llvm::Value *PropagatingExnVar = 0;
3120
John McCallf1549f62010-07-06 01:34:17 +00003121 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003122 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003123 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003124 CallTryExitVar,
3125 ExceptionData,
3126 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003127
3128 // Enter a try block:
3129 // - Call objc_exception_try_enter to push ExceptionData on top of
3130 // the EH stack.
3131 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3132 ->setDoesNotThrow();
3133
3134 // - Call setjmp on the exception data buffer.
3135 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3136 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3137 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003138 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003139 llvm::CallInst *SetJmpResult =
3140 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3141 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003142 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003143
3144 // If setjmp returned 0, enter the protected block; otherwise,
3145 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003146 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3147 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003148 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003149 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3150 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003151
John McCallf1549f62010-07-06 01:34:17 +00003152 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003153 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003154 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003155 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003156 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003157
3158 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003159
John McCallf1549f62010-07-06 01:34:17 +00003160 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003161 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003162
John McCall87bb5822010-07-31 23:20:56 +00003163 // Don't optimize loads of the in-scope locals across this point.
3164 Hazards.emitWriteHazard();
3165
John McCallf1549f62010-07-06 01:34:17 +00003166 // For a @synchronized (or a @try with no catches), just branch
3167 // through the cleanup to the rethrow block.
3168 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3169 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003170 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003171 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003172
3173 // Otherwise, we have to match against the caught exceptions.
3174 } else {
John McCall0b251722010-08-04 05:59:32 +00003175 // Retrieve the exception object. We may emit multiple blocks but
3176 // nothing can cross this so the value is already in SSA form.
3177 llvm::CallInst *Caught =
3178 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3179 ExceptionData, "caught");
3180 Caught->setDoesNotThrow();
3181
John McCallf1549f62010-07-06 01:34:17 +00003182 // Push the exception to rethrow onto the EH value stack for the
3183 // benefit of any @throws in the handlers.
3184 CGF.ObjCEHValueStack.push_back(Caught);
3185
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003186 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003187
John McCall0b251722010-08-04 05:59:32 +00003188 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003189
John McCall0b251722010-08-04 05:59:32 +00003190 llvm::BasicBlock *CatchBlock = 0;
3191 llvm::BasicBlock *CatchHandler = 0;
3192 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003193 // Save the currently-propagating exception before
3194 // objc_exception_try_enter clears the exception slot.
3195 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3196 "propagating_exception");
3197 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3198
John McCall0b251722010-08-04 05:59:32 +00003199 // Enter a new exception try block (in case a @catch block
3200 // throws an exception).
3201 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3202 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003203
John McCall0b251722010-08-04 05:59:32 +00003204 llvm::CallInst *SetJmpResult =
3205 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3206 "setjmp.result");
3207 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003208 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003209
John McCall0b251722010-08-04 05:59:32 +00003210 llvm::Value *Threw =
3211 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3212
3213 CatchBlock = CGF.createBasicBlock("catch");
3214 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3215 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3216
3217 CGF.EmitBlock(CatchBlock);
3218 }
3219
3220 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003221
Daniel Dunbar55e40722008-09-27 07:03:52 +00003222 // Handle catch list. As a special case we check if everything is
3223 // matched and avoid generating code for falling off the end if
3224 // so.
3225 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003226 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3227 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003228
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003229 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003230 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003231
Anders Carlsson80f25672008-09-09 17:59:25 +00003232 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003233 if (!CatchParam) {
3234 AllMatched = true;
3235 } else {
John McCall183700f2009-09-21 23:43:11 +00003236 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003237
John McCallf1549f62010-07-06 01:34:17 +00003238 // catch(id e) always matches under this ABI, since only
3239 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003240 // FIXME: For the time being we also match id<X>; this should
3241 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003242 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003243 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003244 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003245
John McCallf1549f62010-07-06 01:34:17 +00003246 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003247 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003248 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3249
Anders Carlssondde0a942008-09-11 09:15:33 +00003250 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003251 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003252 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003253
3254 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003255 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003256 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003257
Anders Carlssondde0a942008-09-11 09:15:33 +00003258 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003259
3260 // The scope of the catch variable ends right here.
3261 CatchVarCleanups.ForceCleanup();
3262
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003263 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003264 break;
3265 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003266
Steve Naroff14108da2009-07-10 23:34:53 +00003267 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003268 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003269
3270 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003271 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3272 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003273
3274 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003275 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003276
John McCallf1549f62010-07-06 01:34:17 +00003277 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003278 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3279 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003280 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281
John McCallf1549f62010-07-06 01:34:17 +00003282 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3283 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284
3285 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003286 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003287
Anders Carlsson80f25672008-09-09 17:59:25 +00003288 // Emit the @catch block.
3289 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003290
3291 // Collect any cleanups for the catch variable. The scope lasts until
3292 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003293 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003294
John McCallb6bbcc92010-10-15 04:57:14 +00003295 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003296 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003297
John McCallf1549f62010-07-06 01:34:17 +00003298 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003299 llvm::Value *Tmp =
3300 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003301 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003302 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003303
Anders Carlssondde0a942008-09-11 09:15:33 +00003304 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003305
3306 // We're done with the catch variable.
3307 CatchVarCleanups.ForceCleanup();
3308
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003309 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003310
Anders Carlsson80f25672008-09-09 17:59:25 +00003311 CGF.EmitBlock(NextCatchBlock);
3312 }
3313
John McCallf1549f62010-07-06 01:34:17 +00003314 CGF.ObjCEHValueStack.pop_back();
3315
John McCall0b251722010-08-04 05:59:32 +00003316 // If nothing wanted anything to do with the caught exception,
3317 // kill the extract call.
3318 if (Caught->use_empty())
3319 Caught->eraseFromParent();
3320
3321 if (!AllMatched)
3322 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3323
3324 if (HasFinally) {
3325 // Emit the exception handler for the @catch blocks.
3326 CGF.EmitBlock(CatchHandler);
3327
3328 // In theory we might now need a write hazard, but actually it's
3329 // unnecessary because there's no local-accessing code between
3330 // the try's write hazard and here.
3331 //Hazards.emitWriteHazard();
3332
John McCall9e2213d2010-10-04 23:42:51 +00003333 // Extract the new exception and save it to the
3334 // propagating-exception slot.
3335 assert(PropagatingExnVar);
3336 llvm::CallInst *NewCaught =
3337 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3338 ExceptionData, "caught");
3339 NewCaught->setDoesNotThrow();
3340 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3341
John McCall0b251722010-08-04 05:59:32 +00003342 // Don't pop the catch handler; the throw already did.
3343 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003344 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003345 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003346 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003347
John McCall87bb5822010-07-31 23:20:56 +00003348 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003349 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003350
John McCallf1549f62010-07-06 01:34:17 +00003351 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003352 CGF.Builder.restoreIP(TryFallthroughIP);
3353 if (CGF.HaveInsertPoint())
3354 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003355 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003356 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003357
John McCallf1549f62010-07-06 01:34:17 +00003358 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003359 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003360 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003361 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003362 // If we have a propagating-exception variable, check it.
3363 llvm::Value *PropagatingExn;
3364 if (PropagatingExnVar) {
3365 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003366
John McCall9e2213d2010-10-04 23:42:51 +00003367 // Otherwise, just look in the buffer for the exception to throw.
3368 } else {
3369 llvm::CallInst *Caught =
3370 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3371 ExceptionData);
3372 Caught->setDoesNotThrow();
3373 PropagatingExn = Caught;
3374 }
3375
3376 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003377 ->setDoesNotThrow();
3378 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003379 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003380
John McCall87bb5822010-07-31 23:20:56 +00003381 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003382}
3383
3384void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003385 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003386 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003387
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003388 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003389 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003390 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003391 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003392 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003393 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003394 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003395 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003396 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003397
John McCallf1549f62010-07-06 01:34:17 +00003398 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3399 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003400 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003401
3402 // Clear the insertion point to indicate we are in unreachable code.
3403 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003404}
3405
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003406/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003407/// object: objc_read_weak (id *src)
3408///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003409llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003410 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003411 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003412 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3413 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3414 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003415 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003416 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003417 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003418 return read_weak;
3419}
3420
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003421/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3422/// objc_assign_weak (id src, id *dst)
3423///
3424void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003425 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003426 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003427 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003428 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003429 assert(Size <= 8 && "does not support size > 8");
3430 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003431 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003432 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3433 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003434 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3435 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003436 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003437 src, dst, "weakassign");
3438 return;
3439}
3440
Fariborz Jahanian58626502008-11-19 00:59:10 +00003441/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3442/// objc_assign_global (id src, id *dst)
3443///
3444void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003445 llvm::Value *src, llvm::Value *dst,
3446 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003447 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003448 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003449 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003450 assert(Size <= 8 && "does not support size > 8");
3451 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003452 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003453 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3454 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003455 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3456 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003457 if (!threadlocal)
3458 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3459 src, dst, "globalassign");
3460 else
3461 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3462 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003463 return;
3464}
3465
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003466/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003467/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003468///
3469void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003470 llvm::Value *src, llvm::Value *dst,
3471 llvm::Value *ivarOffset) {
3472 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003473 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003474 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003475 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003476 assert(Size <= 8 && "does not support size > 8");
3477 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003478 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003479 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3480 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003481 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3482 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003483 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3484 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003485 return;
3486}
3487
Fariborz Jahanian58626502008-11-19 00:59:10 +00003488/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3489/// objc_assign_strongCast (id src, id *dst)
3490///
3491void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003492 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003493 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003494 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003495 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003496 assert(Size <= 8 && "does not support size > 8");
3497 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003498 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003499 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3500 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003501 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3502 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003503 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003504 src, dst, "weakassign");
3505 return;
3506}
3507
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003508void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003509 llvm::Value *DestPtr,
3510 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003511 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003512 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3513 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003514 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003515 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003516 return;
3517}
3518
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003519/// EmitObjCValueForIvar - Code Gen for ivar reference.
3520///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003521LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3522 QualType ObjectTy,
3523 llvm::Value *BaseValue,
3524 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003525 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003526 const ObjCInterfaceDecl *ID =
3527 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003528 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3529 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003530}
3531
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003532llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003533 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003534 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003535 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003536 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003537 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3538 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003539}
3540
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003541/* *** Private Interface *** */
3542
3543/// EmitImageInfo - Emit the image info marker used to encode some module
3544/// level information.
3545///
3546/// See: <rdr://4810609&4810587&4810587>
3547/// struct IMAGE_INFO {
3548/// unsigned version;
3549/// unsigned flags;
3550/// };
3551enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003552 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003553 eImageInfo_GarbageCollected = (1 << 1),
3554 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003555 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3556
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003557 // A flag indicating that the module has no instances of a @synthesize of a
3558 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003559 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003560};
3561
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003562void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003563 unsigned version = 0; // Version is unused?
3564 unsigned flags = 0;
3565
3566 // FIXME: Fix and continue?
Douglas Gregore289d812011-09-13 17:21:33 +00003567 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003568 flags |= eImageInfo_GarbageCollected;
Douglas Gregore289d812011-09-13 17:21:33 +00003569 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003570 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003572 // We never allow @synthesize of a superclass property.
3573 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003574
Chris Lattner2acc6e32011-07-18 04:24:23 +00003575 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003576
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003577 // Emitted as int[2];
3578 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003579 llvm::ConstantInt::get(Int32Ty, version),
3580 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003581 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003582 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003583
3584 const char *Section;
3585 if (ObjCABI == 1)
3586 Section = "__OBJC, __image_info,regular";
3587 else
3588 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003589 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003590 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003591 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003592 Section,
3593 0,
3594 true);
3595 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003596}
3597
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003598
3599// struct objc_module {
3600// unsigned long version;
3601// unsigned long size;
3602// const char *name;
3603// Symtab symtab;
3604// };
3605
3606// FIXME: Get from somewhere
3607static const int ModuleVersion = 7;
3608
3609void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003610 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003611
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003612 llvm::Constant *Values[] = {
3613 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3614 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3615 // This used to be the filename, now it is unused. <rdr://4327263>
3616 GetClassName(&CGM.getContext().Idents.get("")),
3617 EmitModuleSymbols()
3618 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003619 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003620 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003621 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003622 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003623}
3624
3625llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003626 unsigned NumClasses = DefinedClasses.size();
3627 unsigned NumCategories = DefinedCategories.size();
3628
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003629 // Return null if no symbols were defined.
3630 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003631 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003632
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003633 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003634 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003635 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003636 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3637 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003638
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003639 // The runtime expects exactly the list of defined classes followed
3640 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003641 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003642 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003643 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003644 ObjCTypes.Int8PtrTy);
3645 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003646 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003647 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003648 ObjCTypes.Int8PtrTy);
3649
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003650 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003651 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003652 NumClasses + NumCategories),
3653 Symbols);
3654
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003655 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003656
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003657 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003658 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3659 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003660 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003661 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003662}
3663
John McCallf85e1932011-06-15 23:02:42 +00003664llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3665 IdentifierInfo *II) {
3666 LazySymbols.insert(II);
3667
3668 llvm::GlobalVariable *&Entry = ClassReferences[II];
3669
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003670 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003671 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003672 llvm::ConstantExpr::getBitCast(GetClassName(II),
3673 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003675 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3676 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3677 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003678 }
John McCallf85e1932011-06-15 23:02:42 +00003679
Benjamin Kramer578faa82011-09-27 21:06:10 +00003680 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003681}
3682
John McCallf85e1932011-06-15 23:02:42 +00003683llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3684 const ObjCInterfaceDecl *ID) {
3685 return EmitClassRefFromId(Builder, ID->getIdentifier());
3686}
3687
3688llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3689 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3690 return EmitClassRefFromId(Builder, II);
3691}
3692
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003693llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3694 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003695 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003696
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003697 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003698 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003699 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003700 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003701 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003702 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3703 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003704 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003705 }
3706
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003707 if (lvalue)
3708 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003709 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003710}
3711
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003712llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003713 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003714
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003715 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003716 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003717 llvm::ConstantArray::get(VMContext,
3718 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003719 ((ObjCABI == 2) ?
3720 "__TEXT,__objc_classname,cstring_literals" :
3721 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003722 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003723
Owen Andersona1cf15f2009-07-14 23:10:40 +00003724 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003725}
3726
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003727llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3728 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3729 I = MethodDefinitions.find(MD);
3730 if (I != MethodDefinitions.end())
3731 return I->second;
3732
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003733 return NULL;
3734}
3735
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003736/// GetIvarLayoutName - Returns a unique constant for the given
3737/// ivar layout bitmap.
3738llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003739 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003740 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003741}
3742
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003743void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003744 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003745 bool ForStrongLayout,
3746 bool &HasUnion) {
3747 const RecordDecl *RD = RT->getDecl();
3748 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003749 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003750 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003751 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003752 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003754 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3755 ForStrongLayout, HasUnion);
3756}
3757
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003758void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003759 const llvm::StructLayout *Layout,
3760 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003761 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003762 unsigned int BytePos, bool ForStrongLayout,
3763 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003764 bool IsUnion = (RD && RD->isUnion());
3765 uint64_t MaxUnionIvarSize = 0;
3766 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003767 const FieldDecl *MaxField = 0;
3768 const FieldDecl *MaxSkippedField = 0;
3769 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003770 uint64_t MaxFieldOffset = 0;
3771 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003772 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003773 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003774
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003775 if (RecFields.empty())
3776 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003777 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3778 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003779 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003780 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003781 FirstFieldDelta =
3782 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3783 }
3784
Chris Lattnerf1690852009-03-31 08:48:01 +00003785 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003786 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003787 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003788 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003789 // Note that 'i' here is actually the field index inside RD of Field,
3790 // although this dependency is hidden.
3791 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003792 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003793 } else
John McCallf85e1932011-06-15 23:02:42 +00003794 FieldOffset =
3795 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003796
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003797 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003798 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003799 LastFieldBitfieldOrUnnamed = Field;
3800 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003801 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003802 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003803
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003804 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003805 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003806 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003807 if (FQT->isUnionType())
3808 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003809
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003810 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003811 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003812 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003813 continue;
3814 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003815
Chris Lattnerf1690852009-03-31 08:48:01 +00003816 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003817 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003818 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003819 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003820 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003821 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003822 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3823 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003824 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003825 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003826 FQT = CArray->getElementType();
3827 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003828
3829 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003830 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003831 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003832 int OldIndex = IvarsInfo.size() - 1;
3833 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003834
Ted Kremenek6217b802009-07-29 21:53:49 +00003835 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003836 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003837 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003838
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003839 // Replicate layout information for each array element. Note that
3840 // one element is already done.
3841 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003842 for (int FirstIndex = IvarsInfo.size() - 1,
3843 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003844 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003845 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3846 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3847 IvarsInfo[i].ivar_size));
3848 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3849 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3850 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003851 }
3852 continue;
3853 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003854 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003855 // At this point, we are done with Record/Union and array there of.
3856 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003857 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003858
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003859 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003860 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3861 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003862 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003863 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003864 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003865 MaxUnionIvarSize = UnionIvarSize;
3866 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003867 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003868 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003869 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003870 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003871 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003872 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003873 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003874 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3875 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003876 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003877 // FIXME: Why the asymmetry? We divide by word size in bits on other
3878 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003879 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003880 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003881 MaxSkippedUnionIvarSize = UnionIvarSize;
3882 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003883 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003884 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003885 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003886 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003887 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003888 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003889 }
3890 }
3891 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003892
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003893 if (LastFieldBitfieldOrUnnamed) {
3894 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3895 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003896 uint64_t BitFieldSize
3897 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003898 GC_IVAR skivar;
3899 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3900 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3901 + ((BitFieldSize % ByteSizeInBits) != 0);
3902 SkipIvars.push_back(skivar);
3903 } else {
3904 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3905 // Last field was unnamed. Must update skip info.
3906 unsigned FieldSize
3907 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3908 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3909 FieldSize / ByteSizeInBits));
3910 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003911 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003912
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003913 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003914 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003915 MaxUnionIvarSize));
3916 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003917 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003918 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003919}
3920
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003921/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3922/// the computations and returning the layout bitmap (for ivar or blocks) in
3923/// the given argument BitMap string container. Routine reads
3924/// two containers, IvarsInfo and SkipIvars which are assumed to be
3925/// filled already by the caller.
3926llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003927 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003928 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003929
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003930 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003931 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003932 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003933 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003934 if (IvarsInfo[0].ivar_bytepos == 0) {
3935 WordsToSkip = 0;
3936 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003937 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003938 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3939 WordsToScan = IvarsInfo[0].ivar_size;
3940 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003941 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003942 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003943 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003944 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003945 // consecutive 'scanned' object pointers.
3946 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003947 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003948 // Skip over 'gc'able object pointer which lay over each other.
3949 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3950 continue;
3951 // Must skip over 1 or more words. We save current skip/scan values
3952 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003953 SKIP_SCAN SkScan;
3954 SkScan.skip = WordsToSkip;
3955 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003956 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003957
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003958 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003959 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3960 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003961 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003962 WordsToSkip = 0;
3963 WordsToScan = IvarsInfo[i].ivar_size;
3964 }
3965 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003966 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003967 SKIP_SCAN SkScan;
3968 SkScan.skip = WordsToSkip;
3969 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003970 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003971 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003972
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003973 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003974 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003975 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003976 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003977 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003978 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003979 IvarsInfo[LastIndex].ivar_bytepos +
3980 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003981 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003982 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003983 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003984 SKIP_SCAN SkScan;
3985 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3986 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003987 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003988 }
3989 }
3990 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3991 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003992 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003993 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003994 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3995 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3996 // 0xM0 followed by 0x0N detected.
3997 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3998 for (int j = i+1; j < SkipScan; j++)
3999 SkipScanIvars[j] = SkipScanIvars[j+1];
4000 --SkipScan;
4001 }
4002 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004003
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004004 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004005 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004006 unsigned char byte;
4007 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4008 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4009 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4010 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004011
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004012 // first skip big.
4013 for (unsigned int ix = 0; ix < skip_big; ix++)
4014 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004015
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004016 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004017 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004018 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004019 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004020 byte |= 0xf;
4021 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004022 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004023 byte |= scan_small;
4024 scan_small = 0;
4025 }
4026 BitMap += byte;
4027 }
4028 // next scan big
4029 for (unsigned int ix = 0; ix < scan_big; ix++)
4030 BitMap += (unsigned char)(0x0f);
4031 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004032 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004033 byte = scan_small;
4034 BitMap += byte;
4035 }
4036 }
4037 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004038 unsigned char zero = 0;
4039 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004040
4041 llvm::GlobalVariable * Entry =
4042 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
4043 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004044 ((ObjCABI == 2) ?
4045 "__TEXT,__objc_classname,cstring_literals" :
4046 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004047 1, true);
4048 return getConstantGEP(VMContext, Entry, 0, 0);
4049}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004050
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004051/// BuildIvarLayout - Builds ivar layout bitmap for the class
4052/// implementation for the __strong or __weak case.
4053/// The layout map displays which words in ivar list must be skipped
4054/// and which must be scanned by GC (see below). String is built of bytes.
4055/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4056/// of words to skip and right nibble is count of words to scan. So, each
4057/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4058/// represented by a 0x00 byte which also ends the string.
4059/// 1. when ForStrongLayout is true, following ivars are scanned:
4060/// - id, Class
4061/// - object *
4062/// - __strong anything
4063///
4064/// 2. When ForStrongLayout is false, following ivars are scanned:
4065/// - __weak anything
4066///
4067llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4068 const ObjCImplementationDecl *OMD,
4069 bool ForStrongLayout) {
4070 bool hasUnion = false;
4071
Chris Lattner2acc6e32011-07-18 04:24:23 +00004072 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregore289d812011-09-13 17:21:33 +00004073 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00004074 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004075 return llvm::Constant::getNullValue(PtrTy);
4076
Jordy Rosedb8264e2011-07-22 02:08:32 +00004077 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4078 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004079 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004080 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004081 IVD; IVD = IVD->getNextIvar())
4082 RecFields.push_back(cast<FieldDecl>(IVD));
4083 }
4084 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004085 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004086 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004087
Jordy Rosedb8264e2011-07-22 02:08:32 +00004088 // FIXME: This is not ideal; we shouldn't have to do this copy.
4089 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004090 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004091
4092 if (RecFields.empty())
4093 return llvm::Constant::getNullValue(PtrTy);
4094
4095 SkipIvars.clear();
4096 IvarsInfo.clear();
4097
4098 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4099 if (IvarsInfo.empty())
4100 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004101 // Sort on byte position in case we encounterred a union nested in
4102 // the ivar list.
4103 if (hasUnion && !IvarsInfo.empty())
4104 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4105 if (hasUnion && !SkipIvars.empty())
4106 std::sort(SkipIvars.begin(), SkipIvars.end());
4107
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004108 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004109 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004110
4111 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004112 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004113 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004114 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004115 const unsigned char *s = (unsigned char*)BitMap.c_str();
4116 for (unsigned i = 0; i < BitMap.size(); i++)
4117 if (!(s[i] & 0xf0))
4118 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4119 else
4120 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4121 printf("\n");
4122 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004123 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004124}
4125
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004126llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004127 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4128
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004129 // FIXME: Avoid std::string copying.
4130 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00004132 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004133 ((ObjCABI == 2) ?
4134 "__TEXT,__objc_methname,cstring_literals" :
4135 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004136 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004137
Owen Andersona1cf15f2009-07-14 23:10:40 +00004138 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004139}
4140
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004141// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004142llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004143 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4144}
4145
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004146llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004147 std::string TypeStr;
4148 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4149
4150 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004151
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004152 if (!Entry)
4153 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004154 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004155 ((ObjCABI == 2) ?
4156 "__TEXT,__objc_methtype,cstring_literals" :
4157 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004158 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004159
Owen Andersona1cf15f2009-07-14 23:10:40 +00004160 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004161}
4162
Bob Wilsondc8dab62011-11-30 01:57:58 +00004163llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4164 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004165 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00004166 if (CGM.getContext().getObjCEncodingForMethodDecl(
4167 const_cast<ObjCMethodDecl*>(D),
Bob Wilsondc8dab62011-11-30 01:57:58 +00004168 TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004169 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004170
4171 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4172
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004173 if (!Entry)
4174 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004175 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004176 ((ObjCABI == 2) ?
4177 "__TEXT,__objc_methtype,cstring_literals" :
4178 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004179 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004180
Owen Andersona1cf15f2009-07-14 23:10:40 +00004181 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004182}
4183
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004184// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004185llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004186 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004188 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004189 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004190 llvm::ConstantArray::get(VMContext,
4191 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004192 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004193 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004194
Owen Andersona1cf15f2009-07-14 23:10:40 +00004195 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004196}
4197
4198// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004199// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004200llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4202 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004203 std::string TypeStr;
4204 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004205 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4206}
4207
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004208void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004209 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004210 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004211 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004212 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004213 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4214 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004215 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004216 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004217 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004218 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004219}
4220
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004221void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004222 EmitModuleInfo();
4223
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004224 // Emit the dummy bodies for any protocols which were referenced but
4225 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004226 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004227 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4228 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004229 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004230
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004231 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004232 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004233 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004234 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004235 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004236 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004237 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004238 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004239 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004240 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004241 }
4242
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004243 // Add assembler directives to add lazy undefined symbol references
4244 // for classes which are referenced but not defined. This is
4245 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004246 //
4247 // FIXME: It would be nice if we had an LLVM construct for this.
4248 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4249 llvm::SmallString<256> Asm;
4250 Asm += CGM.getModule().getModuleInlineAsm();
4251 if (!Asm.empty() && Asm.back() != '\n')
4252 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004253
Daniel Dunbar33063492009-09-07 00:20:42 +00004254 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004255 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4256 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004257 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4258 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004259 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004260 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004261 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004262 }
4263
4264 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4265 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4266 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4267 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004268
Daniel Dunbar33063492009-09-07 00:20:42 +00004269 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004270 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004271}
4272
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004273CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004274 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004275 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004276 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004277 ObjCABI = 2;
4278}
4279
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004280/* *** */
4281
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004282ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004283 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4284{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004285 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4286 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004287
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004288 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004289 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004290 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004291 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004292 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilsondc8dab62011-11-30 01:57:58 +00004293 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004294
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004295 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004296 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004297 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004298
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004299 // I'm not sure I like this. The implicit coordination is a bit
4300 // gross. We should solve this in a reasonable fashion because this
4301 // is a pretty common task (match some runtime data structure with
4302 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004303
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004304 // FIXME: This is leaked.
4305 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004306
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004307 // struct _objc_super {
4308 // id self;
4309 // Class cls;
4310 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004311 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004312 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004313 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004315 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004316 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004317 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004318 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004319 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004320
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004321 SuperCTy = Ctx.getTagDeclType(RD);
4322 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004323
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004324 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004325 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4326
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004327 // struct _prop_t {
4328 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004329 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004330 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004331 PropertyTy = llvm::StructType::create("struct._prop_t",
4332 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004333
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004334 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004335 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004336 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004337 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004338 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004339 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004340 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4341 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004342 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004343 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004344
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004345 // struct _objc_method {
4346 // SEL _cmd;
4347 // char *method_type;
4348 // char *_imp;
4349 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004350 MethodTy = llvm::StructType::create("struct._objc_method",
4351 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4352 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004354 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004355 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004356 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004357
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004358}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004359
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004360ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004361 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004362 // struct _objc_method_description {
4363 // SEL name;
4364 // char *types;
4365 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004366 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004367 llvm::StructType::create("struct._objc_method_description",
4368 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004369
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004370 // struct _objc_method_description_list {
4371 // int count;
4372 // struct _objc_method_description[1];
4373 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004375 llvm::StructType::create("struct._objc_method_description_list",
4376 IntTy,
4377 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004378
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004379 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004380 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004381 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004382
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004383 // Protocol description structures
4384
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004385 // struct _objc_protocol_extension {
4386 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4387 // struct _objc_method_description_list *optional_instance_methods;
4388 // struct _objc_method_description_list *optional_class_methods;
4389 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004390 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004391 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004392 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004393 llvm::StructType::create("struct._objc_protocol_extension",
4394 IntTy, MethodDescriptionListPtrTy,
4395 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004396 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004397
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004398 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004399 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004400
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004401 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004402
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004403 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004404 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004405
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004406 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004407 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004408 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004409 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004410 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004411 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004412
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004413 // struct _objc_protocol {
4414 // struct _objc_protocol_extension *isa;
4415 // char *protocol_name;
4416 // struct _objc_protocol **_objc_protocol_list;
4417 // struct _objc_method_description_list *instance_methods;
4418 // struct _objc_method_description_list *class_methods;
4419 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004420 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4421 llvm::PointerType::getUnqual(ProtocolListTy),
4422 MethodDescriptionListPtrTy,
4423 MethodDescriptionListPtrTy,
4424 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004425
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004426 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004427 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004428
Owen Anderson96e0fc72009-07-29 22:16:19 +00004429 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004430
4431 // Class description structures
4432
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004433 // struct _objc_ivar {
4434 // char *ivar_name;
4435 // char *ivar_type;
4436 // int ivar_offset;
4437 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004438 IvarTy = llvm::StructType::create("struct._objc_ivar",
4439 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004440
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004441 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004442 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004443 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004444 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004445
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004446 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004447 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004448 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004449 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004450
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004451 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004452 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004453 llvm::StructType::create("struct._objc_class_extension",
4454 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004455 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004456
Chris Lattnerc1c20112011-08-12 17:43:31 +00004457 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004458
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004459 // struct _objc_class {
4460 // Class isa;
4461 // Class super_class;
4462 // char *name;
4463 // long version;
4464 // long info;
4465 // long instance_size;
4466 // struct _objc_ivar_list *ivars;
4467 // struct _objc_method_list *methods;
4468 // struct _objc_cache *cache;
4469 // struct _objc_protocol_list *protocols;
4470 // char *ivar_layout;
4471 // struct _objc_class_ext *ext;
4472 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004473 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4474 llvm::PointerType::getUnqual(ClassTy),
4475 Int8PtrTy,
4476 LongTy,
4477 LongTy,
4478 LongTy,
4479 IvarListPtrTy,
4480 MethodListPtrTy,
4481 CachePtrTy,
4482 ProtocolListPtrTy,
4483 Int8PtrTy,
4484 ClassExtensionPtrTy,
4485 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004486
Owen Anderson96e0fc72009-07-29 22:16:19 +00004487 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004488
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004489 // struct _objc_category {
4490 // char *category_name;
4491 // char *class_name;
4492 // struct _objc_method_list *instance_method;
4493 // struct _objc_method_list *class_method;
4494 // uint32_t size; // sizeof(struct _objc_category)
4495 // struct _objc_property_list *instance_properties;// category's @property
4496 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004497 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004498 llvm::StructType::create("struct._objc_category",
4499 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4500 MethodListPtrTy, ProtocolListPtrTy,
4501 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004502
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004503 // Global metadata structures
4504
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004505 // struct _objc_symtab {
4506 // long sel_ref_cnt;
4507 // SEL *refs;
4508 // short cls_def_cnt;
4509 // short cat_def_cnt;
4510 // char *defs[cls_def_cnt + cat_def_cnt];
4511 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004512 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004513 llvm::StructType::create("struct._objc_symtab",
4514 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4515 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004516 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004517
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004518 // struct _objc_module {
4519 // long version;
4520 // long size; // sizeof(struct _objc_module)
4521 // char *name;
4522 // struct _objc_symtab* symtab;
4523 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004525 llvm::StructType::create("struct._objc_module",
4526 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004527
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004528
Mike Stumpf5408fe2009-05-16 07:57:57 +00004529 // FIXME: This is the size of the setjmp buffer and should be target
4530 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004531 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004532
Anders Carlsson124526b2008-09-09 10:10:21 +00004533 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004534 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004535 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
4537 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004538 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004539 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4540 SetJmpBufferSize),
4541 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004542
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004543}
4544
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004546 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004547 // struct _method_list_t {
4548 // uint32_t entsize; // sizeof(struct _objc_method)
4549 // uint32_t method_count;
4550 // struct _objc_method method_list[method_count];
4551 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004552 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004553 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4554 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004555 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004556 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004558 // struct _protocol_t {
4559 // id isa; // NULL
4560 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004561 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004562 // const struct method_list_t * const instance_methods;
4563 // const struct method_list_t * const class_methods;
4564 // const struct method_list_t *optionalInstanceMethods;
4565 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004566 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004567 // const uint32_t size; // sizeof(struct _protocol_t)
4568 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004569 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004570 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004571
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004572 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004573 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004574 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004576 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004577 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4578 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4579 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4580 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004581 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4582 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004583
4584 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004585 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Fariborz Jahanianda320092009-01-29 19:24:30 +00004587 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004588 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004589 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004590 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004591 ProtocolListnfABITy->setBody(LongTy,
4592 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4593 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004595 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004596 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004598 // struct _ivar_t {
4599 // unsigned long int *offset; // pointer to ivar offset location
4600 // char *name;
4601 // char *type;
4602 // uint32_t alignment;
4603 // uint32_t size;
4604 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004605 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004606 llvm::StructType::create("struct._ivar_t",
4607 llvm::PointerType::getUnqual(LongTy),
4608 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004609
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004610 // struct _ivar_list_t {
4611 // uint32 entsize; // sizeof(struct _ivar_t)
4612 // uint32 count;
4613 // struct _iver_t list[count];
4614 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004615 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004616 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4617 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618
Owen Anderson96e0fc72009-07-29 22:16:19 +00004619 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004621 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004622 // uint32_t const flags;
4623 // uint32_t const instanceStart;
4624 // uint32_t const instanceSize;
4625 // uint32_t const reserved; // only when building for 64bit targets
4626 // const uint8_t * const ivarLayout;
4627 // const char *const name;
4628 // const struct _method_list_t * const baseMethods;
4629 // const struct _objc_protocol_list *const baseProtocols;
4630 // const struct _ivar_list_t *const ivars;
4631 // const uint8_t * const weakIvarLayout;
4632 // const struct _prop_list_t * const properties;
4633 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004635 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004636 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4637 IntTy, IntTy, IntTy, Int8PtrTy,
4638 Int8PtrTy, MethodListnfABIPtrTy,
4639 ProtocolListnfABIPtrTy,
4640 IvarListnfABIPtrTy,
4641 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004642
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004643 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004644 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004645 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4646 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004647
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004648 // struct _class_t {
4649 // struct _class_t *isa;
4650 // struct _class_t * const superclass;
4651 // void *cache;
4652 // IMP *vtable;
4653 // struct class_ro_t *ro;
4654 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004655
Chris Lattnerc1c20112011-08-12 17:43:31 +00004656 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004657 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4658 llvm::PointerType::getUnqual(ClassnfABITy),
4659 CachePtrTy,
4660 llvm::PointerType::getUnqual(ImpnfABITy),
4661 llvm::PointerType::getUnqual(ClassRonfABITy),
4662 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004664 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004665 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004666
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004667 // struct _category_t {
4668 // const char * const name;
4669 // struct _class_t *const cls;
4670 // const struct _method_list_t * const instance_methods;
4671 // const struct _method_list_t * const class_methods;
4672 // const struct _protocol_list_t * const protocols;
4673 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004674 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004675 CategorynfABITy = llvm::StructType::create("struct._category_t",
4676 Int8PtrTy, ClassnfABIPtrTy,
4677 MethodListnfABIPtrTy,
4678 MethodListnfABIPtrTy,
4679 ProtocolListnfABIPtrTy,
4680 PropertyListPtrTy,
4681 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004682
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004683 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004684 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4685 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004686
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004687 // MessageRefTy - LLVM for:
4688 // struct _message_ref_t {
4689 // IMP messenger;
4690 // SEL name;
4691 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004693 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004694 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004695 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004696 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004697 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004698 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004699 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004700 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004701 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004702 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004703
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004704 MessageRefCTy = Ctx.getTagDeclType(RD);
4705 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4706 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004708 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004709 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004711 // SuperMessageRefTy - LLVM for:
4712 // struct _super_message_ref_t {
4713 // SUPER_IMP messenger;
4714 // SEL name;
4715 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004716 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004717 llvm::StructType::create("struct._super_message_ref_t",
4718 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004719
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004720 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004721 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004722
Daniel Dunbare588b992009-03-01 04:46:24 +00004723
4724 // struct objc_typeinfo {
4725 // const void** vtable; // objc_ehtype_vtable + 2
4726 // const char* name; // c++ typeinfo string
4727 // Class cls;
4728 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004729 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004730 llvm::StructType::create("struct._objc_typeinfo",
4731 llvm::PointerType::getUnqual(Int8PtrTy),
4732 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004733 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004734}
4735
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004736llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004737 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004738
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004739 return NULL;
4740}
4741
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004742void CGObjCNonFragileABIMac::AddModuleClassList(const
4743 std::vector<llvm::GlobalValue*>
4744 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004745 const char *SymbolName,
4746 const char *SectionName) {
4747 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004748
Daniel Dunbar463b8762009-05-15 21:48:48 +00004749 if (!NumClasses)
4750 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004751
Daniel Dunbar463b8762009-05-15 21:48:48 +00004752 std::vector<llvm::Constant*> Symbols(NumClasses);
4753 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004754 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004755 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004756 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004757 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004758 NumClasses),
4759 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Daniel Dunbar463b8762009-05-15 21:48:48 +00004761 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004762 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004763 llvm::GlobalValue::InternalLinkage,
4764 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004765 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004766 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004767 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004768 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004769}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004771void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4772 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004773
Daniel Dunbar463b8762009-05-15 21:48:48 +00004774 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004775 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004777 "\01L_OBJC_LABEL_CLASS_$",
4778 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004779
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004780 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4781 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4782 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4783 continue;
4784 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004785 }
4786
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004787 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4788 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4789 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4790 continue;
4791 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4792 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004793
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004795 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4796 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004797
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004798 // Build list of all implemented category addresses in array
4799 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004800 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004801 "\01L_OBJC_LABEL_CATEGORY_$",
4802 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004803 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004804 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4805 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004806
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004807 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004808}
4809
John McCall944c8432011-05-14 03:10:52 +00004810/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4811/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004812/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004813/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004814bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4815 // At various points we've experimented with using vtable-based
4816 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004817 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004818 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004819 return false;
John McCall944c8432011-05-14 03:10:52 +00004820 case CodeGenOptions::NonLegacy:
4821 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004822 case CodeGenOptions::Mixed:
4823 break;
4824 }
4825
4826 // If so, see whether this selector is in the white-list of things which must
4827 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004828 if (VTableDispatchMethods.empty()) {
4829 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4830 VTableDispatchMethods.insert(GetNullarySelector("class"));
4831 VTableDispatchMethods.insert(GetNullarySelector("self"));
4832 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4833 VTableDispatchMethods.insert(GetNullarySelector("length"));
4834 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004835
John McCall944c8432011-05-14 03:10:52 +00004836 // These are vtable-based if GC is disabled.
4837 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004838 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004839 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4840 VTableDispatchMethods.insert(GetNullarySelector("release"));
4841 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4842 }
4843
4844 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4845 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4846 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4847 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4848 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4849 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4850 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4851
4852 // These are vtable-based if GC is enabled.
4853 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004854 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004855 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4856 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4857
4858 // "countByEnumeratingWithState:objects:count"
4859 IdentifierInfo *KeyIdents[] = {
4860 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4861 &CGM.getContext().Idents.get("objects"),
4862 &CGM.getContext().Idents.get("count")
4863 };
4864 VTableDispatchMethods.insert(
4865 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4866 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004867 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004868
John McCall944c8432011-05-14 03:10:52 +00004869 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004870}
4871
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004872// Metadata flags
4873enum MetaDataDlags {
4874 CLS = 0x0,
4875 CLS_META = 0x1,
4876 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004877 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004878 CLS_EXCEPTION = 0x20,
4879
4880 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4881 CLS_HAS_IVAR_RELEASER = 0x40,
4882 /// class was compiled with -fobjc-arr
4883 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004884};
4885/// BuildClassRoTInitializer - generate meta-data for:
4886/// struct _class_ro_t {
4887/// uint32_t const flags;
4888/// uint32_t const instanceStart;
4889/// uint32_t const instanceSize;
4890/// uint32_t const reserved; // only when building for 64bit targets
4891/// const uint8_t * const ivarLayout;
4892/// const char *const name;
4893/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004895/// const struct _ivar_list_t *const ivars;
4896/// const uint8_t * const weakIvarLayout;
4897/// const struct _prop_list_t * const properties;
4898/// }
4899///
4900llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004901 unsigned flags,
4902 unsigned InstanceStart,
4903 unsigned InstanceSize,
4904 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004905 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004906 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004907
4908 if (CGM.getLangOptions().ObjCAutoRefCount)
4909 flags |= CLS_COMPILED_BY_ARC;
4910
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004911 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4912 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4913 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004914 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004915 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4916 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004917 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004918 // const struct _method_list_t * const baseMethods;
4919 std::vector<llvm::Constant*> Methods;
4920 std::string MethodListName("\01l_OBJC_$_");
4921 if (flags & CLS_META) {
4922 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004924 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004925 // Class methods should always be defined.
4926 Methods.push_back(GetMethodConstant(*i));
4927 }
4928 } else {
4929 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004930 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004931 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004932 // Instance methods should always be defined.
4933 Methods.push_back(GetMethodConstant(*i));
4934 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004935 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004936 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004937 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004938
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004939 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4940 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004941
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004942 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4943 if (llvm::Constant *C = GetMethodConstant(MD))
4944 Methods.push_back(C);
4945 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4946 if (llvm::Constant *C = GetMethodConstant(MD))
4947 Methods.push_back(C);
4948 }
4949 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004950 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004951 Values[ 5] = EmitMethodList(MethodListName,
4952 "__DATA, __objc_const", Methods);
4953
Fariborz Jahanianda320092009-01-29 19:24:30 +00004954 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4955 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004957 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004958 OID->all_referenced_protocol_begin(),
4959 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004961 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004962 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004963 else
4964 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004965 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4966 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004967 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004968 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004969 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004970 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4971 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004972 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004973 Values);
4974 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4976 llvm::GlobalValue::InternalLinkage,
4977 Init,
4978 (flags & CLS_META) ?
4979 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4980 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004981 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004982 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004983 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004984 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004985
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004986}
4987
4988/// BuildClassMetaData - This routine defines that to-level meta-data
4989/// for the given ClassName for:
4990/// struct _class_t {
4991/// struct _class_t *isa;
4992/// struct _class_t * const superclass;
4993/// void *cache;
4994/// IMP *vtable;
4995/// struct class_ro_t *ro;
4996/// }
4997///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004998llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004999 std::string &ClassName,
5000 llvm::Constant *IsAGV,
5001 llvm::Constant *SuperClassGV,
5002 llvm::Constant *ClassRoGV,
5003 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005004 llvm::Constant *Values[] = {
5005 IsAGV,
5006 SuperClassGV,
5007 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5008 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5009 ClassRoGV // &CLASS_RO_GV
5010 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005011 if (!Values[1])
5012 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005014 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005015 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5016 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005017 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005018 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005019 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005020 if (HiddenVisibility)
5021 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005022 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005023}
5024
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005026CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005027 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005028}
5029
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005030void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005031 uint32_t &InstanceStart,
5032 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005033 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005034 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005035
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005036 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005037 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005038
5039 // If there are no fields, the start is the same as the end.
5040 if (!RL.getFieldCount())
5041 InstanceStart = InstanceSize;
5042 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005043 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005044}
5045
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005046void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5047 std::string ClassName = ID->getNameAsString();
5048 if (!ObjCEmptyCacheVar) {
5049 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005050 CGM.getModule(),
5051 ObjCTypes.CacheTy,
5052 false,
5053 llvm::GlobalValue::ExternalLinkage,
5054 0,
5055 "_objc_empty_cache");
5056
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005057 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005058 CGM.getModule(),
5059 ObjCTypes.ImpnfABITy,
5060 false,
5061 llvm::GlobalValue::ExternalLinkage,
5062 0,
5063 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005064 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005065 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005066 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005067 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005068 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00005069 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005070 uint32_t InstanceSize = InstanceStart;
5071 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005072 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5073 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005074
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005075 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005076
5077 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005078 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005079 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005080 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005081 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005082 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005083 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005084 // class is root
5085 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005086 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005087 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005088 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005089 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005090 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5091 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5092 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005093 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005094 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005095 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005096 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005097 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005098 ObjCMetaClassName +
5099 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005100 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005101 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005102 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005103 }
5104 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5105 InstanceStart,
5106 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005107 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005108 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005109 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5110 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005111 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005112
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005113 // Metadata for the class
5114 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005115 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005116 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005117 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005118 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005119
Douglas Gregor68584ed2009-06-18 16:11:24 +00005120 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005121 flags |= CLS_EXCEPTION;
5122
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005123 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005124 flags |= CLS_ROOT;
5125 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005126 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005127 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005128 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005129 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005130 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005131 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005132 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005133 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005134 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005135 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005136 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005138 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005139
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005140 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005141 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005142 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5143 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005144 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005145
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005146 // Determine if this class is also "non-lazy".
5147 if (ImplementationIsNonLazy(ID))
5148 DefinedNonLazyClasses.push_back(ClassMD);
5149
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005150 // Force the definition of the EHType if necessary.
5151 if (flags & CLS_EXCEPTION)
5152 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005153 // Make sure method definition entries are all clear for next implementation.
5154 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005155}
5156
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005157/// GenerateProtocolRef - This routine is called to generate code for
5158/// a protocol reference expression; as in:
5159/// @code
5160/// @protocol(Proto1);
5161/// @endcode
5162/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5163/// which will hold address of the protocol meta-data.
5164///
5165llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005166 const ObjCProtocolDecl *PD) {
5167
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005168 // This routine is called for @protocol only. So, we must build definition
5169 // of protocol's meta-data (not a reference to it!)
5170 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005171 llvm::Constant *Init =
5172 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005173 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005174
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005175 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005176 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005177
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005178 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5179 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005180 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005181 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005182 CGM.getModule(),
5183 Init->getType(), false,
5184 llvm::GlobalValue::WeakAnyLinkage,
5185 Init,
5186 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005187 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5188 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005189 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005190 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005191}
5192
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005193/// GenerateCategory - Build metadata for a category implementation.
5194/// struct _category_t {
5195/// const char * const name;
5196/// struct _class_t *const cls;
5197/// const struct _method_list_t * const instance_methods;
5198/// const struct _method_list_t * const class_methods;
5199/// const struct _protocol_list_t * const protocols;
5200/// const struct _prop_list_t * const properties;
5201/// }
5202///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005203void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005204 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005205 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005206 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5207 "_$_" + OCD->getNameAsString());
5208 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005209 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005210
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005211 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005212 Values[0] = GetClassName(OCD->getIdentifier());
5213 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005214 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005215 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005216 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5217
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005218 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005219 std::vector<llvm::Constant*> Methods;
5220 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005221 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005222 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005223
5224 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005225 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005226 // Instance methods should always be defined.
5227 Methods.push_back(GetMethodConstant(*i));
5228 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005229
5230 Values[2] = EmitMethodList(MethodListName,
5231 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005232 Methods);
5233
5234 MethodListName = Prefix;
5235 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5236 OCD->getNameAsString();
5237 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005239 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005240 // Class methods should always be defined.
5241 Methods.push_back(GetMethodConstant(*i));
5242 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005243
5244 Values[3] = EmitMethodList(MethodListName,
5245 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005246 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005248 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005249 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005250 llvm::SmallString<256> ExtName;
5251 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5252 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005253 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005254 + Interface->getName() + "_$_"
5255 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005256 Category->protocol_begin(),
5257 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005258 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5259 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005260 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005261 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5262 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005263 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005264
5265 llvm::Constant *Init =
5266 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005267 Values);
5268 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005269 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005270 false,
5271 llvm::GlobalValue::InternalLinkage,
5272 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005273 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005274 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005275 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005276 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005277 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005278 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005279
5280 // Determine if this category is also "non-lazy".
5281 if (ImplementationIsNonLazy(OCD))
5282 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005283 // method definition entries must be clear for next implementation.
5284 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005285}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005286
5287/// GetMethodConstant - Return a struct objc_method constant for the
5288/// given method if it has been defined. The result is null if the
5289/// method has not been defined. The return value has type MethodPtrTy.
5290llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005292 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005293 if (!Fn)
5294 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005295
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005296 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005297 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005298 ObjCTypes.SelectorPtrTy),
5299 GetMethodVarType(MD),
5300 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5301 };
Owen Anderson08e25242009-07-27 22:29:56 +00005302 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005303}
5304
5305/// EmitMethodList - Build meta-data for method declarations
5306/// struct _method_list_t {
5307/// uint32_t entsize; // sizeof(struct _objc_method)
5308/// uint32_t method_count;
5309/// struct _objc_method method_list[method_count];
5310/// }
5311///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005312llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005313 const char *Section,
5314 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005315 // Return null for empty list.
5316 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005317 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005319 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005320 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005321 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005322 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005323 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005324 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005325 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005326 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005327 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005328 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005329
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005330 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005331 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005332 llvm::GlobalValue::InternalLinkage, Init, Name);
5333 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005334 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005335 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005336 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005337}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005338
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005339/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5340/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005341llvm::GlobalVariable *
5342CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5343 const ObjCIvarDecl *Ivar) {
5344 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005345 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005346 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005348 CGM.getModule().getGlobalVariable(Name);
5349 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005350 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005351 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005352 false,
5353 llvm::GlobalValue::ExternalLinkage,
5354 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005355 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005356 return IvarOffsetGV;
5357}
5358
Daniel Dunbare83be122010-04-02 21:14:02 +00005359llvm::Constant *
5360CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5361 const ObjCIvarDecl *Ivar,
5362 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005363 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005364 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005365 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005366 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005367 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005368
Mike Stumpf5408fe2009-05-16 07:57:57 +00005369 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5370 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005371 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5372 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005373 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005374 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005375 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005376 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005377 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005378 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005379}
5380
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005381/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005382/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005383/// IvarListnfABIPtrTy.
5384/// struct _ivar_t {
5385/// unsigned long int *offset; // pointer to ivar offset location
5386/// char *name;
5387/// char *type;
5388/// uint32_t alignment;
5389/// uint32_t size;
5390/// }
5391/// struct _ivar_list_t {
5392/// uint32 entsize; // sizeof(struct _ivar_t)
5393/// uint32 count;
5394/// struct _iver_t list[count];
5395/// }
5396///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005397
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005398llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005399 const ObjCImplementationDecl *ID) {
5400
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005401 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005402
Jordy Rosedb8264e2011-07-22 02:08:32 +00005403 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005404 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005405
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005406 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005407
Jordy Rosedb8264e2011-07-22 02:08:32 +00005408 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005409 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005410 // Ignore unnamed bit-fields.
5411 if (!IVD->getDeclName())
5412 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005413 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005414 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005415 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005416 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5417 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005418 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005419 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005420 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005421 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005422 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005423 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005424 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005425 // NOTE. Size of a bitfield does not match gcc's, because of the
5426 // way bitfields are treated special in each. But I am told that
5427 // 'size' for bitfield ivars is ignored by the runtime so it does
5428 // not matter. If it matters, there is enough info to get the
5429 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005430 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005431 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005432 }
5433 // Return null for empty list.
5434 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005435 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005436
5437 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005438 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005439 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5440 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005441 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005442 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005443 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005444 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005445 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5446 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005447 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005448 llvm::GlobalValue::InternalLinkage,
5449 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005450 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005451 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005452 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005453 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454
Chris Lattnerad64e022009-07-17 23:57:13 +00005455 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005456 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457}
5458
5459llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005460 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005461 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462
Fariborz Jahanianda320092009-01-29 19:24:30 +00005463 if (!Entry) {
5464 // We use the initializer as a marker of whether this is a forward
5465 // reference or not. At module finalization we add the empty
5466 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467 Entry =
5468 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5469 llvm::GlobalValue::ExternalLinkage,
5470 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005471 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005472 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005473 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005474
Fariborz Jahanianda320092009-01-29 19:24:30 +00005475 return Entry;
5476}
5477
5478/// GetOrEmitProtocol - Generate the protocol meta-data:
5479/// @code
5480/// struct _protocol_t {
5481/// id isa; // NULL
5482/// const char * const protocol_name;
5483/// const struct _protocol_list_t * protocol_list; // super protocols
5484/// const struct method_list_t * const instance_methods;
5485/// const struct method_list_t * const class_methods;
5486/// const struct method_list_t *optionalInstanceMethods;
5487/// const struct method_list_t *optionalClassMethods;
5488/// const struct _prop_list_t * properties;
5489/// const uint32_t size; // sizeof(struct _protocol_t)
5490/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005491/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005492/// }
5493/// @endcode
5494///
5495
5496llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005497 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005498 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499
Fariborz Jahanianda320092009-01-29 19:24:30 +00005500 // Early exit if a defining object has already been generated.
5501 if (Entry && Entry->hasInitializer())
5502 return Entry;
5503
Douglas Gregor1d784b22012-01-01 19:51:50 +00005504 // Use the protocol definition, if there is one.
5505 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5506 PD = Def;
5507
Fariborz Jahanianda320092009-01-29 19:24:30 +00005508 // Construct method lists.
5509 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5510 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005511 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005512 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005513 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005514 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005515 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005516 if (!C)
5517 return GetOrEmitProtocolRef(PD);
5518
Fariborz Jahanianda320092009-01-29 19:24:30 +00005519 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5520 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005521 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005522 } else {
5523 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005524 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005526 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005527
5528 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005529 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005530 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005531 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005532 if (!C)
5533 return GetOrEmitProtocolRef(PD);
5534
Fariborz Jahanianda320092009-01-29 19:24:30 +00005535 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5536 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005537 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 } else {
5539 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005540 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005542 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005543
Bob Wilsondc8dab62011-11-30 01:57:58 +00005544 MethodTypesExt.insert(MethodTypesExt.end(),
5545 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5546
5547 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005548 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005549 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005550 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005551 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5552 PD->protocol_begin(),
5553 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005554
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005555 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005556 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005557 "__DATA, __objc_const",
5558 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005559 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005560 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005561 "__DATA, __objc_const",
5562 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005563 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005564 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005565 "__DATA, __objc_const",
5566 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005567 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005568 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005569 "__DATA, __objc_const",
5570 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005571 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005572 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005574 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005575 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005576 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005577 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5578 + PD->getName(),
5579 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005580 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005581 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582
Fariborz Jahanianda320092009-01-29 19:24:30 +00005583 if (Entry) {
5584 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005585 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005586 Entry->setInitializer(Init);
5587 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005588 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005589 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5590 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5591 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005592 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005593 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005594 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005595 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005596 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005597 CGM.AddUsedGlobal(Entry);
5598
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005599 // Use this protocol meta-data to build protocol list table in section
5600 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005601 llvm::GlobalVariable *PTGV =
5602 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5603 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5604 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005605 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005606 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005607 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005608 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005609 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005610 return Entry;
5611}
5612
5613/// EmitProtocolList - Generate protocol list meta-data:
5614/// @code
5615/// struct _protocol_list_t {
5616/// long protocol_count; // Note, this is 32/64 bit
5617/// struct _protocol_t[protocol_count];
5618/// }
5619/// @endcode
5620///
5621llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005622CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005623 ObjCProtocolDecl::protocol_iterator begin,
5624 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005625 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005626
Fariborz Jahanianda320092009-01-29 19:24:30 +00005627 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005628 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005629 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005630
Daniel Dunbar948e2582009-02-15 07:36:20 +00005631 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005632 llvm::SmallString<256> TmpName;
5633 Name.toVector(TmpName);
5634 llvm::GlobalVariable *GV =
5635 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005636 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005637 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005638
Daniel Dunbar948e2582009-02-15 07:36:20 +00005639 for (; begin != end; ++begin)
5640 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5641
Fariborz Jahanianda320092009-01-29 19:24:30 +00005642 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005643 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005644 ObjCTypes.ProtocolnfABIPtrTy));
5645
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005646 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005647 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005648 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005649 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005650 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005651 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652 ProtocolRefs.size()),
5653 ProtocolRefs);
5654
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005655 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005656 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005657 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005658 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005659 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005660 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005661 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005662 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005664 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005665}
5666
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005667/// GetMethodDescriptionConstant - This routine build following meta-data:
5668/// struct _objc_method {
5669/// SEL _cmd;
5670/// char *method_type;
5671/// char *_imp;
5672/// }
5673
5674llvm::Constant *
5675CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005676 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005677 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005678 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5679 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005680 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005681 if (!Desc[1])
5682 return 0;
5683
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005684 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005685 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005686 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005687}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005688
5689/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5690/// This code gen. amounts to generating code for:
5691/// @code
5692/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5693/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005694///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005695LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005696 CodeGen::CodeGenFunction &CGF,
5697 QualType ObjectTy,
5698 llvm::Value *BaseValue,
5699 const ObjCIvarDecl *Ivar,
5700 unsigned CVRQualifiers) {
5701 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005702 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5703 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005704}
5705
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005706llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 CodeGen::CodeGenFunction &CGF,
5708 const ObjCInterfaceDecl *Interface,
5709 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005710 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005711}
5712
John McCallb1e81442011-05-13 23:16:18 +00005713static void appendSelectorForMessageRefTable(std::string &buffer,
5714 Selector selector) {
5715 if (selector.isUnarySelector()) {
5716 buffer += selector.getNameForSlot(0);
5717 return;
5718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719
John McCallb1e81442011-05-13 23:16:18 +00005720 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5721 buffer += selector.getNameForSlot(i);
5722 buffer += '_';
5723 }
5724}
5725
John McCall944c8432011-05-14 03:10:52 +00005726/// Emit a "v-table" message send. We emit a weak hidden-visibility
5727/// struct, initially containing the selector pointer and a pointer to
5728/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5729/// load and call the function pointer, passing the address of the
5730/// struct as the second parameter. The runtime determines whether
5731/// the selector is currently emitted using vtable dispatch; if so, it
5732/// substitutes a stub function which simply tail-calls through the
5733/// appropriate vtable slot, and if not, it substitues a stub function
5734/// which tail-calls objc_msgSend. Both stubs adjust the selector
5735/// argument to correctly point to the selector.
5736RValue
5737CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5738 ReturnValueSlot returnSlot,
5739 QualType resultType,
5740 Selector selector,
5741 llvm::Value *arg0,
5742 QualType arg0Type,
5743 bool isSuper,
5744 const CallArgList &formalArgs,
5745 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005746 // Compute the actual arguments.
5747 CallArgList args;
5748
John McCall944c8432011-05-14 03:10:52 +00005749 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005750 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005751 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5752 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005753
John McCall944c8432011-05-14 03:10:52 +00005754 // Second argument: a pointer to the message ref structure. Leave
5755 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005756 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5757
5758 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5759
5760 const CGFunctionInfo &fnInfo =
5761 CGM.getTypes().getFunctionInfo(resultType, args,
5762 FunctionType::ExtInfo());
5763
John McCallcba681a2011-05-14 21:12:11 +00005764 NullReturnState nullReturn;
5765
John McCall944c8432011-05-14 03:10:52 +00005766 // Find the function to call and the mangled name for the message
5767 // ref structure. Using a different mangled name wouldn't actually
5768 // be a problem; it would just be a waste.
5769 //
5770 // The runtime currently never uses vtable dispatch for anything
5771 // except normal, non-super message-sends.
5772 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005773 llvm::Constant *fn = 0;
5774 std::string messageRefName("\01l_");
5775 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005776 if (isSuper) {
5777 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5778 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005779 } else {
John McCallcba681a2011-05-14 21:12:11 +00005780 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005781 fn = ObjCTypes.getMessageSendStretFixupFn();
5782 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005783 }
John McCallb1e81442011-05-13 23:16:18 +00005784 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5785 fn = ObjCTypes.getMessageSendFpretFixupFn();
5786 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005787 } else {
John McCallb1e81442011-05-13 23:16:18 +00005788 if (isSuper) {
5789 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5790 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005791 } else {
John McCallb1e81442011-05-13 23:16:18 +00005792 fn = ObjCTypes.getMessageSendFixupFn();
5793 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005794 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005795 }
John McCallb1e81442011-05-13 23:16:18 +00005796 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5797 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005798
John McCallb1e81442011-05-13 23:16:18 +00005799 // Append the selector name, except use underscores anywhere we
5800 // would have used colons.
5801 appendSelectorForMessageRefTable(messageRefName, selector);
5802
5803 llvm::GlobalVariable *messageRef
5804 = CGM.getModule().getGlobalVariable(messageRefName);
5805 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005806 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005807 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005808 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005809 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5810 init->getType(),
5811 /*constant*/ false,
5812 llvm::GlobalValue::WeakAnyLinkage,
5813 init,
5814 messageRefName);
5815 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5816 messageRef->setAlignment(16);
5817 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5818 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005819
5820 bool requiresnullCheck = false;
5821 if (CGM.getLangOptions().ObjCAutoRefCount && method)
5822 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5823 e = method->param_end(); i != e; ++i) {
5824 const ParmVarDecl *ParamDecl = (*i);
5825 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5826 if (!nullReturn.NullBB)
5827 nullReturn.init(CGF, arg0);
5828 requiresnullCheck = true;
5829 break;
5830 }
5831 }
5832
John McCallb1e81442011-05-13 23:16:18 +00005833 llvm::Value *mref =
5834 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5835
John McCall944c8432011-05-14 03:10:52 +00005836 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005837 args[1].RV = RValue::get(mref);
5838
5839 // Load the function to call from the message ref table.
5840 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5841 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5842
5843 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005844 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005845 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5846 callee = CGF.Builder.CreateBitCast(callee,
5847 llvm::PointerType::getUnqual(fnType));
5848
John McCallcba681a2011-05-14 21:12:11 +00005849 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005850 return nullReturn.complete(CGF, result, resultType, formalArgs,
5851 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005852}
5853
5854/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005855CodeGen::RValue
5856CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005857 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005858 QualType ResultType,
5859 Selector Sel,
5860 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005861 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005862 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005863 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005864 return isVTableDispatchedSelector(Sel)
5865 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005866 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005867 false, CallArgs, Method)
5868 : EmitMessageSend(CGF, Return, ResultType,
5869 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005871 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005872}
5873
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005874llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005875CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005876 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5877
Daniel Dunbardfff2302009-03-02 05:18:14 +00005878 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005880 false, llvm::GlobalValue::ExternalLinkage,
5881 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005882 }
5883
5884 return GV;
5885}
5886
John McCallf85e1932011-06-15 23:02:42 +00005887llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5888 IdentifierInfo *II) {
5889 llvm::GlobalVariable *&Entry = ClassReferences[II];
5890
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005891 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005892 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005893 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005894 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005895 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5896 false, llvm::GlobalValue::InternalLinkage,
5897 ClassGV,
5898 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005899 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005900 CGM.getTargetData().getABITypeAlignment(
5901 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005902 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005903 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005904 }
John McCallf85e1932011-06-15 23:02:42 +00005905
Benjamin Kramer578faa82011-09-27 21:06:10 +00005906 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005907}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005908
John McCallf85e1932011-06-15 23:02:42 +00005909llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5910 const ObjCInterfaceDecl *ID) {
5911 return EmitClassRefFromId(Builder, ID->getIdentifier());
5912}
5913
5914llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5915 CGBuilderTy &Builder) {
5916 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5917 return EmitClassRefFromId(Builder, II);
5918}
5919
Daniel Dunbar11394522009-04-18 08:51:00 +00005920llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005922 const ObjCInterfaceDecl *ID) {
5923 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005924
Daniel Dunbar11394522009-04-18 08:51:00 +00005925 if (!Entry) {
5926 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5927 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005928 Entry =
5929 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005930 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005931 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005932 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005933 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005934 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005935 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005936 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005937 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005938 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005939
Benjamin Kramer578faa82011-09-27 21:06:10 +00005940 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005941}
5942
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005943/// EmitMetaClassRef - Return a Value * of the address of _class_t
5944/// meta-data
5945///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005946llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5947 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005948 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5949 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005950 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005951
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005952 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005953 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005954 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005955 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005956 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005957 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005958 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005959 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005960 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005961 ObjCTypes.ClassnfABIPtrTy));
5962
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005963 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005964 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005965
Benjamin Kramer578faa82011-09-27 21:06:10 +00005966 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005967}
5968
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005969/// GetClass - Return a reference to the class for the given interface
5970/// decl.
5971llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5972 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005973 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005974 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5975 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5976 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5977 }
5978
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005979 return EmitClassRef(Builder, ID);
5980}
5981
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005982/// Generates a message send where the super is the receiver. This is
5983/// a message send to self with special delivery semantics indicating
5984/// which class's method should be called.
5985CodeGen::RValue
5986CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005987 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005988 QualType ResultType,
5989 Selector Sel,
5990 const ObjCInterfaceDecl *Class,
5991 bool isCategoryImpl,
5992 llvm::Value *Receiver,
5993 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005994 const CodeGen::CallArgList &CallArgs,
5995 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005996 // ...
5997 // Create and init a super structure; this is a (receiver, class)
5998 // pair we will pass to objc_msgSendSuper.
5999 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006000 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006001
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006002 llvm::Value *ReceiverAsObject =
6003 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6004 CGF.Builder.CreateStore(ReceiverAsObject,
6005 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006007 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006008 llvm::Value *Target;
6009 if (IsClassMessage) {
6010 if (isCategoryImpl) {
6011 // Message sent to "super' in a class method defined in
6012 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00006013 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006014 Target = CGF.Builder.CreateStructGEP(Target, 0);
6015 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00006016 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006017 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00006018 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00006019 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006020
Mike Stumpf5408fe2009-05-16 07:57:57 +00006021 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6022 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006023 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006024 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6025 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6026 CGF.Builder.CreateStore(Target,
6027 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006028
John McCall944c8432011-05-14 03:10:52 +00006029 return (isVTableDispatchedSelector(Sel))
6030 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006031 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006032 true, CallArgs, Method)
6033 : EmitMessageSend(CGF, Return, ResultType,
6034 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006035 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006036 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006037}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006038
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006039llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006040 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006041 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006042
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006043 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006044 llvm::Constant *Casted =
6045 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6046 ObjCTypes.SelectorPtrTy);
6047 Entry =
6048 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6049 llvm::GlobalValue::InternalLinkage,
6050 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006051 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006052 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006053 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006054
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006055 if (lval)
6056 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006057 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6058
6059 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6060 llvm::MDNode::get(VMContext,
6061 ArrayRef<llvm::Value*>()));
6062 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006063}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006064/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006065/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006066///
6067void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006068 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006069 llvm::Value *dst,
6070 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006071 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006072 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006073 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006074 assert(Size <= 8 && "does not support size > 8");
6075 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6076 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006077 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6078 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006079 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6080 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006081 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6082 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006083 return;
6084}
6085
6086/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6087/// objc_assign_strongCast (id src, id *dst)
6088///
6089void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006090 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006091 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006092 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006093 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006094 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006095 assert(Size <= 8 && "does not support size > 8");
6096 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006097 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006098 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6099 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006100 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6101 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006102 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006103 src, dst, "weakassign");
6104 return;
6105}
6106
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006107void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006108 CodeGen::CodeGenFunction &CGF,
6109 llvm::Value *DestPtr,
6110 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006111 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006112 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6113 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006114 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006115 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006116 return;
6117}
6118
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006119/// EmitObjCWeakRead - Code gen for loading value of a __weak
6120/// object: objc_read_weak (id *src)
6121///
6122llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006123 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006124 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006125 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006126 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6127 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006128 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006129 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006130 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006131 return read_weak;
6132}
6133
6134/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6135/// objc_assign_weak (id src, id *dst)
6136///
6137void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006138 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006139 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006140 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006141 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006142 assert(Size <= 8 && "does not support size > 8");
6143 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6144 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006145 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6146 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006147 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6148 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006149 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006150 src, dst, "weakassign");
6151 return;
6152}
6153
6154/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6155/// objc_assign_global (id src, id *dst)
6156///
6157void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006158 llvm::Value *src, llvm::Value *dst,
6159 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006160 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006161 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006162 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006163 assert(Size <= 8 && "does not support size > 8");
6164 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6165 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006166 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6167 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006168 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6169 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006170 if (!threadlocal)
6171 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6172 src, dst, "globalassign");
6173 else
6174 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6175 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006176 return;
6177}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006178
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006179void
John McCallf1549f62010-07-06 01:34:17 +00006180CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6181 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006182 EmitAtSynchronizedStmt(CGF, S,
6183 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6184 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006185}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006186
John McCall5a180392010-07-24 00:37:23 +00006187llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006188CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006189 // There's a particular fixed type info for 'id'.
6190 if (T->isObjCIdType() ||
6191 T->isObjCQualifiedIdType()) {
6192 llvm::Constant *IDEHType =
6193 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6194 if (!IDEHType)
6195 IDEHType =
6196 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6197 false,
6198 llvm::GlobalValue::ExternalLinkage,
6199 0, "OBJC_EHTYPE_id");
6200 return IDEHType;
6201 }
6202
6203 // All other types should be Objective-C interface pointer types.
6204 const ObjCObjectPointerType *PT =
6205 T->getAs<ObjCObjectPointerType>();
6206 assert(PT && "Invalid @catch type.");
6207 const ObjCInterfaceType *IT = PT->getInterfaceType();
6208 assert(IT && "Invalid @catch type.");
6209 return GetInterfaceEHType(IT->getDecl(), false);
6210}
6211
John McCallf1549f62010-07-06 01:34:17 +00006212void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6213 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006214 EmitTryCatchStmt(CGF, S,
6215 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6216 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6217 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006218}
6219
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006220/// EmitThrowStmt - Generate code for a throw statement.
6221void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6222 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006223 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006224 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006225 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006226 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006227 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006228 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006229 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006230 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006231 }
6232
John McCall7ec404c2010-10-16 08:21:07 +00006233 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006234 CGF.Builder.ClearInsertionPoint();
6235}
Daniel Dunbare588b992009-03-01 04:46:24 +00006236
John McCall5a180392010-07-24 00:37:23 +00006237llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006238CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006239 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006240 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006241
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006242 // If we don't need a definition, return the entry if found or check
6243 // if we use an external reference.
6244 if (!ForDefinition) {
6245 if (Entry)
6246 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006247
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006248 // If this type (or a super class) has the __objc_exception__
6249 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006250 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006251 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006252 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006253 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006254 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006255 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006256 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006257 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006258
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006259 // Otherwise we need to either make a new entry or fill in the
6260 // initializer.
6261 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006262 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006263 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006264 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006265 CGM.getModule().getGlobalVariable(VTableName);
6266 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006267 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6268 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006269 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006270 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006271
Chris Lattner77b89b82010-06-27 07:15:29 +00006272 llvm::Value *VTableIdx =
6273 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006274
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006275 llvm::Constant *Values[] = {
6276 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6277 GetClassName(ID->getIdentifier()),
6278 GetClassGlobal(ClassName)
6279 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006280 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006281 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006282
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006283 if (Entry) {
6284 Entry->setInitializer(Init);
6285 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006286 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006287 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006288 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006289 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006290 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006291 }
6292
John McCall1fb0caa2010-10-22 21:05:15 +00006293 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006294 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006295 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6296 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006297
6298 if (ForDefinition) {
6299 Entry->setSection("__DATA,__objc_const");
6300 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6301 } else {
6302 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6303 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006304
6305 return Entry;
6306}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006307
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006308/* *** */
6309
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006310CodeGen::CGObjCRuntime *
6311CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006312 if (CGM.getLangOptions().ObjCNonFragileABI)
6313 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006314 return new CGObjCMac(CGM);
6315}