blob: b61852765a671723b3d697320b9e93d79e8963c1 [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"
John McCalld16c2cf2011-02-08 08:22:06 +000015#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000016#include "CGCleanup.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CGRecordLayout.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000027#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/ADT/SmallString.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000031#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/InlineAsm.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000042
43namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000044
Daniel Dunbar6bff2512009-08-03 17:06:42 +000045// FIXME: We should find a nicer way to make the labels for metadata, string
46// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000047
Fariborz Jahanianee0af742009-01-21 22:04:16 +000048class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000049protected:
50 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000051
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000052private:
John McCall0774cb82011-05-15 01:53:33 +000053 // The types of these functions don't really matter because we
54 // should always bitcast before calling them.
55
56 /// id objc_msgSend (id, SEL, ...)
57 ///
58 /// The default messenger, used for sends whose ABI is unchanged from
59 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000060 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000061 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
62 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000063 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Bill Wendlingc4c62fd2013-01-31 00:30:05 +000064 return
65 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
66 params, true),
67 "objc_msgSend",
68 llvm::AttributeSet::get(CGM.getLLVMContext(),
69 llvm::AttributeSet::FunctionIndex,
70 llvm::Attribute::NonLazyBind));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000071 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000072
John McCall0774cb82011-05-15 01:53:33 +000073 /// void objc_msgSend_stret (id, SEL, ...)
74 ///
75 /// The messenger used when the return value is an aggregate returned
76 /// by indirect reference in the first argument, and therefore the
77 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000078 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000079 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000080 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
81 params, true),
82 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000083
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000084 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
John McCall0774cb82011-05-15 01:53:33 +000086 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
87 ///
88 /// The messenger used when the return value is returned on the x87
89 /// floating-point stack; without a special entrypoint, the nil case
90 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000092 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000093 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
94 params, true),
John McCall0774cb82011-05-15 01:53:33 +000095 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000096
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000097 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000098
Anders Carlssoneea64802011-10-31 16:27:11 +000099 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
100 ///
101 /// The messenger used when the return value is returned in two values on the
102 /// x87 floating point stack; without a special entrypoint, the nil case
103 /// would be unbalanced. Only used on 64-bit X86.
104 llvm::Constant *getMessageSendFp2retFn() const {
105 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
106 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
107 llvm::Type *resultType =
108 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
109
110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
111 params, true),
112 "objc_msgSend_fp2ret");
113 }
114
John McCall0774cb82011-05-15 01:53:33 +0000115 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
116 ///
117 /// The messenger used for super calls, which have different dispatch
118 /// semantics. The class passed is the superclass of the current
119 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000120 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000121 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000122 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000123 params, true),
124 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000126
John McCall0774cb82011-05-15 01:53:33 +0000127 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
128 ///
129 /// A slightly different messenger used for super calls. The class
130 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000131 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000132 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000133 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000134 params, true),
135 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000137
John McCall0774cb82011-05-15 01:53:33 +0000138 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
139 /// SEL op, ...)
140 ///
141 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000142 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000143 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000144 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000145 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000148
John McCall0774cb82011-05-15 01:53:33 +0000149 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
150 /// SEL op, ...)
151 ///
152 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000153 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000155 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000156 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000158 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160 llvm::Constant *getMessageSendSuperFpretFn() const {
161 // There is no objc_msgSendSuper_fpret? How can that work?
162 return getMessageSendSuperFn();
163 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000164
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 llvm::Constant *getMessageSendSuperFpretFn2() const {
166 // There is no objc_msgSendSuper_fpret? How can that work?
167 return getMessageSendSuperFn2();
168 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000170protected:
171 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000173public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000175 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000176
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000177 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000178 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000180 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000181 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000183 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000185
186private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000187 /// ProtocolPtrTy - LLVM type for external protocol handles
188 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000189 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000190
191public:
192 llvm::Type *getExternalProtocolPtrTy() {
193 if (!ExternalProtocolPtrTy) {
194 // FIXME: It would be nice to unify this with the opaque type, so that the
195 // IR comes out a bit cleaner.
196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
197 ASTContext &Ctx = CGM.getContext();
198 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
199 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
200 }
201
202 return ExternalProtocolPtrTy;
203 }
204
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000205 // SuperCTy - clang type for struct objc_super.
206 QualType SuperCTy;
207 // SuperPtrCTy - clang type for struct objc_super *.
208 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000209
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000210 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000211 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000212 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
216 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000217 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000219 /// PropertyListTy - LLVM type for struct objc_property_list
220 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000221 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000224
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000225 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000226 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000228 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000230 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000232
Chris Lattner72db6c32009-04-22 02:44:54 +0000233 llvm::Constant *getGetPropertyFn() {
234 CodeGen::CodeGenTypes &Types = CGM.getTypes();
235 ASTContext &Ctx = CGM.getContext();
236 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000238 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
239 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000240 Params.push_back(IdType);
241 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000242 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000243 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000244 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000245 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
246 FunctionType::ExtInfo(),
247 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Chris Lattner72db6c32009-04-22 02:44:54 +0000251 llvm::Constant *getSetPropertyFn() {
252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
253 ASTContext &Ctx = CGM.getContext();
254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000256 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
257 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000258 Params.push_back(IdType);
259 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000260 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(Ctx.BoolTy);
263 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000264 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000265 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
266 FunctionType::ExtInfo(),
267 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000268 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
269 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000271 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_setProperty_atomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_nonatomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282
283 SmallVector<CanQualType,4> Params;
284 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
285 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
286 Params.push_back(IdType);
287 Params.push_back(SelType);
288 Params.push_back(IdType);
289 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
290 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000291 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
292 FunctionType::ExtInfo(),
293 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000294 const char *name;
295 if (atomic && copy)
296 name = "objc_setProperty_atomic_copy";
297 else if (atomic && !copy)
298 name = "objc_setProperty_atomic";
299 else if (!atomic && copy)
300 name = "objc_setProperty_nonatomic_copy";
301 else
302 name = "objc_setProperty_nonatomic";
303
304 return CGM.CreateRuntimeFunction(FTy, name);
305 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000306
307 llvm::Constant *getCopyStructFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000318 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
319 FunctionType::ExtInfo(),
320 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
322 }
323
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000324 /// This routine declares and returns address of:
325 /// void objc_copyCppObjectAtomic(
326 /// void *dest, const void *src,
327 /// void (*copyHelper) (void *dest, const void *source));
328 llvm::Constant *getCppAtomicObjectFunction() {
329 CodeGen::CodeGenTypes &Types = CGM.getTypes();
330 ASTContext &Ctx = CGM.getContext();
331 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
332 SmallVector<CanQualType,3> Params;
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000337 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
338 FunctionType::ExtInfo(),
339 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
341 }
342
Chris Lattner72db6c32009-04-22 02:44:54 +0000343 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000346 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000348 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000349 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000350 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000351 FunctionType::ExtInfo(),
352 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
354 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000355
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000357 llvm::Constant *getGcReadWeakFn() {
358 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000361 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000363 }
364
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000365 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000366 llvm::Constant *getGcAssignWeakFn() {
367 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000368 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000369 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000370 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignGlobalFn() {
376 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
381 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000383 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
384 llvm::Constant *getGcAssignThreadLocalFn() {
385 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000387 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000388 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
390 }
391
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000392 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000393 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000394 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000395 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
396 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000398 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000402 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
403 llvm::Constant *GcMemmoveCollectableFn() {
404 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000406 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000410 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000411 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000412 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000413 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000414 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000415 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
417 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000418
419 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000420 llvm::Constant *getExceptionThrowFn() {
421 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000422 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000424 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
426 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000427
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000428 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
429 llvm::Constant *getExceptionRethrowFn() {
430 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000431 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
433 }
434
Daniel Dunbar1c566672009-02-24 01:43:46 +0000435 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000436 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000437 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000439 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000440 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000443
Daniel Dunbar1c566672009-02-24 01:43:46 +0000444 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000446 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000449 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
451 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000453 llvm::Constant *getSendFn(bool IsSuper) const {
454 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000456
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000457 llvm::Constant *getSendFn2(bool IsSuper) const {
458 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000461 llvm::Constant *getSendStretFn(bool IsSuper) const {
462 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
463 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000464
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000465 llvm::Constant *getSendStretFn2(bool IsSuper) const {
466 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
467 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000468
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000469 llvm::Constant *getSendFpretFn(bool IsSuper) const {
470 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000473 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
474 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
475 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000476
Anders Carlssoneea64802011-10-31 16:27:11 +0000477 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
478 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
479 }
480
481 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
482 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
483 }
484
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000485 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
486 ~ObjCCommonTypesHelper(){}
487};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000488
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000489/// ObjCTypesHelper - Helper class that encapsulates lazy
490/// construction of varies types used during ObjC generation.
491class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000492public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000493 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000497 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000499
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000500 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000503 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504 /// ProtocolExtensionTy - LLVM type for struct
505 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 /// ProtocolExtensionTy - LLVM type for struct
508 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000510 /// MethodDescriptionTy - LLVM type for struct
511 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000513 /// MethodDescriptionListTy - LLVM type for struct
514 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// MethodDescriptionListPtrTy - LLVM type for struct
517 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000523 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Anders Carlsson124526b2008-09-09 10:10:21 +0000544 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000546
Anders Carlsson124526b2008-09-09 10:10:21 +0000547 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000548 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000549 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000550 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000551 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000553 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000554
555 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000558 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000559 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000561 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000562
563 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000564 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000566 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000567 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000568 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Anders Carlsson124526b2008-09-09 10:10:21 +0000571 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000572 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000573 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000575 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 "objc_exception_match");
577
Chris Lattner34b02a12009-04-22 02:26:14 +0000578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Anders Carlsson124526b2008-09-09 10:10:21 +0000580 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000582 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
Bill Wendlingc4c62fd2013-01-31 00:30:05 +0000584 return
585 CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
586 params, false),
587 "_setjmp",
588 llvm::AttributeSet::get(CGM.getLLVMContext(),
589 llvm::AttributeSet::FunctionIndex,
590 llvm::Attribute::NonLazyBind));
Chris Lattner34b02a12009-04-22 02:26:14 +0000591 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000592
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000593public:
594 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000595 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000596};
597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000599/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000600class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000601public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000603 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000604 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000606 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000607 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000608
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000609 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000610 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Daniel Dunbar948e2582009-02-15 07:36:20 +0000612 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000613 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000614
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000615 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000616 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000617
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000618 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000619 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000621 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000622 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000623
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000624 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000625 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000627 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000628 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000629
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000630 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000631 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000632
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000633 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000634 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000635
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000636 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000637 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000639 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000640 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000641
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000642 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000643 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000645 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000647 // MessageRefTy - LLVM for:
648 // struct _message_ref_t {
649 // IMP messenger;
650 // SEL name;
651 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000652 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000653 // MessageRefCTy - clang type for struct _message_ref_t
654 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000656 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000657 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000658 // MessageRefCPtrTy - clang type for struct _message_ref_t*
659 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahanianef163782009-02-05 01:13:09 +0000661 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000662 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000663
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000664 // SuperMessageRefTy - LLVM for:
665 // struct _super_message_ref_t {
666 // SUPER_IMP messenger;
667 // SEL name;
668 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000669 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000671 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000673
Chris Lattner1c02f862009-04-22 02:53:24 +0000674 llvm::Constant *getMessageSendFixupFn() {
675 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000676 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000677 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000678 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000679 "objc_msgSend_fixup");
680 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000681
Chris Lattner1c02f862009-04-22 02:53:24 +0000682 llvm::Constant *getMessageSendFpretFixupFn() {
683 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000684 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000686 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000687 "objc_msgSend_fpret_fixup");
688 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Chris Lattner1c02f862009-04-22 02:53:24 +0000690 llvm::Constant *getMessageSendStretFixupFn() {
691 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000692 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000693 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000694 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000695 "objc_msgSend_stret_fixup");
696 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000701 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000702 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000703 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000704 "objc_msgSendSuper2_fixup");
705 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000708 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000710 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000711 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000712 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000713 "objc_msgSendSuper2_stret_fixup");
714 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Chris Lattner8a569112009-04-22 02:15:23 +0000716 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000718 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner8a569112009-04-22 02:15:23 +0000720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721
Chris Lattner8a569112009-04-22 02:15:23 +0000722 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000723 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000724 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000725 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000726 "objc_begin_catch");
727 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000728
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000729 llvm::StructType *EHTypeTy;
730 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000731
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000732 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
733 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000735
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000736class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000737public:
738 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000739 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000740 public:
Eli Friedmane5b46662012-11-06 22:15:52 +0000741 unsigned ivar_bytepos;
742 unsigned ivar_size;
743 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000744 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000745
746 // Allow sorting based on byte pos.
747 bool operator<(const GC_IVAR &b) const {
748 return ivar_bytepos < b.ivar_bytepos;
749 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000750 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000751
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000752 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000753 public:
754 unsigned skip;
755 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000756 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000757 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000758 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000759
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000760 /// opcode for captured block variables layout 'instructions'.
761 /// In the following descriptions, 'I' is the value of the immediate field.
762 /// (field following the opcode).
763 ///
764 enum BLOCK_LAYOUT_OPCODE {
765 /// An operator which affects how the following layout should be
766 /// interpreted.
767 /// I == 0: Halt interpretation and treat everything else as
768 /// a non-pointer. Note that this instruction is equal
769 /// to '\0'.
770 /// I != 0: Currently unused.
771 BLOCK_LAYOUT_OPERATOR = 0,
772
773 /// The next I+1 bytes do not contain a value of object pointer type.
774 /// Note that this can leave the stream unaligned, meaning that
775 /// subsequent word-size instructions do not begin at a multiple of
776 /// the pointer size.
777 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
778
779 /// The next I+1 words do not contain a value of object pointer type.
780 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
781 /// when the required skip quantity is a multiple of the pointer size.
782 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
783
784 /// The next I+1 words are __strong pointers to Objective-C
785 /// objects or blocks.
786 BLOCK_LAYOUT_STRONG = 3,
787
788 /// The next I+1 words are pointers to __block variables.
789 BLOCK_LAYOUT_BYREF = 4,
790
791 /// The next I+1 words are __weak pointers to Objective-C
792 /// objects or blocks.
793 BLOCK_LAYOUT_WEAK = 5,
794
795 /// The next I+1 words are __unsafe_unretained pointers to
796 /// Objective-C objects or blocks.
797 BLOCK_LAYOUT_UNRETAINED = 6
798
799 /// The next I+1 words are block or object pointers with some
800 /// as-yet-unspecified ownership semantics. If we add more
801 /// flavors of ownership semantics, values will be taken from
802 /// this range.
803 ///
804 /// This is included so that older tools can at least continue
805 /// processing the layout past such things.
806 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
807
808 /// All other opcodes are reserved. Halt interpretation and
809 /// treat everything else as opaque.
810 };
811
812 class RUN_SKIP {
813 public:
814 enum BLOCK_LAYOUT_OPCODE opcode;
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000815 CharUnits block_var_bytepos;
816 CharUnits block_var_size;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000817 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000818 CharUnits BytePos = CharUnits::Zero(),
819 CharUnits Size = CharUnits::Zero())
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000820 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000821
822 // Allow sorting based on byte pos.
823 bool operator<(const RUN_SKIP &b) const {
824 return block_var_bytepos < b.block_var_bytepos;
825 }
826 };
827
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000828protected:
Owen Anderson69243822009-07-13 04:10:07 +0000829 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000830 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000831 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000833 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000834 SmallVector<GC_IVAR, 16> SkipIvars;
835 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000836
837 // arc/mrr layout of captured block literal variables.
838 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000839
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000840 /// LazySymbols - Symbols to generate a lazy reference for. See
841 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000842 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000843
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000844 /// DefinedSymbols - External symbols which are defined by this
845 /// module. The symbols in this list and LazySymbols are used to add
846 /// special linker symbols which ensure that Objective-C modules are
847 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000848 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000850 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000851 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000853 /// MethodVarNames - uniqued method variable names.
854 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000855
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000856 /// DefinedCategoryNames - list of category names in form Class_Category.
857 llvm::SetVector<std::string> DefinedCategoryNames;
858
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000859 /// MethodVarTypes - uniqued method type signatures. We have to use
860 /// a StringMap here because have no other unique reference.
861 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000863 /// MethodDefinitions - map of methods which have been defined in
864 /// this translation unit.
865 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000867 /// PropertyNames - uniqued method variable names.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000869
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000870 /// ClassReferences - uniqued class references.
871 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000873 /// SelectorReferences - uniqued selector references.
874 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000875
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000876 /// Protocols - Protocols for which an objc_protocol structure has
877 /// been emitted. Forward declarations are handled by creating an
878 /// empty structure whose initializer is filled in when/if defined.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000881 /// DefinedProtocols - Protocols which have actually been
882 /// defined. We should not need this, see FIXME in GenerateProtocol.
883 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000884
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000885 /// DefinedClasses - List of defined classes.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000886 SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000887
888 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000889 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000891 /// DefinedCategories - List of defined categories.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000892 SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000894 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000895 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000896
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000897 /// GetNameForMethod - Return a name for the given method.
898 /// \param[out] NameOut - The return value.
899 void GetNameForMethod(const ObjCMethodDecl *OMD,
900 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000901 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000903 /// GetMethodVarName - Return a unique constant for the given
904 /// selector's name. The return value has type char *.
905 llvm::Constant *GetMethodVarName(Selector Sel);
906 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000909 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000910
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000911 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000912 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
913 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000914 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000915
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000916 /// GetPropertyName - Return a unique constant for the given
917 /// name. The return value has type char *.
918 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000920 // FIXME: This can be dropped once string functions are unified.
921 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
922 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000924 /// GetClassName - Return a unique constant for the given selector's
925 /// name. The return value has type char *.
926 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000928 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
929
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000930 /// BuildIvarLayout - Builds ivar layout bitmap for the class
931 /// implementation for the __strong or __weak case.
932 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000933 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
934 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000935
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000936 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000937
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000938 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedmane5b46662012-11-06 22:15:52 +0000939 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000940 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000941 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000942 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000943 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000944 ArrayRef<const FieldDecl*> RecFields,
Eli Friedmane5b46662012-11-06 22:15:52 +0000945 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000946 bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000947
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000948 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout);
Fariborz Jahanian44fcff92012-11-02 22:51:18 +0000949
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000950 void UpdateRunSkipBlockVars(bool IsByref,
951 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000952 CharUnits FieldOffset,
953 CharUnits FieldSize);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000954
955 void BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000956 CharUnits BytePos, bool &HasUnion,
957 bool ByrefLayout=false);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000958
959 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
960 const RecordDecl *RD,
961 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000962 CharUnits BytePos, bool &HasUnion,
963 bool ByrefLayout);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000964
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000965 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
966
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000967 llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout);
968
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000969
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000970 /// GetIvarLayoutName - Returns a unique constant for the given
971 /// ivar layout bitmap.
972 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
973 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000974
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000975 /// EmitPropertyList - Emit the given property list. The return
976 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000977 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000978 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000979 const ObjCContainerDecl *OCD,
980 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000981
Bob Wilsondc8dab62011-11-30 01:57:58 +0000982 /// EmitProtocolMethodTypes - Generate the array of extended method type
983 /// strings. The return value has type Int8PtrPtrTy.
984 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000985 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000986 const ObjCCommonTypesHelper &ObjCTypes);
987
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000988 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000989 void PushProtocolProperties(
990 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000991 SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendling3964e622012-02-09 22:16:49 +0000992 const Decl *Container,
993 const ObjCProtocolDecl *PROTO,
994 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000995
Fariborz Jahanianda320092009-01-29 19:24:30 +0000996 /// GetProtocolRef - Return a reference to the internal protocol
997 /// description, creating an empty one if it has not been
998 /// defined. The return value has type ProtocolPtrTy.
999 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001000
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001001 /// CreateMetadataVar - Create a global variable with internal
1002 /// linkage for use by the Objective-C runtime.
1003 ///
1004 /// This is a convenience wrapper which not only creates the
1005 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +00001006 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001007 ///
1008 /// \param Name - The variable name.
1009 /// \param Init - The variable initializer; this is also used to
1010 /// define the type of the variable.
1011 /// \param Section - The section the variable should go into, or 0.
1012 /// \param Align - The alignment for the variable, or 0.
1013 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +00001014 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +00001015 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001016 llvm::Constant *Init,
1017 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001018 unsigned Align,
1019 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001020
John McCall944c8432011-05-14 03:10:52 +00001021 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1022 ReturnValueSlot Return,
1023 QualType ResultType,
1024 llvm::Value *Sel,
1025 llvm::Value *Arg0,
1026 QualType Arg0Ty,
1027 bool IsSuper,
1028 const CallArgList &CallArgs,
1029 const ObjCMethodDecl *OMD,
1030 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001031
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001032 /// EmitImageInfo - Emit the image info marker used to encode some module
1033 /// level information.
1034 void EmitImageInfo();
1035
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001036public:
Owen Anderson69243822009-07-13 04:10:07 +00001037 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +00001038 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001039
David Chisnall0d13f6f2010-01-23 02:40:42 +00001040 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001041
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001042 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1043 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001044
Fariborz Jahanianda320092009-01-29 19:24:30 +00001045 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001046
Fariborz Jahanianda320092009-01-29 19:24:30 +00001047 /// GetOrEmitProtocol - Get the protocol object for the given
1048 /// declaration, emitting it if necessary. The return value has type
1049 /// ProtocolPtrTy.
1050 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001051
Fariborz Jahanianda320092009-01-29 19:24:30 +00001052 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1053 /// object for the given declaration, emitting it if needed. These
1054 /// forward references will be filled in with empty bodies if no
1055 /// definition is seen. The return value has type ProtocolPtrTy.
1056 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001057 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1058 const CGBlockInfo &blockInfo);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001059 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1060 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001061
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00001062 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
1063 QualType T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001064};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001065
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001066class CGObjCMac : public CGObjCCommonMac {
1067private:
1068 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001069
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001070 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001071 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001072 void EmitModuleInfo();
1073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 /// EmitModuleSymols - Emit module symbols, the list of defined
1075 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001076 llvm::Constant *EmitModuleSymbols();
1077
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001078 /// FinishModule - Write out global data structures at the end of
1079 /// processing a translation unit.
1080 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001081
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001082 /// EmitClassExtension - Generate the class extension structure used
1083 /// to store the weak ivar layout and properties. The return value
1084 /// has type ClassExtensionPtrTy.
1085 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1086
1087 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1088 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001089 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001090 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001091
John McCallf85e1932011-06-15 23:02:42 +00001092 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1093 IdentifierInfo *II);
1094
1095 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1096
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001097 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1098 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001099
1100 /// EmitIvarList - Emit the ivar list for the given
1101 /// implementation. If ForClass is true the list of class ivars
1102 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1103 /// interface ivars will be emitted. The return value has type
1104 /// IvarListPtrTy.
1105 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001106 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001107
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001108 /// EmitMetaClass - Emit a forward reference to the class structure
1109 /// for the metaclass of the given interface. The return value has
1110 /// type ClassPtrTy.
1111 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1112
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001114 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001115 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1116 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001117 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001118
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001119 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001120
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001121 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001122
1123 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001124 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001125 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001126 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001127 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001128
1129 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001130 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001131 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001132 /// - IsProtocol: True iff these methods are for a protocol.
1133 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134 /// - Required: When true, only "required" methods are
1135 /// listed. Similarly, when false only "optional" methods are
1136 /// listed. For classes this should always be true.
1137 /// - begin, end: The method list to output.
1138 ///
1139 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001140 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001141 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001142 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001143
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001144 /// GetOrEmitProtocol - Get the protocol object for the given
1145 /// declaration, emitting it if necessary. The return value has type
1146 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001147 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001148
1149 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1150 /// object for the given declaration, emitting it if needed. These
1151 /// forward references will be filled in with empty bodies if no
1152 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001153 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001154
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001155 /// EmitProtocolExtension - Generate the protocol extension
1156 /// structure used to store optional instance and class methods, and
1157 /// protocol properties. The return value has type
1158 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001159 llvm::Constant *
1160 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001161 ArrayRef<llvm::Constant*> OptInstanceMethods,
1162 ArrayRef<llvm::Constant*> OptClassMethods,
1163 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001164
1165 /// EmitProtocolList - Generate the list of referenced
1166 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001167 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001168 ObjCProtocolDecl::protocol_iterator begin,
1169 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001170
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001171 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1172 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001173 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1174 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001175
1176public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001177 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001178
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001179 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001180
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001181 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001182 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001183 QualType ResultType,
1184 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001185 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001186 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001187 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001188 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001189
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001190 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001191 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001192 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001193 QualType ResultType,
1194 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001195 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001196 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001197 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001198 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001199 const CallArgList &CallArgs,
1200 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001201
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001202 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001203 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001204
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001205 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1206 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001207
1208 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1209 /// untyped one.
1210 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1211 const ObjCMethodDecl *Method);
1212
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001213 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001214
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001215 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001216
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001217 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001218
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001219 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001220
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001221 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001222 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001223
Chris Lattner74391b42009-03-22 21:03:39 +00001224 virtual llvm::Constant *GetPropertyGetFunction();
1225 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001226 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1227 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001228 virtual llvm::Constant *GetGetStructFunction();
1229 virtual llvm::Constant *GetSetStructFunction();
David Chisnalld397cfe2012-12-17 18:54:24 +00001230 virtual llvm::Constant *GetCppAtomicObjectGetFunction();
1231 virtual llvm::Constant *GetCppAtomicObjectSetFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001232 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001233
John McCallf1549f62010-07-06 01:34:17 +00001234 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1235 const ObjCAtTryStmt &S);
1236 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1237 const ObjCAtSynchronizedStmt &S);
1238 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001239 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00001240 const ObjCAtThrowStmt &S,
1241 bool ClearInsertionPoint=true);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001242 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001243 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001244 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001246 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001247 llvm::Value *src, llvm::Value *dest,
1248 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001249 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001250 llvm::Value *src, llvm::Value *dest,
1251 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001252 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1253 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001254 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1255 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001256 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001258 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1259 QualType ObjectTy,
1260 llvm::Value *BaseValue,
1261 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001262 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001263 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001264 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001265 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001266
1267 /// GetClassGlobal - Return the global variable for the Objective-C
1268 /// class of the given name.
1269 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001270 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001271 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001272};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001274class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001275private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001276 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001277 llvm::GlobalVariable* ObjCEmptyCacheVar;
1278 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Daniel Dunbar11394522009-04-18 08:51:00 +00001280 /// SuperClassReferences - uniqued super class references.
1281 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001282
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001283 /// MetaClassReferences - uniqued meta class references.
1284 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001285
1286 /// EHTypeReferences - uniqued class ehtype references.
1287 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001288
John McCall944c8432011-05-14 03:10:52 +00001289 /// VTableDispatchMethods - List of methods for which we generate
1290 /// vtable-based message dispatch.
1291 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001293 /// DefinedMetaClasses - List of defined meta-classes.
1294 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1295
John McCall944c8432011-05-14 03:10:52 +00001296 /// isVTableDispatchedSelector - Returns true if SEL is a
1297 /// vtable-based selector.
1298 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001300 /// FinishNonFragileABIModule - Write out global data structures at the end of
1301 /// processing a translation unit.
1302 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001303
Daniel Dunbar463b8762009-05-15 21:48:48 +00001304 /// AddModuleClassList - Add the given list of class pointers to the
1305 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001306 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001307 const char *SymbolName,
1308 const char *SectionName);
1309
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1311 unsigned InstanceStart,
1312 unsigned InstanceSize,
1313 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001314 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001315 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001316 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001317 llvm::Constant *ClassRoGV,
1318 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001319
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001320 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001321
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001322 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001323
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001324 /// EmitMethodList - Emit the method list for the given
1325 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001326 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001327 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001328 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001329 /// EmitIvarList - Emit the ivar list for the given
1330 /// implementation. If ForClass is true the list of class ivars
1331 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1332 /// interface ivars will be emitted. The return value has type
1333 /// IvarListnfABIPtrTy.
1334 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001335
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001336 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001337 const ObjCIvarDecl *Ivar,
Eli Friedmane5b46662012-11-06 22:15:52 +00001338 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001339
Fariborz Jahanianda320092009-01-29 19:24:30 +00001340 /// GetOrEmitProtocol - Get the protocol object for the given
1341 /// declaration, emitting it if necessary. The return value has type
1342 /// ProtocolPtrTy.
1343 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001344
Fariborz Jahanianda320092009-01-29 19:24:30 +00001345 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1346 /// object for the given declaration, emitting it if needed. These
1347 /// forward references will be filled in with empty bodies if no
1348 /// definition is seen. The return value has type ProtocolPtrTy.
1349 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001350
Fariborz Jahanianda320092009-01-29 19:24:30 +00001351 /// EmitProtocolList - Generate the list of referenced
1352 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001353 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001354 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001355 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001356
John McCall944c8432011-05-14 03:10:52 +00001357 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1358 ReturnValueSlot Return,
1359 QualType ResultType,
1360 Selector Sel,
1361 llvm::Value *Receiver,
1362 QualType Arg0Ty,
1363 bool IsSuper,
1364 const CallArgList &CallArgs,
1365 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001366
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001367 /// GetClassGlobal - Return the global variable for the Objective-C
1368 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001369 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001370
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001371 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001372 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001373 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001374 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001375
1376 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1377 IdentifierInfo *II);
1378
1379 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001380
Daniel Dunbar11394522009-04-18 08:51:00 +00001381 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1382 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001383 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1384 const ObjCInterfaceDecl *ID);
1385
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001386 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1387 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001388 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001389 const ObjCInterfaceDecl *ID);
1390
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001391 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1392 /// the given ivar.
1393 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001394 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001395 const ObjCInterfaceDecl *ID,
1396 const ObjCIvarDecl *Ivar);
1397
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001398 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1399 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001400 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1401 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001402
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001403 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001404 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001405 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001406 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001407
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001409 return "OBJC_METACLASS_$_";
1410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001411
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001412 const char *getClassSymbolPrefix() const {
1413 return "OBJC_CLASS_$_";
1414 }
1415
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001416 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001417 uint32_t &InstanceStart,
1418 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001419
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001420 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001421 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001422 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1423 return CGM.getContext().Selectors.getSelector(0, &II);
1424 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001425
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001426 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001427 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1428 return CGM.getContext().Selectors.getSelector(1, &II);
1429 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001430
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001431 /// ImplementationIsNonLazy - Check whether the given category or
1432 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001433 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001434
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001435public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001436 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001437 // FIXME. All stubs for now!
1438 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001439
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001440 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001441 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001442 QualType ResultType,
1443 Selector Sel,
1444 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001445 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001446 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001447 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001448
1449 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001450 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001451 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001452 QualType ResultType,
1453 Selector Sel,
1454 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001455 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001456 llvm::Value *Receiver,
1457 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001458 const CallArgList &CallArgs,
1459 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001460
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001461 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001462 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001463
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001464 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1465 bool lvalue = false)
1466 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001467
1468 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1469 /// untyped one.
1470 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1471 const ObjCMethodDecl *Method)
1472 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001473
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001474 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001475
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001476 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001477
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001478 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001479
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001480 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001481 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001482
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001483 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001484
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001485 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001486 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001487 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001488 virtual llvm::Constant *GetPropertySetFunction() {
1489 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001490 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001491
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001492 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1493 bool copy) {
1494 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1495 }
1496
David Chisnall8fac25d2010-12-26 22:13:16 +00001497 virtual llvm::Constant *GetSetStructFunction() {
1498 return ObjCTypes.getCopyStructFn();
1499 }
1500 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001501 return ObjCTypes.getCopyStructFn();
1502 }
David Chisnalld397cfe2012-12-17 18:54:24 +00001503 virtual llvm::Constant *GetCppAtomicObjectSetFunction() {
1504 return ObjCTypes.getCppAtomicObjectFunction();
1505 }
1506 virtual llvm::Constant *GetCppAtomicObjectGetFunction() {
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001507 return ObjCTypes.getCppAtomicObjectFunction();
1508 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001509
Chris Lattner74391b42009-03-22 21:03:39 +00001510 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001511 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001512 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001513
John McCallf1549f62010-07-06 01:34:17 +00001514 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1515 const ObjCAtTryStmt &S);
1516 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1517 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001518 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00001519 const ObjCAtThrowStmt &S,
1520 bool ClearInsertionPoint=true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001521 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001522 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001523 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001524 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001525 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001526 llvm::Value *src, llvm::Value *dest,
1527 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001528 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001529 llvm::Value *src, llvm::Value *dest,
1530 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001531 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001532 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001533 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1534 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001535 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001536 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1537 QualType ObjectTy,
1538 llvm::Value *BaseValue,
1539 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001540 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001541 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001542 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001543 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001544};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001545
1546/// A helper class for performing the null-initialization of a return
1547/// value.
1548struct NullReturnState {
1549 llvm::BasicBlock *NullBB;
John McCall06098582013-02-12 05:53:35 +00001550 NullReturnState() : NullBB(0) {}
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001551
John McCall06098582013-02-12 05:53:35 +00001552 /// Perform a null-check of the given receiver.
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001553 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
John McCall06098582013-02-12 05:53:35 +00001554 // Make blocks for the null-receiver and call edges.
1555 NullBB = CGF.createBasicBlock("msgSend.null-receiver");
1556 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001557
1558 // Check for a null receiver and, if there is one, jump to the
John McCall06098582013-02-12 05:53:35 +00001559 // null-receiver block. There's no point in trying to avoid it:
1560 // we're always going to put *something* there, because otherwise
1561 // we shouldn't have done this null-check in the first place.
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001562 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1563 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1564
1565 // Otherwise, start performing the call.
1566 CGF.EmitBlock(callBB);
1567 }
1568
John McCall06098582013-02-12 05:53:35 +00001569 /// Complete the null-return operation. It is valid to call this
1570 /// regardless of whether 'init' has been called.
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001571 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1572 const CallArgList &CallArgs,
1573 const ObjCMethodDecl *Method) {
John McCall06098582013-02-12 05:53:35 +00001574 // If we never had to do a null-check, just use the raw result.
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001575 if (!NullBB) return result;
John McCall06098582013-02-12 05:53:35 +00001576
1577 // The continuation block. This will be left null if we don't have an
1578 // IP, which can happen if the method we're calling is marked noreturn.
1579 llvm::BasicBlock *contBB = 0;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001580
John McCall06098582013-02-12 05:53:35 +00001581 // Finish the call path.
1582 llvm::BasicBlock *callBB = CGF.Builder.GetInsertBlock();
1583 if (callBB) {
1584 contBB = CGF.createBasicBlock("msgSend.cont");
1585 CGF.Builder.CreateBr(contBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001586 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001587
John McCall06098582013-02-12 05:53:35 +00001588 // Okay, start emitting the null-receiver block.
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001589 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001590
John McCall06098582013-02-12 05:53:35 +00001591 // Release any consumed arguments we've got.
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001592 if (Method) {
1593 CallArgList::const_iterator I = CallArgs.begin();
1594 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1595 e = Method->param_end(); i != e; ++i, ++I) {
1596 const ParmVarDecl *ParamDecl = (*i);
1597 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1598 RValue RV = I->RV;
1599 assert(RV.isScalar() &&
1600 "NullReturnState::complete - arg not on object");
1601 CGF.EmitARCRelease(RV.getScalarVal(), true);
1602 }
1603 }
1604 }
John McCall06098582013-02-12 05:53:35 +00001605
1606 // The phi code below assumes that we haven't needed any control flow yet.
1607 assert(CGF.Builder.GetInsertBlock() == NullBB);
1608
1609 // If we've got a void return, just jump to the continuation block.
1610 if (result.isScalar() && resultType->isVoidType()) {
1611 // No jumps required if the message-send was noreturn.
1612 if (contBB) CGF.EmitBlock(contBB);
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001613 return result;
1614 }
1615
John McCall06098582013-02-12 05:53:35 +00001616 // If we've got a scalar return, build a phi.
1617 if (result.isScalar()) {
1618 // Derive the null-initialization value.
1619 llvm::Constant *null = CGF.CGM.EmitNullConstant(resultType);
1620
1621 // If no join is necessary, just flow out.
1622 if (!contBB) return RValue::get(null);
1623
1624 // Otherwise, build a phi.
1625 CGF.EmitBlock(contBB);
1626 llvm::PHINode *phi = CGF.Builder.CreatePHI(null->getType(), 2);
1627 phi->addIncoming(result.getScalarVal(), callBB);
1628 phi->addIncoming(null, NullBB);
1629 return RValue::get(phi);
1630 }
1631
1632 // If we've got an aggregate return, null the buffer out.
1633 // FIXME: maybe we should be doing things differently for all the
1634 // cases where the ABI has us returning (1) non-agg values in
1635 // memory or (2) agg values in registers.
1636 if (result.isAggregate()) {
1637 assert(result.isAggregate() && "null init of non-aggregate result?");
1638 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1639 if (contBB) CGF.EmitBlock(contBB);
1640 return result;
1641 }
1642
1643 // Complex types.
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001644 CGF.EmitBlock(contBB);
John McCall06098582013-02-12 05:53:35 +00001645 CodeGenFunction::ComplexPairTy callResult = result.getComplexVal();
1646
1647 // Find the scalar type and its zero value.
1648 llvm::Type *scalarTy = callResult.first->getType();
1649 llvm::Constant *scalarZero = llvm::Constant::getNullValue(scalarTy);
1650
1651 // Build phis for both coordinates.
1652 llvm::PHINode *real = CGF.Builder.CreatePHI(scalarTy, 2);
1653 real->addIncoming(callResult.first, callBB);
1654 real->addIncoming(scalarZero, NullBB);
1655 llvm::PHINode *imag = CGF.Builder.CreatePHI(scalarTy, 2);
1656 imag->addIncoming(callResult.second, callBB);
1657 imag->addIncoming(scalarZero, NullBB);
1658 return RValue::getComplex(real, imag);
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001659 }
1660};
1661
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001662} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001663
1664/* *** Helper Functions *** */
1665
1666/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001667static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001668 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001669 unsigned idx0,
1670 unsigned idx1) {
1671 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001672 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1673 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001674 };
Jay Foada5c04342011-07-21 14:31:17 +00001675 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001676}
1677
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001678/// hasObjCExceptionAttribute - Return true if this class or any super
1679/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001680static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001681 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001682 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001683 return true;
1684 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001685 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001686 return false;
1687}
1688
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001689/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001690
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001691CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001692 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001693 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001694 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001695}
1696
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001697/// GetClass - Return a reference to the class for the given interface
1698/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001699llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001700 const ObjCInterfaceDecl *ID) {
1701 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001702}
1703
1704/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001705llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1706 bool lval) {
1707 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001708}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001709llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001710 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001711 return EmitSelector(Builder, Method->getSelector());
1712}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001713
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001714llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001715 if (T->isObjCIdType() ||
1716 T->isObjCQualifiedIdType()) {
1717 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001718 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001719 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001720 if (T->isObjCClassType() ||
1721 T->isObjCQualifiedClassType()) {
1722 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001723 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001724 }
1725 if (T->isObjCObjectPointerType())
1726 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1727
John McCall5a180392010-07-24 00:37:23 +00001728 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001729}
1730
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001731/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001732/*
1733 struct __builtin_CFString {
1734 const int *isa; // point to __CFConstantStringClassReference
1735 int flags;
1736 const char *str;
1737 long length;
1738 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001739*/
1740
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001741/// or Generate a constant NSString object.
1742/*
1743 struct __builtin_NSString {
1744 const int *isa; // point to __NSConstantStringClassReference
1745 const char *str;
1746 unsigned int length;
1747 };
1748*/
1749
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001750llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001751 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001752 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001753 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001754 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001755}
1756
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001757enum {
1758 kCFTaggedObjectID_Integer = (1 << 1) + 1
1759};
1760
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001761/// Generates a message send where the super is the receiver. This is
1762/// a message send to self with special delivery semantics indicating
1763/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001764CodeGen::RValue
1765CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001766 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001767 QualType ResultType,
1768 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001769 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001770 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001771 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001772 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001773 const CodeGen::CallArgList &CallArgs,
1774 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001775 // Create and init a super structure; this is a (receiver, class)
1776 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001777 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001778 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001779 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001780 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001782 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001783
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001784 // If this is a class message the metaclass is passed as the target.
1785 llvm::Value *Target;
1786 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001787 if (isCategoryImpl) {
1788 // Message sent to 'super' in a class method defined in a category
1789 // implementation requires an odd treatment.
1790 // If we are in a class method, we must retrieve the
1791 // _metaclass_ for the current class, pointed at by
1792 // the class's "isa" pointer. The following assumes that
1793 // isa" is the first ivar in a class (which it must be).
1794 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1795 Target = CGF.Builder.CreateStructGEP(Target, 0);
1796 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001797 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001798 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1799 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1800 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1801 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001802 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001803 }
1804 else if (isCategoryImpl)
1805 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1806 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001807 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1808 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1809 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001810 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001811 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1812 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001813 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001814 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001815 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001816 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001817 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001818 return EmitMessageSend(CGF, Return, ResultType,
1819 EmitSelector(CGF.Builder, Sel),
1820 ObjCSuper, ObjCTypes.SuperPtrCTy,
1821 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001822}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001823
1824/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001825CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001826 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001827 QualType ResultType,
1828 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001829 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001830 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001831 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001832 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001833 return EmitMessageSend(CGF, Return, ResultType,
1834 EmitSelector(CGF.Builder, Sel),
1835 Receiver, CGF.getContext().getObjCIdType(),
1836 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001837}
1838
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001839CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001840CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1841 ReturnValueSlot Return,
1842 QualType ResultType,
1843 llvm::Value *Sel,
1844 llvm::Value *Arg0,
1845 QualType Arg0Ty,
1846 bool IsSuper,
1847 const CallArgList &CallArgs,
1848 const ObjCMethodDecl *Method,
1849 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001850 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001851 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001852 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001853 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1854 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001855 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001856
John McCallde5d3c72012-02-17 03:33:10 +00001857 // If we're calling a method, use the formal signature.
1858 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001860 if (Method)
1861 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1862 CGM.getContext().getCanonicalType(ResultType) &&
1863 "Result type mismatch!");
1864
John McCallcba681a2011-05-14 21:12:11 +00001865 NullReturnState nullReturn;
1866
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001867 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001868 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001869 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001870 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001871 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001872 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1873 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1874 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001875 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1876 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1877 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001878 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001879 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001880 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001881 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001882
1883 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001884 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001885 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1886 e = Method->param_end(); i != e; ++i) {
1887 const ParmVarDecl *ParamDecl = (*i);
1888 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1889 if (!nullReturn.NullBB)
1890 nullReturn.init(CGF, Arg0);
1891 requiresnullCheck = true;
1892 break;
1893 }
1894 }
1895
John McCallde5d3c72012-02-17 03:33:10 +00001896 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1897 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001898 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1899 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001900}
1901
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001902static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1903 if (FQT.isObjCGCStrong())
1904 return Qualifiers::Strong;
1905
John McCallf85e1932011-06-15 23:02:42 +00001906 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001907 return Qualifiers::Weak;
1908
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001909 // check for __unsafe_unretained
1910 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1911 return Qualifiers::GCNone;
1912
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001913 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1914 return Qualifiers::Strong;
1915
1916 if (const PointerType *PT = FQT->getAs<PointerType>())
1917 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1918
1919 return Qualifiers::GCNone;
1920}
1921
John McCall6b5a61b2011-02-07 10:33:21 +00001922llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1923 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001924
Chris Lattner8b418682012-02-07 00:39:47 +00001925 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikie4e4d0842012-03-11 07:00:24 +00001926 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1927 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001928 return nullPtr;
1929
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001930 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001931 SkipIvars.clear();
1932 IvarsInfo.clear();
Eli Friedmane5b46662012-11-06 22:15:52 +00001933 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1934 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001935
Fariborz Jahanian81979822010-09-09 00:21:45 +00001936 // __isa is the first field in block descriptor and must assume by runtime's
1937 // convention that it is GC'able.
Eli Friedmane5b46662012-11-06 22:15:52 +00001938 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001939
1940 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1941
1942 // Calculate the basic layout of the block structure.
1943 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001944 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001945
1946 // Ignore the optional 'this' capture: C++ objects are not assumed
1947 // to be GC'ed.
1948
1949 // Walk the captured variables.
1950 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1951 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1952 const VarDecl *variable = ci->getVariable();
1953 QualType type = variable->getType();
1954
1955 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1956
1957 // Ignore constant captures.
1958 if (capture.isConstant()) continue;
1959
Eli Friedmane5b46662012-11-06 22:15:52 +00001960 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
John McCall6b5a61b2011-02-07 10:33:21 +00001961
1962 // __block variables are passed by their descriptor address.
1963 if (ci->isByRef()) {
Eli Friedmane5b46662012-11-06 22:15:52 +00001964 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001965 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001966 }
1967
1968 assert(!type->isArrayType() && "array variable should not be caught");
1969 if (const RecordType *record = type->getAs<RecordType>()) {
1970 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001971 continue;
1972 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001973
John McCall6b5a61b2011-02-07 10:33:21 +00001974 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
Eli Friedmane5b46662012-11-06 22:15:52 +00001975 unsigned fieldSize = CGM.getContext().getTypeSize(type);
John McCall6b5a61b2011-02-07 10:33:21 +00001976
1977 if (GCAttr == Qualifiers::Strong)
Eli Friedmane5b46662012-11-06 22:15:52 +00001978 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1979 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001980 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
Eli Friedmane5b46662012-11-06 22:15:52 +00001981 SkipIvars.push_back(GC_IVAR(fieldOffset,
1982 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001983 }
1984
1985 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001986 return nullPtr;
1987
1988 // Sort on byte position; captures might not be allocated in order,
1989 // and unions can do funny things.
1990 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1991 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001992
1993 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001994 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001995 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001996 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001997 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001998 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001999 if (!(s[i] & 0xf0))
2000 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
2001 else
2002 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
2003 printf("\n");
2004 }
2005
2006 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00002007}
2008
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002009/// getBlockCaptureLifetime - This routine returns life time of the captured
2010/// block variable for the purpose of block layout meta-data generation. FQT is
2011/// the type of the variable captured in the block.
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002012Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT,
2013 bool ByrefLayout) {
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002014 if (CGM.getLangOpts().ObjCAutoRefCount)
2015 return FQT.getObjCLifetime();
2016
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002017 // MRR.
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002018 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002019 return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong;
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002020
2021 return Qualifiers::OCL_None;
2022}
2023
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002024void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
2025 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002026 CharUnits FieldOffset,
2027 CharUnits FieldSize) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002028 // __block variables are passed by their descriptor address.
2029 if (IsByref)
2030 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002031 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002032 else if (LifeTime == Qualifiers::OCL_Strong)
2033 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002034 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002035 else if (LifeTime == Qualifiers::OCL_Weak)
2036 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002037 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002038 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2039 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002040 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002041 else
2042 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2043 FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002044 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002045}
2046
2047void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2048 const RecordDecl *RD,
2049 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002050 CharUnits BytePos, bool &HasUnion,
2051 bool ByrefLayout) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002052 bool IsUnion = (RD && RD->isUnion());
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002053 CharUnits MaxUnionSize = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002054 const FieldDecl *MaxField = 0;
2055 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002056 CharUnits MaxFieldOffset = CharUnits::Zero();
2057 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002058
2059 if (RecFields.empty())
2060 return;
2061 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2062
2063 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2064 const FieldDecl *Field = RecFields[i];
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002065 // Note that 'i' here is actually the field index inside RD of Field,
2066 // although this dependency is hidden.
2067 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002068 CharUnits FieldOffset =
2069 CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002070
2071 // Skip over unnamed or bitfields
2072 if (!Field->getIdentifier() || Field->isBitField()) {
2073 LastFieldBitfieldOrUnnamed = Field;
2074 LastBitfieldOrUnnamedOffset = FieldOffset;
2075 continue;
2076 }
2077
2078 LastFieldBitfieldOrUnnamed = 0;
2079 QualType FQT = Field->getType();
2080 if (FQT->isRecordType() || FQT->isUnionType()) {
2081 if (FQT->isUnionType())
2082 HasUnion = true;
2083
2084 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2085 BytePos + FieldOffset, HasUnion);
2086 continue;
2087 }
2088
2089 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2090 const ConstantArrayType *CArray =
2091 dyn_cast_or_null<ConstantArrayType>(Array);
2092 uint64_t ElCount = CArray->getSize().getZExtValue();
2093 assert(CArray && "only array with known element size is supported");
2094 FQT = CArray->getElementType();
2095 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2096 const ConstantArrayType *CArray =
2097 dyn_cast_or_null<ConstantArrayType>(Array);
2098 ElCount *= CArray->getSize().getZExtValue();
2099 FQT = CArray->getElementType();
2100 }
2101
2102 assert(!FQT->isUnionType() &&
2103 "layout for array of unions not supported");
2104 if (FQT->isRecordType() && ElCount) {
2105 int OldIndex = RunSkipBlockVars.size() - 1;
2106 const RecordType *RT = FQT->getAs<RecordType>();
2107 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2108 HasUnion);
2109
2110 // Replicate layout information for each array element. Note that
2111 // one element is already done.
2112 uint64_t ElIx = 1;
2113 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002114 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002115 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2116 RunSkipBlockVars.push_back(
2117 RUN_SKIP(RunSkipBlockVars[i].opcode,
2118 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2119 RunSkipBlockVars[i].block_var_size));
2120 }
2121 continue;
2122 }
2123 }
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002124 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002125 if (IsUnion) {
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002126 CharUnits UnionIvarSize = FieldSize;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002127 if (UnionIvarSize > MaxUnionSize) {
2128 MaxUnionSize = UnionIvarSize;
2129 MaxField = Field;
2130 MaxFieldOffset = FieldOffset;
2131 }
2132 } else {
2133 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002134 getBlockCaptureLifetime(FQT, ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002135 BytePos + FieldOffset,
2136 FieldSize);
2137 }
2138 }
2139
2140 if (LastFieldBitfieldOrUnnamed) {
2141 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2142 // Last field was a bitfield. Must update the info.
2143 uint64_t BitFieldSize
2144 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002145 unsigned UnsSize = (BitFieldSize / ByteSizeInBits) +
Eli Friedmane5b46662012-11-06 22:15:52 +00002146 ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002147 CharUnits Size = CharUnits::fromQuantity(UnsSize);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002148 Size += LastBitfieldOrUnnamedOffset;
2149 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002150 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2151 ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002152 BytePos + LastBitfieldOrUnnamedOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002153 Size);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002154 } else {
2155 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2156 // Last field was unnamed. Must update skip info.
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002157 CharUnits FieldSize
2158 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002159 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002160 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2161 ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002162 BytePos + LastBitfieldOrUnnamedOffset,
2163 FieldSize);
2164 }
2165 }
2166
2167 if (MaxField)
2168 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002169 getBlockCaptureLifetime(MaxField->getType(), ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002170 BytePos + MaxFieldOffset,
2171 MaxUnionSize);
2172}
2173
2174void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002175 CharUnits BytePos,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002176 bool &HasUnion,
2177 bool ByrefLayout) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002178 const RecordDecl *RD = RT->getDecl();
2179 SmallVector<const FieldDecl*, 16> Fields;
2180 for (RecordDecl::field_iterator i = RD->field_begin(),
2181 e = RD->field_end(); i != e; ++i)
2182 Fields.push_back(*i);
2183 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2184 const llvm::StructLayout *RecLayout =
2185 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2186
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002187 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002188}
2189
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002190/// InlineLayoutInstruction - This routine produce an inline instruction for the
2191/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2192/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2193/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2194/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2195/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2196/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2197/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2198/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2199uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2200 SmallVectorImpl<unsigned char> &Layout) {
2201 uint64_t Result = 0;
2202 if (Layout.size() <= 3) {
2203 unsigned size = Layout.size();
2204 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2205 unsigned char inst;
2206 enum BLOCK_LAYOUT_OPCODE opcode ;
2207 switch (size) {
2208 case 3:
2209 inst = Layout[0];
2210 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2211 if (opcode == BLOCK_LAYOUT_STRONG)
2212 strong_word_count = (inst & 0xF)+1;
2213 else
2214 return 0;
2215 inst = Layout[1];
2216 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2217 if (opcode == BLOCK_LAYOUT_BYREF)
2218 byref_word_count = (inst & 0xF)+1;
2219 else
2220 return 0;
2221 inst = Layout[2];
2222 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2223 if (opcode == BLOCK_LAYOUT_WEAK)
2224 weak_word_count = (inst & 0xF)+1;
2225 else
2226 return 0;
2227 break;
2228
2229 case 2:
2230 inst = Layout[0];
2231 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2232 if (opcode == BLOCK_LAYOUT_STRONG) {
2233 strong_word_count = (inst & 0xF)+1;
2234 inst = Layout[1];
2235 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2236 if (opcode == BLOCK_LAYOUT_BYREF)
2237 byref_word_count = (inst & 0xF)+1;
2238 else if (opcode == BLOCK_LAYOUT_WEAK)
2239 weak_word_count = (inst & 0xF)+1;
2240 else
2241 return 0;
2242 }
2243 else if (opcode == BLOCK_LAYOUT_BYREF) {
2244 byref_word_count = (inst & 0xF)+1;
2245 inst = Layout[1];
2246 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2247 if (opcode == BLOCK_LAYOUT_WEAK)
2248 weak_word_count = (inst & 0xF)+1;
2249 else
2250 return 0;
2251 }
2252 else
2253 return 0;
2254 break;
2255
2256 case 1:
2257 inst = Layout[0];
2258 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2259 if (opcode == BLOCK_LAYOUT_STRONG)
2260 strong_word_count = (inst & 0xF)+1;
2261 else if (opcode == BLOCK_LAYOUT_BYREF)
2262 byref_word_count = (inst & 0xF)+1;
2263 else if (opcode == BLOCK_LAYOUT_WEAK)
2264 weak_word_count = (inst & 0xF)+1;
2265 else
2266 return 0;
2267 break;
2268
2269 default:
2270 return 0;
2271 }
2272
2273 // Cannot inline when any of the word counts is 15. Because this is one less
2274 // than the actual work count (so 15 means 16 actual word counts),
2275 // and we can only display 0 thru 15 word counts.
2276 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2277 return 0;
2278
2279 unsigned count =
2280 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2281
2282 if (size == count) {
2283 if (strong_word_count)
2284 Result = strong_word_count;
2285 Result <<= 4;
2286 if (byref_word_count)
2287 Result += byref_word_count;
2288 Result <<= 4;
2289 if (weak_word_count)
2290 Result += weak_word_count;
2291 }
2292 }
2293 return Result;
2294}
2295
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002296llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) {
2297 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2298 if (RunSkipBlockVars.empty())
2299 return nullPtr;
2300 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2301 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2302 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2303
2304 // Sort on byte position; captures might not be allocated in order,
2305 // and unions can do funny things.
2306 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
2307 SmallVector<unsigned char, 16> Layout;
2308
2309 unsigned size = RunSkipBlockVars.size();
2310 for (unsigned i = 0; i < size; i++) {
2311 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2312 CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2313 CharUnits end_byte_pos = start_byte_pos;
2314 unsigned j = i+1;
2315 while (j < size) {
2316 if (opcode == RunSkipBlockVars[j].opcode) {
2317 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2318 i++;
2319 }
2320 else
2321 break;
2322 }
2323 CharUnits size_in_bytes =
2324 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
2325 if (j < size) {
2326 CharUnits gap =
2327 RunSkipBlockVars[j].block_var_bytepos -
2328 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2329 size_in_bytes += gap;
2330 }
2331 CharUnits residue_in_bytes = CharUnits::Zero();
2332 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2333 residue_in_bytes = size_in_bytes % WordSizeInBytes;
2334 size_in_bytes -= residue_in_bytes;
2335 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2336 }
2337
2338 unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes;
2339 while (size_in_words >= 16) {
2340 // Note that value in imm. is one less that the actual
2341 // value. So, 0xf means 16 words follow!
2342 unsigned char inst = (opcode << 4) | 0xf;
2343 Layout.push_back(inst);
2344 size_in_words -= 16;
2345 }
2346 if (size_in_words > 0) {
2347 // Note that value in imm. is one less that the actual
2348 // value. So, we subtract 1 away!
2349 unsigned char inst = (opcode << 4) | (size_in_words-1);
2350 Layout.push_back(inst);
2351 }
2352 if (residue_in_bytes > CharUnits::Zero()) {
2353 unsigned char inst =
2354 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1);
2355 Layout.push_back(inst);
2356 }
2357 }
2358
2359 int e = Layout.size()-1;
2360 while (e >= 0) {
2361 unsigned char inst = Layout[e--];
2362 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2363 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2364 Layout.pop_back();
2365 else
2366 break;
2367 }
2368
2369 uint64_t Result = InlineLayoutInstruction(Layout);
2370 if (Result != 0) {
2371 // Block variable layout instruction has been inlined.
2372 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2373 if (ComputeByrefLayout)
2374 printf("\n Inline instruction for BYREF variable layout: ");
2375 else
2376 printf("\n Inline instruction for block variable layout: ");
2377 printf("0x0%llx\n", (unsigned long long)Result);
2378 }
2379 if (WordSizeInBytes == 8) {
2380 const llvm::APInt Instruction(64, Result);
2381 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2382 }
2383 else {
2384 const llvm::APInt Instruction(32, Result);
2385 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2386 }
2387 }
2388
2389 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2390 Layout.push_back(inst);
2391 std::string BitMap;
2392 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2393 BitMap += Layout[i];
2394
2395 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2396 if (ComputeByrefLayout)
2397 printf("\n BYREF variable layout: ");
2398 else
2399 printf("\n block variable layout: ");
2400 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2401 unsigned char inst = BitMap[i];
2402 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2403 unsigned delta = 1;
2404 switch (opcode) {
2405 case BLOCK_LAYOUT_OPERATOR:
2406 printf("BL_OPERATOR:");
2407 delta = 0;
2408 break;
2409 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2410 printf("BL_NON_OBJECT_BYTES:");
2411 break;
2412 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2413 printf("BL_NON_OBJECT_WORD:");
2414 break;
2415 case BLOCK_LAYOUT_STRONG:
2416 printf("BL_STRONG:");
2417 break;
2418 case BLOCK_LAYOUT_BYREF:
2419 printf("BL_BYREF:");
2420 break;
2421 case BLOCK_LAYOUT_WEAK:
2422 printf("BL_WEAK:");
2423 break;
2424 case BLOCK_LAYOUT_UNRETAINED:
2425 printf("BL_UNRETAINED:");
2426 break;
2427 }
2428 // Actual value of word count is one more that what is in the imm.
2429 // field of the instruction
2430 printf("%d", (inst & 0xf) + delta);
2431 if (i < e-1)
2432 printf(", ");
2433 else
2434 printf("\n");
2435 }
2436 }
2437
2438 llvm::GlobalVariable * Entry =
2439 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2440 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2441 "__TEXT,__objc_classname,cstring_literals", 1, true);
2442 return getConstantGEP(VMContext, Entry, 0, 0);
2443}
2444
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002445llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2446 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002447 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2448
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002449 RunSkipBlockVars.clear();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002450 bool hasUnion = false;
2451
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002452 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2453 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2454 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2455
2456 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2457
2458 // Calculate the basic layout of the block structure.
2459 const llvm::StructLayout *layout =
2460 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2461
2462 // Ignore the optional 'this' capture: C++ objects are not assumed
2463 // to be GC'ed.
Fariborz Jahanianff685c52012-12-04 17:20:57 +00002464 if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero())
2465 UpdateRunSkipBlockVars(false, Qualifiers::OCL_None,
2466 blockInfo.BlockHeaderForcedGapOffset,
2467 blockInfo.BlockHeaderForcedGapSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002468 // Walk the captured variables.
2469 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2470 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2471 const VarDecl *variable = ci->getVariable();
2472 QualType type = variable->getType();
2473
2474 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2475
2476 // Ignore constant captures.
2477 if (capture.isConstant()) continue;
2478
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002479 CharUnits fieldOffset =
2480 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002481
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002482 assert(!type->isArrayType() && "array variable should not be caught");
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002483 if (!ci->isByRef())
2484 if (const RecordType *record = type->getAs<RecordType>()) {
2485 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
2486 continue;
2487 }
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002488 CharUnits fieldSize;
2489 if (ci->isByRef())
2490 fieldSize = CharUnits::fromQuantity(WordSizeInBytes);
2491 else
2492 fieldSize = CGM.getContext().getTypeSizeInChars(type);
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002493 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002494 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002495 }
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002496 return getBitmapBlockLayout(false);
2497}
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002498
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002499
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002500llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM,
2501 QualType T) {
2502 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2503 assert(!T->isArrayType() && "__block array variable should not be caught");
2504 CharUnits fieldOffset;
2505 RunSkipBlockVars.clear();
2506 bool hasUnion = false;
2507 if (const RecordType *record = T->getAs<RecordType>()) {
2508 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */);
2509 llvm::Constant *Result = getBitmapBlockLayout(true);
2510 return Result;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002511 }
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002512 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2513 return nullPtr;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002514}
2515
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002516llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002517 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002518 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002519 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002520 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2521
Owen Anderson3c4972d2009-07-29 18:54:39 +00002522 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002523 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002524}
2525
Fariborz Jahanianda320092009-01-29 19:24:30 +00002526void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002527 // FIXME: We shouldn't need this, the protocol decl should contain enough
2528 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002529 DefinedProtocols.insert(PD->getIdentifier());
2530
2531 // If we have generated a forward reference to this protocol, emit
2532 // it now. Otherwise do nothing, the protocol objects are lazily
2533 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002534 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002535 GetOrEmitProtocol(PD);
2536}
2537
Fariborz Jahanianda320092009-01-29 19:24:30 +00002538llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002539 if (DefinedProtocols.count(PD->getIdentifier()))
2540 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002541
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002542 return GetOrEmitProtocolRef(PD);
2543}
2544
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002545/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002546// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2547struct _objc_protocol {
2548struct _objc_protocol_extension *isa;
2549char *protocol_name;
2550struct _objc_protocol_list *protocol_list;
2551struct _objc__method_prototype_list *instance_methods;
2552struct _objc__method_prototype_list *class_methods
2553};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002554
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002555See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002556*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002557llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002558 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002559
2560 // Early exit if a defining object has already been generated.
2561 if (Entry && Entry->hasInitializer())
2562 return Entry;
2563
Douglas Gregor1d784b22012-01-01 19:51:50 +00002564 // Use the protocol definition, if there is one.
2565 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2566 PD = Def;
2567
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002568 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002569 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002570 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2571
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002572 // Construct method lists.
2573 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2574 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002575 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002576 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002577 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002578 ObjCMethodDecl *MD = *i;
2579 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002580 if (!C)
2581 return GetOrEmitProtocolRef(PD);
2582
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002583 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2584 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002585 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002586 } else {
2587 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002588 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002589 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002590 }
2591
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002592 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002593 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002594 ObjCMethodDecl *MD = *i;
2595 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002596 if (!C)
2597 return GetOrEmitProtocolRef(PD);
2598
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002599 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2600 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002601 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002602 } else {
2603 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002604 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002605 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002606 }
2607
Bob Wilsondc8dab62011-11-30 01:57:58 +00002608 MethodTypesExt.insert(MethodTypesExt.end(),
2609 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2610
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002611 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002612 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2613 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002614 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002615 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002616 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002617 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002618 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002619 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002620 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002621 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002622 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002623 ClassMethods)
2624 };
Owen Anderson08e25242009-07-27 22:29:56 +00002625 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002626 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002627
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002628 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002629 // Already created, fix the linkage and update the initializer.
2630 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002631 Entry->setInitializer(Init);
2632 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002633 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002634 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002635 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002636 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002637 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002638 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002639 // FIXME: Is this necessary? Why only for protocol?
2640 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002641
2642 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002643 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002644 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002645
2646 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002647}
2648
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002649llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002650 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2651
2652 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002653 // We use the initializer as a marker of whether this is a forward
2654 // reference or not. At module finalization we add the empty
2655 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002656 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002657 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002658 llvm::GlobalValue::ExternalLinkage,
2659 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002660 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002661 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002662 // FIXME: Is this necessary? Why only for protocol?
2663 Entry->setAlignment(4);
2664 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002665
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002666 return Entry;
2667}
2668
2669/*
2670 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002671 uint32_t size;
2672 struct objc_method_description_list *optional_instance_methods;
2673 struct objc_method_description_list *optional_class_methods;
2674 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002675 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002676 };
2677*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002678llvm::Constant *
2679CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002680 ArrayRef<llvm::Constant*> OptInstanceMethods,
2681 ArrayRef<llvm::Constant*> OptClassMethods,
2682 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002683 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002684 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002685 llvm::Constant *Values[] = {
2686 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002687 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002688 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002689 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002690 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002691 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002692 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002693 OptClassMethods),
2694 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002695 ObjCTypes),
2696 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2697 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002698 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002699
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002700 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002701 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002702 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002703 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002704
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002705 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002706 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002707
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002708 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002709 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002710 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002711 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712}
2713
2714/*
2715 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002716 struct objc_protocol_list *next;
2717 long count;
2718 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002719 };
2720*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002721llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002723 ObjCProtocolDecl::protocol_iterator begin,
2724 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002725 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002726
Daniel Dunbardbc93372008-08-21 21:57:41 +00002727 for (; begin != end; ++begin)
2728 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002729
2730 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002731 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002732 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002733
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002734 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002735 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002736
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002737 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002738 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002739 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002740 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002741 ProtocolRefs.size() - 1);
2742 Values[2] =
2743 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2744 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002745 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002746
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002747 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002748 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002749 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002750 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002751 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002752}
2753
Bill Wendlingbb028552012-02-07 09:25:09 +00002754void CGObjCCommonMac::
2755PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002756 SmallVectorImpl<llvm::Constant *> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002757 const Decl *Container,
2758 const ObjCProtocolDecl *PROTO,
2759 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002760 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2761 E = PROTO->protocol_end(); P != E; ++P)
2762 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2763 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2764 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002765 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002766 if (!PropertySet.insert(PD->getIdentifier()))
2767 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002768 llvm::Constant *Prop[] = {
2769 GetPropertyName(PD->getIdentifier()),
2770 GetPropertyTypeString(PD, Container)
2771 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002772 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2773 }
2774}
2775
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002776/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002777 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002778 const char * const name;
2779 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002780 };
2781
2782 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002783 uint32_t entsize; // sizeof (struct _objc_property)
2784 uint32_t prop_count;
2785 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002786 };
2787*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002788llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002789 const Decl *Container,
2790 const ObjCContainerDecl *OCD,
2791 const ObjCCommonTypesHelper &ObjCTypes) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002792 SmallVector<llvm::Constant *, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002793 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002794 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2795 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002796 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002797 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002798 llvm::Constant *Prop[] = {
2799 GetPropertyName(PD->getIdentifier()),
2800 GetPropertyTypeString(PD, Container)
2801 };
Owen Anderson08e25242009-07-27 22:29:56 +00002802 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002803 Prop));
2804 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002805 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002806 for (ObjCInterfaceDecl::all_protocol_iterator
2807 P = OID->all_referenced_protocol_begin(),
2808 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002809 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2810 ObjCTypes);
2811 }
2812 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2813 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2814 E = CD->protocol_end(); P != E; ++P)
2815 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2816 ObjCTypes);
2817 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002818
2819 // Return null for empty list.
2820 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002821 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002822
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002823 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002824 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002825 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002826 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2827 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002828 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002829 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002830 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002831 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002832
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002833 llvm::GlobalVariable *GV =
2834 CreateMetadataVar(Name, Init,
2835 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002836 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002837 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002838 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002839 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002840}
2841
Bill Wendlingbb028552012-02-07 09:25:09 +00002842llvm::Constant *
2843CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2844 ArrayRef<llvm::Constant*> MethodTypes,
2845 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002846 // Return null for empty list.
2847 if (MethodTypes.empty())
2848 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2849
2850 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2851 MethodTypes.size());
2852 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2853
2854 llvm::GlobalVariable *GV =
2855 CreateMetadataVar(Name, Init,
2856 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2857 (ObjCABI == 2) ? 8 : 4,
2858 true);
2859 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2860}
2861
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002862/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002863 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864 int count;
2865 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002866 };
2867*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002868llvm::Constant *
2869CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002870 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002871 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002872 ObjCTypes.SelectorPtrTy),
2873 GetMethodVarType(MD)
2874 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002875 if (!Desc[1])
2876 return 0;
2877
Owen Anderson08e25242009-07-27 22:29:56 +00002878 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002879 Desc);
2880}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002881
Bill Wendlingbb028552012-02-07 09:25:09 +00002882llvm::Constant *
2883CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2884 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002885 // Return null for empty list.
2886 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002887 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002888
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002889 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002890 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002891 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002892 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002893 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002894 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002895
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002896 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002897 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002898 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002899}
2900
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002901/*
2902 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903 char *category_name;
2904 char *class_name;
2905 struct _objc_method_list *instance_methods;
2906 struct _objc_method_list *class_methods;
2907 struct _objc_protocol_list *protocols;
2908 uint32_t size; // <rdar://4585769>
2909 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002910 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002912void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002913 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002914
Mike Stumpf5408fe2009-05-16 07:57:57 +00002915 // FIXME: This is poor design, the OCD should have a pointer to the category
2916 // decl. Additionally, note that Category can be null for the @implementation
2917 // w/o an @interface case. Sema should just create one for us as it does for
2918 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002919 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002920 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002921 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002922
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002923 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002924 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2925 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002926
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002927 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002928 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002929 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002930 // Instance methods should always be defined.
2931 InstanceMethods.push_back(GetMethodConstant(*i));
2932 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002933 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002934 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002935 // Class methods should always be defined.
2936 ClassMethods.push_back(GetMethodConstant(*i));
2937 }
2938
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002939 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002940 Values[0] = GetClassName(OCD->getIdentifier());
2941 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002942 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002943 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002944 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002945 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002946 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002947 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002948 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002949 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002950 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002951 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002952 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002953 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002954 Category->protocol_begin(),
2955 Category->protocol_end());
2956 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002957 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002958 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002959 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002960
2961 // If there is no category @interface then there can be no properties.
2962 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002963 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002964 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002965 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002966 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002967 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002968
Owen Anderson08e25242009-07-27 22:29:56 +00002969 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002970 Values);
2971
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002972 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002973 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002974 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002975 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002976 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002977 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002978 // method definition entries must be clear for next implementation.
2979 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002980}
2981
John McCall621915c2012-10-17 04:53:23 +00002982enum FragileClassFlags {
2983 FragileABI_Class_Factory = 0x00001,
2984 FragileABI_Class_Meta = 0x00002,
2985 FragileABI_Class_HasCXXStructors = 0x02000,
2986 FragileABI_Class_Hidden = 0x20000
2987};
2988
2989enum NonFragileClassFlags {
2990 /// Is a meta-class.
2991 NonFragileABI_Class_Meta = 0x00001,
2992
2993 /// Is a root class.
2994 NonFragileABI_Class_Root = 0x00002,
2995
2996 /// Has a C++ constructor and destructor.
2997 NonFragileABI_Class_HasCXXStructors = 0x00004,
2998
2999 /// Has hidden visibility.
3000 NonFragileABI_Class_Hidden = 0x00010,
3001
3002 /// Has the exception attribute.
3003 NonFragileABI_Class_Exception = 0x00020,
3004
3005 /// (Obsolete) ARC-specific: this class has a .release_ivars method
3006 NonFragileABI_Class_HasIvarReleaser = 0x00040,
3007
3008 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00003009 NonFragileABI_Class_CompiledByARC = 0x00080,
3010
3011 /// Class has non-trivial destructors, but zero-initialization is okay.
3012 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003013};
3014
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003015/*
3016 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003017 Class isa;
3018 Class super_class;
3019 const char *name;
3020 long version;
3021 long info;
3022 long instance_size;
3023 struct _objc_ivar_list *ivars;
3024 struct _objc_method_list *methods;
3025 struct _objc_cache *cache;
3026 struct _objc_protocol_list *protocols;
3027 // Objective-C 1.0 extensions (<rdr://4585769>)
3028 const char *ivar_layout;
3029 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003030 };
3031
3032 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003033*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003034void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003035 DefinedSymbols.insert(ID->getIdentifier());
3036
Chris Lattner8ec03f52008-11-24 03:54:41 +00003037 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003038 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003039 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003040 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003041 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003042 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00003043 Interface->all_referenced_protocol_begin(),
3044 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00003045 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00003046 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00003047 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00003049 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003050
3051 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00003052 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003053 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003054
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003055 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003056 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003057 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003058 // Instance methods should always be defined.
3059 InstanceMethods.push_back(GetMethodConstant(*i));
3060 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003061 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003062 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003063 // Class methods should always be defined.
3064 ClassMethods.push_back(GetMethodConstant(*i));
3065 }
3066
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003067 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003068 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003069 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003070
3071 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
3072 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3073
3074 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3075 if (llvm::Constant *C = GetMethodConstant(MD))
3076 InstanceMethods.push_back(C);
3077 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3078 if (llvm::Constant *C = GetMethodConstant(MD))
3079 InstanceMethods.push_back(C);
3080 }
3081 }
3082
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003083 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003084 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003085 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003086 // Record a reference to the super class.
3087 LazySymbols.insert(Super->getIdentifier());
3088
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003089 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003090 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003091 ObjCTypes.ClassPtrTy);
3092 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003093 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003094 }
3095 Values[ 2] = GetClassName(ID->getIdentifier());
3096 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003097 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3098 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3099 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003100 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003101 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003102 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003103 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003104 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003105 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003106 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003107 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003108 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003109 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003110 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003111 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003112 std::string Name("\01L_OBJC_CLASS_");
3113 Name += ClassName;
3114 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3115 // Check for a forward reference.
3116 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3117 if (GV) {
3118 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3119 "Forward metaclass reference has incorrect type.");
3120 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3121 GV->setInitializer(Init);
3122 GV->setSection(Section);
3123 GV->setAlignment(4);
3124 CGM.AddUsedGlobal(GV);
3125 }
3126 else
3127 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003128 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003129 // method definition entries must be clear for next implementation.
3130 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003131}
3132
3133llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3134 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003135 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003136 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003137 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003138
John McCall1fb0caa2010-10-22 21:05:15 +00003139 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003140 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003141
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003142 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003143 // The isa for the metaclass is the root of the hierarchy.
3144 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3145 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3146 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003147 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003148 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003149 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003150 // The super class for the metaclass is emitted as the name of the
3151 // super class. The runtime fixes this up to point to the
3152 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003153 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003154 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003155 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003156 ObjCTypes.ClassPtrTy);
3157 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003158 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003159 }
3160 Values[ 2] = GetClassName(ID->getIdentifier());
3161 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003162 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3163 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3164 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003165 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003166 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003167 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003168 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003169 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003170 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003171 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003172 Values[ 9] = Protocols;
3173 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003174 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003175 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003176 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003177 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003178 Values);
3179
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003180 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003181 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003182
3183 // Check for a forward reference.
3184 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3185 if (GV) {
3186 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3187 "Forward metaclass reference has incorrect type.");
3188 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3189 GV->setInitializer(Init);
3190 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003191 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003192 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003193 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003194 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003195 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003196 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003197 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003198
3199 return GV;
3200}
3201
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003202llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003203 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003204
Mike Stumpf5408fe2009-05-16 07:57:57 +00003205 // FIXME: Should we look these up somewhere other than the module. Its a bit
3206 // silly since we only generate these while processing an implementation, so
3207 // exactly one pointer would work if know when we entered/exitted an
3208 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003209
3210 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003211 // Previously, metaclass with internal linkage may have been defined.
3212 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3214 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003215 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3216 "Forward metaclass reference has incorrect type.");
3217 return GV;
3218 } else {
3219 // Generate as an external reference to keep a consistent
3220 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003221 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003222 llvm::GlobalValue::ExternalLinkage,
3223 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003224 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003225 }
3226}
3227
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003228llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3229 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3230
3231 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3232 true)) {
3233 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3234 "Forward class metadata reference has incorrect type.");
3235 return GV;
3236 } else {
3237 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3238 llvm::GlobalValue::ExternalLinkage,
3239 0,
3240 Name);
3241 }
3242}
3243
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003244/*
3245 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003246 uint32_t size;
3247 const char *weak_ivar_layout;
3248 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003249 };
3250*/
3251llvm::Constant *
3252CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003253 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003254 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003255
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003256 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003257 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003258 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003259 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003260 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003261
3262 // Return null if no extension bits are used.
3263 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003264 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003265
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003266 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003267 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003268 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003270 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003271}
3272
3273/*
3274 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003275 char *ivar_name;
3276 char *ivar_type;
3277 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003278 };
3279
3280 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003281 int ivar_count;
3282 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003283 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003285llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003286 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003287 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003288
3289 // When emitting the root class GCC emits ivar entries for the
3290 // actual class structure. It is not clear if we need to follow this
3291 // behavior; for now lets try and get away with not doing it. If so,
3292 // the cleanest solution would be to make up an ObjCInterfaceDecl
3293 // for the class.
3294 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003295 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003296
Jordy Rosedb8264e2011-07-22 02:08:32 +00003297 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003298
Jordy Rosedb8264e2011-07-22 02:08:32 +00003299 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003300 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003301 // Ignore unnamed bit-fields.
3302 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003303 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003304 llvm::Constant *Ivar[] = {
3305 GetMethodVarName(IVD->getIdentifier()),
3306 GetMethodVarType(IVD),
3307 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedmane5b46662012-11-06 22:15:52 +00003308 ComputeIvarBaseOffset(CGM, OID, IVD))
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003309 };
Owen Anderson08e25242009-07-27 22:29:56 +00003310 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003311 }
3312
3313 // Return null for empty list.
3314 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003315 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003316
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003317 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003318 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003319 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003320 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003321 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003322 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003323
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003324 llvm::GlobalVariable *GV;
3325 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003326 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003327 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003328 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003329 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003330 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003331 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003332 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003333 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003334}
3335
3336/*
3337 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003338 SEL method_name;
3339 char *method_types;
3340 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003341 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003342
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003343 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003344 struct objc_method_list *obsolete;
3345 int count;
3346 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003347 };
3348*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003349
3350/// GetMethodConstant - Return a struct objc_method constant for the
3351/// given method if it has been defined. The result is null if the
3352/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003353llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003354 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003355 if (!Fn)
3356 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003357
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003358 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003359 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003360 ObjCTypes.SelectorPtrTy),
3361 GetMethodVarType(MD),
3362 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3363 };
Owen Anderson08e25242009-07-27 22:29:56 +00003364 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003365}
3366
Chris Lattner5f9e2722011-07-23 10:55:15 +00003367llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003368 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003369 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003370 // Return null for empty list.
3371 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003372 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003373
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003374 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003375 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003376 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003377 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003378 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003379 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003380 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003381
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003382 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003383 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003384}
3385
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003386llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003387 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003388 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003389 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003390
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003391 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003392 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003393 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003395 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003396 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003397 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003398 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003399 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003400
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003401 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003402}
3403
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003404llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003405CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003406 llvm::Constant *Init,
3407 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003408 unsigned Align,
3409 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003410 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003411 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003412 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003413 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003414 if (Section)
3415 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003416 if (Align)
3417 GV->setAlignment(Align);
3418 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003419 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003420 return GV;
3421}
3422
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003423llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003424 // Abuse this interface function as a place to finalize.
3425 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003426 return NULL;
3427}
3428
Chris Lattner74391b42009-03-22 21:03:39 +00003429llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003430 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003431}
3432
Chris Lattner74391b42009-03-22 21:03:39 +00003433llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003434 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003435}
3436
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003437llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3438 bool copy) {
3439 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3440}
3441
David Chisnall8fac25d2010-12-26 22:13:16 +00003442llvm::Constant *CGObjCMac::GetGetStructFunction() {
3443 return ObjCTypes.getCopyStructFn();
3444}
3445llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003446 return ObjCTypes.getCopyStructFn();
3447}
3448
David Chisnalld397cfe2012-12-17 18:54:24 +00003449llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() {
3450 return ObjCTypes.getCppAtomicObjectFunction();
3451}
3452llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() {
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003453 return ObjCTypes.getCppAtomicObjectFunction();
3454}
3455
Chris Lattner74391b42009-03-22 21:03:39 +00003456llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003457 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003458}
3459
John McCallf1549f62010-07-06 01:34:17 +00003460void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3461 return EmitTryOrSynchronizedStmt(CGF, S);
3462}
3463
3464void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3465 const ObjCAtSynchronizedStmt &S) {
3466 return EmitTryOrSynchronizedStmt(CGF, S);
3467}
3468
John McCallcc505292010-07-21 06:59:36 +00003469namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003470 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003471 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003472 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003473 llvm::Value *CallTryExitVar;
3474 llvm::Value *ExceptionData;
3475 ObjCTypesHelper &ObjCTypes;
3476 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003477 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003478 llvm::Value *CallTryExitVar,
3479 llvm::Value *ExceptionData,
3480 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003481 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003482 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3483
John McCallad346f42011-07-12 20:27:29 +00003484 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003485 // Check whether we need to call objc_exception_try_exit.
3486 // In optimized code, this branch will always be folded.
3487 llvm::BasicBlock *FinallyCallExit =
3488 CGF.createBasicBlock("finally.call_exit");
3489 llvm::BasicBlock *FinallyNoCallExit =
3490 CGF.createBasicBlock("finally.no_call_exit");
3491 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3492 FinallyCallExit, FinallyNoCallExit);
3493
3494 CGF.EmitBlock(FinallyCallExit);
3495 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3496 ->setDoesNotThrow();
3497
3498 CGF.EmitBlock(FinallyNoCallExit);
3499
3500 if (isa<ObjCAtTryStmt>(S)) {
3501 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003502 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3503 // Save the current cleanup destination in case there's
3504 // control flow inside the finally statement.
3505 llvm::Value *CurCleanupDest =
3506 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3507
John McCallcc505292010-07-21 06:59:36 +00003508 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3509
John McCalld96a8e72010-08-11 00:16:14 +00003510 if (CGF.HaveInsertPoint()) {
3511 CGF.Builder.CreateStore(CurCleanupDest,
3512 CGF.getNormalCleanupDestSlot());
3513 } else {
3514 // Currently, the end of the cleanup must always exist.
3515 CGF.EnsureInsertPoint();
3516 }
3517 }
John McCallcc505292010-07-21 06:59:36 +00003518 } else {
3519 // Emit objc_sync_exit(expr); as finally's sole statement for
3520 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003521 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003522 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3523 ->setDoesNotThrow();
3524 }
3525 }
3526 };
John McCall87bb5822010-07-31 23:20:56 +00003527
3528 class FragileHazards {
3529 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003530 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003531 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3532
3533 llvm::InlineAsm *ReadHazard;
3534 llvm::InlineAsm *WriteHazard;
3535
3536 llvm::FunctionType *GetAsmFnType();
3537
3538 void collectLocals();
3539 void emitReadHazard(CGBuilderTy &Builder);
3540
3541 public:
3542 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003543
John McCall87bb5822010-07-31 23:20:56 +00003544 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003545 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003546 };
3547}
3548
3549/// Create the fragile-ABI read and write hazards based on the current
3550/// state of the function, which is presumed to be immediately prior
3551/// to a @try block. These hazards are used to maintain correct
3552/// semantics in the face of optimization and the fragile ABI's
3553/// cavalier use of setjmp/longjmp.
3554FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3555 collectLocals();
3556
3557 if (Locals.empty()) return;
3558
3559 // Collect all the blocks in the function.
3560 for (llvm::Function::iterator
3561 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3562 BlocksBeforeTry.insert(&*I);
3563
3564 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3565
3566 // Create a read hazard for the allocas. This inhibits dead-store
3567 // optimizations and forces the values to memory. This hazard is
3568 // inserted before any 'throwing' calls in the protected scope to
3569 // reflect the possibility that the variables might be read from the
3570 // catch block if the call throws.
3571 {
3572 std::string Constraint;
3573 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3574 if (I) Constraint += ',';
3575 Constraint += "*m";
3576 }
3577
3578 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3579 }
3580
3581 // Create a write hazard for the allocas. This inhibits folding
3582 // loads across the hazard. This hazard is inserted at the
3583 // beginning of the catch path to reflect the possibility that the
3584 // variables might have been written within the protected scope.
3585 {
3586 std::string Constraint;
3587 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3588 if (I) Constraint += ',';
3589 Constraint += "=*m";
3590 }
3591
3592 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3593 }
3594}
3595
3596/// Emit a write hazard at the current location.
3597void FragileHazards::emitWriteHazard() {
3598 if (Locals.empty()) return;
3599
Jay Foad4c7d9f12011-07-15 08:37:34 +00003600 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003601}
3602
John McCall87bb5822010-07-31 23:20:56 +00003603void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3604 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003605 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003606}
3607
3608/// Emit read hazards in all the protected blocks, i.e. all the blocks
3609/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003610void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003611 if (Locals.empty()) return;
3612
3613 CGBuilderTy Builder(CGF.getLLVMContext());
3614
3615 // Iterate through all blocks, skipping those prior to the try.
3616 for (llvm::Function::iterator
3617 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3618 llvm::BasicBlock &BB = *FI;
3619 if (BlocksBeforeTry.count(&BB)) continue;
3620
3621 // Walk through all the calls in the block.
3622 for (llvm::BasicBlock::iterator
3623 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3624 llvm::Instruction &I = *BI;
3625
3626 // Ignore instructions that aren't non-intrinsic calls.
3627 // These are the only calls that can possibly call longjmp.
3628 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3629 if (isa<llvm::IntrinsicInst>(I))
3630 continue;
3631
3632 // Ignore call sites marked nounwind. This may be questionable,
3633 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3634 llvm::CallSite CS(&I);
3635 if (CS.doesNotThrow()) continue;
3636
John McCall0b251722010-08-04 05:59:32 +00003637 // Insert a read hazard before the call. This will ensure that
3638 // any writes to the locals are performed before making the
3639 // call. If the call throws, then this is sufficient to
3640 // guarantee correctness as long as it doesn't also write to any
3641 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003642 Builder.SetInsertPoint(&BB, BI);
3643 emitReadHazard(Builder);
3644 }
3645 }
3646}
3647
3648static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3649 if (V) S.insert(V);
3650}
3651
3652void FragileHazards::collectLocals() {
3653 // Compute a set of allocas to ignore.
3654 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3655 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3656 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003657
3658 // Collect all the allocas currently in the function. This is
3659 // probably way too aggressive.
3660 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3661 for (llvm::BasicBlock::iterator
3662 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3663 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3664 Locals.push_back(&*I);
3665}
3666
3667llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003668 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003669 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3670 tys[i] = Locals[i]->getType();
3671 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003672}
3673
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003675
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 Objective-C setjmp-longjmp (sjlj) Exception Handling
3677 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003678
John McCallf1549f62010-07-06 01:34:17 +00003679 A catch buffer is a setjmp buffer plus:
3680 - a pointer to the exception that was caught
3681 - a pointer to the previous exception data buffer
3682 - two pointers of reserved storage
3683 Therefore catch buffers form a stack, with a pointer to the top
3684 of the stack kept in thread-local storage.
3685
3686 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3687 objc_exception_try_exit pops the given catch buffer, which is
3688 required to be the top of the EH stack.
3689 objc_exception_throw pops the top of the EH stack, writes the
3690 thrown exception into the appropriate field, and longjmps
3691 to the setjmp buffer. It crashes the process (with a printf
3692 and an abort()) if there are no catch buffers on the stack.
3693 objc_exception_extract just reads the exception pointer out of the
3694 catch buffer.
3695
3696 There's no reason an implementation couldn't use a light-weight
3697 setjmp here --- something like __builtin_setjmp, but API-compatible
3698 with the heavyweight setjmp. This will be more important if we ever
3699 want to implement correct ObjC/C++ exception interactions for the
3700 fragile ABI.
3701
3702 Note that for this use of setjmp/longjmp to be correct, we may need
3703 to mark some local variables volatile: if a non-volatile local
3704 variable is modified between the setjmp and the longjmp, it has
3705 indeterminate value. For the purposes of LLVM IR, it may be
3706 sufficient to make loads and stores within the @try (to variables
3707 declared outside the @try) volatile. This is necessary for
3708 optimized correctness, but is not currently being done; this is
3709 being tracked as rdar://problem/8160285
3710
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003711 The basic framework for a @try-catch-finally is as follows:
3712 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003713 objc_exception_data d;
3714 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003715 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003716
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003717 objc_exception_try_enter(&d);
3718 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003719 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003720 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003721 // exception path
3722 id _caught = objc_exception_extract(&d);
3723
3724 // enter new try scope for handlers
3725 if (!setjmp(d.jmp_buf)) {
3726 ... match exception and execute catch blocks ...
3727
3728 // fell off end, rethrow.
3729 _rethrow = _caught;
3730 ... jump-through-finally to finally_rethrow ...
3731 } else {
3732 // exception in catch block
3733 _rethrow = objc_exception_extract(&d);
3734 _call_try_exit = false;
3735 ... jump-through-finally to finally_rethrow ...
3736 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003737 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003738 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003739
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003740 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003741 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003742 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003743
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003744 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003745 ... dispatch to finally destination ...
3746
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003747 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003748 objc_exception_throw(_rethrow);
3749
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003750 finally_end:
3751 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003752
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753 This framework differs slightly from the one gcc uses, in that gcc
3754 uses _rethrow to determine if objc_exception_try_exit should be called
3755 and if the object should be rethrown. This breaks in the face of
3756 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003757
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003758 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003759
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003760 - If there are no catch blocks, then we avoid emitting the second
3761 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003762
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003763 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3764 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003765
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003766 - FIXME: If there is no @finally block we can do a few more
3767 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003768
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003769 Rethrows and Jumps-Through-Finally
3770 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003771
John McCallf1549f62010-07-06 01:34:17 +00003772 '@throw;' is supported by pushing the currently-caught exception
3773 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003774
John McCallf1549f62010-07-06 01:34:17 +00003775 Branches through the @finally block are handled with an ordinary
3776 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3777 exceptions are not compatible with C++ exceptions, and this is
3778 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003779
John McCallf1549f62010-07-06 01:34:17 +00003780 @synchronized(expr) { stmt; } is emitted as if it were:
3781 id synch_value = expr;
3782 objc_sync_enter(synch_value);
3783 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003784*/
3785
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003786void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3787 const Stmt &S) {
3788 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003789
3790 // A destination for the fall-through edges of the catch handlers to
3791 // jump to.
3792 CodeGenFunction::JumpDest FinallyEnd =
3793 CGF.getJumpDestInCurrentScope("finally.end");
3794
3795 // A destination for the rethrow edge of the catch handlers to jump
3796 // to.
3797 CodeGenFunction::JumpDest FinallyRethrow =
3798 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003799
Daniel Dunbar1c566672009-02-24 01:43:46 +00003800 // For @synchronized, call objc_sync_enter(sync.expr). The
3801 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003802 // @synchronized. We can't avoid a temp here because we need the
3803 // value to be preserved. If the backend ever does liveness
3804 // correctly after setjmp, this will be unnecessary.
3805 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003806 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003807 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003808 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3809 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003810 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3811 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003812
3813 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3814 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003815 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003816
John McCall0b251722010-08-04 05:59:32 +00003817 // Allocate memory for the setjmp buffer. This needs to be kept
3818 // live throughout the try and catch blocks.
3819 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3820 "exceptiondata.ptr");
3821
John McCall87bb5822010-07-31 23:20:56 +00003822 // Create the fragile hazards. Note that this will not capture any
3823 // of the allocas required for exception processing, but will
3824 // capture the current basic block (which extends all the way to the
3825 // setjmp call) as "before the @try".
3826 FragileHazards Hazards(CGF);
3827
John McCallf1549f62010-07-06 01:34:17 +00003828 // Create a flag indicating whether the cleanup needs to call
3829 // objc_exception_try_exit. This is true except when
3830 // - no catches match and we're branching through the cleanup
3831 // just to rethrow the exception, or
3832 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003833 // The setjmp-safety rule here is that we should always store to this
3834 // variable in a place that dominates the branch through the cleanup
3835 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003836 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003837 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003838
John McCall9e2213d2010-10-04 23:42:51 +00003839 // A slot containing the exception to rethrow. Only needed when we
3840 // have both a @catch and a @finally.
3841 llvm::Value *PropagatingExnVar = 0;
3842
John McCallf1549f62010-07-06 01:34:17 +00003843 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003844 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003845 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003846 CallTryExitVar,
3847 ExceptionData,
3848 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003849
3850 // Enter a try block:
3851 // - Call objc_exception_try_enter to push ExceptionData on top of
3852 // the EH stack.
3853 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3854 ->setDoesNotThrow();
3855
3856 // - Call setjmp on the exception data buffer.
3857 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3858 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3859 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003860 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003861 llvm::CallInst *SetJmpResult =
3862 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3863 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003864 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003865
3866 // If setjmp returned 0, enter the protected block; otherwise,
3867 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003868 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3869 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003870 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003871 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3872 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003873
John McCallf1549f62010-07-06 01:34:17 +00003874 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003875 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003876 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003877 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003878 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003879
3880 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003881
John McCallf1549f62010-07-06 01:34:17 +00003882 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003883 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003884
John McCall87bb5822010-07-31 23:20:56 +00003885 // Don't optimize loads of the in-scope locals across this point.
3886 Hazards.emitWriteHazard();
3887
John McCallf1549f62010-07-06 01:34:17 +00003888 // For a @synchronized (or a @try with no catches), just branch
3889 // through the cleanup to the rethrow block.
3890 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3891 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003892 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003893 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003894
3895 // Otherwise, we have to match against the caught exceptions.
3896 } else {
John McCall0b251722010-08-04 05:59:32 +00003897 // Retrieve the exception object. We may emit multiple blocks but
3898 // nothing can cross this so the value is already in SSA form.
3899 llvm::CallInst *Caught =
3900 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3901 ExceptionData, "caught");
3902 Caught->setDoesNotThrow();
3903
John McCallf1549f62010-07-06 01:34:17 +00003904 // Push the exception to rethrow onto the EH value stack for the
3905 // benefit of any @throws in the handlers.
3906 CGF.ObjCEHValueStack.push_back(Caught);
3907
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003908 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003909
John McCall0b251722010-08-04 05:59:32 +00003910 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003911
John McCall0b251722010-08-04 05:59:32 +00003912 llvm::BasicBlock *CatchBlock = 0;
3913 llvm::BasicBlock *CatchHandler = 0;
3914 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003915 // Save the currently-propagating exception before
3916 // objc_exception_try_enter clears the exception slot.
3917 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3918 "propagating_exception");
3919 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3920
John McCall0b251722010-08-04 05:59:32 +00003921 // Enter a new exception try block (in case a @catch block
3922 // throws an exception).
3923 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3924 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003925
John McCall0b251722010-08-04 05:59:32 +00003926 llvm::CallInst *SetJmpResult =
3927 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3928 "setjmp.result");
3929 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003930 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003931
John McCall0b251722010-08-04 05:59:32 +00003932 llvm::Value *Threw =
3933 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3934
3935 CatchBlock = CGF.createBasicBlock("catch");
3936 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3937 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3938
3939 CGF.EmitBlock(CatchBlock);
3940 }
3941
3942 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003943
Daniel Dunbar55e40722008-09-27 07:03:52 +00003944 // Handle catch list. As a special case we check if everything is
3945 // matched and avoid generating code for falling off the end if
3946 // so.
3947 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003948 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3949 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003950
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003951 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003952 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003953
Anders Carlsson80f25672008-09-09 17:59:25 +00003954 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003955 if (!CatchParam) {
3956 AllMatched = true;
3957 } else {
John McCall183700f2009-09-21 23:43:11 +00003958 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003959
John McCallf1549f62010-07-06 01:34:17 +00003960 // catch(id e) always matches under this ABI, since only
3961 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003962 // FIXME: For the time being we also match id<X>; this should
3963 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003964 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003965 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003966 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003967
John McCallf1549f62010-07-06 01:34:17 +00003968 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003969 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003970 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3971
Anders Carlssondde0a942008-09-11 09:15:33 +00003972 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003973 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003974 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003975
3976 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003977 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003978 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003979
Anders Carlssondde0a942008-09-11 09:15:33 +00003980 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003981
3982 // The scope of the catch variable ends right here.
3983 CatchVarCleanups.ForceCleanup();
3984
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003985 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003986 break;
3987 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003988
Steve Naroff14108da2009-07-10 23:34:53 +00003989 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003990 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003991
3992 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003993 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3994 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003995
3996 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003997 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003998
John McCallf1549f62010-07-06 01:34:17 +00003999 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00004000 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
4001 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00004002 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004003
John McCallf1549f62010-07-06 01:34:17 +00004004 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
4005 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004006
4007 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00004008 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004009
Anders Carlsson80f25672008-09-09 17:59:25 +00004010 // Emit the @catch block.
4011 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00004012
4013 // Collect any cleanups for the catch variable. The scope lasts until
4014 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00004015 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00004016
John McCallb6bbcc92010-10-15 04:57:14 +00004017 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00004018 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004019
John McCallf1549f62010-07-06 01:34:17 +00004020 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004021 llvm::Value *Tmp =
4022 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00004023 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00004024 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004025
Anders Carlssondde0a942008-09-11 09:15:33 +00004026 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00004027
4028 // We're done with the catch variable.
4029 CatchVarCleanups.ForceCleanup();
4030
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004031 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004032
Anders Carlsson80f25672008-09-09 17:59:25 +00004033 CGF.EmitBlock(NextCatchBlock);
4034 }
4035
John McCallf1549f62010-07-06 01:34:17 +00004036 CGF.ObjCEHValueStack.pop_back();
4037
John McCall0b251722010-08-04 05:59:32 +00004038 // If nothing wanted anything to do with the caught exception,
4039 // kill the extract call.
4040 if (Caught->use_empty())
4041 Caught->eraseFromParent();
4042
4043 if (!AllMatched)
4044 CGF.EmitBranchThroughCleanup(FinallyRethrow);
4045
4046 if (HasFinally) {
4047 // Emit the exception handler for the @catch blocks.
4048 CGF.EmitBlock(CatchHandler);
4049
4050 // In theory we might now need a write hazard, but actually it's
4051 // unnecessary because there's no local-accessing code between
4052 // the try's write hazard and here.
4053 //Hazards.emitWriteHazard();
4054
John McCall9e2213d2010-10-04 23:42:51 +00004055 // Extract the new exception and save it to the
4056 // propagating-exception slot.
4057 assert(PropagatingExnVar);
4058 llvm::CallInst *NewCaught =
4059 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4060 ExceptionData, "caught");
4061 NewCaught->setDoesNotThrow();
4062 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
4063
John McCall0b251722010-08-04 05:59:32 +00004064 // Don't pop the catch handler; the throw already did.
4065 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004066 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00004067 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004068 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004069
John McCall87bb5822010-07-31 23:20:56 +00004070 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00004071 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00004072
John McCallf1549f62010-07-06 01:34:17 +00004073 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00004074 CGF.Builder.restoreIP(TryFallthroughIP);
4075 if (CGF.HaveInsertPoint())
4076 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00004077 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00004078 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004079
John McCallf1549f62010-07-06 01:34:17 +00004080 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00004081 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004082 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004083 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004084 // If we have a propagating-exception variable, check it.
4085 llvm::Value *PropagatingExn;
4086 if (PropagatingExnVar) {
4087 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004088
John McCall9e2213d2010-10-04 23:42:51 +00004089 // Otherwise, just look in the buffer for the exception to throw.
4090 } else {
4091 llvm::CallInst *Caught =
4092 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4093 ExceptionData);
4094 Caught->setDoesNotThrow();
4095 PropagatingExn = Caught;
4096 }
4097
4098 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004099 ->setDoesNotThrow();
4100 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004101 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004102
John McCall87bb5822010-07-31 23:20:56 +00004103 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004104}
4105
4106void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00004107 const ObjCAtThrowStmt &S,
4108 bool ClearInsertionPoint) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004109 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004110
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004111 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004112 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004113 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004114 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004115 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004116 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004117 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004118 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004119 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004120
John McCallf1549f62010-07-06 01:34:17 +00004121 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4122 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004123 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004124
4125 // Clear the insertion point to indicate we are in unreachable code.
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00004126 if (ClearInsertionPoint)
4127 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004128}
4129
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004130/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004131/// object: objc_read_weak (id *src)
4132///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004133llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004134 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004135 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004136 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4137 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4138 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004139 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004140 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004141 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004142 return read_weak;
4143}
4144
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004145/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4146/// objc_assign_weak (id src, id *dst)
4147///
4148void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004149 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004150 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004151 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004152 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004153 assert(Size <= 8 && "does not support size > 8");
4154 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004155 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004156 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4157 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004158 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4159 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004160 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004161 src, dst, "weakassign");
4162 return;
4163}
4164
Fariborz Jahanian58626502008-11-19 00:59:10 +00004165/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4166/// objc_assign_global (id src, id *dst)
4167///
4168void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004169 llvm::Value *src, llvm::Value *dst,
4170 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004171 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004172 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004173 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004174 assert(Size <= 8 && "does not support size > 8");
4175 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004177 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4178 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004179 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4180 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004181 if (!threadlocal)
4182 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4183 src, dst, "globalassign");
4184 else
4185 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4186 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004187 return;
4188}
4189
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004190/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004191/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004192///
4193void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004194 llvm::Value *src, llvm::Value *dst,
4195 llvm::Value *ivarOffset) {
4196 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004197 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004198 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004199 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004200 assert(Size <= 8 && "does not support size > 8");
4201 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004202 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004203 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4204 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004205 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4206 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004207 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4208 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004209 return;
4210}
4211
Fariborz Jahanian58626502008-11-19 00:59:10 +00004212/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4213/// objc_assign_strongCast (id src, id *dst)
4214///
4215void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004216 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004217 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004218 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004219 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004220 assert(Size <= 8 && "does not support size > 8");
4221 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004222 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004223 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4224 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004225 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4226 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004227 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004228 src, dst, "weakassign");
4229 return;
4230}
4231
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004232void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004233 llvm::Value *DestPtr,
4234 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004235 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004236 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4237 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004238 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004239 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004240 return;
4241}
4242
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004243/// EmitObjCValueForIvar - Code Gen for ivar reference.
4244///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004245LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4246 QualType ObjectTy,
4247 llvm::Value *BaseValue,
4248 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004249 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004250 const ObjCInterfaceDecl *ID =
4251 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004252 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4253 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004254}
4255
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004256llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004257 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004258 const ObjCIvarDecl *Ivar) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004259 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4260 return llvm::ConstantInt::get(
4261 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4262 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004263}
4264
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004265/* *** Private Interface *** */
4266
4267/// EmitImageInfo - Emit the image info marker used to encode some module
4268/// level information.
4269///
4270/// See: <rdr://4810609&4810587&4810587>
4271/// struct IMAGE_INFO {
4272/// unsigned version;
4273/// unsigned flags;
4274/// };
4275enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004276 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004277 eImageInfo_GarbageCollected = (1 << 1),
4278 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004279 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4280
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004281 // A flag indicating that the module has no instances of a @synthesize of a
4282 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004283 eImageInfo_CorrectedSynthesize = (1 << 4),
4284 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004285};
4286
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004287void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004288 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004289 const char *Section = (ObjCABI == 1) ?
4290 "__OBJC, __image_info,regular" :
4291 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004292
Bill Wendling3973acc2012-02-16 01:13:30 +00004293 // Generate module-level named metadata to convey this information to the
4294 // linker and code-gen.
4295 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004296
Bill Wendling3973acc2012-02-16 01:13:30 +00004297 // Add the ObjC ABI version to the module flags.
4298 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4299 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4300 version);
4301 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4302 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004303
David Blaikie4e4d0842012-03-11 07:00:24 +00004304 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004305 // Non-GC overrides those files which specify GC.
4306 Mod.addModuleFlag(llvm::Module::Override,
4307 "Objective-C Garbage Collection", (uint32_t)0);
4308 } else {
4309 // Add the ObjC garbage collection value.
4310 Mod.addModuleFlag(llvm::Module::Error,
4311 "Objective-C Garbage Collection",
4312 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004313
David Blaikie4e4d0842012-03-11 07:00:24 +00004314 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004315 // Add the ObjC GC Only value.
4316 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4317 eImageInfo_GCOnly);
4318
4319 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4320 llvm::Value *Ops[2] = {
4321 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4322 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4323 eImageInfo_GarbageCollected)
4324 };
4325 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4326 llvm::MDNode::get(VMContext, Ops));
4327 }
4328 }
Bill Wendling09822512012-04-24 11:04:57 +00004329
4330 // Indicate whether we're compiling this to run on a simulator.
4331 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4332 if (Triple.getOS() == llvm::Triple::IOS &&
4333 (Triple.getArch() == llvm::Triple::x86 ||
4334 Triple.getArch() == llvm::Triple::x86_64))
4335 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4336 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004337}
4338
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004339// struct objc_module {
4340// unsigned long version;
4341// unsigned long size;
4342// const char *name;
4343// Symtab symtab;
4344// };
4345
4346// FIXME: Get from somewhere
4347static const int ModuleVersion = 7;
4348
4349void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004350 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004351
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004352 llvm::Constant *Values[] = {
4353 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4354 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4355 // This used to be the filename, now it is unused. <rdr://4327263>
4356 GetClassName(&CGM.getContext().Idents.get("")),
4357 EmitModuleSymbols()
4358 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004359 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004360 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004361 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004362 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004363}
4364
4365llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004366 unsigned NumClasses = DefinedClasses.size();
4367 unsigned NumCategories = DefinedCategories.size();
4368
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004369 // Return null if no symbols were defined.
4370 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004371 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004372
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004373 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004374 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004375 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004376 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4377 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004378
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004379 // The runtime expects exactly the list of defined classes followed
4380 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004381 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004382 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004383 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004384 ObjCTypes.Int8PtrTy);
4385 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004386 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004387 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004388 ObjCTypes.Int8PtrTy);
4389
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004390 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004391 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004392 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004393 Symbols);
4394
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004395 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004396
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004397 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004398 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4399 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004400 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004401 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004402}
4403
John McCallf85e1932011-06-15 23:02:42 +00004404llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4405 IdentifierInfo *II) {
4406 LazySymbols.insert(II);
4407
4408 llvm::GlobalVariable *&Entry = ClassReferences[II];
4409
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004410 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004412 llvm::ConstantExpr::getBitCast(GetClassName(II),
4413 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004414 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004415 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4416 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4417 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004418 }
John McCallf85e1932011-06-15 23:02:42 +00004419
Benjamin Kramer578faa82011-09-27 21:06:10 +00004420 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004421}
4422
John McCallf85e1932011-06-15 23:02:42 +00004423llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4424 const ObjCInterfaceDecl *ID) {
4425 return EmitClassRefFromId(Builder, ID->getIdentifier());
4426}
4427
4428llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4429 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4430 return EmitClassRefFromId(Builder, II);
4431}
4432
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004433llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4434 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004435 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004436
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004437 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004439 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004440 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004441 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004442 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4443 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004444 4, true);
Michael Gottesman8f98bf92013-02-05 23:08:45 +00004445 Entry->setExternallyInitialized(true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004446 }
4447
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004448 if (lvalue)
4449 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004450 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004451}
4452
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004453llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004454 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004455
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004456 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004457 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004458 llvm::ConstantDataArray::getString(VMContext,
4459 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004460 ((ObjCABI == 2) ?
4461 "__TEXT,__objc_classname,cstring_literals" :
4462 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004463 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004464
Owen Andersona1cf15f2009-07-14 23:10:40 +00004465 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004466}
4467
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004468llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4469 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4470 I = MethodDefinitions.find(MD);
4471 if (I != MethodDefinitions.end())
4472 return I->second;
4473
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004474 return NULL;
4475}
4476
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004477/// GetIvarLayoutName - Returns a unique constant for the given
4478/// ivar layout bitmap.
4479llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004480 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004481 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004482}
4483
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004484void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedmane5b46662012-11-06 22:15:52 +00004485 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004486 bool ForStrongLayout,
4487 bool &HasUnion) {
4488 const RecordDecl *RD = RT->getDecl();
4489 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004490 SmallVector<const FieldDecl*, 16> Fields;
4491 for (RecordDecl::field_iterator i = RD->field_begin(),
4492 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004493 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004494 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004495 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004496 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004497
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004498 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4499 ForStrongLayout, HasUnion);
4500}
4501
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004502void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004503 const llvm::StructLayout *Layout,
4504 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004505 ArrayRef<const FieldDecl*> RecFields,
Eli Friedmane5b46662012-11-06 22:15:52 +00004506 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004507 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004508 bool IsUnion = (RD && RD->isUnion());
Eli Friedmane5b46662012-11-06 22:15:52 +00004509 uint64_t MaxUnionIvarSize = 0;
4510 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00004511 const FieldDecl *MaxField = 0;
4512 const FieldDecl *MaxSkippedField = 0;
4513 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedmane5b46662012-11-06 22:15:52 +00004514 uint64_t MaxFieldOffset = 0;
4515 uint64_t MaxSkippedFieldOffset = 0;
4516 uint64_t LastBitfieldOrUnnamedOffset = 0;
4517 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004518
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004519 if (RecFields.empty())
4520 return;
Eli Friedmane5b46662012-11-06 22:15:52 +00004521 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004522 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004523 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004524 const FieldDecl *FirstField = RecFields[0];
4525 FirstFieldDelta =
4526 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
John McCallf85e1932011-06-15 23:02:42 +00004527 }
4528
Chris Lattnerf1690852009-03-31 08:48:01 +00004529 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004530 const FieldDecl *Field = RecFields[i];
Eli Friedmane5b46662012-11-06 22:15:52 +00004531 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004532 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004533 // Note that 'i' here is actually the field index inside RD of Field,
4534 // although this dependency is hidden.
4535 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedmane5b46662012-11-06 22:15:52 +00004536 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004537 } else
John McCallf85e1932011-06-15 23:02:42 +00004538 FieldOffset =
4539 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004540
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004541 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004542 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004543 LastFieldBitfieldOrUnnamed = Field;
4544 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004545 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004546 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004547
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004548 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004549 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004550 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004551 if (FQT->isUnionType())
4552 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004553
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004554 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004555 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004556 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004557 continue;
4558 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004559
Chris Lattnerf1690852009-03-31 08:48:01 +00004560 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004561 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004562 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004563 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004564 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004565 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004566 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4567 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004568 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004569 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004570 FQT = CArray->getElementType();
4571 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004572
4573 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004574 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00004575 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004576 int OldIndex = IvarsInfo.size() - 1;
4577 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004578
Ted Kremenek6217b802009-07-29 21:53:49 +00004579 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004580 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004581 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004582
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004583 // Replicate layout information for each array element. Note that
4584 // one element is already done.
4585 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586 for (int FirstIndex = IvarsInfo.size() - 1,
4587 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004588 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004589 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4590 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4591 IvarsInfo[i].ivar_size));
4592 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4593 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4594 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004595 }
4596 continue;
4597 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004598 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004599 // At this point, we are done with Record/Union and array there of.
4600 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00004601 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004602
Eli Friedmane5b46662012-11-06 22:15:52 +00004603 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00004604 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4605 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004606 if (IsUnion) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004607 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004608 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004609 MaxUnionIvarSize = UnionIvarSize;
4610 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004611 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004612 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004613 } else {
Eli Friedmane5b46662012-11-06 22:15:52 +00004614 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
4615 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004616 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004617 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004618 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4619 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004620 if (IsUnion) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004621 // FIXME: Why the asymmetry? We divide by word size in bits on other
4622 // side.
4623 uint64_t UnionIvarSize = FieldSize / ByteSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004624 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004625 MaxSkippedUnionIvarSize = UnionIvarSize;
4626 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004627 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004628 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004629 } else {
Eli Friedmane5b46662012-11-06 22:15:52 +00004630 // FIXME: Why the asymmetry, we divide by byte size in bits here?
4631 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
4632 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004633 }
4634 }
4635 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004636
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004637 if (LastFieldBitfieldOrUnnamed) {
4638 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4639 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004640 uint64_t BitFieldSize
4641 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004642 GC_IVAR skivar;
4643 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
Eli Friedmane5b46662012-11-06 22:15:52 +00004644 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4645 + ((BitFieldSize % ByteSizeInBits) != 0);
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004646 SkipIvars.push_back(skivar);
4647 } else {
4648 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4649 // Last field was unnamed. Must update skip info.
Eli Friedmane5b46662012-11-06 22:15:52 +00004650 unsigned FieldSize
4651 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004652 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
Eli Friedmane5b46662012-11-06 22:15:52 +00004653 FieldSize / ByteSizeInBits));
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004654 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004655 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004656
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004657 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004658 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004659 MaxUnionIvarSize));
4660 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004661 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004662 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004663}
4664
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004665/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4666/// the computations and returning the layout bitmap (for ivar or blocks) in
4667/// the given argument BitMap string container. Routine reads
4668/// two containers, IvarsInfo and SkipIvars which are assumed to be
4669/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004670llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004671 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004672 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004673
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004674 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004675 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedmane5b46662012-11-06 22:15:52 +00004676 unsigned int WordSize =
4677 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
4678 if (IvarsInfo[0].ivar_bytepos == 0) {
4679 WordsToSkip = 0;
4680 WordsToScan = IvarsInfo[0].ivar_size;
4681 } else {
4682 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4683 WordsToScan = IvarsInfo[0].ivar_size;
4684 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004685 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004686 unsigned int TailPrevGCObjC =
4687 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004688 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004689 // consecutive 'scanned' object pointers.
Eli Friedmane5b46662012-11-06 22:15:52 +00004690 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004691 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004692 // Skip over 'gc'able object pointer which lay over each other.
4693 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4694 continue;
4695 // Must skip over 1 or more words. We save current skip/scan values
4696 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004697 SKIP_SCAN SkScan;
4698 SkScan.skip = WordsToSkip;
4699 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004700 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004701
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004702 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004703 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4704 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004705 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004706 WordsToSkip = 0;
Eli Friedmane5b46662012-11-06 22:15:52 +00004707 WordsToScan = IvarsInfo[i].ivar_size;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004708 }
4709 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004710 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004711 SKIP_SCAN SkScan;
4712 SkScan.skip = WordsToSkip;
4713 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004714 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004715 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004716
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004717 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004718 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedmane5b46662012-11-06 22:15:52 +00004719 int LastByteSkipped =
4720 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004721 LastIndex = IvarsInfo.size()-1;
Eli Friedmane5b46662012-11-06 22:15:52 +00004722 int LastByteScanned =
4723 IvarsInfo[LastIndex].ivar_bytepos +
4724 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004725 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004726 if (LastByteSkipped > LastByteScanned) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004727 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004728 SKIP_SCAN SkScan;
Eli Friedmane5b46662012-11-06 22:15:52 +00004729 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004730 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004731 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004732 }
4733 }
4734 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4735 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004736 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004737 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004738 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4739 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4740 // 0xM0 followed by 0x0N detected.
4741 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4742 for (int j = i+1; j < SkipScan; j++)
4743 SkipScanIvars[j] = SkipScanIvars[j+1];
4744 --SkipScan;
4745 }
4746 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004747
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004748 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004749 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004750 unsigned char byte;
4751 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4752 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4753 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4754 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004755
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004756 // first skip big.
4757 for (unsigned int ix = 0; ix < skip_big; ix++)
4758 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004759
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004760 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004761 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004762 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004763 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004764 byte |= 0xf;
4765 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004766 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004767 byte |= scan_small;
4768 scan_small = 0;
4769 }
4770 BitMap += byte;
4771 }
4772 // next scan big
4773 for (unsigned int ix = 0; ix < scan_big; ix++)
4774 BitMap += (unsigned char)(0x0f);
4775 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004776 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004777 byte = scan_small;
4778 BitMap += byte;
4779 }
4780 }
4781 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004782 unsigned char zero = 0;
4783 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004784
4785 llvm::GlobalVariable * Entry =
4786 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004787 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004788 ((ObjCABI == 2) ?
4789 "__TEXT,__objc_classname,cstring_literals" :
4790 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004791 1, true);
4792 return getConstantGEP(VMContext, Entry, 0, 0);
4793}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004795/// BuildIvarLayout - Builds ivar layout bitmap for the class
4796/// implementation for the __strong or __weak case.
4797/// The layout map displays which words in ivar list must be skipped
4798/// and which must be scanned by GC (see below). String is built of bytes.
4799/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4800/// of words to skip and right nibble is count of words to scan. So, each
4801/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4802/// represented by a 0x00 byte which also ends the string.
4803/// 1. when ForStrongLayout is true, following ivars are scanned:
4804/// - id, Class
4805/// - object *
4806/// - __strong anything
4807///
4808/// 2. When ForStrongLayout is false, following ivars are scanned:
4809/// - __weak anything
4810///
4811llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4812 const ObjCImplementationDecl *OMD,
4813 bool ForStrongLayout) {
4814 bool hasUnion = false;
4815
Chris Lattner8b418682012-02-07 00:39:47 +00004816 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004817 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4818 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004819 return llvm::Constant::getNullValue(PtrTy);
4820
Jordy Rosedb8264e2011-07-22 02:08:32 +00004821 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4822 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004823 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004824 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004825 IVD; IVD = IVD->getNextIvar())
4826 RecFields.push_back(cast<FieldDecl>(IVD));
4827 }
4828 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004829 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004830 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004831
Jordy Rosedb8264e2011-07-22 02:08:32 +00004832 // FIXME: This is not ideal; we shouldn't have to do this copy.
4833 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004834 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004835
4836 if (RecFields.empty())
4837 return llvm::Constant::getNullValue(PtrTy);
4838
4839 SkipIvars.clear();
4840 IvarsInfo.clear();
4841
Eli Friedmane5b46662012-11-06 22:15:52 +00004842 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004843 if (IvarsInfo.empty())
4844 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004845 // Sort on byte position in case we encounterred a union nested in
4846 // the ivar list.
4847 if (hasUnion && !IvarsInfo.empty())
4848 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4849 if (hasUnion && !SkipIvars.empty())
4850 std::sort(SkipIvars.begin(), SkipIvars.end());
4851
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004852 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004853 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004854
David Blaikie4e4d0842012-03-11 07:00:24 +00004855 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004857 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004858 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004859 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004860 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004861 if (!(s[i] & 0xf0))
4862 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4863 else
4864 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4865 printf("\n");
4866 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004867 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004868}
4869
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004870llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004871 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4872
Chris Lattner0b239712012-02-06 22:16:34 +00004873 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004874 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004875 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004876 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004877 ((ObjCABI == 2) ?
4878 "__TEXT,__objc_methname,cstring_literals" :
4879 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004880 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004881
Owen Andersona1cf15f2009-07-14 23:10:40 +00004882 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004883}
4884
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004885// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004886llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004887 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4888}
4889
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004890llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004891 std::string TypeStr;
4892 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4893
4894 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004895
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004896 if (!Entry)
4897 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004898 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004899 ((ObjCABI == 2) ?
4900 "__TEXT,__objc_methtype,cstring_literals" :
4901 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004902 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004903
Owen Andersona1cf15f2009-07-14 23:10:40 +00004904 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004905}
4906
Bob Wilsondc8dab62011-11-30 01:57:58 +00004907llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4908 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004909 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004910 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004911 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004912
4913 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4914
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004915 if (!Entry)
4916 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004917 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004918 ((ObjCABI == 2) ?
4919 "__TEXT,__objc_methtype,cstring_literals" :
4920 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004921 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004922
Owen Andersona1cf15f2009-07-14 23:10:40 +00004923 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004924}
4925
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004926// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004927llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004928 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004929
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004930 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004931 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004932 llvm::ConstantDataArray::getString(VMContext,
4933 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004934 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004935 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004936
Owen Andersona1cf15f2009-07-14 23:10:40 +00004937 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004938}
4939
4940// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004941// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004942llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004943CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4944 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004945 std::string TypeStr;
4946 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004947 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4948}
4949
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004950void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004951 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004952 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004953 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004954 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004955 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4956 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004957 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004958 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004959 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004960 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004961}
4962
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004963void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004964 EmitModuleInfo();
4965
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004966 // Emit the dummy bodies for any protocols which were referenced but
4967 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004969 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4970 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004971 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004972
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004973 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004974 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004975 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004976 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004977 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004978 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004979 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004980 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004981 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004982 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004983 }
4984
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004985 // Add assembler directives to add lazy undefined symbol references
4986 // for classes which are referenced but not defined. This is
4987 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004988 //
4989 // FIXME: It would be nice if we had an LLVM construct for this.
4990 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004991 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004992 Asm += CGM.getModule().getModuleInlineAsm();
4993 if (!Asm.empty() && Asm.back() != '\n')
4994 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004995
Daniel Dunbar33063492009-09-07 00:20:42 +00004996 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004997 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4998 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004999 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
5000 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00005001 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00005002 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00005003 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00005004 }
5005
Bill Wendling13562a12012-02-07 09:06:01 +00005006 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00005007 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
5008 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
5009 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00005010
Daniel Dunbar33063492009-09-07 00:20:42 +00005011 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00005012 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00005013}
5014
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005015CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005016 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00005017 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005018 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005019 ObjCABI = 2;
5020}
5021
Daniel Dunbarf77ac862008-08-11 21:35:06 +00005022/* *** */
5023
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005024ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00005025 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
5026{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005027 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5028 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005030 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005031 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005032 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005033 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00005034 Int8PtrTy = CGM.Int8PtrTy;
5035 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005037 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005038 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005039 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005041 // I'm not sure I like this. The implicit coordination is a bit
5042 // gross. We should solve this in a reasonable fashion because this
5043 // is a pretty common task (match some runtime data structure with
5044 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005045
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005046 // FIXME: This is leaked.
5047 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005048
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005049 // struct _objc_super {
5050 // id self;
5051 // Class cls;
5052 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005053 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005054 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005055 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005056 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005057 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005058 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005059 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005060 Ctx.getObjCClassType(), 0, 0, false,
5061 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005062 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005063
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005064 SuperCTy = Ctx.getTagDeclType(RD);
5065 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005066
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005067 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005068 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
5069
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005070 // struct _prop_t {
5071 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005072 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005073 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005074 PropertyTy = llvm::StructType::create("struct._prop_t",
5075 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005076
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005077 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005078 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005079 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005080 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005081 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005082 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005083 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
5084 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005085 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005086 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005087
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005088 // struct _objc_method {
5089 // SEL _cmd;
5090 // char *method_type;
5091 // char *_imp;
5092 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005093 MethodTy = llvm::StructType::create("struct._objc_method",
5094 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5095 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005097 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005098 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005099 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005100
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005101}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005102
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005103ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005104 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005105 // struct _objc_method_description {
5106 // SEL name;
5107 // char *types;
5108 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005110 llvm::StructType::create("struct._objc_method_description",
5111 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005112
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005113 // struct _objc_method_description_list {
5114 // int count;
5115 // struct _objc_method_description[1];
5116 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005117 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005118 llvm::StructType::create("struct._objc_method_description_list",
5119 IntTy,
5120 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005121
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005122 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005123 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005124 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005126 // Protocol description structures
5127
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005128 // struct _objc_protocol_extension {
5129 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5130 // struct _objc_method_description_list *optional_instance_methods;
5131 // struct _objc_method_description_list *optional_class_methods;
5132 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005133 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005134 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005135 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005136 llvm::StructType::create("struct._objc_protocol_extension",
5137 IntTy, MethodDescriptionListPtrTy,
5138 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005139 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005140
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005141 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005142 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005143
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005144 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005145
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005146 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005147 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005148
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005149 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005150 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005151 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005152 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005153 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005154 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005155
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005156 // struct _objc_protocol {
5157 // struct _objc_protocol_extension *isa;
5158 // char *protocol_name;
5159 // struct _objc_protocol **_objc_protocol_list;
5160 // struct _objc_method_description_list *instance_methods;
5161 // struct _objc_method_description_list *class_methods;
5162 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005163 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5164 llvm::PointerType::getUnqual(ProtocolListTy),
5165 MethodDescriptionListPtrTy,
5166 MethodDescriptionListPtrTy,
5167 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005168
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005169 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005170 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005171
Owen Anderson96e0fc72009-07-29 22:16:19 +00005172 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005173
5174 // Class description structures
5175
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005176 // struct _objc_ivar {
5177 // char *ivar_name;
5178 // char *ivar_type;
5179 // int ivar_offset;
5180 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005181 IvarTy = llvm::StructType::create("struct._objc_ivar",
5182 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005183
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005184 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005185 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005186 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005187 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005188
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005189 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005190 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005191 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005192 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005193
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005194 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005195 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005196 llvm::StructType::create("struct._objc_class_extension",
5197 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005198 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005199
Chris Lattnerc1c20112011-08-12 17:43:31 +00005200 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005201
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005202 // struct _objc_class {
5203 // Class isa;
5204 // Class super_class;
5205 // char *name;
5206 // long version;
5207 // long info;
5208 // long instance_size;
5209 // struct _objc_ivar_list *ivars;
5210 // struct _objc_method_list *methods;
5211 // struct _objc_cache *cache;
5212 // struct _objc_protocol_list *protocols;
5213 // char *ivar_layout;
5214 // struct _objc_class_ext *ext;
5215 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005216 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5217 llvm::PointerType::getUnqual(ClassTy),
5218 Int8PtrTy,
5219 LongTy,
5220 LongTy,
5221 LongTy,
5222 IvarListPtrTy,
5223 MethodListPtrTy,
5224 CachePtrTy,
5225 ProtocolListPtrTy,
5226 Int8PtrTy,
5227 ClassExtensionPtrTy,
5228 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005229
Owen Anderson96e0fc72009-07-29 22:16:19 +00005230 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005231
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005232 // struct _objc_category {
5233 // char *category_name;
5234 // char *class_name;
5235 // struct _objc_method_list *instance_method;
5236 // struct _objc_method_list *class_method;
5237 // uint32_t size; // sizeof(struct _objc_category)
5238 // struct _objc_property_list *instance_properties;// category's @property
5239 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005240 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005241 llvm::StructType::create("struct._objc_category",
5242 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5243 MethodListPtrTy, ProtocolListPtrTy,
5244 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005245
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005246 // Global metadata structures
5247
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005248 // struct _objc_symtab {
5249 // long sel_ref_cnt;
5250 // SEL *refs;
5251 // short cls_def_cnt;
5252 // short cat_def_cnt;
5253 // char *defs[cls_def_cnt + cat_def_cnt];
5254 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005255 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005256 llvm::StructType::create("struct._objc_symtab",
5257 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5258 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005259 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005260
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005261 // struct _objc_module {
5262 // long version;
5263 // long size; // sizeof(struct _objc_module)
5264 // char *name;
5265 // struct _objc_symtab* symtab;
5266 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005267 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005268 llvm::StructType::create("struct._objc_module",
5269 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005270
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005271
Mike Stumpf5408fe2009-05-16 07:57:57 +00005272 // FIXME: This is the size of the setjmp buffer and should be target
5273 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005274 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005275
Anders Carlsson124526b2008-09-09 10:10:21 +00005276 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005277 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278
5279 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005280 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005281 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5282 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005283
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005284}
5285
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005286ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005287 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005288 // struct _method_list_t {
5289 // uint32_t entsize; // sizeof(struct _objc_method)
5290 // uint32_t method_count;
5291 // struct _objc_method method_list[method_count];
5292 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005293 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005294 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5295 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005296 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005297 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005299 // struct _protocol_t {
5300 // id isa; // NULL
5301 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005302 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005303 // const struct method_list_t * const instance_methods;
5304 // const struct method_list_t * const class_methods;
5305 // const struct method_list_t *optionalInstanceMethods;
5306 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005307 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005308 // const uint32_t size; // sizeof(struct _protocol_t)
5309 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005310 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005311 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005313 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005314 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005315 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005317 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005318 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5319 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5320 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5321 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005322 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5323 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005324
5325 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005326 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005327
Fariborz Jahanianda320092009-01-29 19:24:30 +00005328 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005329 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005330 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005331 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005332 ProtocolListnfABITy->setBody(LongTy,
5333 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5334 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005335
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005336 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005337 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005338
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005339 // struct _ivar_t {
5340 // unsigned long int *offset; // pointer to ivar offset location
5341 // char *name;
5342 // char *type;
5343 // uint32_t alignment;
5344 // uint32_t size;
5345 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005346 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005347 llvm::StructType::create("struct._ivar_t",
5348 llvm::PointerType::getUnqual(LongTy),
5349 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005350
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005351 // struct _ivar_list_t {
5352 // uint32 entsize; // sizeof(struct _ivar_t)
5353 // uint32 count;
5354 // struct _iver_t list[count];
5355 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005356 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005357 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5358 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005359
Owen Anderson96e0fc72009-07-29 22:16:19 +00005360 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005361
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005362 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005363 // uint32_t const flags;
5364 // uint32_t const instanceStart;
5365 // uint32_t const instanceSize;
5366 // uint32_t const reserved; // only when building for 64bit targets
5367 // const uint8_t * const ivarLayout;
5368 // const char *const name;
5369 // const struct _method_list_t * const baseMethods;
5370 // const struct _objc_protocol_list *const baseProtocols;
5371 // const struct _ivar_list_t *const ivars;
5372 // const uint8_t * const weakIvarLayout;
5373 // const struct _prop_list_t * const properties;
5374 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005375
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005376 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005377 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5378 IntTy, IntTy, IntTy, Int8PtrTy,
5379 Int8PtrTy, MethodListnfABIPtrTy,
5380 ProtocolListnfABIPtrTy,
5381 IvarListnfABIPtrTy,
5382 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005383
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005384 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005385 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005386 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5387 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005388
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005389 // struct _class_t {
5390 // struct _class_t *isa;
5391 // struct _class_t * const superclass;
5392 // void *cache;
5393 // IMP *vtable;
5394 // struct class_ro_t *ro;
5395 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005396
Chris Lattnerc1c20112011-08-12 17:43:31 +00005397 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005398 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5399 llvm::PointerType::getUnqual(ClassnfABITy),
5400 CachePtrTy,
5401 llvm::PointerType::getUnqual(ImpnfABITy),
5402 llvm::PointerType::getUnqual(ClassRonfABITy),
5403 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005405 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005406 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005407
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005408 // struct _category_t {
5409 // const char * const name;
5410 // struct _class_t *const cls;
5411 // const struct _method_list_t * const instance_methods;
5412 // const struct _method_list_t * const class_methods;
5413 // const struct _protocol_list_t * const protocols;
5414 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005415 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005416 CategorynfABITy = llvm::StructType::create("struct._category_t",
5417 Int8PtrTy, ClassnfABIPtrTy,
5418 MethodListnfABIPtrTy,
5419 MethodListnfABIPtrTy,
5420 ProtocolListnfABIPtrTy,
5421 PropertyListPtrTy,
5422 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005423
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005424 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005425 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5426 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005427
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005428 // MessageRefTy - LLVM for:
5429 // struct _message_ref_t {
5430 // IMP messenger;
5431 // SEL name;
5432 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005433
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005434 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005435 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005436 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005437 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005438 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005439 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005440 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005441 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005442 Ctx.getObjCSelType(), 0, 0, false,
5443 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005444 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005445
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005446 MessageRefCTy = Ctx.getTagDeclType(RD);
5447 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5448 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005450 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005451 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005452
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005453 // SuperMessageRefTy - LLVM for:
5454 // struct _super_message_ref_t {
5455 // SUPER_IMP messenger;
5456 // SEL name;
5457 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005458 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005459 llvm::StructType::create("struct._super_message_ref_t",
5460 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005462 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005463 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005464
Daniel Dunbare588b992009-03-01 04:46:24 +00005465
5466 // struct objc_typeinfo {
5467 // const void** vtable; // objc_ehtype_vtable + 2
5468 // const char* name; // c++ typeinfo string
5469 // Class cls;
5470 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005471 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005472 llvm::StructType::create("struct._objc_typeinfo",
5473 llvm::PointerType::getUnqual(Int8PtrTy),
5474 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005475 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005476}
5477
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005479 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005481 return NULL;
5482}
5483
Bill Wendlingbb028552012-02-07 09:25:09 +00005484void CGObjCNonFragileABIMac::
5485AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5486 const char *SymbolName,
5487 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005488 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005489
Daniel Dunbar463b8762009-05-15 21:48:48 +00005490 if (!NumClasses)
5491 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005492
Chris Lattner0b239712012-02-06 22:16:34 +00005493 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005494 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005495 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005496 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005497 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005498 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005499 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005500 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005501
Daniel Dunbar463b8762009-05-15 21:48:48 +00005502 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005503 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005504 llvm::GlobalValue::InternalLinkage,
5505 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005506 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005507 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005508 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005509 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005510}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005512void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5513 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005514
Daniel Dunbar463b8762009-05-15 21:48:48 +00005515 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005516 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005517 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005518 "\01L_OBJC_LABEL_CLASS_$",
5519 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005520
Bill Wendling13562a12012-02-07 09:06:01 +00005521 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005522 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5523 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5524 continue;
5525 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005526 }
5527
Bill Wendling13562a12012-02-07 09:06:01 +00005528 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005529 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5530 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5531 continue;
5532 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5533 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005534
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005536 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5537 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005538
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005539 // Build list of all implemented category addresses in array
5540 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005542 "\01L_OBJC_LABEL_CATEGORY_$",
5543 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005544 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005545 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5546 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005547
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005548 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005549}
5550
John McCall944c8432011-05-14 03:10:52 +00005551/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5552/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005553/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005554/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005555bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5556 // At various points we've experimented with using vtable-based
5557 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005558 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005559 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005560 return false;
John McCall944c8432011-05-14 03:10:52 +00005561 case CodeGenOptions::NonLegacy:
5562 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005563 case CodeGenOptions::Mixed:
5564 break;
5565 }
5566
5567 // If so, see whether this selector is in the white-list of things which must
5568 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005569 if (VTableDispatchMethods.empty()) {
5570 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5571 VTableDispatchMethods.insert(GetNullarySelector("class"));
5572 VTableDispatchMethods.insert(GetNullarySelector("self"));
5573 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5574 VTableDispatchMethods.insert(GetNullarySelector("length"));
5575 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576
John McCall944c8432011-05-14 03:10:52 +00005577 // These are vtable-based if GC is disabled.
5578 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005579 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005580 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5581 VTableDispatchMethods.insert(GetNullarySelector("release"));
5582 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5583 }
5584
5585 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5586 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5587 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5588 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5589 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5590 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5591 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5592
5593 // These are vtable-based if GC is enabled.
5594 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005595 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005596 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5597 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5598
5599 // "countByEnumeratingWithState:objects:count"
5600 IdentifierInfo *KeyIdents[] = {
5601 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5602 &CGM.getContext().Idents.get("objects"),
5603 &CGM.getContext().Idents.get("count")
5604 };
5605 VTableDispatchMethods.insert(
5606 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5607 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005608 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005609
John McCall944c8432011-05-14 03:10:52 +00005610 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005611}
5612
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005613/// BuildClassRoTInitializer - generate meta-data for:
5614/// struct _class_ro_t {
5615/// uint32_t const flags;
5616/// uint32_t const instanceStart;
5617/// uint32_t const instanceSize;
5618/// uint32_t const reserved; // only when building for 64bit targets
5619/// const uint8_t * const ivarLayout;
5620/// const char *const name;
5621/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005622/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005623/// const struct _ivar_list_t *const ivars;
5624/// const uint8_t * const weakIvarLayout;
5625/// const struct _prop_list_t * const properties;
5626/// }
5627///
5628llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005629 unsigned flags,
5630 unsigned InstanceStart,
5631 unsigned InstanceSize,
5632 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005633 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005634 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005635
David Blaikie4e4d0842012-03-11 07:00:24 +00005636 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005637 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005638
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005639 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5640 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5641 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005642 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005643 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5644 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005645 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005646 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005647 // const struct _method_list_t * const baseMethods;
5648 std::vector<llvm::Constant*> Methods;
5649 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005650 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005651 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005653 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005654 // Class methods should always be defined.
5655 Methods.push_back(GetMethodConstant(*i));
5656 }
5657 } else {
5658 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005660 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005661 // Instance methods should always be defined.
5662 Methods.push_back(GetMethodConstant(*i));
5663 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005664 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005665 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005666 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005668 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5669 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005670
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005671 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5672 if (llvm::Constant *C = GetMethodConstant(MD))
5673 Methods.push_back(C);
5674 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5675 if (llvm::Constant *C = GetMethodConstant(MD))
5676 Methods.push_back(C);
5677 }
5678 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005679 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680 Values[ 5] = EmitMethodList(MethodListName,
5681 "__DATA, __objc_const", Methods);
5682
Fariborz Jahanianda320092009-01-29 19:24:30 +00005683 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5684 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005686 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005687 OID->all_referenced_protocol_begin(),
5688 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005689
John McCall621915c2012-10-17 04:53:23 +00005690 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005691 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005692 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005693 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005694 } else {
5695 Values[ 7] = EmitIvarList(ID);
5696 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005697 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5698 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005699 }
Owen Anderson08e25242009-07-27 22:29:56 +00005700 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005701 Values);
5702 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5704 llvm::GlobalValue::InternalLinkage,
5705 Init,
John McCall621915c2012-10-17 04:53:23 +00005706 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5708 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005709 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005710 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005711 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005712 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005713
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005714}
5715
5716/// BuildClassMetaData - This routine defines that to-level meta-data
5717/// for the given ClassName for:
5718/// struct _class_t {
5719/// struct _class_t *isa;
5720/// struct _class_t * const superclass;
5721/// void *cache;
5722/// IMP *vtable;
5723/// struct class_ro_t *ro;
5724/// }
5725///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005726llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005727 std::string &ClassName,
5728 llvm::Constant *IsAGV,
5729 llvm::Constant *SuperClassGV,
5730 llvm::Constant *ClassRoGV,
5731 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005732 llvm::Constant *Values[] = {
5733 IsAGV,
5734 SuperClassGV,
5735 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5736 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5737 ClassRoGV // &CLASS_RO_GV
5738 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005739 if (!Values[1])
5740 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005742 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005743 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5744 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005745 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005746 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005747 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005748 if (HiddenVisibility)
5749 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005750 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005751}
5752
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005753bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005754CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005755 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005756}
5757
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005758void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005759 uint32_t &InstanceStart,
5760 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005761 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005762 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005764 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005765 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005766
5767 // If there are no fields, the start is the same as the end.
5768 if (!RL.getFieldCount())
5769 InstanceStart = InstanceSize;
5770 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005771 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005772}
5773
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005774void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5775 std::string ClassName = ID->getNameAsString();
5776 if (!ObjCEmptyCacheVar) {
5777 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005778 CGM.getModule(),
5779 ObjCTypes.CacheTy,
5780 false,
5781 llvm::GlobalValue::ExternalLinkage,
5782 0,
5783 "_objc_empty_cache");
5784
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005785 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005786 CGM.getModule(),
5787 ObjCTypes.ImpnfABITy,
5788 false,
5789 llvm::GlobalValue::ExternalLinkage,
5790 0,
5791 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005792 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005793 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005794 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005795 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005796 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005797 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005798 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005799 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005800 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5801 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005802
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005803 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005804
John McCallb03527a2012-10-17 04:53:31 +00005805 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005806 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005807 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005808 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005809 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005810
5811 // FIXME: why is this flag set on the metaclass?
5812 // ObjC metaclasses have no fields and don't really get constructed.
5813 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005814 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005815 if (!ID->hasNonZeroConstructors())
5816 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5817 }
5818
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005819 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005820 // class is root
John McCall621915c2012-10-17 04:53:23 +00005821 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005822 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005823 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005824 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005825 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005826 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5827 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5828 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005829 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005830 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005831 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005832 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005833 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005834 ObjCMetaClassName +
5835 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005836 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005837 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005838 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005839 }
5840 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5841 InstanceStart,
5842 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005843 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005845 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5846 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005847 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005848
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005849 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005850 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005851 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005852 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005853
5854 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005855 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005856
John McCallb03527a2012-10-17 04:53:31 +00005857 // Set a flag to enable a runtime optimization when a class has
5858 // fields that require destruction but which don't require
5859 // anything except zero-initialization during construction. This
5860 // is most notably true of __strong and __weak types, but you can
5861 // also imagine there being C++ types with non-trivial default
5862 // constructors that merely set all fields to null.
5863 if (!ID->hasNonZeroConstructors())
5864 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5865 }
5866
Douglas Gregor68584ed2009-06-18 16:11:24 +00005867 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005868 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005869
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005870 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005871 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005872 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005873 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005874 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005875 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005876 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005877 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005878 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005879 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005880 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005881 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005882 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005883 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005884 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005885 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005886
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005887 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005888 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005889 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5890 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005891 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005892
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005893 // Determine if this class is also "non-lazy".
5894 if (ImplementationIsNonLazy(ID))
5895 DefinedNonLazyClasses.push_back(ClassMD);
5896
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005897 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005898 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005899 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005900 // Make sure method definition entries are all clear for next implementation.
5901 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005902}
5903
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005904/// GenerateProtocolRef - This routine is called to generate code for
5905/// a protocol reference expression; as in:
5906/// @code
5907/// @protocol(Proto1);
5908/// @endcode
5909/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5910/// which will hold address of the protocol meta-data.
5911///
5912llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005913 const ObjCProtocolDecl *PD) {
5914
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005915 // This routine is called for @protocol only. So, we must build definition
5916 // of protocol's meta-data (not a reference to it!)
5917 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005918 llvm::Constant *Init =
5919 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005920 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005922 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005923 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005924
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005925 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5926 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005927 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005928 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005929 CGM.getModule(),
5930 Init->getType(), false,
5931 llvm::GlobalValue::WeakAnyLinkage,
5932 Init,
5933 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005934 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5935 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005936 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005937 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005938}
5939
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005940/// GenerateCategory - Build metadata for a category implementation.
5941/// struct _category_t {
5942/// const char * const name;
5943/// struct _class_t *const cls;
5944/// const struct _method_list_t * const instance_methods;
5945/// const struct _method_list_t * const class_methods;
5946/// const struct _protocol_list_t * const protocols;
5947/// const struct _prop_list_t * const properties;
5948/// }
5949///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005950void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005951 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005952 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005953 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5954 "_$_" + OCD->getNameAsString());
5955 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005956 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005957
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005958 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005959 Values[0] = GetClassName(OCD->getIdentifier());
5960 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005961 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005962 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005963 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5964
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005965 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005966 std::vector<llvm::Constant*> Methods;
5967 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005968 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005969 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005970
5971 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005972 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005973 // Instance methods should always be defined.
5974 Methods.push_back(GetMethodConstant(*i));
5975 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005976
5977 Values[2] = EmitMethodList(MethodListName,
5978 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005979 Methods);
5980
5981 MethodListName = Prefix;
5982 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5983 OCD->getNameAsString();
5984 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005985 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005986 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005987 // Class methods should always be defined.
5988 Methods.push_back(GetMethodConstant(*i));
5989 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005990
5991 Values[3] = EmitMethodList(MethodListName,
5992 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005993 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005994 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005995 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005996 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005997 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005998 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5999 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00006000 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006001 + Interface->getName() + "_$_"
6002 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00006003 Category->protocol_begin(),
6004 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006005 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
6006 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00006007 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00006008 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
6009 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00006010 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006011
6012 llvm::Constant *Init =
6013 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00006014 Values);
6015 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006016 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00006017 false,
6018 llvm::GlobalValue::InternalLinkage,
6019 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00006020 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006021 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006022 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006023 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00006024 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00006025 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00006026
6027 // Determine if this category is also "non-lazy".
6028 if (ImplementationIsNonLazy(OCD))
6029 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00006030 // method definition entries must be clear for next implementation.
6031 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00006032}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006033
6034/// GetMethodConstant - Return a struct objc_method constant for the
6035/// given method if it has been defined. The result is null if the
6036/// method has not been defined. The return value has type MethodPtrTy.
6037llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006038 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00006039 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006040 if (!Fn)
6041 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006042
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006043 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00006044 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006045 ObjCTypes.SelectorPtrTy),
6046 GetMethodVarType(MD),
6047 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
6048 };
Owen Anderson08e25242009-07-27 22:29:56 +00006049 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006050}
6051
6052/// EmitMethodList - Build meta-data for method declarations
6053/// struct _method_list_t {
6054/// uint32_t entsize; // sizeof(struct _objc_method)
6055/// uint32_t method_count;
6056/// struct _objc_method method_list[method_count];
6057/// }
6058///
Bill Wendlingbb028552012-02-07 09:25:09 +00006059llvm::Constant *
6060CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
6061 const char *Section,
6062 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006063 // Return null for empty list.
6064 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006065 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006066
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006067 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006068 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00006069 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006070 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006071 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006072 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006073 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006074 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006075 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006076 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006077
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006078 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006079 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006080 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00006081 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006082 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00006083 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006084 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006085}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006086
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006087/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6088/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00006089llvm::GlobalVariable *
6090CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6091 const ObjCIvarDecl *Ivar) {
6092 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006093 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006094 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006095 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006096 CGM.getModule().getGlobalVariable(Name);
6097 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006098 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006099 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006100 false,
6101 llvm::GlobalValue::ExternalLinkage,
6102 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006103 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006104 return IvarOffsetGV;
6105}
6106
Daniel Dunbare83be122010-04-02 21:14:02 +00006107llvm::Constant *
6108CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6109 const ObjCIvarDecl *Ivar,
Eli Friedmane5b46662012-11-06 22:15:52 +00006110 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006111 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006112 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedmane5b46662012-11-06 22:15:52 +00006113 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006114 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006115 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006116
Mike Stumpf5408fe2009-05-16 07:57:57 +00006117 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6118 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006119 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6120 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006121 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006122 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006123 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006124 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006125 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006126 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006127}
6128
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006129/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006130/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006131/// IvarListnfABIPtrTy.
6132/// struct _ivar_t {
6133/// unsigned long int *offset; // pointer to ivar offset location
6134/// char *name;
6135/// char *type;
6136/// uint32_t alignment;
6137/// uint32_t size;
6138/// }
6139/// struct _ivar_list_t {
6140/// uint32 entsize; // sizeof(struct _ivar_t)
6141/// uint32 count;
6142/// struct _iver_t list[count];
6143/// }
6144///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006145
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006146llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006147 const ObjCImplementationDecl *ID) {
6148
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006149 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006150
Jordy Rosedb8264e2011-07-22 02:08:32 +00006151 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006152 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006153
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006154 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006155
Jordy Rosedb8264e2011-07-22 02:08:32 +00006156 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006157 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006158 // Ignore unnamed bit-fields.
6159 if (!IVD->getDeclName())
6160 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006161 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006162 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006163 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006164 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6165 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006166 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006167 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006168 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006169 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006170 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006171 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006172 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006173 // NOTE. Size of a bitfield does not match gcc's, because of the
6174 // way bitfields are treated special in each. But I am told that
6175 // 'size' for bitfield ivars is ignored by the runtime so it does
6176 // not matter. If it matters, there is enough info to get the
6177 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006178 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006179 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006180 }
6181 // Return null for empty list.
6182 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006183 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006184
6185 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006186 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006187 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6188 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006189 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006190 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006191 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006192 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006193 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6194 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006195 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006196 llvm::GlobalValue::InternalLinkage,
6197 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006198 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006199 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006200 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006201 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006202
Chris Lattnerad64e022009-07-17 23:57:13 +00006203 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006204 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006205}
6206
6207llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006208 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006209 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006210
Fariborz Jahanianda320092009-01-29 19:24:30 +00006211 if (!Entry) {
6212 // We use the initializer as a marker of whether this is a forward
6213 // reference or not. At module finalization we add the empty
6214 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006215 Entry =
6216 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6217 llvm::GlobalValue::ExternalLinkage,
6218 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006219 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006220 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006221 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006222
Fariborz Jahanianda320092009-01-29 19:24:30 +00006223 return Entry;
6224}
6225
6226/// GetOrEmitProtocol - Generate the protocol meta-data:
6227/// @code
6228/// struct _protocol_t {
6229/// id isa; // NULL
6230/// const char * const protocol_name;
6231/// const struct _protocol_list_t * protocol_list; // super protocols
6232/// const struct method_list_t * const instance_methods;
6233/// const struct method_list_t * const class_methods;
6234/// const struct method_list_t *optionalInstanceMethods;
6235/// const struct method_list_t *optionalClassMethods;
6236/// const struct _prop_list_t * properties;
6237/// const uint32_t size; // sizeof(struct _protocol_t)
6238/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006239/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006240/// }
6241/// @endcode
6242///
6243
6244llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006245 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006246 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006247
Fariborz Jahanianda320092009-01-29 19:24:30 +00006248 // Early exit if a defining object has already been generated.
6249 if (Entry && Entry->hasInitializer())
6250 return Entry;
6251
Douglas Gregor1d784b22012-01-01 19:51:50 +00006252 // Use the protocol definition, if there is one.
6253 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6254 PD = Def;
6255
Fariborz Jahanianda320092009-01-29 19:24:30 +00006256 // Construct method lists.
6257 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6258 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006259 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006260 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006261 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006262 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006263 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006264 if (!C)
6265 return GetOrEmitProtocolRef(PD);
6266
Fariborz Jahanianda320092009-01-29 19:24:30 +00006267 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6268 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006269 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006270 } else {
6271 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006272 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006273 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006274 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006275
6276 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006277 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006278 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006279 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006280 if (!C)
6281 return GetOrEmitProtocolRef(PD);
6282
Fariborz Jahanianda320092009-01-29 19:24:30 +00006283 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6284 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006285 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006286 } else {
6287 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006288 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006289 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006290 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006291
Bob Wilsondc8dab62011-11-30 01:57:58 +00006292 MethodTypesExt.insert(MethodTypesExt.end(),
6293 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6294
6295 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006296 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006297 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006298 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006299 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6300 PD->protocol_begin(),
6301 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006302
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006303 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006304 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006305 "__DATA, __objc_const",
6306 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006307 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006308 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006309 "__DATA, __objc_const",
6310 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006311 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006312 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006313 "__DATA, __objc_const",
6314 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006315 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006316 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006317 "__DATA, __objc_const",
6318 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006319 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006320 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006321 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006322 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006323 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006324 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006325 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6326 + PD->getName(),
6327 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006328 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006329 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006330
Fariborz Jahanianda320092009-01-29 19:24:30 +00006331 if (Entry) {
6332 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006333 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006334 Entry->setInitializer(Init);
6335 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006336 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006337 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6338 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6339 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006340 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006341 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006342 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006343
6344 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006345 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006346 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006347 CGM.AddUsedGlobal(Entry);
6348
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006349 // Use this protocol meta-data to build protocol list table in section
6350 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006351 llvm::GlobalVariable *PTGV =
6352 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6353 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6354 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006355 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006356 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006357 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006358 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006359 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006360 return Entry;
6361}
6362
6363/// EmitProtocolList - Generate protocol list meta-data:
6364/// @code
6365/// struct _protocol_list_t {
6366/// long protocol_count; // Note, this is 32/64 bit
6367/// struct _protocol_t[protocol_count];
6368/// }
6369/// @endcode
6370///
6371llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006372CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006373 ObjCProtocolDecl::protocol_iterator begin,
6374 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006375 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006376
Fariborz Jahanianda320092009-01-29 19:24:30 +00006377 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006378 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006379 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006380
Daniel Dunbar948e2582009-02-15 07:36:20 +00006381 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006382 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006383 Name.toVector(TmpName);
6384 llvm::GlobalVariable *GV =
6385 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006386 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006387 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006388
Daniel Dunbar948e2582009-02-15 07:36:20 +00006389 for (; begin != end; ++begin)
6390 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6391
Fariborz Jahanianda320092009-01-29 19:24:30 +00006392 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006393 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006394 ObjCTypes.ProtocolnfABIPtrTy));
6395
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006396 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006397 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006398 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006399 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006400 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6401 ProtocolRefs.size()),
6402 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006403
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006404 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006405 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006406 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006407 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006408 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006409 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006410 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006411 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006412 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006413 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006414}
6415
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006416/// GetMethodDescriptionConstant - This routine build following meta-data:
6417/// struct _objc_method {
6418/// SEL _cmd;
6419/// char *method_type;
6420/// char *_imp;
6421/// }
6422
6423llvm::Constant *
6424CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006425 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006426 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006427 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6428 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006429 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006430 if (!Desc[1])
6431 return 0;
6432
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006433 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006434 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006435 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006436}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006437
6438/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6439/// This code gen. amounts to generating code for:
6440/// @code
6441/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6442/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006443///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006444LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006445 CodeGen::CodeGenFunction &CGF,
6446 QualType ObjectTy,
6447 llvm::Value *BaseValue,
6448 const ObjCIvarDecl *Ivar,
6449 unsigned CVRQualifiers) {
6450 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006451 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6452 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6453 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6454 llvm::MDNode::get(VMContext,
6455 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006456 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006457 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006458}
6459
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006460llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006461 CodeGen::CodeGenFunction &CGF,
6462 const ObjCInterfaceDecl *Interface,
6463 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006464 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006465}
6466
John McCallb1e81442011-05-13 23:16:18 +00006467static void appendSelectorForMessageRefTable(std::string &buffer,
6468 Selector selector) {
6469 if (selector.isUnarySelector()) {
6470 buffer += selector.getNameForSlot(0);
6471 return;
6472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006473
John McCallb1e81442011-05-13 23:16:18 +00006474 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6475 buffer += selector.getNameForSlot(i);
6476 buffer += '_';
6477 }
6478}
6479
John McCall944c8432011-05-14 03:10:52 +00006480/// Emit a "v-table" message send. We emit a weak hidden-visibility
6481/// struct, initially containing the selector pointer and a pointer to
6482/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6483/// load and call the function pointer, passing the address of the
6484/// struct as the second parameter. The runtime determines whether
6485/// the selector is currently emitted using vtable dispatch; if so, it
6486/// substitutes a stub function which simply tail-calls through the
6487/// appropriate vtable slot, and if not, it substitues a stub function
6488/// which tail-calls objc_msgSend. Both stubs adjust the selector
6489/// argument to correctly point to the selector.
6490RValue
6491CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6492 ReturnValueSlot returnSlot,
6493 QualType resultType,
6494 Selector selector,
6495 llvm::Value *arg0,
6496 QualType arg0Type,
6497 bool isSuper,
6498 const CallArgList &formalArgs,
6499 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006500 // Compute the actual arguments.
6501 CallArgList args;
6502
John McCall944c8432011-05-14 03:10:52 +00006503 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006504 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006505 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6506 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006507
John McCall944c8432011-05-14 03:10:52 +00006508 // Second argument: a pointer to the message ref structure. Leave
6509 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006510 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6511
6512 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6513
John McCallde5d3c72012-02-17 03:33:10 +00006514 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006515
John McCallcba681a2011-05-14 21:12:11 +00006516 NullReturnState nullReturn;
6517
John McCall944c8432011-05-14 03:10:52 +00006518 // Find the function to call and the mangled name for the message
6519 // ref structure. Using a different mangled name wouldn't actually
6520 // be a problem; it would just be a waste.
6521 //
6522 // The runtime currently never uses vtable dispatch for anything
6523 // except normal, non-super message-sends.
6524 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006525 llvm::Constant *fn = 0;
6526 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006527 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006528 if (isSuper) {
6529 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6530 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006531 } else {
John McCallcba681a2011-05-14 21:12:11 +00006532 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006533 fn = ObjCTypes.getMessageSendStretFixupFn();
6534 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006535 }
John McCallb1e81442011-05-13 23:16:18 +00006536 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6537 fn = ObjCTypes.getMessageSendFpretFixupFn();
6538 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006539 } else {
John McCallb1e81442011-05-13 23:16:18 +00006540 if (isSuper) {
6541 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6542 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006543 } else {
John McCallb1e81442011-05-13 23:16:18 +00006544 fn = ObjCTypes.getMessageSendFixupFn();
6545 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006546 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006547 }
John McCallb1e81442011-05-13 23:16:18 +00006548 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6549 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006550
John McCallb1e81442011-05-13 23:16:18 +00006551 // Append the selector name, except use underscores anywhere we
6552 // would have used colons.
6553 appendSelectorForMessageRefTable(messageRefName, selector);
6554
6555 llvm::GlobalVariable *messageRef
6556 = CGM.getModule().getGlobalVariable(messageRefName);
6557 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006558 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006559 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006560 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006561 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6562 init->getType(),
6563 /*constant*/ false,
6564 llvm::GlobalValue::WeakAnyLinkage,
6565 init,
6566 messageRefName);
6567 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6568 messageRef->setAlignment(16);
6569 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6570 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006571
6572 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006573 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006574 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6575 e = method->param_end(); i != e; ++i) {
6576 const ParmVarDecl *ParamDecl = (*i);
6577 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6578 if (!nullReturn.NullBB)
6579 nullReturn.init(CGF, arg0);
6580 requiresnullCheck = true;
6581 break;
6582 }
6583 }
6584
John McCallb1e81442011-05-13 23:16:18 +00006585 llvm::Value *mref =
6586 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6587
John McCall944c8432011-05-14 03:10:52 +00006588 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006589 args[1].RV = RValue::get(mref);
6590
6591 // Load the function to call from the message ref table.
6592 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6593 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6594
John McCallde5d3c72012-02-17 03:33:10 +00006595 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006596
John McCallde5d3c72012-02-17 03:33:10 +00006597 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006598 return nullReturn.complete(CGF, result, resultType, formalArgs,
6599 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006600}
6601
6602/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006603CodeGen::RValue
6604CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006605 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006606 QualType ResultType,
6607 Selector Sel,
6608 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006609 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006610 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006611 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006612 return isVTableDispatchedSelector(Sel)
6613 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006614 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006615 false, CallArgs, Method)
6616 : EmitMessageSend(CGF, Return, ResultType,
6617 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006618 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006619 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006620}
6621
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006622llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006623CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006624 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6625
Daniel Dunbardfff2302009-03-02 05:18:14 +00006626 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006627 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006628 false, llvm::GlobalValue::ExternalLinkage,
6629 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006630 }
6631
6632 return GV;
6633}
6634
John McCallf85e1932011-06-15 23:02:42 +00006635llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6636 IdentifierInfo *II) {
6637 llvm::GlobalVariable *&Entry = ClassReferences[II];
6638
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006639 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006640 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006641 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006642 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006643 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6644 false, llvm::GlobalValue::InternalLinkage,
6645 ClassGV,
6646 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006647 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006648 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006649 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006650 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006651 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006652 }
John McCallf85e1932011-06-15 23:02:42 +00006653
Benjamin Kramer578faa82011-09-27 21:06:10 +00006654 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006655}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006656
John McCallf85e1932011-06-15 23:02:42 +00006657llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6658 const ObjCInterfaceDecl *ID) {
6659 return EmitClassRefFromId(Builder, ID->getIdentifier());
6660}
6661
6662llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6663 CGBuilderTy &Builder) {
6664 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6665 return EmitClassRefFromId(Builder, II);
6666}
6667
Daniel Dunbar11394522009-04-18 08:51:00 +00006668llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006669CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006670 const ObjCInterfaceDecl *ID) {
6671 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006672
Daniel Dunbar11394522009-04-18 08:51:00 +00006673 if (!Entry) {
6674 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6675 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006676 Entry =
6677 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006678 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006679 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006680 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006681 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006682 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006683 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006684 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006685 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006686 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006687
Benjamin Kramer578faa82011-09-27 21:06:10 +00006688 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006689}
6690
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006691/// EmitMetaClassRef - Return a Value * of the address of _class_t
6692/// meta-data
6693///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006694llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6695 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006696 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6697 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006698 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006699
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006700 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006701 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006702 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006703 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006704 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006705 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006706 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006707 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006708 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006709 ObjCTypes.ClassnfABIPtrTy));
6710
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006711 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006712 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006713
Benjamin Kramer578faa82011-09-27 21:06:10 +00006714 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006715}
6716
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006717/// GetClass - Return a reference to the class for the given interface
6718/// decl.
6719llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6720 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006721 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006722 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6723 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6724 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6725 }
6726
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006727 return EmitClassRef(Builder, ID);
6728}
6729
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006730/// Generates a message send where the super is the receiver. This is
6731/// a message send to self with special delivery semantics indicating
6732/// which class's method should be called.
6733CodeGen::RValue
6734CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006735 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006736 QualType ResultType,
6737 Selector Sel,
6738 const ObjCInterfaceDecl *Class,
6739 bool isCategoryImpl,
6740 llvm::Value *Receiver,
6741 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006742 const CodeGen::CallArgList &CallArgs,
6743 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006744 // ...
6745 // Create and init a super structure; this is a (receiver, class)
6746 // pair we will pass to objc_msgSendSuper.
6747 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006748 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006749
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006750 llvm::Value *ReceiverAsObject =
6751 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6752 CGF.Builder.CreateStore(ReceiverAsObject,
6753 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006754
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006755 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006756 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006757 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006758 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006759 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006760 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006761
Mike Stumpf5408fe2009-05-16 07:57:57 +00006762 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6763 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006764 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006765 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6766 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6767 CGF.Builder.CreateStore(Target,
6768 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006769
John McCall944c8432011-05-14 03:10:52 +00006770 return (isVTableDispatchedSelector(Sel))
6771 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006772 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006773 true, CallArgs, Method)
6774 : EmitMessageSend(CGF, Return, ResultType,
6775 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006776 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006777 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006778}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006779
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006780llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006781 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006782 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006783
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006784 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006785 llvm::Constant *Casted =
6786 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6787 ObjCTypes.SelectorPtrTy);
6788 Entry =
6789 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6790 llvm::GlobalValue::InternalLinkage,
6791 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Michael Gottesman8f98bf92013-02-05 23:08:45 +00006792 Entry->setExternallyInitialized(true);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006793 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006794 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006795 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006796
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006797 if (lval)
6798 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006799 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6800
6801 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6802 llvm::MDNode::get(VMContext,
6803 ArrayRef<llvm::Value*>()));
6804 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006805}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006806/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006807/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006808///
6809void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006810 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006811 llvm::Value *dst,
6812 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006813 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006814 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006815 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006816 assert(Size <= 8 && "does not support size > 8");
6817 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6818 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006819 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6820 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006821 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6822 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006823 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6824 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006825 return;
6826}
6827
6828/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6829/// objc_assign_strongCast (id src, id *dst)
6830///
6831void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006832 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006833 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006834 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006835 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006836 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006837 assert(Size <= 8 && "does not support size > 8");
6838 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006839 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006840 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6841 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006842 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6843 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006844 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006845 src, dst, "weakassign");
6846 return;
6847}
6848
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006849void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006850 CodeGen::CodeGenFunction &CGF,
6851 llvm::Value *DestPtr,
6852 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006853 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006854 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6855 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006856 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006857 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006858 return;
6859}
6860
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006861/// EmitObjCWeakRead - Code gen for loading value of a __weak
6862/// object: objc_read_weak (id *src)
6863///
6864llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006865 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006866 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006867 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006868 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6869 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006870 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006871 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006872 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006873 return read_weak;
6874}
6875
6876/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6877/// objc_assign_weak (id src, id *dst)
6878///
6879void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006880 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006881 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006882 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006883 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006884 assert(Size <= 8 && "does not support size > 8");
6885 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6886 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006887 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6888 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006889 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6890 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006891 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006892 src, dst, "weakassign");
6893 return;
6894}
6895
6896/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6897/// objc_assign_global (id src, id *dst)
6898///
6899void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006900 llvm::Value *src, llvm::Value *dst,
6901 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006902 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006903 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006904 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006905 assert(Size <= 8 && "does not support size > 8");
6906 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6907 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006908 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6909 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006910 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6911 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006912 if (!threadlocal)
6913 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6914 src, dst, "globalassign");
6915 else
6916 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6917 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006918 return;
6919}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006920
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006921void
John McCallf1549f62010-07-06 01:34:17 +00006922CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6923 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006924 EmitAtSynchronizedStmt(CGF, S,
6925 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6926 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006927}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006928
John McCall5a180392010-07-24 00:37:23 +00006929llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006930CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006931 // There's a particular fixed type info for 'id'.
6932 if (T->isObjCIdType() ||
6933 T->isObjCQualifiedIdType()) {
6934 llvm::Constant *IDEHType =
6935 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6936 if (!IDEHType)
6937 IDEHType =
6938 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6939 false,
6940 llvm::GlobalValue::ExternalLinkage,
6941 0, "OBJC_EHTYPE_id");
6942 return IDEHType;
6943 }
6944
6945 // All other types should be Objective-C interface pointer types.
6946 const ObjCObjectPointerType *PT =
6947 T->getAs<ObjCObjectPointerType>();
6948 assert(PT && "Invalid @catch type.");
6949 const ObjCInterfaceType *IT = PT->getInterfaceType();
6950 assert(IT && "Invalid @catch type.");
6951 return GetInterfaceEHType(IT->getDecl(), false);
6952}
6953
John McCallf1549f62010-07-06 01:34:17 +00006954void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6955 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006956 EmitTryCatchStmt(CGF, S,
6957 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6958 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6959 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006960}
6961
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006962/// EmitThrowStmt - Generate code for a throw statement.
6963void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00006964 const ObjCAtThrowStmt &S,
6965 bool ClearInsertionPoint) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006966 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006967 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006968 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006969 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006970 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006971 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006972 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006973 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006974 }
6975
John McCall7ec404c2010-10-16 08:21:07 +00006976 CGF.Builder.CreateUnreachable();
Fariborz Jahanian6a3c70e2013-01-10 19:02:56 +00006977 if (ClearInsertionPoint)
6978 CGF.Builder.ClearInsertionPoint();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006979}
Daniel Dunbare588b992009-03-01 04:46:24 +00006980
John McCall5a180392010-07-24 00:37:23 +00006981llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006982CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006983 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006984 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006985
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006986 // If we don't need a definition, return the entry if found or check
6987 // if we use an external reference.
6988 if (!ForDefinition) {
6989 if (Entry)
6990 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006991
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006992 // If this type (or a super class) has the __objc_exception__
6993 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006994 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006995 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006996 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006997 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006998 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006999 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00007000 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007001 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007002
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007003 // Otherwise we need to either make a new entry or fill in the
7004 // initializer.
7005 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00007006 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00007007 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007008 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00007009 CGM.getModule().getGlobalVariable(VTableName);
7010 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007011 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
7012 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00007013 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00007014 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00007015
Chris Lattner8b418682012-02-07 00:39:47 +00007016 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00007017
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00007018 llvm::Constant *Values[] = {
7019 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
7020 GetClassName(ID->getIdentifier()),
7021 GetClassGlobal(ClassName)
7022 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00007023 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00007024 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00007025
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007026 if (Entry) {
7027 Entry->setInitializer(Init);
7028 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00007029 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007030 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007031 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00007032 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00007033 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007034 }
7035
David Blaikie4e4d0842012-03-11 07:00:24 +00007036 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00007037 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00007038 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00007039 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007040
7041 if (ForDefinition) {
7042 Entry->setSection("__DATA,__objc_const");
7043 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
7044 } else {
7045 Entry->setSection("__DATA,__datacoal_nt,coalesced");
7046 }
Daniel Dunbare588b992009-03-01 04:46:24 +00007047
7048 return Entry;
7049}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007050
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00007051/* *** */
7052
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00007053CodeGen::CGObjCRuntime *
7054CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00007055 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
7056 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00007057 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00007058
7059 case ObjCRuntime::MacOSX:
7060 case ObjCRuntime::iOS:
7061 return new CGObjCNonFragileABIMac(CGM);
7062
David Chisnall11d3f4c2012-07-03 20:49:52 +00007063 case ObjCRuntime::GNUstep:
7064 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00007065 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00007066 llvm_unreachable("these runtimes are not Mac runtimes");
7067 }
7068 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00007069}