blob: ece778cc12b804651a448bdba7c0803bfe93133a [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
45namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046
Daniel Dunbar6bff2512009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000049
Fariborz Jahanianee0af742009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000053
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000054private:
John McCall0774cb82011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCallf85e1932011-06-15 23:02:42 +000068 "objc_msgSend",
69 llvm::Attribute::NonLazyBind);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000070 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000071
John McCall0774cb82011-05-15 01:53:33 +000072 /// void objc_msgSend_stret (id, SEL, ...)
73 ///
74 /// The messenger used when the return value is an aggregate returned
75 /// by indirect reference in the first argument, and therefore the
76 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000077 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000078 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000079 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
80 params, true),
81 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000082
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000083 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000084
John McCall0774cb82011-05-15 01:53:33 +000085 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
86 ///
87 /// The messenger used when the return value is returned on the x87
88 /// floating-point stack; without a special entrypoint, the nil case
89 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000090 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000091 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000092 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
93 params, true),
John McCall0774cb82011-05-15 01:53:33 +000094 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000095
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000096 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000097
Anders Carlssoneea64802011-10-31 16:27:11 +000098 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
99 ///
100 /// The messenger used when the return value is returned in two values on the
101 /// x87 floating point stack; without a special entrypoint, the nil case
102 /// would be unbalanced. Only used on 64-bit X86.
103 llvm::Constant *getMessageSendFp2retFn() const {
104 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
105 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
106 llvm::Type *resultType =
107 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
108
109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
110 params, true),
111 "objc_msgSend_fp2ret");
112 }
113
John McCall0774cb82011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
115 ///
116 /// The messenger used for super calls, which have different dispatch
117 /// semantics. The class passed is the superclass of the current
118 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000120 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000121 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000122 params, true),
123 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000124 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000125
John McCall0774cb82011-05-15 01:53:33 +0000126 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
127 ///
128 /// A slightly different messenger used for super calls. The class
129 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000131 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000132 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000133 params, true),
134 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000135 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000136
John McCall0774cb82011-05-15 01:53:33 +0000137 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
138 /// SEL op, ...)
139 ///
140 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000142 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000143 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000144 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000145 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000146 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000147
John McCall0774cb82011-05-15 01:53:33 +0000148 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
149 /// SEL op, ...)
150 ///
151 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000153 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000154 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000155 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000156 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000157 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000158
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000159 llvm::Constant *getMessageSendSuperFpretFn() const {
160 // There is no objc_msgSendSuper_fpret? How can that work?
161 return getMessageSendSuperFn();
162 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000164 llvm::Constant *getMessageSendSuperFpretFn2() const {
165 // There is no objc_msgSendSuper_fpret? How can that work?
166 return getMessageSendSuperFn2();
167 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000168
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000169protected:
170 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000171
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000172public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000173 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000174 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000176 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000177 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000179 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000180 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000182 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000183 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000184
185private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000186 /// ProtocolPtrTy - LLVM type for external protocol handles
187 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000188 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000189
190public:
191 llvm::Type *getExternalProtocolPtrTy() {
192 if (!ExternalProtocolPtrTy) {
193 // FIXME: It would be nice to unify this with the opaque type, so that the
194 // IR comes out a bit cleaner.
195 CodeGen::CodeGenTypes &Types = CGM.getTypes();
196 ASTContext &Ctx = CGM.getContext();
197 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
198 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
199 }
200
201 return ExternalProtocolPtrTy;
202 }
203
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000204 // SuperCTy - clang type for struct objc_super.
205 QualType SuperCTy;
206 // SuperPtrCTy - clang type for struct objc_super *.
207 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000208
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000209 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000210 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000211 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000212 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000213
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000214 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
215 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000216 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000217
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000218 /// PropertyListTy - LLVM type for struct objc_property_list
219 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000220 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000221 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000222 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000223
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000224 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000225 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000226
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000227 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000228 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000229 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000230 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000231
Chris Lattner72db6c32009-04-22 02:44:54 +0000232 llvm::Constant *getGetPropertyFn() {
233 CodeGen::CodeGenTypes &Types = CGM.getTypes();
234 ASTContext &Ctx = CGM.getContext();
235 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000237 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
238 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000239 Params.push_back(IdType);
240 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000241 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000242 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000243 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000244 Types.GetFunctionType(Types.arrangeFunctionType(IdType, Params,
245 FunctionType::ExtInfo(),
246 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000247 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
248 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000249
Chris Lattner72db6c32009-04-22 02:44:54 +0000250 llvm::Constant *getSetPropertyFn() {
251 CodeGen::CodeGenTypes &Types = CGM.getTypes();
252 ASTContext &Ctx = CGM.getContext();
253 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000254 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000255 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
256 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000257 Params.push_back(IdType);
258 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000259 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000260 Params.push_back(IdType);
261 Params.push_back(Ctx.BoolTy);
262 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000263 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000264 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
265 FunctionType::ExtInfo(),
266 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000267 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
268 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000270 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
271 CodeGen::CodeGenTypes &Types = CGM.getTypes();
272 ASTContext &Ctx = CGM.getContext();
273 // void objc_setProperty_atomic(id self, SEL _cmd,
274 // id newValue, ptrdiff_t offset);
275 // void objc_setProperty_nonatomic(id self, SEL _cmd,
276 // id newValue, ptrdiff_t offset);
277 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
278 // id newValue, ptrdiff_t offset);
279 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
280 // id newValue, ptrdiff_t offset);
281
282 SmallVector<CanQualType,4> Params;
283 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
284 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
285 Params.push_back(IdType);
286 Params.push_back(SelType);
287 Params.push_back(IdType);
288 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
289 llvm::FunctionType *FTy =
290 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
291 FunctionType::ExtInfo(),
292 RequiredArgs::All));
293 const char *name;
294 if (atomic && copy)
295 name = "objc_setProperty_atomic_copy";
296 else if (atomic && !copy)
297 name = "objc_setProperty_atomic";
298 else if (!atomic && copy)
299 name = "objc_setProperty_nonatomic_copy";
300 else
301 name = "objc_setProperty_nonatomic";
302
303 return CGM.CreateRuntimeFunction(FTy, name);
304 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000305
306 llvm::Constant *getCopyStructFn() {
307 CodeGen::CodeGenTypes &Types = CGM.getTypes();
308 ASTContext &Ctx = CGM.getContext();
309 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000310 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000311 Params.push_back(Ctx.VoidPtrTy);
312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.LongTy);
314 Params.push_back(Ctx.BoolTy);
315 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000316 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000317 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
318 FunctionType::ExtInfo(),
319 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000320 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
321 }
322
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000323 /// This routine declares and returns address of:
324 /// void objc_copyCppObjectAtomic(
325 /// void *dest, const void *src,
326 /// void (*copyHelper) (void *dest, const void *source));
327 llvm::Constant *getCppAtomicObjectFunction() {
328 CodeGen::CodeGenTypes &Types = CGM.getTypes();
329 ASTContext &Ctx = CGM.getContext();
330 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
331 SmallVector<CanQualType,3> Params;
332 Params.push_back(Ctx.VoidPtrTy);
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000336 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
337 FunctionType::ExtInfo(),
338 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
340 }
341
Chris Lattner72db6c32009-04-22 02:44:54 +0000342 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000343 CodeGen::CodeGenTypes &Types = CGM.getTypes();
344 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000345 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000346 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000347 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000348 llvm::FunctionType *FTy =
John McCallde5d3c72012-02-17 03:33:10 +0000349 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
350 FunctionType::ExtInfo(),
351 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000352 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
353 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000354
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000355 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000356 llvm::Constant *getGcReadWeakFn() {
357 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000358 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000359 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000360 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000361 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000362 }
363
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000364 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000365 llvm::Constant *getGcAssignWeakFn() {
366 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000367 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000368 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000369 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000370 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
371 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000372
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000373 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000374 llvm::Constant *getGcAssignGlobalFn() {
375 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000376 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000377 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000378 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000379 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000382 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
383 llvm::Constant *getGcAssignThreadLocalFn() {
384 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000385 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000386 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000387 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000388 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
389 }
390
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000391 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000392 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000393 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000394 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
395 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000396 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000397 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000398 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000400
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000401 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
402 llvm::Constant *GcMemmoveCollectableFn() {
403 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000404 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000405 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000406 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
407 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000408
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000409 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000410 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000411 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000412 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000413 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000414 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000415 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
416 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000417
418 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000419 llvm::Constant *getExceptionThrowFn() {
420 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000421 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000422 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000423 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
425 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000426
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000427 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
428 llvm::Constant *getExceptionRethrowFn() {
429 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000430 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000431 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
432 }
433
Daniel Dunbar1c566672009-02-24 01:43:46 +0000434 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000435 llvm::Constant *getSyncEnterFn() {
436 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000437 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000438 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000439 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000440 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
441 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000442
Daniel Dunbar1c566672009-02-24 01:43:46 +0000443 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000444 llvm::Constant *getSyncExitFn() {
445 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000446 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000447 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000448 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000449 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
450 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000451
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000452 llvm::Constant *getSendFn(bool IsSuper) const {
453 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000455
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000456 llvm::Constant *getSendFn2(bool IsSuper) const {
457 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
458 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000459
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000460 llvm::Constant *getSendStretFn(bool IsSuper) const {
461 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
462 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000463
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000464 llvm::Constant *getSendStretFn2(bool IsSuper) const {
465 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
466 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000467
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000468 llvm::Constant *getSendFpretFn(bool IsSuper) const {
469 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
470 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000471
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000472 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
473 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
474 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000475
Anders Carlssoneea64802011-10-31 16:27:11 +0000476 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
477 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
478 }
479
480 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
481 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
482 }
483
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000484 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
485 ~ObjCCommonTypesHelper(){}
486};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000487
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000488/// ObjCTypesHelper - Helper class that encapsulates lazy
489/// construction of varies types used during ObjC generation.
490class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000491public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000492 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000493 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000494 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000495 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000496 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000497 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000498
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000499 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000500 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000501 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000503 /// ProtocolExtensionTy - LLVM type for struct
504 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000505 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000506 /// ProtocolExtensionTy - LLVM type for struct
507 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000508 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000509 /// MethodDescriptionTy - LLVM type for struct
510 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000511 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000512 /// MethodDescriptionListTy - LLVM type for struct
513 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000514 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000515 /// MethodDescriptionListPtrTy - LLVM type for struct
516 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000517 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000518 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000519 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000520 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000522 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000523 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000524 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000525 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000526 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000527 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000529 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000530 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000531 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000532 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000533 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000535 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000536 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000538 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000539 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000540 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000541 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000542
Anders Carlsson124526b2008-09-09 10:10:21 +0000543 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000544 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000545
Anders Carlsson124526b2008-09-09 10:10:21 +0000546 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000547 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000548 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000549 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000550 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000551 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000552 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000553
554 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000555 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000556 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000557 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000558 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000559 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000560 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000561
562 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000563 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000564 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000565 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000566 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000567 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000568 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000569
Anders Carlsson124526b2008-09-09 10:10:21 +0000570 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000571 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000572 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000573 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000574 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575 "objc_exception_match");
576
Chris Lattner34b02a12009-04-22 02:26:14 +0000577 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000578
Anders Carlsson124526b2008-09-09 10:10:21 +0000579 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000580 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000581 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000582 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000583 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
584 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000585 "_setjmp",
586 llvm::Attribute::ReturnsTwice);
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000588
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000589public:
590 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000591 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000592};
593
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000594/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000595/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000596class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000597public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000599 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000600 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000601
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000602 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000603 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000605 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000607
Daniel Dunbar948e2582009-02-15 07:36:20 +0000608 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000609 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000610
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000611 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000612 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000614 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000615 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000616
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000617 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000618 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000620 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000621 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000622
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000623 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000624 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000626 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000627 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000629 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000630 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000633 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000636 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000639 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000641 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000643 // MessageRefTy - LLVM for:
644 // struct _message_ref_t {
645 // IMP messenger;
646 // SEL name;
647 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000648 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000649 // MessageRefCTy - clang type for struct _message_ref_t
650 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000652 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000653 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000654 // MessageRefCPtrTy - clang type for struct _message_ref_t*
655 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahanianef163782009-02-05 01:13:09 +0000657 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000658 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000659
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000660 // SuperMessageRefTy - LLVM for:
661 // struct _super_message_ref_t {
662 // SUPER_IMP messenger;
663 // SEL name;
664 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000665 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000666
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000667 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000668 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000669
Chris Lattner1c02f862009-04-22 02:53:24 +0000670 llvm::Constant *getMessageSendFixupFn() {
671 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000674 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000675 "objc_msgSend_fixup");
676 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000677
Chris Lattner1c02f862009-04-22 02:53:24 +0000678 llvm::Constant *getMessageSendFpretFixupFn() {
679 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000680 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000681 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000682 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000683 "objc_msgSend_fpret_fixup");
684 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000685
Chris Lattner1c02f862009-04-22 02:53:24 +0000686 llvm::Constant *getMessageSendStretFixupFn() {
687 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000688 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000689 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000690 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000691 "objc_msgSend_stret_fixup");
692 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000693
Chris Lattner1c02f862009-04-22 02:53:24 +0000694 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000697 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000698 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000699 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 "objc_msgSendSuper2_fixup");
701 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000702
Chris Lattner1c02f862009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000705 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000706 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000708 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 "objc_msgSendSuper2_stret_fixup");
710 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000711
Chris Lattner8a569112009-04-22 02:15:23 +0000712 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000714 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Chris Lattner8a569112009-04-22 02:15:23 +0000716 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000719 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000720 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000721 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000722 "objc_begin_catch");
723 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000724
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000725 llvm::StructType *EHTypeTy;
726 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000727
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000728 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
729 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000730};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000733public:
734 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000735 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000736 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000737 unsigned ivar_bytepos;
738 unsigned ivar_size;
739 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000741
742 // Allow sorting based on byte pos.
743 bool operator<(const GC_IVAR &b) const {
744 return ivar_bytepos < b.ivar_bytepos;
745 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000746 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000747
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000748 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000749 public:
750 unsigned skip;
751 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000753 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000754 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000755
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000756protected:
Owen Anderson69243822009-07-13 04:10:07 +0000757 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000758 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000759 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000760
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000761 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000762 SmallVector<GC_IVAR, 16> SkipIvars;
763 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000765 /// LazySymbols - Symbols to generate a lazy reference for. See
766 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000767 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000768
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000769 /// DefinedSymbols - External symbols which are defined by this
770 /// module. The symbols in this list and LazySymbols are used to add
771 /// special linker symbols which ensure that Objective-C modules are
772 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000773 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000774
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000775 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000776 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000777
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000778 /// MethodVarNames - uniqued method variable names.
779 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000780
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000781 /// DefinedCategoryNames - list of category names in form Class_Category.
782 llvm::SetVector<std::string> DefinedCategoryNames;
783
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000784 /// MethodVarTypes - uniqued method type signatures. We have to use
785 /// a StringMap here because have no other unique reference.
786 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000788 /// MethodDefinitions - map of methods which have been defined in
789 /// this translation unit.
790 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000792 /// PropertyNames - uniqued method variable names.
793 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000794
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000795 /// ClassReferences - uniqued class references.
796 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000797
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000798 /// SelectorReferences - uniqued selector references.
799 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000800
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000801 /// Protocols - Protocols for which an objc_protocol structure has
802 /// been emitted. Forward declarations are handled by creating an
803 /// empty structure whose initializer is filled in when/if defined.
804 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000805
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000806 /// DefinedProtocols - Protocols which have actually been
807 /// defined. We should not need this, see FIXME in GenerateProtocol.
808 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000809
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000810 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000811 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000812
813 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000814 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000816 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000817 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000818
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000819 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000820 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000821
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000822 /// GetNameForMethod - Return a name for the given method.
823 /// \param[out] NameOut - The return value.
824 void GetNameForMethod(const ObjCMethodDecl *OMD,
825 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000826 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000827
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000828 /// GetMethodVarName - Return a unique constant for the given
829 /// selector's name. The return value has type char *.
830 llvm::Constant *GetMethodVarName(Selector Sel);
831 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000833 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000834 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000835
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000836 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000837 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
838 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000839 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000841 /// GetPropertyName - Return a unique constant for the given
842 /// name. The return value has type char *.
843 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000845 // FIXME: This can be dropped once string functions are unified.
846 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
847 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000849 /// GetClassName - Return a unique constant for the given selector's
850 /// name. The return value has type char *.
851 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000853 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
854
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000855 /// BuildIvarLayout - Builds ivar layout bitmap for the class
856 /// implementation for the __strong or __weak case.
857 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000858 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
859 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000860
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000861 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000863 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000864 unsigned int BytePos, bool ForStrongLayout,
865 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000866 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000867 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000868 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000869 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000870 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000871 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000872
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000873 /// GetIvarLayoutName - Returns a unique constant for the given
874 /// ivar layout bitmap.
875 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
876 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000878 /// EmitPropertyList - Emit the given property list. The return
879 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000880 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000882 const ObjCContainerDecl *OCD,
883 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Bob Wilsondc8dab62011-11-30 01:57:58 +0000885 /// EmitProtocolMethodTypes - Generate the array of extended method type
886 /// strings. The return value has type Int8PtrPtrTy.
887 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000888 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000889 const ObjCCommonTypesHelper &ObjCTypes);
890
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000891 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000892 void PushProtocolProperties(
893 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
894 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
895 const Decl *Container,
896 const ObjCProtocolDecl *PROTO,
897 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000898
Fariborz Jahanianda320092009-01-29 19:24:30 +0000899 /// GetProtocolRef - Return a reference to the internal protocol
900 /// description, creating an empty one if it has not been
901 /// defined. The return value has type ProtocolPtrTy.
902 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000903
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000904 /// CreateMetadataVar - Create a global variable with internal
905 /// linkage for use by the Objective-C runtime.
906 ///
907 /// This is a convenience wrapper which not only creates the
908 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000909 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000910 ///
911 /// \param Name - The variable name.
912 /// \param Init - The variable initializer; this is also used to
913 /// define the type of the variable.
914 /// \param Section - The section the variable should go into, or 0.
915 /// \param Align - The alignment for the variable, or 0.
916 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000917 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000918 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000919 llvm::Constant *Init,
920 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000921 unsigned Align,
922 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000923
John McCall944c8432011-05-14 03:10:52 +0000924 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
925 ReturnValueSlot Return,
926 QualType ResultType,
927 llvm::Value *Sel,
928 llvm::Value *Arg0,
929 QualType Arg0Ty,
930 bool IsSuper,
931 const CallArgList &CallArgs,
932 const ObjCMethodDecl *OMD,
933 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000934
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000935 /// EmitImageInfo - Emit the image info marker used to encode some module
936 /// level information.
937 void EmitImageInfo();
938
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000939public:
Owen Anderson69243822009-07-13 04:10:07 +0000940 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +0000941 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000942
David Chisnall0d13f6f2010-01-23 02:40:42 +0000943 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000944
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000945 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
946 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000947
Fariborz Jahanianda320092009-01-29 19:24:30 +0000948 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000949
Fariborz Jahanianda320092009-01-29 19:24:30 +0000950 /// GetOrEmitProtocol - Get the protocol object for the given
951 /// declaration, emitting it if necessary. The return value has type
952 /// ProtocolPtrTy.
953 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000954
Fariborz Jahanianda320092009-01-29 19:24:30 +0000955 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
956 /// object for the given declaration, emitting it if needed. These
957 /// forward references will be filled in with empty bodies if no
958 /// definition is seen. The return value has type ProtocolPtrTy.
959 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000960 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
961 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000962
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000963};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000964
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000965class CGObjCMac : public CGObjCCommonMac {
966private:
967 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000968
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000969 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000970 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000971 void EmitModuleInfo();
972
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000973 /// EmitModuleSymols - Emit module symbols, the list of defined
974 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000975 llvm::Constant *EmitModuleSymbols();
976
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000977 /// FinishModule - Write out global data structures at the end of
978 /// processing a translation unit.
979 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000980
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000981 /// EmitClassExtension - Generate the class extension structure used
982 /// to store the weak ivar layout and properties. The return value
983 /// has type ClassExtensionPtrTy.
984 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
985
986 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
987 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000988 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000989 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000990
John McCallf85e1932011-06-15 23:02:42 +0000991 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
992 IdentifierInfo *II);
993
994 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
995
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000996 /// EmitSuperClassRef - Emits reference to class's main metadata class.
997 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000998
999 /// EmitIvarList - Emit the ivar list for the given
1000 /// implementation. If ForClass is true the list of class ivars
1001 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1002 /// interface ivars will be emitted. The return value has type
1003 /// IvarListPtrTy.
1004 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001005 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001006
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001007 /// EmitMetaClass - Emit a forward reference to the class structure
1008 /// for the metaclass of the given interface. The return value has
1009 /// type ClassPtrTy.
1010 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1011
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001013 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001014 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1015 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001016 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001017
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001018 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001019
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001020 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001021
1022 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001023 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001024 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001025 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001026 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001027
1028 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001029 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001030 /// - TypeName: The name for the type containing the methods.
1031 /// - IsProtocol: True iff these methods are for a protocol.
1032 /// - ClassMethds: True iff these are class methods.
1033 /// - Required: When true, only "required" methods are
1034 /// listed. Similarly, when false only "optional" methods are
1035 /// listed. For classes this should always be true.
1036 /// - begin, end: The method list to output.
1037 ///
1038 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001039 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001040 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001041 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001042
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001043 /// GetOrEmitProtocol - Get the protocol object for the given
1044 /// declaration, emitting it if necessary. The return value has type
1045 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001046 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001047
1048 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1049 /// object for the given declaration, emitting it if needed. These
1050 /// forward references will be filled in with empty bodies if no
1051 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001052 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001053
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001054 /// EmitProtocolExtension - Generate the protocol extension
1055 /// structure used to store optional instance and class methods, and
1056 /// protocol properties. The return value has type
1057 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001058 llvm::Constant *
1059 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001060 ArrayRef<llvm::Constant*> OptInstanceMethods,
1061 ArrayRef<llvm::Constant*> OptClassMethods,
1062 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001063
1064 /// EmitProtocolList - Generate the list of referenced
1065 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001066 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001067 ObjCProtocolDecl::protocol_iterator begin,
1068 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001069
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001070 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1071 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001072 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1073 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001074
1075public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001076 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001077
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001078 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001079
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001080 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001081 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001082 QualType ResultType,
1083 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001084 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001085 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001086 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001087 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001088
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001089 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001090 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001091 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001092 QualType ResultType,
1093 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001094 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001095 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001096 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001097 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001098 const CallArgList &CallArgs,
1099 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001100
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001101 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001102 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001103
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001104 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1105 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001106
1107 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1108 /// untyped one.
1109 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1110 const ObjCMethodDecl *Method);
1111
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001112 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001113
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001114 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001115
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001116 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001117
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001118 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001119
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001120 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001121 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001122
Chris Lattner74391b42009-03-22 21:03:39 +00001123 virtual llvm::Constant *GetPropertyGetFunction();
1124 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001125 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1126 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001127 virtual llvm::Constant *GetGetStructFunction();
1128 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001129 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001130 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001131
John McCallf1549f62010-07-06 01:34:17 +00001132 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1133 const ObjCAtTryStmt &S);
1134 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1135 const ObjCAtSynchronizedStmt &S);
1136 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001137 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1138 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001139 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001140 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001141 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001142 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001143 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001144 llvm::Value *src, llvm::Value *dest,
1145 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001146 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001147 llvm::Value *src, llvm::Value *dest,
1148 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001149 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1150 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001151 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1152 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001153 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001154
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001155 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1156 QualType ObjectTy,
1157 llvm::Value *BaseValue,
1158 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001159 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001160 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001161 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001162 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001163
1164 /// GetClassGlobal - Return the global variable for the Objective-C
1165 /// class of the given name.
1166 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001167 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001168 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001169};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001170
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001171class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001172private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001173 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001174 llvm::GlobalVariable* ObjCEmptyCacheVar;
1175 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176
Daniel Dunbar11394522009-04-18 08:51:00 +00001177 /// SuperClassReferences - uniqued super class references.
1178 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001179
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001180 /// MetaClassReferences - uniqued meta class references.
1181 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001182
1183 /// EHTypeReferences - uniqued class ehtype references.
1184 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001185
John McCall944c8432011-05-14 03:10:52 +00001186 /// VTableDispatchMethods - List of methods for which we generate
1187 /// vtable-based message dispatch.
1188 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001189
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001190 /// DefinedMetaClasses - List of defined meta-classes.
1191 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1192
John McCall944c8432011-05-14 03:10:52 +00001193 /// isVTableDispatchedSelector - Returns true if SEL is a
1194 /// vtable-based selector.
1195 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001196
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001197 /// FinishNonFragileABIModule - Write out global data structures at the end of
1198 /// processing a translation unit.
1199 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001200
Daniel Dunbar463b8762009-05-15 21:48:48 +00001201 /// AddModuleClassList - Add the given list of class pointers to the
1202 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001203 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001204 const char *SymbolName,
1205 const char *SectionName);
1206
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001207 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1208 unsigned InstanceStart,
1209 unsigned InstanceSize,
1210 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001211 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001212 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001213 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001214 llvm::Constant *ClassRoGV,
1215 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001216
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001217 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001219 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001220
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001221 /// EmitMethodList - Emit the method list for the given
1222 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001223 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001224 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001225 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001226 /// EmitIvarList - Emit the ivar list for the given
1227 /// implementation. If ForClass is true the list of class ivars
1228 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1229 /// interface ivars will be emitted. The return value has type
1230 /// IvarListnfABIPtrTy.
1231 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001232
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001233 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001234 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001235 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001236
Fariborz Jahanianda320092009-01-29 19:24:30 +00001237 /// GetOrEmitProtocol - Get the protocol object for the given
1238 /// declaration, emitting it if necessary. The return value has type
1239 /// ProtocolPtrTy.
1240 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
Fariborz Jahanianda320092009-01-29 19:24:30 +00001242 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1243 /// object for the given declaration, emitting it if needed. These
1244 /// forward references will be filled in with empty bodies if no
1245 /// definition is seen. The return value has type ProtocolPtrTy.
1246 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001247
Fariborz Jahanianda320092009-01-29 19:24:30 +00001248 /// EmitProtocolList - Generate the list of referenced
1249 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001250 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001251 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001252 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001253
John McCall944c8432011-05-14 03:10:52 +00001254 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1255 ReturnValueSlot Return,
1256 QualType ResultType,
1257 Selector Sel,
1258 llvm::Value *Receiver,
1259 QualType Arg0Ty,
1260 bool IsSuper,
1261 const CallArgList &CallArgs,
1262 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001263
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001264 /// GetClassGlobal - Return the global variable for the Objective-C
1265 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001266 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001267
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001268 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001269 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001270 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001271 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001272
1273 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1274 IdentifierInfo *II);
1275
1276 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
Daniel Dunbar11394522009-04-18 08:51:00 +00001278 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1279 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001280 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1281 const ObjCInterfaceDecl *ID);
1282
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001283 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1284 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001285 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001286 const ObjCInterfaceDecl *ID);
1287
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001288 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1289 /// the given ivar.
1290 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001291 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292 const ObjCInterfaceDecl *ID,
1293 const ObjCIvarDecl *Ivar);
1294
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001295 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1296 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001297 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1298 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001299
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001300 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001301 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001302 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001303 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001304
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001305 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001306 return "OBJC_METACLASS_$_";
1307 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001309 const char *getClassSymbolPrefix() const {
1310 return "OBJC_CLASS_$_";
1311 }
1312
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001313 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001314 uint32_t &InstanceStart,
1315 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001316
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001317 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001318 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001319 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1320 return CGM.getContext().Selectors.getSelector(0, &II);
1321 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001322
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001323 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001324 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1325 return CGM.getContext().Selectors.getSelector(1, &II);
1326 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001327
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001328 /// ImplementationIsNonLazy - Check whether the given category or
1329 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001330 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001331
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001332public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001333 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001334 // FIXME. All stubs for now!
1335 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001336
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001337 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001338 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001339 QualType ResultType,
1340 Selector Sel,
1341 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001342 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001343 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001344 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001345
1346 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001347 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001348 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 QualType ResultType,
1350 Selector Sel,
1351 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001352 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 llvm::Value *Receiver,
1354 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001355 const CallArgList &CallArgs,
1356 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001357
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001358 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001359 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001360
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001361 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1362 bool lvalue = false)
1363 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001364
1365 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1366 /// untyped one.
1367 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1368 const ObjCMethodDecl *Method)
1369 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001370
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001371 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001372
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001373 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001374
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001375 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001376
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001377 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001378 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001380 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001381
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001382 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001383 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001385 virtual llvm::Constant *GetPropertySetFunction() {
1386 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001387 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001388
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001389 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1390 bool copy) {
1391 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1392 }
1393
David Chisnall8fac25d2010-12-26 22:13:16 +00001394 virtual llvm::Constant *GetSetStructFunction() {
1395 return ObjCTypes.getCopyStructFn();
1396 }
1397 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001398 return ObjCTypes.getCopyStructFn();
1399 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001400 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1401 return ObjCTypes.getCppAtomicObjectFunction();
1402 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001403
Chris Lattner74391b42009-03-22 21:03:39 +00001404 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001405 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001406 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001407
John McCallf1549f62010-07-06 01:34:17 +00001408 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1409 const ObjCAtTryStmt &S);
1410 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1411 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001412 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001413 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001414 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001415 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001416 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001417 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001419 llvm::Value *src, llvm::Value *dest,
1420 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001421 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001422 llvm::Value *src, llvm::Value *dest,
1423 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001424 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001425 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001426 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1427 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001428 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001429 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1430 QualType ObjectTy,
1431 llvm::Value *BaseValue,
1432 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001433 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001434 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001435 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001436 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001437};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001438
1439/// A helper class for performing the null-initialization of a return
1440/// value.
1441struct NullReturnState {
1442 llvm::BasicBlock *NullBB;
1443 llvm::BasicBlock *callBB;
1444 NullReturnState() : NullBB(0), callBB(0) {}
1445
1446 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1447 // Make blocks for the null-init and call edges.
1448 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1449 callBB = CGF.createBasicBlock("msgSend.call");
1450
1451 // Check for a null receiver and, if there is one, jump to the
1452 // null-init test.
1453 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1454 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1455
1456 // Otherwise, start performing the call.
1457 CGF.EmitBlock(callBB);
1458 }
1459
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001460 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1461 const CallArgList &CallArgs,
1462 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001463 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001464
1465 llvm::Value *NullInitPtr = 0;
1466 if (result.isScalar() && !resultType->isVoidType()) {
1467 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1468 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1469 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001470
1471 // Finish the call path.
1472 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1473 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1474
1475 // Emit the null-init block and perform the null-initialization there.
1476 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001477
1478 // Release consumed arguments along the null-receiver path.
1479 if (Method) {
1480 CallArgList::const_iterator I = CallArgs.begin();
1481 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1482 e = Method->param_end(); i != e; ++i, ++I) {
1483 const ParmVarDecl *ParamDecl = (*i);
1484 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1485 RValue RV = I->RV;
1486 assert(RV.isScalar() &&
1487 "NullReturnState::complete - arg not on object");
1488 CGF.EmitARCRelease(RV.getScalarVal(), true);
1489 }
1490 }
1491 }
1492
1493 if (result.isScalar()) {
1494 if (NullInitPtr)
1495 CGF.EmitNullInitialization(NullInitPtr, resultType);
1496 // Jump to the continuation block.
1497 CGF.EmitBlock(contBB);
1498 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1499 : result;
1500 }
1501
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001502 if (!resultType->isAnyComplexType()) {
1503 assert(result.isAggregate() && "null init of non-aggregate result?");
1504 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1505 // Jump to the continuation block.
1506 CGF.EmitBlock(contBB);
1507 return result;
1508 }
1509
1510 // _Complex type
1511 // FIXME. Now easy to handle any other scalar type whose result is returned
1512 // in memory due to ABI limitations.
1513 CGF.EmitBlock(contBB);
1514 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1515 llvm::Type *MemberType = CallCV.first->getType();
1516 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1517 // Create phi instruction for scalar complex value.
1518 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1519 PHIReal->addIncoming(ZeroCV, NullBB);
1520 PHIReal->addIncoming(CallCV.first, callBB);
1521 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1522 PHIImag->addIncoming(ZeroCV, NullBB);
1523 PHIImag->addIncoming(CallCV.second, callBB);
1524 return RValue::getComplex(PHIReal, PHIImag);
1525 }
1526};
1527
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001528} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001529
1530/* *** Helper Functions *** */
1531
1532/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001533static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001534 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001535 unsigned idx0,
1536 unsigned idx1) {
1537 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001538 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1539 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001540 };
Jay Foada5c04342011-07-21 14:31:17 +00001541 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001542}
1543
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001544/// hasObjCExceptionAttribute - Return true if this class or any super
1545/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001546static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001547 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001548 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001549 return true;
1550 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001551 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001552 return false;
1553}
1554
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001555/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001556
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001557CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001558 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001559 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001560 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001561}
1562
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001563/// GetClass - Return a reference to the class for the given interface
1564/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001565llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001566 const ObjCInterfaceDecl *ID) {
1567 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001568}
1569
1570/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001571llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1572 bool lval) {
1573 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001574}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001575llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001576 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001577 return EmitSelector(Builder, Method->getSelector());
1578}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001579
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001580llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001581 if (T->isObjCIdType() ||
1582 T->isObjCQualifiedIdType()) {
1583 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001584 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001585 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001586 if (T->isObjCClassType() ||
1587 T->isObjCQualifiedClassType()) {
1588 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001589 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001590 }
1591 if (T->isObjCObjectPointerType())
1592 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1593
John McCall5a180392010-07-24 00:37:23 +00001594 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001595}
1596
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001597/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001598/*
1599 struct __builtin_CFString {
1600 const int *isa; // point to __CFConstantStringClassReference
1601 int flags;
1602 const char *str;
1603 long length;
1604 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001605*/
1606
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001607/// or Generate a constant NSString object.
1608/*
1609 struct __builtin_NSString {
1610 const int *isa; // point to __NSConstantStringClassReference
1611 const char *str;
1612 unsigned int length;
1613 };
1614*/
1615
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001616llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001617 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001618 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001619 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001620 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001621}
1622
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001623enum {
1624 kCFTaggedObjectID_Integer = (1 << 1) + 1
1625};
1626
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001627/// Generates a message send where the super is the receiver. This is
1628/// a message send to self with special delivery semantics indicating
1629/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001630CodeGen::RValue
1631CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001632 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001633 QualType ResultType,
1634 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001635 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001636 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001637 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001638 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001639 const CodeGen::CallArgList &CallArgs,
1640 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001641 // Create and init a super structure; this is a (receiver, class)
1642 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001643 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001644 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001645 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001646 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001647 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001648 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001649
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001650 // If this is a class message the metaclass is passed as the target.
1651 llvm::Value *Target;
1652 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001653 if (isCategoryImpl) {
1654 // Message sent to 'super' in a class method defined in a category
1655 // implementation requires an odd treatment.
1656 // If we are in a class method, we must retrieve the
1657 // _metaclass_ for the current class, pointed at by
1658 // the class's "isa" pointer. The following assumes that
1659 // isa" is the first ivar in a class (which it must be).
1660 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1661 Target = CGF.Builder.CreateStructGEP(Target, 0);
1662 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001663 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001664 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1665 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1666 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1667 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001668 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001669 }
1670 else if (isCategoryImpl)
1671 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1672 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001673 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1674 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1675 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001676 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001677 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1678 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001679 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001680 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001681 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001682 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001683 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001684 return EmitMessageSend(CGF, Return, ResultType,
1685 EmitSelector(CGF.Builder, Sel),
1686 ObjCSuper, ObjCTypes.SuperPtrCTy,
1687 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001688}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001689
1690/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001691CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001692 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001693 QualType ResultType,
1694 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001695 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001696 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001697 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001698 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001699 return EmitMessageSend(CGF, Return, ResultType,
1700 EmitSelector(CGF.Builder, Sel),
1701 Receiver, CGF.getContext().getObjCIdType(),
1702 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001703}
1704
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001705CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001706CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1707 ReturnValueSlot Return,
1708 QualType ResultType,
1709 llvm::Value *Sel,
1710 llvm::Value *Arg0,
1711 QualType Arg0Ty,
1712 bool IsSuper,
1713 const CallArgList &CallArgs,
1714 const ObjCMethodDecl *Method,
1715 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001716 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001717 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001718 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001719 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1720 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001721 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001722
John McCallde5d3c72012-02-17 03:33:10 +00001723 // If we're calling a method, use the formal signature.
1724 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001725
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001726 if (Method)
1727 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1728 CGM.getContext().getCanonicalType(ResultType) &&
1729 "Result type mismatch!");
1730
John McCallcba681a2011-05-14 21:12:11 +00001731 NullReturnState nullReturn;
1732
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001733 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001734 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001735 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001736 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001737 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001738 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1739 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1740 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001741 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1742 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1743 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001744 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001745 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001746 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001747 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001748
1749 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001750 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001751 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1752 e = Method->param_end(); i != e; ++i) {
1753 const ParmVarDecl *ParamDecl = (*i);
1754 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1755 if (!nullReturn.NullBB)
1756 nullReturn.init(CGF, Arg0);
1757 requiresnullCheck = true;
1758 break;
1759 }
1760 }
1761
John McCallde5d3c72012-02-17 03:33:10 +00001762 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1763 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001764 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1765 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001766}
1767
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001768static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1769 if (FQT.isObjCGCStrong())
1770 return Qualifiers::Strong;
1771
John McCallf85e1932011-06-15 23:02:42 +00001772 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001773 return Qualifiers::Weak;
1774
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001775 // check for __unsafe_unretained
1776 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1777 return Qualifiers::GCNone;
1778
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001779 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1780 return Qualifiers::Strong;
1781
1782 if (const PointerType *PT = FQT->getAs<PointerType>())
1783 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1784
1785 return Qualifiers::GCNone;
1786}
1787
John McCall6b5a61b2011-02-07 10:33:21 +00001788llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1789 const CGBlockInfo &blockInfo) {
Chris Lattner8b418682012-02-07 00:39:47 +00001790 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall6b5a61b2011-02-07 10:33:21 +00001791
David Blaikie4e4d0842012-03-11 07:00:24 +00001792 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1793 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001794 return nullPtr;
1795
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001796 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001797 SkipIvars.clear();
1798 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001799 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1800 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001801
Fariborz Jahanian81979822010-09-09 00:21:45 +00001802 // __isa is the first field in block descriptor and must assume by runtime's
1803 // convention that it is GC'able.
1804 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001805
1806 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1807
1808 // Calculate the basic layout of the block structure.
1809 const llvm::StructLayout *layout =
1810 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1811
1812 // Ignore the optional 'this' capture: C++ objects are not assumed
1813 // to be GC'ed.
1814
1815 // Walk the captured variables.
1816 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1817 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1818 const VarDecl *variable = ci->getVariable();
1819 QualType type = variable->getType();
1820
1821 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1822
1823 // Ignore constant captures.
1824 if (capture.isConstant()) continue;
1825
1826 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1827
1828 // __block variables are passed by their descriptor address.
1829 if (ci->isByRef()) {
1830 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001831 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001832 }
1833
1834 assert(!type->isArrayType() && "array variable should not be caught");
1835 if (const RecordType *record = type->getAs<RecordType>()) {
1836 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001837 continue;
1838 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001839
John McCall6b5a61b2011-02-07 10:33:21 +00001840 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1841 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1842
1843 if (GCAttr == Qualifiers::Strong)
1844 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1845 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001846 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001847 SkipIvars.push_back(GC_IVAR(fieldOffset,
1848 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001849 }
1850
1851 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001852 return nullPtr;
1853
1854 // Sort on byte position; captures might not be allocated in order,
1855 // and unions can do funny things.
1856 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1857 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001858
1859 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001860 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001861 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001862 printf("\n block variable layout for block: ");
1863 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001864 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001865 if (!(s[i] & 0xf0))
1866 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1867 else
1868 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1869 printf("\n");
1870 }
1871
1872 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001873}
1874
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001875llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001876 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001877 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001878 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001879 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1880
Owen Anderson3c4972d2009-07-29 18:54:39 +00001881 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001882 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001883}
1884
Fariborz Jahanianda320092009-01-29 19:24:30 +00001885void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001886 // FIXME: We shouldn't need this, the protocol decl should contain enough
1887 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001888 DefinedProtocols.insert(PD->getIdentifier());
1889
1890 // If we have generated a forward reference to this protocol, emit
1891 // it now. Otherwise do nothing, the protocol objects are lazily
1892 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001893 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001894 GetOrEmitProtocol(PD);
1895}
1896
Fariborz Jahanianda320092009-01-29 19:24:30 +00001897llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001898 if (DefinedProtocols.count(PD->getIdentifier()))
1899 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001900
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001901 return GetOrEmitProtocolRef(PD);
1902}
1903
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001904/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001905// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1906struct _objc_protocol {
1907struct _objc_protocol_extension *isa;
1908char *protocol_name;
1909struct _objc_protocol_list *protocol_list;
1910struct _objc__method_prototype_list *instance_methods;
1911struct _objc__method_prototype_list *class_methods
1912};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001914See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001915*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001916llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1917 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1918
1919 // Early exit if a defining object has already been generated.
1920 if (Entry && Entry->hasInitializer())
1921 return Entry;
1922
Douglas Gregor1d784b22012-01-01 19:51:50 +00001923 // Use the protocol definition, if there is one.
1924 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1925 PD = Def;
1926
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001927 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001928 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001929 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1930
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001931 // Construct method lists.
1932 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1933 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001934 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001935 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001936 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001937 ObjCMethodDecl *MD = *i;
1938 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001939 if (!C)
1940 return GetOrEmitProtocolRef(PD);
1941
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001942 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1943 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001944 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001945 } else {
1946 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001947 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001948 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001949 }
1950
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001952 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001953 ObjCMethodDecl *MD = *i;
1954 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001955 if (!C)
1956 return GetOrEmitProtocolRef(PD);
1957
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001958 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1959 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001960 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001961 } else {
1962 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001963 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001964 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001965 }
1966
Bob Wilsondc8dab62011-11-30 01:57:58 +00001967 MethodTypesExt.insert(MethodTypesExt.end(),
1968 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1969
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001970 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001971 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1972 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001973 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001974 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001975 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001976 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001977 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001978 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001979 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001980 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001981 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001982 ClassMethods)
1983 };
Owen Anderson08e25242009-07-27 22:29:56 +00001984 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001985 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001986
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001987 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001988 // Already created, fix the linkage and update the initializer.
1989 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001990 Entry->setInitializer(Init);
1991 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001992 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001993 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001994 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001995 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001996 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001997 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001998 // FIXME: Is this necessary? Why only for protocol?
1999 Entry->setAlignment(4);
2000 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002001 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002002
2003 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002004}
2005
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002006llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002007 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2008
2009 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002010 // We use the initializer as a marker of whether this is a forward
2011 // reference or not. At module finalization we add the empty
2012 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002013 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002014 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002015 llvm::GlobalValue::ExternalLinkage,
2016 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002017 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002018 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002019 // FIXME: Is this necessary? Why only for protocol?
2020 Entry->setAlignment(4);
2021 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002022
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002023 return Entry;
2024}
2025
2026/*
2027 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002028 uint32_t size;
2029 struct objc_method_description_list *optional_instance_methods;
2030 struct objc_method_description_list *optional_class_methods;
2031 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002032 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002033 };
2034*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002035llvm::Constant *
2036CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002037 ArrayRef<llvm::Constant*> OptInstanceMethods,
2038 ArrayRef<llvm::Constant*> OptClassMethods,
2039 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002040 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002041 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002042 llvm::Constant *Values[] = {
2043 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002044 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002045 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002046 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002047 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002048 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002049 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002050 OptClassMethods),
2051 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002052 ObjCTypes),
2053 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2054 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002055 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002056
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002057 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002058 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002059 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002060 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002061
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002062 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002063 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002064
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002065 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002066 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002067 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002068 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002069}
2070
2071/*
2072 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002073 struct objc_protocol_list *next;
2074 long count;
2075 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002076 };
2077*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002078llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002079CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002080 ObjCProtocolDecl::protocol_iterator begin,
2081 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002082 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002083
Daniel Dunbardbc93372008-08-21 21:57:41 +00002084 for (; begin != end; ++begin)
2085 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002086
2087 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002088 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002089 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002090
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002091 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002092 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002093
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002094 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002095 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002096 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002097 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002098 ProtocolRefs.size() - 1);
2099 Values[2] =
2100 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2101 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002102 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002104 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002105 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002106 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002107 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002108 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002109}
2110
Bill Wendlingbb028552012-02-07 09:25:09 +00002111void CGObjCCommonMac::
2112PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002113 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002114 const Decl *Container,
2115 const ObjCProtocolDecl *PROTO,
2116 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002117 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2118 E = PROTO->protocol_end(); P != E; ++P)
2119 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2120 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2121 E = PROTO->prop_end(); I != E; ++I) {
2122 const ObjCPropertyDecl *PD = *I;
2123 if (!PropertySet.insert(PD->getIdentifier()))
2124 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002125 llvm::Constant *Prop[] = {
2126 GetPropertyName(PD->getIdentifier()),
2127 GetPropertyTypeString(PD, Container)
2128 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002129 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2130 }
2131}
2132
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002133/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002134 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002135 const char * const name;
2136 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002137 };
2138
2139 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002140 uint32_t entsize; // sizeof (struct _objc_property)
2141 uint32_t prop_count;
2142 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002143 };
2144*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002145llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002146 const Decl *Container,
2147 const ObjCContainerDecl *OCD,
2148 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002149 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002150 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002151 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2152 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002153 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002154 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002155 llvm::Constant *Prop[] = {
2156 GetPropertyName(PD->getIdentifier()),
2157 GetPropertyTypeString(PD, Container)
2158 };
Owen Anderson08e25242009-07-27 22:29:56 +00002159 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002160 Prop));
2161 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002162 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002163 for (ObjCInterfaceDecl::all_protocol_iterator
2164 P = OID->all_referenced_protocol_begin(),
2165 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002166 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2167 ObjCTypes);
2168 }
2169 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2170 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2171 E = CD->protocol_end(); P != E; ++P)
2172 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2173 ObjCTypes);
2174 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002175
2176 // Return null for empty list.
2177 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002178 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002179
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002180 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002181 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002182 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002183 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2184 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002185 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002186 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002187 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002188 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002189
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002190 llvm::GlobalVariable *GV =
2191 CreateMetadataVar(Name, Init,
2192 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002193 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002194 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002195 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002196 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002197}
2198
Bill Wendlingbb028552012-02-07 09:25:09 +00002199llvm::Constant *
2200CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2201 ArrayRef<llvm::Constant*> MethodTypes,
2202 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002203 // Return null for empty list.
2204 if (MethodTypes.empty())
2205 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2206
2207 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2208 MethodTypes.size());
2209 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2210
2211 llvm::GlobalVariable *GV =
2212 CreateMetadataVar(Name, Init,
2213 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2214 (ObjCABI == 2) ? 8 : 4,
2215 true);
2216 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2217}
2218
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002219/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002220 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002221 int count;
2222 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002223 };
2224*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002225llvm::Constant *
2226CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002227 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002228 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002229 ObjCTypes.SelectorPtrTy),
2230 GetMethodVarType(MD)
2231 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002232 if (!Desc[1])
2233 return 0;
2234
Owen Anderson08e25242009-07-27 22:29:56 +00002235 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002236 Desc);
2237}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002238
Bill Wendlingbb028552012-02-07 09:25:09 +00002239llvm::Constant *
2240CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2241 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002242 // Return null for empty list.
2243 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002244 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002245
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002246 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002247 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002248 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002249 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002250 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002251 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002252
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002253 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002254 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002255 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002256}
2257
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002258/*
2259 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002260 char *category_name;
2261 char *class_name;
2262 struct _objc_method_list *instance_methods;
2263 struct _objc_method_list *class_methods;
2264 struct _objc_protocol_list *protocols;
2265 uint32_t size; // <rdar://4585769>
2266 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002267 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002268*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002269void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002270 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002271
Mike Stumpf5408fe2009-05-16 07:57:57 +00002272 // FIXME: This is poor design, the OCD should have a pointer to the category
2273 // decl. Additionally, note that Category can be null for the @implementation
2274 // w/o an @interface case. Sema should just create one for us as it does for
2275 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002276 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002277 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002278 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002279
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002280 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002281 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2282 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002283
Bill Wendling3964e622012-02-09 22:16:49 +00002284 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002285 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002286 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002287 // Instance methods should always be defined.
2288 InstanceMethods.push_back(GetMethodConstant(*i));
2289 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002290 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002291 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002292 // Class methods should always be defined.
2293 ClassMethods.push_back(GetMethodConstant(*i));
2294 }
2295
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002296 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002297 Values[0] = GetClassName(OCD->getIdentifier());
2298 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002299 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002300 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002301 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002302 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002303 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002304 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002305 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002306 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002307 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002308 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002309 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002310 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002311 Category->protocol_begin(),
2312 Category->protocol_end());
2313 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002314 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002315 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002316 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002317
2318 // If there is no category @interface then there can be no properties.
2319 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002320 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002321 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002322 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002323 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002324 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002325
Owen Anderson08e25242009-07-27 22:29:56 +00002326 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002327 Values);
2328
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002329 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002330 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002331 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002332 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002333 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002334 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002335 // method definition entries must be clear for next implementation.
2336 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002337}
2338
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339// FIXME: Get from somewhere?
2340enum ClassFlags {
2341 eClassFlags_Factory = 0x00001,
2342 eClassFlags_Meta = 0x00002,
2343 // <rdr://5142207>
2344 eClassFlags_HasCXXStructors = 0x02000,
2345 eClassFlags_Hidden = 0x20000,
2346 eClassFlags_ABI2_Hidden = 0x00010,
2347 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2348};
2349
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002350/*
2351 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002352 Class isa;
2353 Class super_class;
2354 const char *name;
2355 long version;
2356 long info;
2357 long instance_size;
2358 struct _objc_ivar_list *ivars;
2359 struct _objc_method_list *methods;
2360 struct _objc_cache *cache;
2361 struct _objc_protocol_list *protocols;
2362 // Objective-C 1.0 extensions (<rdr://4585769>)
2363 const char *ivar_layout;
2364 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002365 };
2366
2367 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002368*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002369void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002370 DefinedSymbols.insert(ID->getIdentifier());
2371
Chris Lattner8ec03f52008-11-24 03:54:41 +00002372 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002374 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002375 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002376 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002377 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002378 Interface->all_referenced_protocol_begin(),
2379 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002380 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002381 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002382 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002383 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002384 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002385
2386 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002387 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002388 Flags |= eClassFlags_Hidden;
2389
Bill Wendling3964e622012-02-09 22:16:49 +00002390 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002391 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002392 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002393 // Instance methods should always be defined.
2394 InstanceMethods.push_back(GetMethodConstant(*i));
2395 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002396 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002397 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002398 // Class methods should always be defined.
2399 ClassMethods.push_back(GetMethodConstant(*i));
2400 }
2401
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002402 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002403 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002404 ObjCPropertyImplDecl *PID = *i;
2405
2406 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2407 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2408
2409 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2410 if (llvm::Constant *C = GetMethodConstant(MD))
2411 InstanceMethods.push_back(C);
2412 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2413 if (llvm::Constant *C = GetMethodConstant(MD))
2414 InstanceMethods.push_back(C);
2415 }
2416 }
2417
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002418 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002419 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002420 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002421 // Record a reference to the super class.
2422 LazySymbols.insert(Super->getIdentifier());
2423
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002424 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002425 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426 ObjCTypes.ClassPtrTy);
2427 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002428 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 }
2430 Values[ 2] = GetClassName(ID->getIdentifier());
2431 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002432 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2433 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2434 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002435 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002437 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002438 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002439 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002441 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002443 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002445 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002447 std::string Name("\01L_OBJC_CLASS_");
2448 Name += ClassName;
2449 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2450 // Check for a forward reference.
2451 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2452 if (GV) {
2453 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2454 "Forward metaclass reference has incorrect type.");
2455 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2456 GV->setInitializer(Init);
2457 GV->setSection(Section);
2458 GV->setAlignment(4);
2459 CGM.AddUsedGlobal(GV);
2460 }
2461 else
2462 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002463 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002464 // method definition entries must be clear for next implementation.
2465 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002466}
2467
2468llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2469 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00002470 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002472 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473
John McCall1fb0caa2010-10-22 21:05:15 +00002474 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002475 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002476
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002477 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002478 // The isa for the metaclass is the root of the hierarchy.
2479 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2480 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2481 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002483 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002485 // The super class for the metaclass is emitted as the name of the
2486 // super class. The runtime fixes this up to point to the
2487 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002489 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002490 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002491 ObjCTypes.ClassPtrTy);
2492 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002493 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002494 }
2495 Values[ 2] = GetClassName(ID->getIdentifier());
2496 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002497 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2498 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2499 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002500 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002501 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002502 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002503 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002504 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002505 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002506 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002507 Values[ 9] = Protocols;
2508 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002509 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002510 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002511 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002512 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002513 Values);
2514
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002515 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002516 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002517
2518 // Check for a forward reference.
2519 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2520 if (GV) {
2521 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2522 "Forward metaclass reference has incorrect type.");
2523 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2524 GV->setInitializer(Init);
2525 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002526 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002527 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002528 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002529 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002531 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002532 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002533
2534 return GV;
2535}
2536
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002537llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002538 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002539
Mike Stumpf5408fe2009-05-16 07:57:57 +00002540 // FIXME: Should we look these up somewhere other than the module. Its a bit
2541 // silly since we only generate these while processing an implementation, so
2542 // exactly one pointer would work if know when we entered/exitted an
2543 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002544
2545 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002546 // Previously, metaclass with internal linkage may have been defined.
2547 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002548 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2549 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002550 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2551 "Forward metaclass reference has incorrect type.");
2552 return GV;
2553 } else {
2554 // Generate as an external reference to keep a consistent
2555 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002556 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002557 llvm::GlobalValue::ExternalLinkage,
2558 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002559 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002560 }
2561}
2562
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002563llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2564 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2565
2566 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2567 true)) {
2568 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2569 "Forward class metadata reference has incorrect type.");
2570 return GV;
2571 } else {
2572 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2573 llvm::GlobalValue::ExternalLinkage,
2574 0,
2575 Name);
2576 }
2577}
2578
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002579/*
2580 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002581 uint32_t size;
2582 const char *weak_ivar_layout;
2583 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002584 };
2585*/
2586llvm::Constant *
2587CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002588 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002589 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002590
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002591 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002592 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002593 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002594 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002595 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002596
2597 // Return null if no extension bits are used.
2598 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002599 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002600
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002601 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002602 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002603 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002604 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002605 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002606}
2607
2608/*
2609 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00002610 char *ivar_name;
2611 char *ivar_type;
2612 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002613 };
2614
2615 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00002616 int ivar_count;
2617 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002618 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002619*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002620llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002621 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002622 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002623
2624 // When emitting the root class GCC emits ivar entries for the
2625 // actual class structure. It is not clear if we need to follow this
2626 // behavior; for now lets try and get away with not doing it. If so,
2627 // the cleanest solution would be to make up an ObjCInterfaceDecl
2628 // for the class.
2629 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002630 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002631
Jordy Rosedb8264e2011-07-22 02:08:32 +00002632 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002633
Jordy Rosedb8264e2011-07-22 02:08:32 +00002634 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002635 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002636 // Ignore unnamed bit-fields.
2637 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002638 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002639 llvm::Constant *Ivar[] = {
2640 GetMethodVarName(IVD->getIdentifier()),
2641 GetMethodVarType(IVD),
2642 llvm::ConstantInt::get(ObjCTypes.IntTy,
2643 ComputeIvarBaseOffset(CGM, OID, IVD))
2644 };
Owen Anderson08e25242009-07-27 22:29:56 +00002645 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002646 }
2647
2648 // Return null for empty list.
2649 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002650 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002651
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002652 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002653 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002654 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002655 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002656 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002657 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002658
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002659 llvm::GlobalVariable *GV;
2660 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002661 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002662 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002663 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002664 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002665 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002666 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002667 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002668 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002669}
2670
2671/*
2672 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002673 SEL method_name;
2674 char *method_types;
2675 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002676 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002677
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002678 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002679 struct objc_method_list *obsolete;
2680 int count;
2681 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002682 };
2683*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002684
2685/// GetMethodConstant - Return a struct objc_method constant for the
2686/// given method if it has been defined. The result is null if the
2687/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002688llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002689 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002690 if (!Fn)
2691 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002692
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002693 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002694 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002695 ObjCTypes.SelectorPtrTy),
2696 GetMethodVarType(MD),
2697 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2698 };
Owen Anderson08e25242009-07-27 22:29:56 +00002699 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002700}
2701
Chris Lattner5f9e2722011-07-23 10:55:15 +00002702llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002703 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00002704 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002705 // Return null for empty list.
2706 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002707 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002708
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002709 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002710 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002711 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002712 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002713 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002714 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002715 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002716
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002717 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002718 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002719}
2720
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002721llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002722 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002723 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002724 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002725
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002726 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002727 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00002728 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002729 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002730 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002731 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002732 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002733 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002734 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002735
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002736 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002737}
2738
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002739llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002740CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002741 llvm::Constant *Init,
2742 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002743 unsigned Align,
2744 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002745 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002746 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002747 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002748 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002749 if (Section)
2750 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002751 if (Align)
2752 GV->setAlignment(Align);
2753 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002754 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002755 return GV;
2756}
2757
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002758llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002759 // Abuse this interface function as a place to finalize.
2760 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002761 return NULL;
2762}
2763
Chris Lattner74391b42009-03-22 21:03:39 +00002764llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002765 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002766}
2767
Chris Lattner74391b42009-03-22 21:03:39 +00002768llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002769 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002770}
2771
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002772llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2773 bool copy) {
2774 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2775}
2776
David Chisnall8fac25d2010-12-26 22:13:16 +00002777llvm::Constant *CGObjCMac::GetGetStructFunction() {
2778 return ObjCTypes.getCopyStructFn();
2779}
2780llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002781 return ObjCTypes.getCopyStructFn();
2782}
2783
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002784llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2785 return ObjCTypes.getCppAtomicObjectFunction();
2786}
2787
Chris Lattner74391b42009-03-22 21:03:39 +00002788llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002789 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002790}
2791
John McCallf1549f62010-07-06 01:34:17 +00002792void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2793 return EmitTryOrSynchronizedStmt(CGF, S);
2794}
2795
2796void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2797 const ObjCAtSynchronizedStmt &S) {
2798 return EmitTryOrSynchronizedStmt(CGF, S);
2799}
2800
John McCallcc505292010-07-21 06:59:36 +00002801namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002802 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002803 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002804 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002805 llvm::Value *CallTryExitVar;
2806 llvm::Value *ExceptionData;
2807 ObjCTypesHelper &ObjCTypes;
2808 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002809 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002810 llvm::Value *CallTryExitVar,
2811 llvm::Value *ExceptionData,
2812 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002813 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002814 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2815
John McCallad346f42011-07-12 20:27:29 +00002816 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002817 // Check whether we need to call objc_exception_try_exit.
2818 // In optimized code, this branch will always be folded.
2819 llvm::BasicBlock *FinallyCallExit =
2820 CGF.createBasicBlock("finally.call_exit");
2821 llvm::BasicBlock *FinallyNoCallExit =
2822 CGF.createBasicBlock("finally.no_call_exit");
2823 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2824 FinallyCallExit, FinallyNoCallExit);
2825
2826 CGF.EmitBlock(FinallyCallExit);
2827 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2828 ->setDoesNotThrow();
2829
2830 CGF.EmitBlock(FinallyNoCallExit);
2831
2832 if (isa<ObjCAtTryStmt>(S)) {
2833 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002834 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2835 // Save the current cleanup destination in case there's
2836 // control flow inside the finally statement.
2837 llvm::Value *CurCleanupDest =
2838 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2839
John McCallcc505292010-07-21 06:59:36 +00002840 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2841
John McCalld96a8e72010-08-11 00:16:14 +00002842 if (CGF.HaveInsertPoint()) {
2843 CGF.Builder.CreateStore(CurCleanupDest,
2844 CGF.getNormalCleanupDestSlot());
2845 } else {
2846 // Currently, the end of the cleanup must always exist.
2847 CGF.EnsureInsertPoint();
2848 }
2849 }
John McCallcc505292010-07-21 06:59:36 +00002850 } else {
2851 // Emit objc_sync_exit(expr); as finally's sole statement for
2852 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002853 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002854 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2855 ->setDoesNotThrow();
2856 }
2857 }
2858 };
John McCall87bb5822010-07-31 23:20:56 +00002859
2860 class FragileHazards {
2861 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002862 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002863 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2864
2865 llvm::InlineAsm *ReadHazard;
2866 llvm::InlineAsm *WriteHazard;
2867
2868 llvm::FunctionType *GetAsmFnType();
2869
2870 void collectLocals();
2871 void emitReadHazard(CGBuilderTy &Builder);
2872
2873 public:
2874 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002875
John McCall87bb5822010-07-31 23:20:56 +00002876 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002877 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002878 };
2879}
2880
2881/// Create the fragile-ABI read and write hazards based on the current
2882/// state of the function, which is presumed to be immediately prior
2883/// to a @try block. These hazards are used to maintain correct
2884/// semantics in the face of optimization and the fragile ABI's
2885/// cavalier use of setjmp/longjmp.
2886FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2887 collectLocals();
2888
2889 if (Locals.empty()) return;
2890
2891 // Collect all the blocks in the function.
2892 for (llvm::Function::iterator
2893 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2894 BlocksBeforeTry.insert(&*I);
2895
2896 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2897
2898 // Create a read hazard for the allocas. This inhibits dead-store
2899 // optimizations and forces the values to memory. This hazard is
2900 // inserted before any 'throwing' calls in the protected scope to
2901 // reflect the possibility that the variables might be read from the
2902 // catch block if the call throws.
2903 {
2904 std::string Constraint;
2905 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2906 if (I) Constraint += ',';
2907 Constraint += "*m";
2908 }
2909
2910 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2911 }
2912
2913 // Create a write hazard for the allocas. This inhibits folding
2914 // loads across the hazard. This hazard is inserted at the
2915 // beginning of the catch path to reflect the possibility that the
2916 // variables might have been written within the protected scope.
2917 {
2918 std::string Constraint;
2919 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2920 if (I) Constraint += ',';
2921 Constraint += "=*m";
2922 }
2923
2924 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2925 }
2926}
2927
2928/// Emit a write hazard at the current location.
2929void FragileHazards::emitWriteHazard() {
2930 if (Locals.empty()) return;
2931
Jay Foad4c7d9f12011-07-15 08:37:34 +00002932 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002933}
2934
John McCall87bb5822010-07-31 23:20:56 +00002935void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2936 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002937 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002938}
2939
2940/// Emit read hazards in all the protected blocks, i.e. all the blocks
2941/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002942void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002943 if (Locals.empty()) return;
2944
2945 CGBuilderTy Builder(CGF.getLLVMContext());
2946
2947 // Iterate through all blocks, skipping those prior to the try.
2948 for (llvm::Function::iterator
2949 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2950 llvm::BasicBlock &BB = *FI;
2951 if (BlocksBeforeTry.count(&BB)) continue;
2952
2953 // Walk through all the calls in the block.
2954 for (llvm::BasicBlock::iterator
2955 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2956 llvm::Instruction &I = *BI;
2957
2958 // Ignore instructions that aren't non-intrinsic calls.
2959 // These are the only calls that can possibly call longjmp.
2960 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2961 if (isa<llvm::IntrinsicInst>(I))
2962 continue;
2963
2964 // Ignore call sites marked nounwind. This may be questionable,
2965 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2966 llvm::CallSite CS(&I);
2967 if (CS.doesNotThrow()) continue;
2968
John McCall0b251722010-08-04 05:59:32 +00002969 // Insert a read hazard before the call. This will ensure that
2970 // any writes to the locals are performed before making the
2971 // call. If the call throws, then this is sufficient to
2972 // guarantee correctness as long as it doesn't also write to any
2973 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002974 Builder.SetInsertPoint(&BB, BI);
2975 emitReadHazard(Builder);
2976 }
2977 }
2978}
2979
2980static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2981 if (V) S.insert(V);
2982}
2983
2984void FragileHazards::collectLocals() {
2985 // Compute a set of allocas to ignore.
2986 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2987 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2988 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002989
2990 // Collect all the allocas currently in the function. This is
2991 // probably way too aggressive.
2992 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2993 for (llvm::BasicBlock::iterator
2994 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2995 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2996 Locals.push_back(&*I);
2997}
2998
2999llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003000 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003001 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3002 tys[i] = Locals[i]->getType();
3003 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003004}
3005
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003006/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003007
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003008 Objective-C setjmp-longjmp (sjlj) Exception Handling
3009 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003010
John McCallf1549f62010-07-06 01:34:17 +00003011 A catch buffer is a setjmp buffer plus:
3012 - a pointer to the exception that was caught
3013 - a pointer to the previous exception data buffer
3014 - two pointers of reserved storage
3015 Therefore catch buffers form a stack, with a pointer to the top
3016 of the stack kept in thread-local storage.
3017
3018 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3019 objc_exception_try_exit pops the given catch buffer, which is
3020 required to be the top of the EH stack.
3021 objc_exception_throw pops the top of the EH stack, writes the
3022 thrown exception into the appropriate field, and longjmps
3023 to the setjmp buffer. It crashes the process (with a printf
3024 and an abort()) if there are no catch buffers on the stack.
3025 objc_exception_extract just reads the exception pointer out of the
3026 catch buffer.
3027
3028 There's no reason an implementation couldn't use a light-weight
3029 setjmp here --- something like __builtin_setjmp, but API-compatible
3030 with the heavyweight setjmp. This will be more important if we ever
3031 want to implement correct ObjC/C++ exception interactions for the
3032 fragile ABI.
3033
3034 Note that for this use of setjmp/longjmp to be correct, we may need
3035 to mark some local variables volatile: if a non-volatile local
3036 variable is modified between the setjmp and the longjmp, it has
3037 indeterminate value. For the purposes of LLVM IR, it may be
3038 sufficient to make loads and stores within the @try (to variables
3039 declared outside the @try) volatile. This is necessary for
3040 optimized correctness, but is not currently being done; this is
3041 being tracked as rdar://problem/8160285
3042
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003043 The basic framework for a @try-catch-finally is as follows:
3044 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003045 objc_exception_data d;
3046 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003047 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003049 objc_exception_try_enter(&d);
3050 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003051 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003052 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003053 // exception path
3054 id _caught = objc_exception_extract(&d);
3055
3056 // enter new try scope for handlers
3057 if (!setjmp(d.jmp_buf)) {
3058 ... match exception and execute catch blocks ...
3059
3060 // fell off end, rethrow.
3061 _rethrow = _caught;
3062 ... jump-through-finally to finally_rethrow ...
3063 } else {
3064 // exception in catch block
3065 _rethrow = objc_exception_extract(&d);
3066 _call_try_exit = false;
3067 ... jump-through-finally to finally_rethrow ...
3068 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003069 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003070 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003071
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003072 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003073 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003074 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003075
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003076 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003077 ... dispatch to finally destination ...
3078
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003079 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003080 objc_exception_throw(_rethrow);
3081
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003082 finally_end:
3083 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003084
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085 This framework differs slightly from the one gcc uses, in that gcc
3086 uses _rethrow to determine if objc_exception_try_exit should be called
3087 and if the object should be rethrown. This breaks in the face of
3088 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003089
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003091
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003092 - If there are no catch blocks, then we avoid emitting the second
3093 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003094
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3096 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003097
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003098 - FIXME: If there is no @finally block we can do a few more
3099 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003100
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003101 Rethrows and Jumps-Through-Finally
3102 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003103
John McCallf1549f62010-07-06 01:34:17 +00003104 '@throw;' is supported by pushing the currently-caught exception
3105 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003106
John McCallf1549f62010-07-06 01:34:17 +00003107 Branches through the @finally block are handled with an ordinary
3108 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3109 exceptions are not compatible with C++ exceptions, and this is
3110 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003111
John McCallf1549f62010-07-06 01:34:17 +00003112 @synchronized(expr) { stmt; } is emitted as if it were:
3113 id synch_value = expr;
3114 objc_sync_enter(synch_value);
3115 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003116*/
3117
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003118void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3119 const Stmt &S) {
3120 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003121
3122 // A destination for the fall-through edges of the catch handlers to
3123 // jump to.
3124 CodeGenFunction::JumpDest FinallyEnd =
3125 CGF.getJumpDestInCurrentScope("finally.end");
3126
3127 // A destination for the rethrow edge of the catch handlers to jump
3128 // to.
3129 CodeGenFunction::JumpDest FinallyRethrow =
3130 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003131
Daniel Dunbar1c566672009-02-24 01:43:46 +00003132 // For @synchronized, call objc_sync_enter(sync.expr). The
3133 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003134 // @synchronized. We can't avoid a temp here because we need the
3135 // value to be preserved. If the backend ever does liveness
3136 // correctly after setjmp, this will be unnecessary.
3137 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003138 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003139 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003140 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3141 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003142 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3143 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003144
3145 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3146 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003147 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003148
John McCall0b251722010-08-04 05:59:32 +00003149 // Allocate memory for the setjmp buffer. This needs to be kept
3150 // live throughout the try and catch blocks.
3151 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3152 "exceptiondata.ptr");
3153
John McCall87bb5822010-07-31 23:20:56 +00003154 // Create the fragile hazards. Note that this will not capture any
3155 // of the allocas required for exception processing, but will
3156 // capture the current basic block (which extends all the way to the
3157 // setjmp call) as "before the @try".
3158 FragileHazards Hazards(CGF);
3159
John McCallf1549f62010-07-06 01:34:17 +00003160 // Create a flag indicating whether the cleanup needs to call
3161 // objc_exception_try_exit. This is true except when
3162 // - no catches match and we're branching through the cleanup
3163 // just to rethrow the exception, or
3164 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003165 // The setjmp-safety rule here is that we should always store to this
3166 // variable in a place that dominates the branch through the cleanup
3167 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003168 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003169 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003170
John McCall9e2213d2010-10-04 23:42:51 +00003171 // A slot containing the exception to rethrow. Only needed when we
3172 // have both a @catch and a @finally.
3173 llvm::Value *PropagatingExnVar = 0;
3174
John McCallf1549f62010-07-06 01:34:17 +00003175 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003176 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003177 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003178 CallTryExitVar,
3179 ExceptionData,
3180 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003181
3182 // Enter a try block:
3183 // - Call objc_exception_try_enter to push ExceptionData on top of
3184 // the EH stack.
3185 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3186 ->setDoesNotThrow();
3187
3188 // - Call setjmp on the exception data buffer.
3189 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3190 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3191 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003192 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003193 llvm::CallInst *SetJmpResult =
3194 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3195 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003196 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003197
3198 // If setjmp returned 0, enter the protected block; otherwise,
3199 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003200 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3201 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003202 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003203 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3204 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003205
John McCallf1549f62010-07-06 01:34:17 +00003206 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003207 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003208 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003209 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003210 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003211
3212 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213
John McCallf1549f62010-07-06 01:34:17 +00003214 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003215 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003216
John McCall87bb5822010-07-31 23:20:56 +00003217 // Don't optimize loads of the in-scope locals across this point.
3218 Hazards.emitWriteHazard();
3219
John McCallf1549f62010-07-06 01:34:17 +00003220 // For a @synchronized (or a @try with no catches), just branch
3221 // through the cleanup to the rethrow block.
3222 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3223 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003224 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003225 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003226
3227 // Otherwise, we have to match against the caught exceptions.
3228 } else {
John McCall0b251722010-08-04 05:59:32 +00003229 // Retrieve the exception object. We may emit multiple blocks but
3230 // nothing can cross this so the value is already in SSA form.
3231 llvm::CallInst *Caught =
3232 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3233 ExceptionData, "caught");
3234 Caught->setDoesNotThrow();
3235
John McCallf1549f62010-07-06 01:34:17 +00003236 // Push the exception to rethrow onto the EH value stack for the
3237 // benefit of any @throws in the handlers.
3238 CGF.ObjCEHValueStack.push_back(Caught);
3239
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003240 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003241
John McCall0b251722010-08-04 05:59:32 +00003242 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003243
John McCall0b251722010-08-04 05:59:32 +00003244 llvm::BasicBlock *CatchBlock = 0;
3245 llvm::BasicBlock *CatchHandler = 0;
3246 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003247 // Save the currently-propagating exception before
3248 // objc_exception_try_enter clears the exception slot.
3249 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3250 "propagating_exception");
3251 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3252
John McCall0b251722010-08-04 05:59:32 +00003253 // Enter a new exception try block (in case a @catch block
3254 // throws an exception).
3255 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3256 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003257
John McCall0b251722010-08-04 05:59:32 +00003258 llvm::CallInst *SetJmpResult =
3259 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3260 "setjmp.result");
3261 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003262 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003263
John McCall0b251722010-08-04 05:59:32 +00003264 llvm::Value *Threw =
3265 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3266
3267 CatchBlock = CGF.createBasicBlock("catch");
3268 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3269 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3270
3271 CGF.EmitBlock(CatchBlock);
3272 }
3273
3274 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003275
Daniel Dunbar55e40722008-09-27 07:03:52 +00003276 // Handle catch list. As a special case we check if everything is
3277 // matched and avoid generating code for falling off the end if
3278 // so.
3279 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003280 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3281 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003282
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003283 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003284 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003285
Anders Carlsson80f25672008-09-09 17:59:25 +00003286 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003287 if (!CatchParam) {
3288 AllMatched = true;
3289 } else {
John McCall183700f2009-09-21 23:43:11 +00003290 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003291
John McCallf1549f62010-07-06 01:34:17 +00003292 // catch(id e) always matches under this ABI, since only
3293 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003294 // FIXME: For the time being we also match id<X>; this should
3295 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003296 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003297 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003298 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003299
John McCallf1549f62010-07-06 01:34:17 +00003300 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003301 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003302 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3303
Anders Carlssondde0a942008-09-11 09:15:33 +00003304 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003305 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003306 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003307
3308 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003309 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003311
Anders Carlssondde0a942008-09-11 09:15:33 +00003312 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003313
3314 // The scope of the catch variable ends right here.
3315 CatchVarCleanups.ForceCleanup();
3316
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003317 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003318 break;
3319 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320
Steve Naroff14108da2009-07-10 23:34:53 +00003321 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003322 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003323
3324 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003325 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3326 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003327
3328 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003329 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003330
John McCallf1549f62010-07-06 01:34:17 +00003331 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003332 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3333 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003334 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003335
John McCallf1549f62010-07-06 01:34:17 +00003336 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3337 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003338
3339 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003340 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003341
Anders Carlsson80f25672008-09-09 17:59:25 +00003342 // Emit the @catch block.
3343 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003344
3345 // Collect any cleanups for the catch variable. The scope lasts until
3346 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003347 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003348
John McCallb6bbcc92010-10-15 04:57:14 +00003349 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003350 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003351
John McCallf1549f62010-07-06 01:34:17 +00003352 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003353 llvm::Value *Tmp =
3354 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003355 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003356 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003357
Anders Carlssondde0a942008-09-11 09:15:33 +00003358 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003359
3360 // We're done with the catch variable.
3361 CatchVarCleanups.ForceCleanup();
3362
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003363 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003364
Anders Carlsson80f25672008-09-09 17:59:25 +00003365 CGF.EmitBlock(NextCatchBlock);
3366 }
3367
John McCallf1549f62010-07-06 01:34:17 +00003368 CGF.ObjCEHValueStack.pop_back();
3369
John McCall0b251722010-08-04 05:59:32 +00003370 // If nothing wanted anything to do with the caught exception,
3371 // kill the extract call.
3372 if (Caught->use_empty())
3373 Caught->eraseFromParent();
3374
3375 if (!AllMatched)
3376 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3377
3378 if (HasFinally) {
3379 // Emit the exception handler for the @catch blocks.
3380 CGF.EmitBlock(CatchHandler);
3381
3382 // In theory we might now need a write hazard, but actually it's
3383 // unnecessary because there's no local-accessing code between
3384 // the try's write hazard and here.
3385 //Hazards.emitWriteHazard();
3386
John McCall9e2213d2010-10-04 23:42:51 +00003387 // Extract the new exception and save it to the
3388 // propagating-exception slot.
3389 assert(PropagatingExnVar);
3390 llvm::CallInst *NewCaught =
3391 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3392 ExceptionData, "caught");
3393 NewCaught->setDoesNotThrow();
3394 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3395
John McCall0b251722010-08-04 05:59:32 +00003396 // Don't pop the catch handler; the throw already did.
3397 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003398 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003399 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003401
John McCall87bb5822010-07-31 23:20:56 +00003402 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003403 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003404
John McCallf1549f62010-07-06 01:34:17 +00003405 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003406 CGF.Builder.restoreIP(TryFallthroughIP);
3407 if (CGF.HaveInsertPoint())
3408 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003409 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003410 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003411
John McCallf1549f62010-07-06 01:34:17 +00003412 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003413 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003414 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003415 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003416 // If we have a propagating-exception variable, check it.
3417 llvm::Value *PropagatingExn;
3418 if (PropagatingExnVar) {
3419 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003420
John McCall9e2213d2010-10-04 23:42:51 +00003421 // Otherwise, just look in the buffer for the exception to throw.
3422 } else {
3423 llvm::CallInst *Caught =
3424 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3425 ExceptionData);
3426 Caught->setDoesNotThrow();
3427 PropagatingExn = Caught;
3428 }
3429
3430 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003431 ->setDoesNotThrow();
3432 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003433 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003434
John McCall87bb5822010-07-31 23:20:56 +00003435 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003436}
3437
3438void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003439 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003440 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003441
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003442 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003443 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003444 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003445 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003446 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003447 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003448 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003449 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003450 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003451
John McCallf1549f62010-07-06 01:34:17 +00003452 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3453 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003454 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003455
3456 // Clear the insertion point to indicate we are in unreachable code.
3457 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003458}
3459
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003460/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003461/// object: objc_read_weak (id *src)
3462///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003463llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003464 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003465 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003466 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3467 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3468 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003469 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003470 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003471 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003472 return read_weak;
3473}
3474
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003475/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3476/// objc_assign_weak (id src, id *dst)
3477///
3478void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003479 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003480 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003481 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003482 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003483 assert(Size <= 8 && "does not support size > 8");
3484 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003485 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003486 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3487 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003488 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3489 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003490 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003491 src, dst, "weakassign");
3492 return;
3493}
3494
Fariborz Jahanian58626502008-11-19 00:59:10 +00003495/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3496/// objc_assign_global (id src, id *dst)
3497///
3498void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003499 llvm::Value *src, llvm::Value *dst,
3500 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003501 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003502 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003503 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003504 assert(Size <= 8 && "does not support size > 8");
3505 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003506 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003507 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3508 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003509 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3510 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003511 if (!threadlocal)
3512 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3513 src, dst, "globalassign");
3514 else
3515 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3516 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003517 return;
3518}
3519
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003520/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003521/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003522///
3523void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003524 llvm::Value *src, llvm::Value *dst,
3525 llvm::Value *ivarOffset) {
3526 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003527 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003528 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003529 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003530 assert(Size <= 8 && "does not support size > 8");
3531 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003532 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003533 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3534 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003535 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3536 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003537 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3538 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003539 return;
3540}
3541
Fariborz Jahanian58626502008-11-19 00:59:10 +00003542/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3543/// objc_assign_strongCast (id src, id *dst)
3544///
3545void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003546 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003547 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003548 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003549 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003550 assert(Size <= 8 && "does not support size > 8");
3551 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003553 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3554 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003555 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3556 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003557 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003558 src, dst, "weakassign");
3559 return;
3560}
3561
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003562void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003563 llvm::Value *DestPtr,
3564 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003565 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003566 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3567 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003568 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003569 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003570 return;
3571}
3572
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003573/// EmitObjCValueForIvar - Code Gen for ivar reference.
3574///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003575LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3576 QualType ObjectTy,
3577 llvm::Value *BaseValue,
3578 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003579 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003580 const ObjCInterfaceDecl *ID =
3581 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003582 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3583 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003584}
3585
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003586llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003587 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003588 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003589 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003590 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003591 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3592 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003593}
3594
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003595/* *** Private Interface *** */
3596
3597/// EmitImageInfo - Emit the image info marker used to encode some module
3598/// level information.
3599///
3600/// See: <rdr://4810609&4810587&4810587>
3601/// struct IMAGE_INFO {
3602/// unsigned version;
3603/// unsigned flags;
3604/// };
3605enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003606 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003607 eImageInfo_GarbageCollected = (1 << 1),
3608 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003609 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3610
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003611 // A flag indicating that the module has no instances of a @synthesize of a
3612 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003613 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003614};
3615
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003616void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003617 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00003618 const char *Section = (ObjCABI == 1) ?
3619 "__OBJC, __image_info,regular" :
3620 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003621
Bill Wendling3973acc2012-02-16 01:13:30 +00003622 // Generate module-level named metadata to convey this information to the
3623 // linker and code-gen.
3624 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003625
Bill Wendling3973acc2012-02-16 01:13:30 +00003626 // Add the ObjC ABI version to the module flags.
3627 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3628 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3629 version);
3630 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3631 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003632
David Blaikie4e4d0842012-03-11 07:00:24 +00003633 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003634 // Non-GC overrides those files which specify GC.
3635 Mod.addModuleFlag(llvm::Module::Override,
3636 "Objective-C Garbage Collection", (uint32_t)0);
3637 } else {
3638 // Add the ObjC garbage collection value.
3639 Mod.addModuleFlag(llvm::Module::Error,
3640 "Objective-C Garbage Collection",
3641 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003642
David Blaikie4e4d0842012-03-11 07:00:24 +00003643 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003644 // Add the ObjC GC Only value.
3645 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3646 eImageInfo_GCOnly);
3647
3648 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3649 llvm::Value *Ops[2] = {
3650 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3651 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3652 eImageInfo_GarbageCollected)
3653 };
3654 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3655 llvm::MDNode::get(VMContext, Ops));
3656 }
3657 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003658}
3659
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003660// struct objc_module {
3661// unsigned long version;
3662// unsigned long size;
3663// const char *name;
3664// Symtab symtab;
3665// };
3666
3667// FIXME: Get from somewhere
3668static const int ModuleVersion = 7;
3669
3670void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003671 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003672
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003673 llvm::Constant *Values[] = {
3674 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3675 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3676 // This used to be the filename, now it is unused. <rdr://4327263>
3677 GetClassName(&CGM.getContext().Idents.get("")),
3678 EmitModuleSymbols()
3679 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003680 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003681 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003682 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003683 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003684}
3685
3686llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003687 unsigned NumClasses = DefinedClasses.size();
3688 unsigned NumCategories = DefinedCategories.size();
3689
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003690 // Return null if no symbols were defined.
3691 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003692 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003693
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003694 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003695 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003696 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003697 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3698 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003699
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003700 // The runtime expects exactly the list of defined classes followed
3701 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00003702 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003703 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003704 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003705 ObjCTypes.Int8PtrTy);
3706 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003707 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003708 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003709 ObjCTypes.Int8PtrTy);
3710
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003711 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003712 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00003713 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003714 Symbols);
3715
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003716 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003717
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003718 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003719 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3720 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003721 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003722 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003723}
3724
John McCallf85e1932011-06-15 23:02:42 +00003725llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3726 IdentifierInfo *II) {
3727 LazySymbols.insert(II);
3728
3729 llvm::GlobalVariable *&Entry = ClassReferences[II];
3730
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003731 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003732 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003733 llvm::ConstantExpr::getBitCast(GetClassName(II),
3734 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003735 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003736 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3737 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3738 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003739 }
John McCallf85e1932011-06-15 23:02:42 +00003740
Benjamin Kramer578faa82011-09-27 21:06:10 +00003741 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003742}
3743
John McCallf85e1932011-06-15 23:02:42 +00003744llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3745 const ObjCInterfaceDecl *ID) {
3746 return EmitClassRefFromId(Builder, ID->getIdentifier());
3747}
3748
3749llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3750 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3751 return EmitClassRefFromId(Builder, II);
3752}
3753
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003754llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3755 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003756 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003757
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003758 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003759 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003760 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003761 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003762 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003763 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3764 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003765 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003766 }
3767
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003768 if (lvalue)
3769 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003770 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003771}
3772
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003773llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003774 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003775
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003776 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003777 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00003778 llvm::ConstantDataArray::getString(VMContext,
3779 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003780 ((ObjCABI == 2) ?
3781 "__TEXT,__objc_classname,cstring_literals" :
3782 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003783 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003784
Owen Andersona1cf15f2009-07-14 23:10:40 +00003785 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003786}
3787
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003788llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3789 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3790 I = MethodDefinitions.find(MD);
3791 if (I != MethodDefinitions.end())
3792 return I->second;
3793
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003794 return NULL;
3795}
3796
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003797/// GetIvarLayoutName - Returns a unique constant for the given
3798/// ivar layout bitmap.
3799llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003800 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003801 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003802}
3803
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003804void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003805 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003806 bool ForStrongLayout,
3807 bool &HasUnion) {
3808 const RecordDecl *RD = RT->getDecl();
3809 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003810 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003811 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003812 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003813 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003814
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003815 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3816 ForStrongLayout, HasUnion);
3817}
3818
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003819void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003820 const llvm::StructLayout *Layout,
3821 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00003822 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003823 unsigned int BytePos, bool ForStrongLayout,
3824 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003825 bool IsUnion = (RD && RD->isUnion());
3826 uint64_t MaxUnionIvarSize = 0;
3827 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003828 const FieldDecl *MaxField = 0;
3829 const FieldDecl *MaxSkippedField = 0;
3830 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003831 uint64_t MaxFieldOffset = 0;
3832 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003833 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003834 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003835
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003836 if (RecFields.empty())
3837 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003838 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3839 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00003840 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003841 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003842 FirstFieldDelta =
3843 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3844 }
3845
Chris Lattnerf1690852009-03-31 08:48:01 +00003846 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003847 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003848 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003849 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003850 // Note that 'i' here is actually the field index inside RD of Field,
3851 // although this dependency is hidden.
3852 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003853 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003854 } else
John McCallf85e1932011-06-15 23:02:42 +00003855 FieldOffset =
3856 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003857
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003858 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003859 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003860 LastFieldBitfieldOrUnnamed = Field;
3861 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003862 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003863 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003864
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003865 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003866 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003867 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003868 if (FQT->isUnionType())
3869 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003870
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003871 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003872 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003873 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003874 continue;
3875 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003876
Chris Lattnerf1690852009-03-31 08:48:01 +00003877 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003878 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003879 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003880 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003881 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003882 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003883 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3884 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003885 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003886 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003887 FQT = CArray->getElementType();
3888 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003889
3890 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003891 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003892 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003893 int OldIndex = IvarsInfo.size() - 1;
3894 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003895
Ted Kremenek6217b802009-07-29 21:53:49 +00003896 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003897 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003898 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003899
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003900 // Replicate layout information for each array element. Note that
3901 // one element is already done.
3902 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003903 for (int FirstIndex = IvarsInfo.size() - 1,
3904 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003905 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003906 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3907 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3908 IvarsInfo[i].ivar_size));
3909 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3910 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3911 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003912 }
3913 continue;
3914 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003915 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003916 // At this point, we are done with Record/Union and array there of.
3917 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003918 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003919
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003920 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003921 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3922 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003923 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003924 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003925 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003926 MaxUnionIvarSize = UnionIvarSize;
3927 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003928 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003929 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003930 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003931 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003932 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003933 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003934 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003935 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3936 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003937 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003938 // FIXME: Why the asymmetry? We divide by word size in bits on other
3939 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003940 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003941 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003942 MaxSkippedUnionIvarSize = UnionIvarSize;
3943 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003944 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003945 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003946 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003947 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003948 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003949 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003950 }
3951 }
3952 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003953
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003954 if (LastFieldBitfieldOrUnnamed) {
3955 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3956 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003957 uint64_t BitFieldSize
3958 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003959 GC_IVAR skivar;
3960 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3961 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3962 + ((BitFieldSize % ByteSizeInBits) != 0);
3963 SkipIvars.push_back(skivar);
3964 } else {
3965 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3966 // Last field was unnamed. Must update skip info.
3967 unsigned FieldSize
3968 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3969 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3970 FieldSize / ByteSizeInBits));
3971 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003972 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003973
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003974 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003975 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003976 MaxUnionIvarSize));
3977 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003978 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003979 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003980}
3981
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003982/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3983/// the computations and returning the layout bitmap (for ivar or blocks) in
3984/// the given argument BitMap string container. Routine reads
3985/// two containers, IvarsInfo and SkipIvars which are assumed to be
3986/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00003987llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003988 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00003989 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003990
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003991 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003992 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003993 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003994 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003995 if (IvarsInfo[0].ivar_bytepos == 0) {
3996 WordsToSkip = 0;
3997 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003998 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003999 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4000 WordsToScan = IvarsInfo[0].ivar_size;
4001 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004002 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004003 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004004 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004005 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004006 // consecutive 'scanned' object pointers.
4007 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004008 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004009 // Skip over 'gc'able object pointer which lay over each other.
4010 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4011 continue;
4012 // Must skip over 1 or more words. We save current skip/scan values
4013 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004014 SKIP_SCAN SkScan;
4015 SkScan.skip = WordsToSkip;
4016 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004017 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004018
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004019 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004020 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4021 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004022 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004023 WordsToSkip = 0;
4024 WordsToScan = IvarsInfo[i].ivar_size;
4025 }
4026 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004027 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004028 SKIP_SCAN SkScan;
4029 SkScan.skip = WordsToSkip;
4030 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004031 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004032 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004033
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004034 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004035 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004036 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004037 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004038 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004039 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004040 IvarsInfo[LastIndex].ivar_bytepos +
4041 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004042 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004043 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004044 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004045 SKIP_SCAN SkScan;
4046 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4047 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004048 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004049 }
4050 }
4051 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4052 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004053 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004054 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004055 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4056 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4057 // 0xM0 followed by 0x0N detected.
4058 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4059 for (int j = i+1; j < SkipScan; j++)
4060 SkipScanIvars[j] = SkipScanIvars[j+1];
4061 --SkipScan;
4062 }
4063 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004064
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004065 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004066 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004067 unsigned char byte;
4068 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4069 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4070 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4071 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004072
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004073 // first skip big.
4074 for (unsigned int ix = 0; ix < skip_big; ix++)
4075 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004076
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004077 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004078 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004079 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004080 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004081 byte |= 0xf;
4082 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004083 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004084 byte |= scan_small;
4085 scan_small = 0;
4086 }
4087 BitMap += byte;
4088 }
4089 // next scan big
4090 for (unsigned int ix = 0; ix < scan_big; ix++)
4091 BitMap += (unsigned char)(0x0f);
4092 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004093 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004094 byte = scan_small;
4095 BitMap += byte;
4096 }
4097 }
4098 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004099 unsigned char zero = 0;
4100 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004101
4102 llvm::GlobalVariable * Entry =
4103 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004104 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004105 ((ObjCABI == 2) ?
4106 "__TEXT,__objc_classname,cstring_literals" :
4107 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004108 1, true);
4109 return getConstantGEP(VMContext, Entry, 0, 0);
4110}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004111
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004112/// BuildIvarLayout - Builds ivar layout bitmap for the class
4113/// implementation for the __strong or __weak case.
4114/// The layout map displays which words in ivar list must be skipped
4115/// and which must be scanned by GC (see below). String is built of bytes.
4116/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4117/// of words to skip and right nibble is count of words to scan. So, each
4118/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4119/// represented by a 0x00 byte which also ends the string.
4120/// 1. when ForStrongLayout is true, following ivars are scanned:
4121/// - id, Class
4122/// - object *
4123/// - __strong anything
4124///
4125/// 2. When ForStrongLayout is false, following ivars are scanned:
4126/// - __weak anything
4127///
4128llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4129 const ObjCImplementationDecl *OMD,
4130 bool ForStrongLayout) {
4131 bool hasUnion = false;
4132
Chris Lattner8b418682012-02-07 00:39:47 +00004133 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004134 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4135 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004136 return llvm::Constant::getNullValue(PtrTy);
4137
Jordy Rosedb8264e2011-07-22 02:08:32 +00004138 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4139 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004140 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004141 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004142 IVD; IVD = IVD->getNextIvar())
4143 RecFields.push_back(cast<FieldDecl>(IVD));
4144 }
4145 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004146 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004147 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004148
Jordy Rosedb8264e2011-07-22 02:08:32 +00004149 // FIXME: This is not ideal; we shouldn't have to do this copy.
4150 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004151 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004152
4153 if (RecFields.empty())
4154 return llvm::Constant::getNullValue(PtrTy);
4155
4156 SkipIvars.clear();
4157 IvarsInfo.clear();
4158
4159 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4160 if (IvarsInfo.empty())
4161 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004162 // Sort on byte position in case we encounterred a union nested in
4163 // the ivar list.
4164 if (hasUnion && !IvarsInfo.empty())
4165 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4166 if (hasUnion && !SkipIvars.empty())
4167 std::sort(SkipIvars.begin(), SkipIvars.end());
4168
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004169 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004170 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004171
David Blaikie4e4d0842012-03-11 07:00:24 +00004172 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004174 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004175 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004176 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004177 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004178 if (!(s[i] & 0xf0))
4179 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4180 else
4181 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4182 printf("\n");
4183 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004184 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004185}
4186
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004187llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004188 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4189
Chris Lattner0b239712012-02-06 22:16:34 +00004190 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004191 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004192 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004193 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004194 ((ObjCABI == 2) ?
4195 "__TEXT,__objc_methname,cstring_literals" :
4196 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004197 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004198
Owen Andersona1cf15f2009-07-14 23:10:40 +00004199 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004200}
4201
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004202// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004203llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004204 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4205}
4206
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004207llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004208 std::string TypeStr;
4209 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4210
4211 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004212
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004213 if (!Entry)
4214 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004215 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004216 ((ObjCABI == 2) ?
4217 "__TEXT,__objc_methtype,cstring_literals" :
4218 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004219 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220
Owen Andersona1cf15f2009-07-14 23:10:40 +00004221 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004222}
4223
Bob Wilsondc8dab62011-11-30 01:57:58 +00004224llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4225 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004226 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004227 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004228 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004229
4230 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4231
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004232 if (!Entry)
4233 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004234 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004235 ((ObjCABI == 2) ?
4236 "__TEXT,__objc_methtype,cstring_literals" :
4237 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004238 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004239
Owen Andersona1cf15f2009-07-14 23:10:40 +00004240 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004241}
4242
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004243// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004244llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004245 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004246
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004247 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004248 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004249 llvm::ConstantDataArray::getString(VMContext,
4250 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004251 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004252 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004253
Owen Andersona1cf15f2009-07-14 23:10:40 +00004254 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004255}
4256
4257// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004258// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004259llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004260CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4261 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004262 std::string TypeStr;
4263 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004264 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4265}
4266
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004267void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004268 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004269 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004270 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004271 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004272 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4273 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004274 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004275 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004276 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004277 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004278}
4279
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004280void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004281 EmitModuleInfo();
4282
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004283 // Emit the dummy bodies for any protocols which were referenced but
4284 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004285 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004286 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4287 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004288 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004289
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004290 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004291 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004292 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004293 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004294 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004295 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004296 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004297 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004298 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004299 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004300 }
4301
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004302 // Add assembler directives to add lazy undefined symbol references
4303 // for classes which are referenced but not defined. This is
4304 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004305 //
4306 // FIXME: It would be nice if we had an LLVM construct for this.
4307 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004308 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004309 Asm += CGM.getModule().getModuleInlineAsm();
4310 if (!Asm.empty() && Asm.back() != '\n')
4311 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004312
Daniel Dunbar33063492009-09-07 00:20:42 +00004313 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004314 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4315 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004316 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4317 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004318 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004319 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004320 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004321 }
4322
Bill Wendling13562a12012-02-07 09:06:01 +00004323 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004324 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4325 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4326 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004327
Daniel Dunbar33063492009-09-07 00:20:42 +00004328 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004329 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004330}
4331
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004332CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004333 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004334 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004335 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004336 ObjCABI = 2;
4337}
4338
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004339/* *** */
4340
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004341ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004342 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4343{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4345 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004346
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004347 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004348 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004349 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004350 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004351 Int8PtrTy = CGM.Int8PtrTy;
4352 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004354 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004355 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004356 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004358 // I'm not sure I like this. The implicit coordination is a bit
4359 // gross. We should solve this in a reasonable fashion because this
4360 // is a pretty common task (match some runtime data structure with
4361 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004362
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004363 // FIXME: This is leaked.
4364 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004365
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004366 // struct _objc_super {
4367 // id self;
4368 // Class cls;
4369 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004370 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004371 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004372 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004374 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004375 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004376 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004377 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004378 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004379
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004380 SuperCTy = Ctx.getTagDeclType(RD);
4381 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004383 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004384 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4385
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004386 // struct _prop_t {
4387 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004388 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004389 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004390 PropertyTy = llvm::StructType::create("struct._prop_t",
4391 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004392
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004393 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004394 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004396 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004397 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004398 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004399 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4400 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004401 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004402 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004404 // struct _objc_method {
4405 // SEL _cmd;
4406 // char *method_type;
4407 // char *_imp;
4408 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004409 MethodTy = llvm::StructType::create("struct._objc_method",
4410 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4411 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004413 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004414 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004415 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004416
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004417}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004418
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004419ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004420 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004421 // struct _objc_method_description {
4422 // SEL name;
4423 // char *types;
4424 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004425 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004426 llvm::StructType::create("struct._objc_method_description",
4427 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004428
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004429 // struct _objc_method_description_list {
4430 // int count;
4431 // struct _objc_method_description[1];
4432 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004433 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004434 llvm::StructType::create("struct._objc_method_description_list",
4435 IntTy,
4436 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004437
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004438 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004439 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004440 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004441
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004442 // Protocol description structures
4443
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004444 // struct _objc_protocol_extension {
4445 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4446 // struct _objc_method_description_list *optional_instance_methods;
4447 // struct _objc_method_description_list *optional_class_methods;
4448 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004449 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004450 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004451 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004452 llvm::StructType::create("struct._objc_protocol_extension",
4453 IntTy, MethodDescriptionListPtrTy,
4454 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004455 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004456
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004457 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004458 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004459
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004460 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004461
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004462 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004463 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004464
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004465 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004466 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004467 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004468 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004469 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004470 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004471
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004472 // struct _objc_protocol {
4473 // struct _objc_protocol_extension *isa;
4474 // char *protocol_name;
4475 // struct _objc_protocol **_objc_protocol_list;
4476 // struct _objc_method_description_list *instance_methods;
4477 // struct _objc_method_description_list *class_methods;
4478 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004479 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4480 llvm::PointerType::getUnqual(ProtocolListTy),
4481 MethodDescriptionListPtrTy,
4482 MethodDescriptionListPtrTy,
4483 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004484
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004485 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004486 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004487
Owen Anderson96e0fc72009-07-29 22:16:19 +00004488 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004489
4490 // Class description structures
4491
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004492 // struct _objc_ivar {
4493 // char *ivar_name;
4494 // char *ivar_type;
4495 // int ivar_offset;
4496 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004497 IvarTy = llvm::StructType::create("struct._objc_ivar",
4498 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004499
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004500 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004501 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004502 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004503 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004504
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004505 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004506 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004507 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004508 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004509
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004510 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004511 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004512 llvm::StructType::create("struct._objc_class_extension",
4513 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004514 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004515
Chris Lattnerc1c20112011-08-12 17:43:31 +00004516 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004517
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004518 // struct _objc_class {
4519 // Class isa;
4520 // Class super_class;
4521 // char *name;
4522 // long version;
4523 // long info;
4524 // long instance_size;
4525 // struct _objc_ivar_list *ivars;
4526 // struct _objc_method_list *methods;
4527 // struct _objc_cache *cache;
4528 // struct _objc_protocol_list *protocols;
4529 // char *ivar_layout;
4530 // struct _objc_class_ext *ext;
4531 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004532 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4533 llvm::PointerType::getUnqual(ClassTy),
4534 Int8PtrTy,
4535 LongTy,
4536 LongTy,
4537 LongTy,
4538 IvarListPtrTy,
4539 MethodListPtrTy,
4540 CachePtrTy,
4541 ProtocolListPtrTy,
4542 Int8PtrTy,
4543 ClassExtensionPtrTy,
4544 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545
Owen Anderson96e0fc72009-07-29 22:16:19 +00004546 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004547
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004548 // struct _objc_category {
4549 // char *category_name;
4550 // char *class_name;
4551 // struct _objc_method_list *instance_method;
4552 // struct _objc_method_list *class_method;
4553 // uint32_t size; // sizeof(struct _objc_category)
4554 // struct _objc_property_list *instance_properties;// category's @property
4555 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004556 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004557 llvm::StructType::create("struct._objc_category",
4558 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4559 MethodListPtrTy, ProtocolListPtrTy,
4560 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004561
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004562 // Global metadata structures
4563
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004564 // struct _objc_symtab {
4565 // long sel_ref_cnt;
4566 // SEL *refs;
4567 // short cls_def_cnt;
4568 // short cat_def_cnt;
4569 // char *defs[cls_def_cnt + cat_def_cnt];
4570 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004571 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004572 llvm::StructType::create("struct._objc_symtab",
4573 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4574 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004575 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004576
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004577 // struct _objc_module {
4578 // long version;
4579 // long size; // sizeof(struct _objc_module)
4580 // char *name;
4581 // struct _objc_symtab* symtab;
4582 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004583 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004584 llvm::StructType::create("struct._objc_module",
4585 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004586
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587
Mike Stumpf5408fe2009-05-16 07:57:57 +00004588 // FIXME: This is the size of the setjmp buffer and should be target
4589 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004590 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004591
Anders Carlsson124526b2008-09-09 10:10:21 +00004592 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00004593 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594
4595 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004596 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00004597 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4598 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004599
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004600}
4601
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004603 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004604 // struct _method_list_t {
4605 // uint32_t entsize; // sizeof(struct _objc_method)
4606 // uint32_t method_count;
4607 // struct _objc_method method_list[method_count];
4608 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004609 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004610 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4611 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004612 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004613 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004614
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004615 // struct _protocol_t {
4616 // id isa; // NULL
4617 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004618 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004619 // const struct method_list_t * const instance_methods;
4620 // const struct method_list_t * const class_methods;
4621 // const struct method_list_t *optionalInstanceMethods;
4622 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004623 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004624 // const uint32_t size; // sizeof(struct _protocol_t)
4625 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004626 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004627 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004628
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004629 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004630 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004631 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004632
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004633 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004634 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4635 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4636 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4637 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004638 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4639 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004640
4641 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004642 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004643
Fariborz Jahanianda320092009-01-29 19:24:30 +00004644 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004645 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004646 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004647 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004648 ProtocolListnfABITy->setBody(LongTy,
4649 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4650 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004652 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004653 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004654
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004655 // struct _ivar_t {
4656 // unsigned long int *offset; // pointer to ivar offset location
4657 // char *name;
4658 // char *type;
4659 // uint32_t alignment;
4660 // uint32_t size;
4661 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004662 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004663 llvm::StructType::create("struct._ivar_t",
4664 llvm::PointerType::getUnqual(LongTy),
4665 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004666
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004667 // struct _ivar_list_t {
4668 // uint32 entsize; // sizeof(struct _ivar_t)
4669 // uint32 count;
4670 // struct _iver_t list[count];
4671 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004672 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004673 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4674 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675
Owen Anderson96e0fc72009-07-29 22:16:19 +00004676 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004677
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004678 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004679 // uint32_t const flags;
4680 // uint32_t const instanceStart;
4681 // uint32_t const instanceSize;
4682 // uint32_t const reserved; // only when building for 64bit targets
4683 // const uint8_t * const ivarLayout;
4684 // const char *const name;
4685 // const struct _method_list_t * const baseMethods;
4686 // const struct _objc_protocol_list *const baseProtocols;
4687 // const struct _ivar_list_t *const ivars;
4688 // const uint8_t * const weakIvarLayout;
4689 // const struct _prop_list_t * const properties;
4690 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004691
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004692 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004693 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4694 IntTy, IntTy, IntTy, Int8PtrTy,
4695 Int8PtrTy, MethodListnfABIPtrTy,
4696 ProtocolListnfABIPtrTy,
4697 IvarListnfABIPtrTy,
4698 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004699
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004700 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004701 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004702 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4703 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004704
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004705 // struct _class_t {
4706 // struct _class_t *isa;
4707 // struct _class_t * const superclass;
4708 // void *cache;
4709 // IMP *vtable;
4710 // struct class_ro_t *ro;
4711 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004712
Chris Lattnerc1c20112011-08-12 17:43:31 +00004713 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004714 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4715 llvm::PointerType::getUnqual(ClassnfABITy),
4716 CachePtrTy,
4717 llvm::PointerType::getUnqual(ImpnfABITy),
4718 llvm::PointerType::getUnqual(ClassRonfABITy),
4719 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004720
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004721 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004722 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004723
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004724 // struct _category_t {
4725 // const char * const name;
4726 // struct _class_t *const cls;
4727 // const struct _method_list_t * const instance_methods;
4728 // const struct _method_list_t * const class_methods;
4729 // const struct _protocol_list_t * const protocols;
4730 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004731 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004732 CategorynfABITy = llvm::StructType::create("struct._category_t",
4733 Int8PtrTy, ClassnfABIPtrTy,
4734 MethodListnfABIPtrTy,
4735 MethodListnfABIPtrTy,
4736 ProtocolListnfABIPtrTy,
4737 PropertyListPtrTy,
4738 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004739
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004740 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004741 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4742 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004744 // MessageRefTy - LLVM for:
4745 // struct _message_ref_t {
4746 // IMP messenger;
4747 // SEL name;
4748 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004749
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004750 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004751 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004752 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004753 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004754 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004755 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004756 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004757 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004758 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004759 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004761 MessageRefCTy = Ctx.getTagDeclType(RD);
4762 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4763 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004765 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004766 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004767
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004768 // SuperMessageRefTy - LLVM for:
4769 // struct _super_message_ref_t {
4770 // SUPER_IMP messenger;
4771 // SEL name;
4772 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004773 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004774 llvm::StructType::create("struct._super_message_ref_t",
4775 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004777 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004778 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004779
Daniel Dunbare588b992009-03-01 04:46:24 +00004780
4781 // struct objc_typeinfo {
4782 // const void** vtable; // objc_ehtype_vtable + 2
4783 // const char* name; // c++ typeinfo string
4784 // Class cls;
4785 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004786 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004787 llvm::StructType::create("struct._objc_typeinfo",
4788 llvm::PointerType::getUnqual(Int8PtrTy),
4789 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004790 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004791}
4792
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004793llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004794 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004795
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004796 return NULL;
4797}
4798
Bill Wendlingbb028552012-02-07 09:25:09 +00004799void CGObjCNonFragileABIMac::
4800AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4801 const char *SymbolName,
4802 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00004803 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004804
Daniel Dunbar463b8762009-05-15 21:48:48 +00004805 if (!NumClasses)
4806 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004807
Chris Lattner0b239712012-02-06 22:16:34 +00004808 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004809 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004810 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004811 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00004812 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004813 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004814 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00004815 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004816
Daniel Dunbar463b8762009-05-15 21:48:48 +00004817 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004818 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004819 llvm::GlobalValue::InternalLinkage,
4820 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004821 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004822 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004823 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004824 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004825}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004826
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004827void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4828 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004829
Daniel Dunbar463b8762009-05-15 21:48:48 +00004830 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004831 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004833 "\01L_OBJC_LABEL_CLASS_$",
4834 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004835
Bill Wendling13562a12012-02-07 09:06:01 +00004836 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004837 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4838 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4839 continue;
4840 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004841 }
4842
Bill Wendling13562a12012-02-07 09:06:01 +00004843 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004844 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4845 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4846 continue;
4847 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4848 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004849
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004850 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004851 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4852 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004854 // Build list of all implemented category addresses in array
4855 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004857 "\01L_OBJC_LABEL_CATEGORY_$",
4858 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004859 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004860 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4861 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004863 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004864}
4865
John McCall944c8432011-05-14 03:10:52 +00004866/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4867/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004869/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004870bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4871 // At various points we've experimented with using vtable-based
4872 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004873 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004874 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004875 return false;
John McCall944c8432011-05-14 03:10:52 +00004876 case CodeGenOptions::NonLegacy:
4877 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004878 case CodeGenOptions::Mixed:
4879 break;
4880 }
4881
4882 // If so, see whether this selector is in the white-list of things which must
4883 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004884 if (VTableDispatchMethods.empty()) {
4885 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4886 VTableDispatchMethods.insert(GetNullarySelector("class"));
4887 VTableDispatchMethods.insert(GetNullarySelector("self"));
4888 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4889 VTableDispatchMethods.insert(GetNullarySelector("length"));
4890 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004891
John McCall944c8432011-05-14 03:10:52 +00004892 // These are vtable-based if GC is disabled.
4893 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004894 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004895 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4896 VTableDispatchMethods.insert(GetNullarySelector("release"));
4897 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4898 }
4899
4900 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4901 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4902 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4903 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4904 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4905 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4906 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4907
4908 // These are vtable-based if GC is enabled.
4909 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004910 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004911 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4912 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4913
4914 // "countByEnumeratingWithState:objects:count"
4915 IdentifierInfo *KeyIdents[] = {
4916 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4917 &CGM.getContext().Idents.get("objects"),
4918 &CGM.getContext().Idents.get("count")
4919 };
4920 VTableDispatchMethods.insert(
4921 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4922 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004923 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004924
John McCall944c8432011-05-14 03:10:52 +00004925 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004926}
4927
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004928// Metadata flags
4929enum MetaDataDlags {
4930 CLS = 0x0,
4931 CLS_META = 0x1,
4932 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004933 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004934 CLS_EXCEPTION = 0x20,
4935
4936 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4937 CLS_HAS_IVAR_RELEASER = 0x40,
4938 /// class was compiled with -fobjc-arr
4939 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004940};
4941/// BuildClassRoTInitializer - generate meta-data for:
4942/// struct _class_ro_t {
4943/// uint32_t const flags;
4944/// uint32_t const instanceStart;
4945/// uint32_t const instanceSize;
4946/// uint32_t const reserved; // only when building for 64bit targets
4947/// const uint8_t * const ivarLayout;
4948/// const char *const name;
4949/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004950/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004951/// const struct _ivar_list_t *const ivars;
4952/// const uint8_t * const weakIvarLayout;
4953/// const struct _prop_list_t * const properties;
4954/// }
4955///
4956llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004957 unsigned flags,
4958 unsigned InstanceStart,
4959 unsigned InstanceSize,
4960 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004961 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004962 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004963
David Blaikie4e4d0842012-03-11 07:00:24 +00004964 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00004965 flags |= CLS_COMPILED_BY_ARC;
4966
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004967 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4968 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4969 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004970 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004971 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4972 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004973 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004974 // const struct _method_list_t * const baseMethods;
4975 std::vector<llvm::Constant*> Methods;
4976 std::string MethodListName("\01l_OBJC_$_");
4977 if (flags & CLS_META) {
4978 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004979 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004980 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004981 // Class methods should always be defined.
4982 Methods.push_back(GetMethodConstant(*i));
4983 }
4984 } else {
4985 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004987 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004988 // Instance methods should always be defined.
4989 Methods.push_back(GetMethodConstant(*i));
4990 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004991 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004992 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004993 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004994
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004995 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4996 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004997
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004998 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4999 if (llvm::Constant *C = GetMethodConstant(MD))
5000 Methods.push_back(C);
5001 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5002 if (llvm::Constant *C = GetMethodConstant(MD))
5003 Methods.push_back(C);
5004 }
5005 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005006 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005007 Values[ 5] = EmitMethodList(MethodListName,
5008 "__DATA, __objc_const", Methods);
5009
Fariborz Jahanianda320092009-01-29 19:24:30 +00005010 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5011 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005013 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005014 OID->all_referenced_protocol_begin(),
5015 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005016
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005017 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005018 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005019 else
5020 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005021 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
5022 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005023 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005024 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005025 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005026 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5027 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005028 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005029 Values);
5030 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005031 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5032 llvm::GlobalValue::InternalLinkage,
5033 Init,
5034 (flags & CLS_META) ?
5035 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5036 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005037 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005038 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005039 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005040 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005041
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005042}
5043
5044/// BuildClassMetaData - This routine defines that to-level meta-data
5045/// for the given ClassName for:
5046/// struct _class_t {
5047/// struct _class_t *isa;
5048/// struct _class_t * const superclass;
5049/// void *cache;
5050/// IMP *vtable;
5051/// struct class_ro_t *ro;
5052/// }
5053///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005054llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055 std::string &ClassName,
5056 llvm::Constant *IsAGV,
5057 llvm::Constant *SuperClassGV,
5058 llvm::Constant *ClassRoGV,
5059 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005060 llvm::Constant *Values[] = {
5061 IsAGV,
5062 SuperClassGV,
5063 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5064 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5065 ClassRoGV // &CLASS_RO_GV
5066 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005067 if (!Values[1])
5068 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005069 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005070 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005071 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5072 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005073 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005074 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005075 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005076 if (HiddenVisibility)
5077 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005078 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005079}
5080
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005082CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005083 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005084}
5085
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005086void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005087 uint32_t &InstanceStart,
5088 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005089 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005090 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005091
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005092 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005093 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005094
5095 // If there are no fields, the start is the same as the end.
5096 if (!RL.getFieldCount())
5097 InstanceStart = InstanceSize;
5098 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005099 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005100}
5101
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005102void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5103 std::string ClassName = ID->getNameAsString();
5104 if (!ObjCEmptyCacheVar) {
5105 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005106 CGM.getModule(),
5107 ObjCTypes.CacheTy,
5108 false,
5109 llvm::GlobalValue::ExternalLinkage,
5110 0,
5111 "_objc_empty_cache");
5112
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005113 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005114 CGM.getModule(),
5115 ObjCTypes.ImpnfABITy,
5116 false,
5117 llvm::GlobalValue::ExternalLinkage,
5118 0,
5119 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005120 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005121 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005122 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005123 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005124 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00005125 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005126 uint32_t InstanceSize = InstanceStart;
5127 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005128 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5129 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005130
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005131 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005132
5133 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005134 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005135 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005136 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005137 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005138 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005139 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005140 // class is root
5141 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005142 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005143 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005144 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005145 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005146 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5147 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5148 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005149 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005150 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005151 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005152 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005153 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005154 ObjCMetaClassName +
5155 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005156 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005157 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005158 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005159 }
5160 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5161 InstanceStart,
5162 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005163 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005164 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005165 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5166 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005167 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005168
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005169 // Metadata for the class
5170 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005171 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005172 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005173 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005174 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005175
Douglas Gregor68584ed2009-06-18 16:11:24 +00005176 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005177 flags |= CLS_EXCEPTION;
5178
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005179 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005180 flags |= CLS_ROOT;
5181 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005182 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005183 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005184 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005185 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005186 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005187 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005188 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005189 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005190 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005191 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005192 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005193 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005194 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005195
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005196 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005197 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005198 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5199 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005200 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005201
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005202 // Determine if this class is also "non-lazy".
5203 if (ImplementationIsNonLazy(ID))
5204 DefinedNonLazyClasses.push_back(ClassMD);
5205
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005206 // Force the definition of the EHType if necessary.
5207 if (flags & CLS_EXCEPTION)
5208 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005209 // Make sure method definition entries are all clear for next implementation.
5210 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005211}
5212
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005213/// GenerateProtocolRef - This routine is called to generate code for
5214/// a protocol reference expression; as in:
5215/// @code
5216/// @protocol(Proto1);
5217/// @endcode
5218/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5219/// which will hold address of the protocol meta-data.
5220///
5221llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005222 const ObjCProtocolDecl *PD) {
5223
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005224 // This routine is called for @protocol only. So, we must build definition
5225 // of protocol's meta-data (not a reference to it!)
5226 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005227 llvm::Constant *Init =
5228 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005229 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005231 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005232 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005233
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005234 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5235 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005236 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005237 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238 CGM.getModule(),
5239 Init->getType(), false,
5240 llvm::GlobalValue::WeakAnyLinkage,
5241 Init,
5242 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005243 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5244 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005245 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005246 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005247}
5248
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005249/// GenerateCategory - Build metadata for a category implementation.
5250/// struct _category_t {
5251/// const char * const name;
5252/// struct _class_t *const cls;
5253/// const struct _method_list_t * const instance_methods;
5254/// const struct _method_list_t * const class_methods;
5255/// const struct _protocol_list_t * const protocols;
5256/// const struct _prop_list_t * const properties;
5257/// }
5258///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005259void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005260 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005261 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005262 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5263 "_$_" + OCD->getNameAsString());
5264 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005265 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005266
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005267 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005268 Values[0] = GetClassName(OCD->getIdentifier());
5269 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005270 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005271 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005272 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5273
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005274 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005275 std::vector<llvm::Constant*> Methods;
5276 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005277 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005278 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005279
5280 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005281 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005282 // Instance methods should always be defined.
5283 Methods.push_back(GetMethodConstant(*i));
5284 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005285
5286 Values[2] = EmitMethodList(MethodListName,
5287 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005288 Methods);
5289
5290 MethodListName = Prefix;
5291 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5292 OCD->getNameAsString();
5293 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005294 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005295 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005296 // Class methods should always be defined.
5297 Methods.push_back(GetMethodConstant(*i));
5298 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
5300 Values[3] = EmitMethodList(MethodListName,
5301 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005302 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005304 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005305 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005306 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005307 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5308 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005309 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005310 + Interface->getName() + "_$_"
5311 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005312 Category->protocol_begin(),
5313 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005314 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5315 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005316 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005317 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5318 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005319 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005320
5321 llvm::Constant *Init =
5322 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005323 Values);
5324 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005325 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005326 false,
5327 llvm::GlobalValue::InternalLinkage,
5328 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005329 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005330 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005331 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005332 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005333 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005334 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005335
5336 // Determine if this category is also "non-lazy".
5337 if (ImplementationIsNonLazy(OCD))
5338 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005339 // method definition entries must be clear for next implementation.
5340 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005341}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005342
5343/// GetMethodConstant - Return a struct objc_method constant for the
5344/// given method if it has been defined. The result is null if the
5345/// method has not been defined. The return value has type MethodPtrTy.
5346llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005348 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005349 if (!Fn)
5350 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005351
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005352 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005353 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005354 ObjCTypes.SelectorPtrTy),
5355 GetMethodVarType(MD),
5356 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5357 };
Owen Anderson08e25242009-07-27 22:29:56 +00005358 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005359}
5360
5361/// EmitMethodList - Build meta-data for method declarations
5362/// struct _method_list_t {
5363/// uint32_t entsize; // sizeof(struct _objc_method)
5364/// uint32_t method_count;
5365/// struct _objc_method method_list[method_count];
5366/// }
5367///
Bill Wendlingbb028552012-02-07 09:25:09 +00005368llvm::Constant *
5369CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5370 const char *Section,
5371 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005372 // Return null for empty list.
5373 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005374 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005375
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005376 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005377 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005378 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005379 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005380 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005381 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005382 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005383 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005384 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005385 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005387 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005388 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005389 llvm::GlobalValue::InternalLinkage, Init, Name);
5390 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005391 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005392 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005393 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005394}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005395
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005396/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5397/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005398llvm::GlobalVariable *
5399CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5400 const ObjCIvarDecl *Ivar) {
5401 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005402 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005403 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005405 CGM.getModule().getGlobalVariable(Name);
5406 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005407 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005408 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005409 false,
5410 llvm::GlobalValue::ExternalLinkage,
5411 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005412 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005413 return IvarOffsetGV;
5414}
5415
Daniel Dunbare83be122010-04-02 21:14:02 +00005416llvm::Constant *
5417CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5418 const ObjCIvarDecl *Ivar,
5419 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005420 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005421 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005422 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005423 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005424 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005425
Mike Stumpf5408fe2009-05-16 07:57:57 +00005426 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5427 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005428 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5429 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005430 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005431 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005432 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005433 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005434 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005435 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005436}
5437
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005438/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005439/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005440/// IvarListnfABIPtrTy.
5441/// struct _ivar_t {
5442/// unsigned long int *offset; // pointer to ivar offset location
5443/// char *name;
5444/// char *type;
5445/// uint32_t alignment;
5446/// uint32_t size;
5447/// }
5448/// struct _ivar_list_t {
5449/// uint32 entsize; // sizeof(struct _ivar_t)
5450/// uint32 count;
5451/// struct _iver_t list[count];
5452/// }
5453///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005454
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005455llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005456 const ObjCImplementationDecl *ID) {
5457
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005458 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459
Jordy Rosedb8264e2011-07-22 02:08:32 +00005460 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005461 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005463 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005464
Jordy Rosedb8264e2011-07-22 02:08:32 +00005465 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005466 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005467 // Ignore unnamed bit-fields.
5468 if (!IVD->getDeclName())
5469 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005470 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005471 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005472 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005473 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5474 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005475 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005476 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005477 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005478 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005479 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005480 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005481 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005482 // NOTE. Size of a bitfield does not match gcc's, because of the
5483 // way bitfields are treated special in each. But I am told that
5484 // 'size' for bitfield ivars is ignored by the runtime so it does
5485 // not matter. If it matters, there is enough info to get the
5486 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005487 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005488 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005489 }
5490 // Return null for empty list.
5491 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005492 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005493
5494 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005495 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005496 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5497 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005498 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005499 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005500 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005501 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005502 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5503 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005504 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005505 llvm::GlobalValue::InternalLinkage,
5506 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005507 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005508 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005509 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005510 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Chris Lattnerad64e022009-07-17 23:57:13 +00005512 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005513 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005514}
5515
5516llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005517 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005518 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005519
Fariborz Jahanianda320092009-01-29 19:24:30 +00005520 if (!Entry) {
5521 // We use the initializer as a marker of whether this is a forward
5522 // reference or not. At module finalization we add the empty
5523 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005524 Entry =
5525 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5526 llvm::GlobalValue::ExternalLinkage,
5527 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005528 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005529 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005530 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005531
Fariborz Jahanianda320092009-01-29 19:24:30 +00005532 return Entry;
5533}
5534
5535/// GetOrEmitProtocol - Generate the protocol meta-data:
5536/// @code
5537/// struct _protocol_t {
5538/// id isa; // NULL
5539/// const char * const protocol_name;
5540/// const struct _protocol_list_t * protocol_list; // super protocols
5541/// const struct method_list_t * const instance_methods;
5542/// const struct method_list_t * const class_methods;
5543/// const struct method_list_t *optionalInstanceMethods;
5544/// const struct method_list_t *optionalClassMethods;
5545/// const struct _prop_list_t * properties;
5546/// const uint32_t size; // sizeof(struct _protocol_t)
5547/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005548/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005549/// }
5550/// @endcode
5551///
5552
5553llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005554 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005555 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005556
Fariborz Jahanianda320092009-01-29 19:24:30 +00005557 // Early exit if a defining object has already been generated.
5558 if (Entry && Entry->hasInitializer())
5559 return Entry;
5560
Douglas Gregor1d784b22012-01-01 19:51:50 +00005561 // Use the protocol definition, if there is one.
5562 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5563 PD = Def;
5564
Fariborz Jahanianda320092009-01-29 19:24:30 +00005565 // Construct method lists.
5566 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5567 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005568 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005569 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005570 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005571 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005572 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005573 if (!C)
5574 return GetOrEmitProtocolRef(PD);
5575
Fariborz Jahanianda320092009-01-29 19:24:30 +00005576 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5577 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005578 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005579 } else {
5580 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005581 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005583 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005584
5585 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005586 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005587 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005588 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005589 if (!C)
5590 return GetOrEmitProtocolRef(PD);
5591
Fariborz Jahanianda320092009-01-29 19:24:30 +00005592 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5593 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005594 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005595 } else {
5596 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005597 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005598 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005599 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600
Bob Wilsondc8dab62011-11-30 01:57:58 +00005601 MethodTypesExt.insert(MethodTypesExt.end(),
5602 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5603
5604 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005605 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005606 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005607 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005608 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5609 PD->protocol_begin(),
5610 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005612 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005613 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005614 "__DATA, __objc_const",
5615 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005616 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005617 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005618 "__DATA, __objc_const",
5619 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005620 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005621 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005622 "__DATA, __objc_const",
5623 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005624 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005625 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005626 "__DATA, __objc_const",
5627 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005628 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005629 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005630 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005631 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005632 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005633 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005634 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5635 + PD->getName(),
5636 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005637 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005638 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639
Fariborz Jahanianda320092009-01-29 19:24:30 +00005640 if (Entry) {
5641 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005642 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005643 Entry->setInitializer(Init);
5644 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005645 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005646 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5647 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5648 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005649 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005650 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005651 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005652 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005653 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005654 CGM.AddUsedGlobal(Entry);
5655
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005656 // Use this protocol meta-data to build protocol list table in section
5657 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005658 llvm::GlobalVariable *PTGV =
5659 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5660 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5661 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005662 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005663 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005664 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005665 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005666 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005667 return Entry;
5668}
5669
5670/// EmitProtocolList - Generate protocol list meta-data:
5671/// @code
5672/// struct _protocol_list_t {
5673/// long protocol_count; // Note, this is 32/64 bit
5674/// struct _protocol_t[protocol_count];
5675/// }
5676/// @endcode
5677///
5678llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005679CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005680 ObjCProtocolDecl::protocol_iterator begin,
5681 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00005682 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005683
Fariborz Jahanianda320092009-01-29 19:24:30 +00005684 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005686 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005687
Daniel Dunbar948e2582009-02-15 07:36:20 +00005688 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005689 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005690 Name.toVector(TmpName);
5691 llvm::GlobalVariable *GV =
5692 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005693 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005694 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005695
Daniel Dunbar948e2582009-02-15 07:36:20 +00005696 for (; begin != end; ++begin)
5697 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5698
Fariborz Jahanianda320092009-01-29 19:24:30 +00005699 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005700 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701 ObjCTypes.ProtocolnfABIPtrTy));
5702
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005703 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005704 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005705 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00005707 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5708 ProtocolRefs.size()),
5709 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005711 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005712 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005713 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005714 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005715 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005716 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005717 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005718 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005720 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005721}
5722
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005723/// GetMethodDescriptionConstant - This routine build following meta-data:
5724/// struct _objc_method {
5725/// SEL _cmd;
5726/// char *method_type;
5727/// char *_imp;
5728/// }
5729
5730llvm::Constant *
5731CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005732 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005733 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5735 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005736 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005737 if (!Desc[1])
5738 return 0;
5739
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005740 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005741 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005742 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005743}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005744
5745/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5746/// This code gen. amounts to generating code for:
5747/// @code
5748/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5749/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005750///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005751LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005752 CodeGen::CodeGenFunction &CGF,
5753 QualType ObjectTy,
5754 llvm::Value *BaseValue,
5755 const ObjCIvarDecl *Ivar,
5756 unsigned CVRQualifiers) {
5757 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005758 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5759 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5760 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5761 llvm::MDNode::get(VMContext,
5762 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00005763 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005764 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005765}
5766
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005767llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005768 CodeGen::CodeGenFunction &CGF,
5769 const ObjCInterfaceDecl *Interface,
5770 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005771 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005772}
5773
John McCallb1e81442011-05-13 23:16:18 +00005774static void appendSelectorForMessageRefTable(std::string &buffer,
5775 Selector selector) {
5776 if (selector.isUnarySelector()) {
5777 buffer += selector.getNameForSlot(0);
5778 return;
5779 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005780
John McCallb1e81442011-05-13 23:16:18 +00005781 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5782 buffer += selector.getNameForSlot(i);
5783 buffer += '_';
5784 }
5785}
5786
John McCall944c8432011-05-14 03:10:52 +00005787/// Emit a "v-table" message send. We emit a weak hidden-visibility
5788/// struct, initially containing the selector pointer and a pointer to
5789/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5790/// load and call the function pointer, passing the address of the
5791/// struct as the second parameter. The runtime determines whether
5792/// the selector is currently emitted using vtable dispatch; if so, it
5793/// substitutes a stub function which simply tail-calls through the
5794/// appropriate vtable slot, and if not, it substitues a stub function
5795/// which tail-calls objc_msgSend. Both stubs adjust the selector
5796/// argument to correctly point to the selector.
5797RValue
5798CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5799 ReturnValueSlot returnSlot,
5800 QualType resultType,
5801 Selector selector,
5802 llvm::Value *arg0,
5803 QualType arg0Type,
5804 bool isSuper,
5805 const CallArgList &formalArgs,
5806 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005807 // Compute the actual arguments.
5808 CallArgList args;
5809
John McCall944c8432011-05-14 03:10:52 +00005810 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005811 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005812 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5813 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005814
John McCall944c8432011-05-14 03:10:52 +00005815 // Second argument: a pointer to the message ref structure. Leave
5816 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005817 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5818
5819 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5820
John McCallde5d3c72012-02-17 03:33:10 +00005821 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00005822
John McCallcba681a2011-05-14 21:12:11 +00005823 NullReturnState nullReturn;
5824
John McCall944c8432011-05-14 03:10:52 +00005825 // Find the function to call and the mangled name for the message
5826 // ref structure. Using a different mangled name wouldn't actually
5827 // be a problem; it would just be a waste.
5828 //
5829 // The runtime currently never uses vtable dispatch for anything
5830 // except normal, non-super message-sends.
5831 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005832 llvm::Constant *fn = 0;
5833 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00005834 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005835 if (isSuper) {
5836 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5837 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005838 } else {
John McCallcba681a2011-05-14 21:12:11 +00005839 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005840 fn = ObjCTypes.getMessageSendStretFixupFn();
5841 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005842 }
John McCallb1e81442011-05-13 23:16:18 +00005843 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5844 fn = ObjCTypes.getMessageSendFpretFixupFn();
5845 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005846 } else {
John McCallb1e81442011-05-13 23:16:18 +00005847 if (isSuper) {
5848 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5849 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005850 } else {
John McCallb1e81442011-05-13 23:16:18 +00005851 fn = ObjCTypes.getMessageSendFixupFn();
5852 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005853 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005854 }
John McCallb1e81442011-05-13 23:16:18 +00005855 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5856 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005857
John McCallb1e81442011-05-13 23:16:18 +00005858 // Append the selector name, except use underscores anywhere we
5859 // would have used colons.
5860 appendSelectorForMessageRefTable(messageRefName, selector);
5861
5862 llvm::GlobalVariable *messageRef
5863 = CGM.getModule().getGlobalVariable(messageRefName);
5864 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005865 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005866 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005867 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005868 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5869 init->getType(),
5870 /*constant*/ false,
5871 llvm::GlobalValue::WeakAnyLinkage,
5872 init,
5873 messageRefName);
5874 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5875 messageRef->setAlignment(16);
5876 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5877 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005878
5879 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00005880 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005881 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5882 e = method->param_end(); i != e; ++i) {
5883 const ParmVarDecl *ParamDecl = (*i);
5884 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5885 if (!nullReturn.NullBB)
5886 nullReturn.init(CGF, arg0);
5887 requiresnullCheck = true;
5888 break;
5889 }
5890 }
5891
John McCallb1e81442011-05-13 23:16:18 +00005892 llvm::Value *mref =
5893 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5894
John McCall944c8432011-05-14 03:10:52 +00005895 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005896 args[1].RV = RValue::get(mref);
5897
5898 // Load the function to call from the message ref table.
5899 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5900 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5901
John McCallde5d3c72012-02-17 03:33:10 +00005902 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00005903
John McCallde5d3c72012-02-17 03:33:10 +00005904 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005905 return nullReturn.complete(CGF, result, resultType, formalArgs,
5906 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005907}
5908
5909/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005910CodeGen::RValue
5911CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005912 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005913 QualType ResultType,
5914 Selector Sel,
5915 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005916 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005917 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005918 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005919 return isVTableDispatchedSelector(Sel)
5920 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005922 false, CallArgs, Method)
5923 : EmitMessageSend(CGF, Return, ResultType,
5924 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005925 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005926 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005927}
5928
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005929llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005930CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005931 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5932
Daniel Dunbardfff2302009-03-02 05:18:14 +00005933 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005934 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005935 false, llvm::GlobalValue::ExternalLinkage,
5936 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005937 }
5938
5939 return GV;
5940}
5941
John McCallf85e1932011-06-15 23:02:42 +00005942llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5943 IdentifierInfo *II) {
5944 llvm::GlobalVariable *&Entry = ClassReferences[II];
5945
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005946 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005947 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005948 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005950 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5951 false, llvm::GlobalValue::InternalLinkage,
5952 ClassGV,
5953 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005954 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005955 CGM.getTargetData().getABITypeAlignment(
5956 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005957 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005958 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005959 }
John McCallf85e1932011-06-15 23:02:42 +00005960
Benjamin Kramer578faa82011-09-27 21:06:10 +00005961 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005962}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005963
John McCallf85e1932011-06-15 23:02:42 +00005964llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5965 const ObjCInterfaceDecl *ID) {
5966 return EmitClassRefFromId(Builder, ID->getIdentifier());
5967}
5968
5969llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5970 CGBuilderTy &Builder) {
5971 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5972 return EmitClassRefFromId(Builder, II);
5973}
5974
Daniel Dunbar11394522009-04-18 08:51:00 +00005975llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005976CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005977 const ObjCInterfaceDecl *ID) {
5978 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005979
Daniel Dunbar11394522009-04-18 08:51:00 +00005980 if (!Entry) {
5981 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5982 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005983 Entry =
5984 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005985 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005986 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005987 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005988 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005989 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005990 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005991 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005992 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005993 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005994
Benjamin Kramer578faa82011-09-27 21:06:10 +00005995 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005996}
5997
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005998/// EmitMetaClassRef - Return a Value * of the address of _class_t
5999/// meta-data
6000///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006001llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6002 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006003 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6004 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006005 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006007 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006008 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006009 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006010 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006011 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006012 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006013 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006014 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006015 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006016 ObjCTypes.ClassnfABIPtrTy));
6017
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006018 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006019 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006020
Benjamin Kramer578faa82011-09-27 21:06:10 +00006021 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006022}
6023
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006024/// GetClass - Return a reference to the class for the given interface
6025/// decl.
6026llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6027 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006028 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006029 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6030 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6031 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6032 }
6033
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006034 return EmitClassRef(Builder, ID);
6035}
6036
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006037/// Generates a message send where the super is the receiver. This is
6038/// a message send to self with special delivery semantics indicating
6039/// which class's method should be called.
6040CodeGen::RValue
6041CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006042 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006043 QualType ResultType,
6044 Selector Sel,
6045 const ObjCInterfaceDecl *Class,
6046 bool isCategoryImpl,
6047 llvm::Value *Receiver,
6048 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006049 const CodeGen::CallArgList &CallArgs,
6050 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006051 // ...
6052 // Create and init a super structure; this is a (receiver, class)
6053 // pair we will pass to objc_msgSendSuper.
6054 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006055 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006056
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006057 llvm::Value *ReceiverAsObject =
6058 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6059 CGF.Builder.CreateStore(ReceiverAsObject,
6060 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006061
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006062 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006063 llvm::Value *Target;
6064 if (IsClassMessage) {
6065 if (isCategoryImpl) {
6066 // Message sent to "super' in a class method defined in
6067 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00006068 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006069 Target = CGF.Builder.CreateStructGEP(Target, 0);
6070 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00006071 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006072 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00006073 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00006074 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006075
Mike Stumpf5408fe2009-05-16 07:57:57 +00006076 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6077 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006078 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006079 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6080 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6081 CGF.Builder.CreateStore(Target,
6082 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006083
John McCall944c8432011-05-14 03:10:52 +00006084 return (isVTableDispatchedSelector(Sel))
6085 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006086 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006087 true, CallArgs, Method)
6088 : EmitMessageSend(CGF, Return, ResultType,
6089 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006090 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006091 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006092}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006093
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006094llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006095 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006096 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006097
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006098 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006099 llvm::Constant *Casted =
6100 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6101 ObjCTypes.SelectorPtrTy);
6102 Entry =
6103 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6104 llvm::GlobalValue::InternalLinkage,
6105 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006106 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006107 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006108 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006109
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006110 if (lval)
6111 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006112 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6113
6114 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6115 llvm::MDNode::get(VMContext,
6116 ArrayRef<llvm::Value*>()));
6117 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006118}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006119/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006120/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006121///
6122void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006123 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006124 llvm::Value *dst,
6125 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006126 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006127 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006128 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006129 assert(Size <= 8 && "does not support size > 8");
6130 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6131 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006132 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6133 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006134 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6135 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006136 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6137 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006138 return;
6139}
6140
6141/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6142/// objc_assign_strongCast (id src, id *dst)
6143///
6144void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006145 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006146 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006147 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006148 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006149 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006150 assert(Size <= 8 && "does not support size > 8");
6151 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006152 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006153 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6154 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006155 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6156 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006157 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006158 src, dst, "weakassign");
6159 return;
6160}
6161
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006162void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006163 CodeGen::CodeGenFunction &CGF,
6164 llvm::Value *DestPtr,
6165 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006166 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006167 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6168 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006169 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006170 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006171 return;
6172}
6173
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006174/// EmitObjCWeakRead - Code gen for loading value of a __weak
6175/// object: objc_read_weak (id *src)
6176///
6177llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006178 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006179 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006180 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006181 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6182 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006183 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006184 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006185 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006186 return read_weak;
6187}
6188
6189/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6190/// objc_assign_weak (id src, id *dst)
6191///
6192void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006193 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006194 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006195 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006196 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006197 assert(Size <= 8 && "does not support size > 8");
6198 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6199 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006200 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6201 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006202 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6203 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006204 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006205 src, dst, "weakassign");
6206 return;
6207}
6208
6209/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6210/// objc_assign_global (id src, id *dst)
6211///
6212void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006213 llvm::Value *src, llvm::Value *dst,
6214 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006215 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006216 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006217 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006218 assert(Size <= 8 && "does not support size > 8");
6219 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6220 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006221 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6222 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006223 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6224 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006225 if (!threadlocal)
6226 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6227 src, dst, "globalassign");
6228 else
6229 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6230 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006231 return;
6232}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006233
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006234void
John McCallf1549f62010-07-06 01:34:17 +00006235CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6236 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006237 EmitAtSynchronizedStmt(CGF, S,
6238 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6239 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006240}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006241
John McCall5a180392010-07-24 00:37:23 +00006242llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006243CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006244 // There's a particular fixed type info for 'id'.
6245 if (T->isObjCIdType() ||
6246 T->isObjCQualifiedIdType()) {
6247 llvm::Constant *IDEHType =
6248 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6249 if (!IDEHType)
6250 IDEHType =
6251 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6252 false,
6253 llvm::GlobalValue::ExternalLinkage,
6254 0, "OBJC_EHTYPE_id");
6255 return IDEHType;
6256 }
6257
6258 // All other types should be Objective-C interface pointer types.
6259 const ObjCObjectPointerType *PT =
6260 T->getAs<ObjCObjectPointerType>();
6261 assert(PT && "Invalid @catch type.");
6262 const ObjCInterfaceType *IT = PT->getInterfaceType();
6263 assert(IT && "Invalid @catch type.");
6264 return GetInterfaceEHType(IT->getDecl(), false);
6265}
6266
John McCallf1549f62010-07-06 01:34:17 +00006267void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6268 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006269 EmitTryCatchStmt(CGF, S,
6270 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6271 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6272 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006273}
6274
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006275/// EmitThrowStmt - Generate code for a throw statement.
6276void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6277 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006278 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006279 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006280 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006281 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006282 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006283 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006284 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006285 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006286 }
6287
John McCall7ec404c2010-10-16 08:21:07 +00006288 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006289 CGF.Builder.ClearInsertionPoint();
6290}
Daniel Dunbare588b992009-03-01 04:46:24 +00006291
John McCall5a180392010-07-24 00:37:23 +00006292llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006293CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006294 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006295 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006296
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006297 // If we don't need a definition, return the entry if found or check
6298 // if we use an external reference.
6299 if (!ForDefinition) {
6300 if (Entry)
6301 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006302
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006303 // If this type (or a super class) has the __objc_exception__
6304 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006305 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006306 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006307 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006308 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006309 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006310 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006311 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006312 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006313
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006314 // Otherwise we need to either make a new entry or fill in the
6315 // initializer.
6316 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006317 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006318 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006319 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006320 CGM.getModule().getGlobalVariable(VTableName);
6321 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006322 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6323 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006324 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006325 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006326
Chris Lattner8b418682012-02-07 00:39:47 +00006327 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006328
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006329 llvm::Constant *Values[] = {
6330 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6331 GetClassName(ID->getIdentifier()),
6332 GetClassGlobal(ClassName)
6333 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006334 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006335 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006336
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006337 if (Entry) {
6338 Entry->setInitializer(Init);
6339 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006340 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006341 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006342 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006343 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006344 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006345 }
6346
David Blaikie4e4d0842012-03-11 07:00:24 +00006347 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006348 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006349 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6350 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006351
6352 if (ForDefinition) {
6353 Entry->setSection("__DATA,__objc_const");
6354 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6355 } else {
6356 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6357 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006358
6359 return Entry;
6360}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006361
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006362/* *** */
6363
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006364CodeGen::CGObjCRuntime *
6365CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Blaikie4e4d0842012-03-11 07:00:24 +00006366 if (CGM.getLangOpts().ObjCNonFragileABI)
David Chisnall60be6072011-03-22 21:21:24 +00006367 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006368 return new CGObjCMac(CGM);
6369}