blob: 4730416dd18c987dd969ecaa0db6e092c239382a [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-04-22 07:32:20 +000045
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000046namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000047
Daniel Dunbar6bff2512009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +000049
Daniel Dunbar6bff2512009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000052
Fariborz Jahanianee0af742009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000056
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000057private:
John McCall0774cb82011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCallf85e1932011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000074
John McCall0774cb82011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000086 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000087
John McCall0774cb82011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson0032b272009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall0774cb82011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000099
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000101
John McCall0774cb82011-05-15 01:53:33 +0000102 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
103 ///
104 /// The messenger used for super calls, which have different dispatch
105 /// semantics. The class passed is the superclass of the current
106 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000107 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000108 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000110 params, true),
111 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000112 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000113
John McCall0774cb82011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
115 ///
116 /// A slightly different messenger used for super calls. The class
117 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000118 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000119 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000121 params, true),
122 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000123 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000124
John McCall0774cb82011-05-15 01:53:33 +0000125 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
126 /// SEL op, ...)
127 ///
128 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000129 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000130 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000131 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000132 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000133 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000134 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000135
John McCall0774cb82011-05-15 01:53:33 +0000136 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
137 /// SEL op, ...)
138 ///
139 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000140 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000141 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000142 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000143 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000144 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 llvm::Constant *getMessageSendSuperFpretFn() const {
148 // There is no objc_msgSendSuper_fpret? How can that work?
149 return getMessageSendSuperFn();
150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000151
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperFpretFn2() const {
153 // There is no objc_msgSendSuper_fpret? How can that work?
154 return getMessageSendSuperFn2();
155 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000156
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000157protected:
158 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000160public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000161 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
162 llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000164 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000165 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000166
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000167 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000168 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000170 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000171 llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000172 /// ProtocolPtrTy - LLVM type for external protocol handles
173 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000176 // SuperCTy - clang type for struct objc_super.
177 QualType SuperCTy;
178 // SuperPtrCTy - clang type for struct objc_super *.
179 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000180
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000181 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000182 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000183 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000185
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000186 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
187 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000188 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000189
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000190 /// PropertyListTy - LLVM type for struct objc_property_list
191 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000192 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000193 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000194 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000195
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000196 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000197 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000198
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000199 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000200 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000201 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000202 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000203
Chris Lattner72db6c32009-04-22 02:44:54 +0000204 llvm::Constant *getGetPropertyFn() {
205 CodeGen::CodeGenTypes &Types = CGM.getTypes();
206 ASTContext &Ctx = CGM.getContext();
207 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000209 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
210 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000211 Params.push_back(IdType);
212 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000213 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000214 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000215 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000216 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000217 FunctionType::ExtInfo()),
218 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000219 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
220 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000221
Chris Lattner72db6c32009-04-22 02:44:54 +0000222 llvm::Constant *getSetPropertyFn() {
223 CodeGen::CodeGenTypes &Types = CGM.getTypes();
224 ASTContext &Ctx = CGM.getContext();
225 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000227 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
228 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000229 Params.push_back(IdType);
230 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000231 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000232 Params.push_back(IdType);
233 Params.push_back(Ctx.BoolTy);
234 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000235 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000236 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000237 FunctionType::ExtInfo()),
238 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000239 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
240 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000241
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000242
243 llvm::Constant *getCopyStructFn() {
244 CodeGen::CodeGenTypes &Types = CGM.getTypes();
245 ASTContext &Ctx = CGM.getContext();
246 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000247 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000248 Params.push_back(Ctx.VoidPtrTy);
249 Params.push_back(Ctx.VoidPtrTy);
250 Params.push_back(Ctx.LongTy);
251 Params.push_back(Ctx.BoolTy);
252 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000253 llvm::FunctionType *FTy =
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000254 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
255 FunctionType::ExtInfo()),
256 false);
257 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
258 }
259
Chris Lattner72db6c32009-04-22 02:44:54 +0000260 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000261 CodeGen::CodeGenTypes &Types = CGM.getTypes();
262 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000263 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000265 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000266 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000267 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000268 FunctionType::ExtInfo()),
269 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
271 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000273 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000274 llvm::Constant *getGcReadWeakFn() {
275 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000276 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000277 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000278 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000280 }
281
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000282 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000283 llvm::Constant *getGcAssignWeakFn() {
284 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000285 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000286 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000287 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000288 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
289 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000290
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000291 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000292 llvm::Constant *getGcAssignGlobalFn() {
293 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000294 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000295 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000296 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000297 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
298 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000299
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000300 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
301 llvm::Constant *getGcAssignThreadLocalFn() {
302 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000303 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000304 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000305 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000306 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
307 }
308
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000309 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000310 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000311 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000312 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
313 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000314 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000315 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000316 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
317 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000318
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000319 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
320 llvm::Constant *GcMemmoveCollectableFn() {
321 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000322 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000323 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000324 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000326
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000327 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000328 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000329 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000330 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000331 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000332 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000333 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
334 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000335
336 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000337 llvm::Constant *getExceptionThrowFn() {
338 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000339 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000340 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000341 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000344
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000345 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
346 llvm::Constant *getExceptionRethrowFn() {
347 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000348 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
350 }
351
Daniel Dunbar1c566672009-02-24 01:43:46 +0000352 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000353 llvm::Constant *getSyncEnterFn() {
354 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000355 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000356 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
359 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360
Daniel Dunbar1c566672009-02-24 01:43:46 +0000361 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000362 llvm::Constant *getSyncExitFn() {
363 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000364 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000365 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000366 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
368 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000369
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000370 llvm::Constant *getSendFn(bool IsSuper) const {
371 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000374 llvm::Constant *getSendFn2(bool IsSuper) const {
375 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000377
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000378 llvm::Constant *getSendStretFn(bool IsSuper) const {
379 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000382 llvm::Constant *getSendStretFn2(bool IsSuper) const {
383 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000385
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFpretFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
388 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000389
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000393
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000394 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
395 ~ObjCCommonTypesHelper(){}
396};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000397
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000398/// ObjCTypesHelper - Helper class that encapsulates lazy
399/// construction of varies types used during ObjC generation.
400class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000401public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000403 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000404 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000406 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000407 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000408
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000409 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000410 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000411 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000412 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000415 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000416 /// ProtocolExtensionTy - LLVM type for struct
417 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000418 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000419 /// MethodDescriptionTy - LLVM type for struct
420 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000421 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000422 /// MethodDescriptionListTy - LLVM type for struct
423 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000424 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000425 /// MethodDescriptionListPtrTy - LLVM type for struct
426 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000427 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000428 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000429 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000430 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000431 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000432 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000433 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000435 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000436 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000437 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000438 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000439 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000440 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000441 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000442 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000443 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000444 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000445 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000446 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000448 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000449 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000450 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000451 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Anders Carlsson124526b2008-09-09 10:10:21 +0000453 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000454 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000455
Anders Carlsson124526b2008-09-09 10:10:21 +0000456 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000457 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000458 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000459 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000460 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000461 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000462 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000463
464 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000465 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000466 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000467 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000468 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000469 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000470 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000471
472 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000473 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000474 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000475 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000476 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000477 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000478 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000479
Anders Carlsson124526b2008-09-09 10:10:21 +0000480 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000482 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000483 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485 "objc_exception_match");
486
Chris Lattner34b02a12009-04-22 02:26:14 +0000487 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000488
Anders Carlsson124526b2008-09-09 10:10:21 +0000489 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000490 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000491 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000493 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
494 params, false),
495 "_setjmp");
Chris Lattner34b02a12009-04-22 02:26:14 +0000496 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000497
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000501};
502
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000503/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000504/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000505class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000506public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000508 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000510
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000513
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000514 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000516
Daniel Dunbar948e2582009-02-15 07:36:20 +0000517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000519
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000522
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000525
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000526 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000527 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000528
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000529 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000531
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000532 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000533 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000534
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000535 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000537
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000539 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000541 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000546
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000547 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000548 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000549
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000550 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000551
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
554 // IMP messenger;
555 // SEL name;
556 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000562 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000565
Fariborz Jahanianef163782009-02-05 01:13:09 +0000566 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
572 // SEL name;
573 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000577 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000578
Chris Lattner1c02f862009-04-22 02:53:24 +0000579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000581 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000583 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000584 "objc_msgSend_fixup");
585 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000586
Chris Lattner1c02f862009-04-22 02:53:24 +0000587 llvm::Constant *getMessageSendFpretFixupFn() {
588 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000589 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000590 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000591 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000592 "objc_msgSend_fpret_fixup");
593 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000594
Chris Lattner1c02f862009-04-22 02:53:24 +0000595 llvm::Constant *getMessageSendStretFixupFn() {
596 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000597 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000599 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000600 "objc_msgSend_stret_fixup");
601 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Chris Lattner1c02f862009-04-22 02:53:24 +0000603 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000605 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000608 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000609 "objc_msgSendSuper2_fixup");
610 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Chris Lattner1c02f862009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000614 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000615 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000616 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000617 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000618 "objc_msgSendSuper2_stret_fixup");
619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Chris Lattner8a569112009-04-22 02:15:23 +0000621 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000623 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Chris Lattner8a569112009-04-22 02:15:23 +0000625 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Chris Lattner8a569112009-04-22 02:15:23 +0000627 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000628 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000629 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000630 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000631 "objc_begin_catch");
632 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000633
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000634 llvm::StructType *EHTypeTy;
635 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000636
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000637 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
638 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000639};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000641class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000642public:
643 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000644 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000645 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000646 unsigned ivar_bytepos;
647 unsigned ivar_size;
648 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000650
651 // Allow sorting based on byte pos.
652 bool operator<(const GC_IVAR &b) const {
653 return ivar_bytepos < b.ivar_bytepos;
654 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000655 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000657 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000658 public:
659 unsigned skip;
660 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000662 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000663 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000665protected:
666 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000667 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000668 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000669 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000671 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000672 SmallVector<GC_IVAR, 16> SkipIvars;
673 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000675 /// LazySymbols - Symbols to generate a lazy reference for. See
676 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000677 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000678
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000679 /// DefinedSymbols - External symbols which are defined by this
680 /// module. The symbols in this list and LazySymbols are used to add
681 /// special linker symbols which ensure that Objective-C modules are
682 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000683 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000685 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000686 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000687
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000688 /// MethodVarNames - uniqued method variable names.
689 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000690
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000691 /// DefinedCategoryNames - list of category names in form Class_Category.
692 llvm::SetVector<std::string> DefinedCategoryNames;
693
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000694 /// MethodVarTypes - uniqued method type signatures. We have to use
695 /// a StringMap here because have no other unique reference.
696 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000698 /// MethodDefinitions - map of methods which have been defined in
699 /// this translation unit.
700 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000701
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000702 /// PropertyNames - uniqued method variable names.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000705 /// ClassReferences - uniqued class references.
706 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000707
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000708 /// SelectorReferences - uniqued selector references.
709 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000710
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000711 /// Protocols - Protocols for which an objc_protocol structure has
712 /// been emitted. Forward declarations are handled by creating an
713 /// empty structure whose initializer is filled in when/if defined.
714 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000716 /// DefinedProtocols - Protocols which have actually been
717 /// defined. We should not need this, see FIXME in GenerateProtocol.
718 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000720 /// DefinedClasses - List of defined classes.
721 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000722
723 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
724 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000725
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000726 /// DefinedCategories - List of defined categories.
727 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000728
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000729 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
730 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000732 /// GetNameForMethod - Return a name for the given method.
733 /// \param[out] NameOut - The return value.
734 void GetNameForMethod(const ObjCMethodDecl *OMD,
735 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000736 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000737
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000738 /// GetMethodVarName - Return a unique constant for the given
739 /// selector's name. The return value has type char *.
740 llvm::Constant *GetMethodVarName(Selector Sel);
741 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000743 /// GetMethodVarType - Return a unique constant for the given
744 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000745
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000746 // FIXME: This is a horrible name.
747 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000748 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000750 /// GetPropertyName - Return a unique constant for the given
751 /// name. The return value has type char *.
752 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000754 // FIXME: This can be dropped once string functions are unified.
755 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
756 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000758 /// GetClassName - Return a unique constant for the given selector's
759 /// name. The return value has type char *.
760 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000762 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
763
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000764 /// BuildIvarLayout - Builds ivar layout bitmap for the class
765 /// implementation for the __strong or __weak case.
766 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000767 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
768 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000769
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000770 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000771
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000772 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773 unsigned int BytePos, bool ForStrongLayout,
774 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000775 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000776 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000777 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000778 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000779 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000780 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000781
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000782 /// GetIvarLayoutName - Returns a unique constant for the given
783 /// ivar layout bitmap.
784 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
785 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000786
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000787 /// EmitPropertyList - Emit the given property list. The return
788 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000789 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000790 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000791 const ObjCContainerDecl *OCD,
792 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000794 /// PushProtocolProperties - Push protocol's property on the input stack.
795 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
796 std::vector<llvm::Constant*> &Properties,
797 const Decl *Container,
798 const ObjCProtocolDecl *PROTO,
799 const ObjCCommonTypesHelper &ObjCTypes);
800
Fariborz Jahanianda320092009-01-29 19:24:30 +0000801 /// GetProtocolRef - Return a reference to the internal protocol
802 /// description, creating an empty one if it has not been
803 /// defined. The return value has type ProtocolPtrTy.
804 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000805
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000806 /// CreateMetadataVar - Create a global variable with internal
807 /// linkage for use by the Objective-C runtime.
808 ///
809 /// This is a convenience wrapper which not only creates the
810 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000811 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000812 ///
813 /// \param Name - The variable name.
814 /// \param Init - The variable initializer; this is also used to
815 /// define the type of the variable.
816 /// \param Section - The section the variable should go into, or 0.
817 /// \param Align - The alignment for the variable, or 0.
818 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000819 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000820 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000821 llvm::Constant *Init,
822 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000823 unsigned Align,
824 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000825
John McCall944c8432011-05-14 03:10:52 +0000826 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
827 ReturnValueSlot Return,
828 QualType ResultType,
829 llvm::Value *Sel,
830 llvm::Value *Arg0,
831 QualType Arg0Ty,
832 bool IsSuper,
833 const CallArgList &CallArgs,
834 const ObjCMethodDecl *OMD,
835 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000836
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000837 /// EmitImageInfo - Emit the image info marker used to encode some module
838 /// level information.
839 void EmitImageInfo();
840
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000841public:
Owen Anderson69243822009-07-13 04:10:07 +0000842 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000843 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
David Chisnall0d13f6f2010-01-23 02:40:42 +0000845 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000847 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
848 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Fariborz Jahanianda320092009-01-29 19:24:30 +0000850 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Fariborz Jahanianda320092009-01-29 19:24:30 +0000852 /// GetOrEmitProtocol - Get the protocol object for the given
853 /// declaration, emitting it if necessary. The return value has type
854 /// ProtocolPtrTy.
855 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000856
Fariborz Jahanianda320092009-01-29 19:24:30 +0000857 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
858 /// object for the given declaration, emitting it if needed. These
859 /// forward references will be filled in with empty bodies if no
860 /// definition is seen. The return value has type ProtocolPtrTy.
861 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000862 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
863 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000864
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000865};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000867class CGObjCMac : public CGObjCCommonMac {
868private:
869 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000870
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000871 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000873 void EmitModuleInfo();
874
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000875 /// EmitModuleSymols - Emit module symbols, the list of defined
876 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000877 llvm::Constant *EmitModuleSymbols();
878
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000879 /// FinishModule - Write out global data structures at the end of
880 /// processing a translation unit.
881 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000882
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000883 /// EmitClassExtension - Generate the class extension structure used
884 /// to store the weak ivar layout and properties. The return value
885 /// has type ClassExtensionPtrTy.
886 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
887
888 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
889 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000891 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000892
John McCallf85e1932011-06-15 23:02:42 +0000893 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
894 IdentifierInfo *II);
895
896 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
897
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000898 /// EmitSuperClassRef - Emits reference to class's main metadata class.
899 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000900
901 /// EmitIvarList - Emit the ivar list for the given
902 /// implementation. If ForClass is true the list of class ivars
903 /// (i.e. metaclass ivars) is emitted, otherwise the list of
904 /// interface ivars will be emitted. The return value has type
905 /// IvarListPtrTy.
906 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000907 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000909 /// EmitMetaClass - Emit a forward reference to the class structure
910 /// for the metaclass of the given interface. The return value has
911 /// type ClassPtrTy.
912 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
913
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000914 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000915 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000916 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
917 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000918 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000920 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000921
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000922 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000923
924 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000925 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000926 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000927 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000928 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000929
930 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000932 /// - TypeName: The name for the type containing the methods.
933 /// - IsProtocol: True iff these methods are for a protocol.
934 /// - ClassMethds: True iff these are class methods.
935 /// - Required: When true, only "required" methods are
936 /// listed. Similarly, when false only "optional" methods are
937 /// listed. For classes this should always be true.
938 /// - begin, end: The method list to output.
939 ///
940 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000941 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000942 const char *Section,
943 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000944
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000945 /// GetOrEmitProtocol - Get the protocol object for the given
946 /// declaration, emitting it if necessary. The return value has type
947 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000948 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000949
950 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
951 /// object for the given declaration, emitting it if needed. These
952 /// forward references will be filled in with empty bodies if no
953 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000954 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000955
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000956 /// EmitProtocolExtension - Generate the protocol extension
957 /// structure used to store optional instance and class methods, and
958 /// protocol properties. The return value has type
959 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000960 llvm::Constant *
961 EmitProtocolExtension(const ObjCProtocolDecl *PD,
962 const ConstantVector &OptInstanceMethods,
963 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000964
965 /// EmitProtocolList - Generate the list of referenced
966 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000967 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +0000968 ObjCProtocolDecl::protocol_iterator begin,
969 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000970
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000971 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
972 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000973 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
974 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000975
976public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000977 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000978
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000979 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000980
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000981 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000982 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000983 QualType ResultType,
984 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000985 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000986 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000987 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000988 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000989
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000990 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000991 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000992 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000993 QualType ResultType,
994 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000995 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000996 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000997 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000998 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000999 const CallArgList &CallArgs,
1000 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001001
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001002 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001003 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001004
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001005 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1006 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001007
1008 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1009 /// untyped one.
1010 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1011 const ObjCMethodDecl *Method);
1012
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001013 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001014
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001015 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001016
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001017 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001019 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001020 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001021
Chris Lattner74391b42009-03-22 21:03:39 +00001022 virtual llvm::Constant *GetPropertyGetFunction();
1023 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001024 virtual llvm::Constant *GetGetStructFunction();
1025 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001026 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027
John McCallf1549f62010-07-06 01:34:17 +00001028 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1029 const ObjCAtTryStmt &S);
1030 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1031 const ObjCAtSynchronizedStmt &S);
1032 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001033 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1034 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001035 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001036 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001037 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001038 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001039 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001040 llvm::Value *src, llvm::Value *dest,
1041 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001042 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001043 llvm::Value *src, llvm::Value *dest,
1044 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001045 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001047 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1048 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001049 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001050
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001051 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1052 QualType ObjectTy,
1053 llvm::Value *BaseValue,
1054 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001055 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001056 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001057 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001058 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001059
1060 /// GetClassGlobal - Return the global variable for the Objective-C
1061 /// class of the given name.
1062 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001063 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001064 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001065};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001066
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001067class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001068private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001069 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001070 llvm::GlobalVariable* ObjCEmptyCacheVar;
1071 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001072
Daniel Dunbar11394522009-04-18 08:51:00 +00001073 /// SuperClassReferences - uniqued super class references.
1074 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001075
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001076 /// MetaClassReferences - uniqued meta class references.
1077 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001078
1079 /// EHTypeReferences - uniqued class ehtype references.
1080 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001081
John McCall944c8432011-05-14 03:10:52 +00001082 /// VTableDispatchMethods - List of methods for which we generate
1083 /// vtable-based message dispatch.
1084 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001085
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001086 /// DefinedMetaClasses - List of defined meta-classes.
1087 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1088
John McCall944c8432011-05-14 03:10:52 +00001089 /// isVTableDispatchedSelector - Returns true if SEL is a
1090 /// vtable-based selector.
1091 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001092
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001093 /// FinishNonFragileABIModule - Write out global data structures at the end of
1094 /// processing a translation unit.
1095 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001096
Daniel Dunbar463b8762009-05-15 21:48:48 +00001097 /// AddModuleClassList - Add the given list of class pointers to the
1098 /// module with the provided symbol and section names.
1099 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1100 const char *SymbolName,
1101 const char *SectionName);
1102
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1104 unsigned InstanceStart,
1105 unsigned InstanceSize,
1106 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001107 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001108 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001109 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001110 llvm::Constant *ClassRoGV,
1111 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001113 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001114
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001115 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001117 /// EmitMethodList - Emit the method list for the given
1118 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001119 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001120 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001121 const ConstantVector &Methods);
1122 /// EmitIvarList - Emit the ivar list for the given
1123 /// implementation. If ForClass is true the list of class ivars
1124 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1125 /// interface ivars will be emitted. The return value has type
1126 /// IvarListnfABIPtrTy.
1127 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001128
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001129 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001130 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001131 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001132
Fariborz Jahanianda320092009-01-29 19:24:30 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
1136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001137
Fariborz Jahanianda320092009-01-29 19:24:30 +00001138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
1142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001143
Fariborz Jahanianda320092009-01-29 19:24:30 +00001144 /// EmitProtocolList - Generate the list of referenced
1145 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001146 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001147 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001148 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
John McCall944c8432011-05-14 03:10:52 +00001150 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1151 ReturnValueSlot Return,
1152 QualType ResultType,
1153 Selector Sel,
1154 llvm::Value *Receiver,
1155 QualType Arg0Ty,
1156 bool IsSuper,
1157 const CallArgList &CallArgs,
1158 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001159
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001160 /// GetClassGlobal - Return the global variable for the Objective-C
1161 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001162 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001163
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001164 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001165 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001166 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001167 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001168
1169 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1170 IdentifierInfo *II);
1171
1172 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001173
Daniel Dunbar11394522009-04-18 08:51:00 +00001174 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1175 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1177 const ObjCInterfaceDecl *ID);
1178
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001179 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1180 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001182 const ObjCInterfaceDecl *ID);
1183
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001184 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1185 /// the given ivar.
1186 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001187 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001188 const ObjCInterfaceDecl *ID,
1189 const ObjCIvarDecl *Ivar);
1190
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001191 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1192 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001193 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1194 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001195
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001196 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001197 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001198 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001199 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001200
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001201 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001202 return "OBJC_METACLASS_$_";
1203 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001204
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001205 const char *getClassSymbolPrefix() const {
1206 return "OBJC_CLASS_$_";
1207 }
1208
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001209 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001210 uint32_t &InstanceStart,
1211 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001212
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001213 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001214 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001215 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1216 return CGM.getContext().Selectors.getSelector(0, &II);
1217 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001219 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001220 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1221 return CGM.getContext().Selectors.getSelector(1, &II);
1222 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001223
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001224 /// ImplementationIsNonLazy - Check whether the given category or
1225 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001226 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001227
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001228public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001229 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001230 // FIXME. All stubs for now!
1231 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001232
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001233 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001234 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001235 QualType ResultType,
1236 Selector Sel,
1237 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001238 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001239 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001240 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
1242 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001243 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001244 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001245 QualType ResultType,
1246 Selector Sel,
1247 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001248 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001249 llvm::Value *Receiver,
1250 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001251 const CallArgList &CallArgs,
1252 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001253
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001254 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001255 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001257 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1258 bool lvalue = false)
1259 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001260
1261 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1262 /// untyped one.
1263 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1264 const ObjCMethodDecl *Method)
1265 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001266
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001267 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001269 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001270 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001271 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001272
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001273 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001274
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001275 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001276 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001277 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001278 virtual llvm::Constant *GetPropertySetFunction() {
1279 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001280 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001281
David Chisnall8fac25d2010-12-26 22:13:16 +00001282 virtual llvm::Constant *GetSetStructFunction() {
1283 return ObjCTypes.getCopyStructFn();
1284 }
1285 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001286 return ObjCTypes.getCopyStructFn();
1287 }
1288
Chris Lattner74391b42009-03-22 21:03:39 +00001289 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001290 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001291 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
John McCallf1549f62010-07-06 01:34:17 +00001293 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1294 const ObjCAtTryStmt &S);
1295 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1296 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001297 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001298 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001299 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001300 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001301 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001302 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001303 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001304 llvm::Value *src, llvm::Value *dest,
1305 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001306 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001307 llvm::Value *src, llvm::Value *dest,
1308 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001309 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001310 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001311 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1312 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001313 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001314 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1315 QualType ObjectTy,
1316 llvm::Value *BaseValue,
1317 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001318 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001319 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001320 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001321 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001322};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001323
John McCallcba681a2011-05-14 21:12:11 +00001324/// A helper class for performing the null-initialization of a return
1325/// value.
1326struct NullReturnState {
1327 llvm::BasicBlock *NullBB;
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001328 llvm::BasicBlock *callBB;
1329 NullReturnState() : NullBB(0), callBB(0) {}
John McCallcba681a2011-05-14 21:12:11 +00001330
1331 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1332 // Make blocks for the null-init and call edges.
1333 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001334 callBB = CGF.createBasicBlock("msgSend.call");
John McCallcba681a2011-05-14 21:12:11 +00001335
1336 // Check for a null receiver and, if there is one, jump to the
1337 // null-init test.
1338 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1339 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1340
1341 // Otherwise, start performing the call.
1342 CGF.EmitBlock(callBB);
1343 }
1344
1345 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1346 if (!NullBB) return result;
1347
1348 // Finish the call path.
1349 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1350 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1351
1352 // Emit the null-init block and perform the null-initialization there.
1353 CGF.EmitBlock(NullBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001354 if (!resultType->isAnyComplexType()) {
1355 assert(result.isAggregate() && "null init of non-aggregate result?");
1356 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1357 // Jump to the continuation block.
1358 CGF.EmitBlock(contBB);
1359 return result;
1360 }
John McCallcba681a2011-05-14 21:12:11 +00001361
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001362 // _Complex type
1363 // FIXME. Now easy to handle any other scalar type whose result is returned
1364 // in memory due to ABI limitations.
John McCallcba681a2011-05-14 21:12:11 +00001365 CGF.EmitBlock(contBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001366 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1367 llvm::Type *MemberType = CallCV.first->getType();
1368 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1369 // Create phi instruction for scalar complex value.
1370 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1371 PHIReal->addIncoming(ZeroCV, NullBB);
1372 PHIReal->addIncoming(CallCV.first, callBB);
1373 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1374 PHIImag->addIncoming(ZeroCV, NullBB);
1375 PHIImag->addIncoming(CallCV.second, callBB);
1376 return RValue::getComplex(PHIReal, PHIImag);
John McCallcba681a2011-05-14 21:12:11 +00001377 }
1378};
1379
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001380} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001381
1382/* *** Helper Functions *** */
1383
1384/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001385static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001386 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001387 unsigned idx0,
1388 unsigned idx1) {
1389 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001390 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1391 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001392 };
Jay Foada5c04342011-07-21 14:31:17 +00001393 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001394}
1395
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001396/// hasObjCExceptionAttribute - Return true if this class or any super
1397/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001398static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001399 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001400 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001401 return true;
1402 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001403 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001404 return false;
1405}
1406
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001407/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001409CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001410 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001411 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001412 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001413}
1414
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001415/// GetClass - Return a reference to the class for the given interface
1416/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001417llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001418 const ObjCInterfaceDecl *ID) {
1419 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001420}
1421
1422/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001423llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1424 bool lval) {
1425 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001426}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001427llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001428 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001429 return EmitSelector(Builder, Method->getSelector());
1430}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001431
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001432llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001433 if (T->isObjCIdType() ||
1434 T->isObjCQualifiedIdType()) {
1435 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001436 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001437 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001438 if (T->isObjCClassType() ||
1439 T->isObjCQualifiedClassType()) {
1440 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001441 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001442 }
1443 if (T->isObjCObjectPointerType())
1444 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1445
John McCall5a180392010-07-24 00:37:23 +00001446 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1447 return 0;
1448}
1449
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001450/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001451/*
1452 struct __builtin_CFString {
1453 const int *isa; // point to __CFConstantStringClassReference
1454 int flags;
1455 const char *str;
1456 long length;
1457 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001458*/
1459
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001460/// or Generate a constant NSString object.
1461/*
1462 struct __builtin_NSString {
1463 const int *isa; // point to __NSConstantStringClassReference
1464 const char *str;
1465 unsigned int length;
1466 };
1467*/
1468
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001469llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001470 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001471 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1472 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001473 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001474}
1475
1476/// Generates a message send where the super is the receiver. This is
1477/// a message send to self with special delivery semantics indicating
1478/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001479CodeGen::RValue
1480CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001481 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001482 QualType ResultType,
1483 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001484 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001485 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001486 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001487 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001488 const CodeGen::CallArgList &CallArgs,
1489 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001490 // Create and init a super structure; this is a (receiver, class)
1491 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001492 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001493 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001494 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001495 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001496 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001497 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001498
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001499 // If this is a class message the metaclass is passed as the target.
1500 llvm::Value *Target;
1501 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001502 if (isCategoryImpl) {
1503 // Message sent to 'super' in a class method defined in a category
1504 // implementation requires an odd treatment.
1505 // If we are in a class method, we must retrieve the
1506 // _metaclass_ for the current class, pointed at by
1507 // the class's "isa" pointer. The following assumes that
1508 // isa" is the first ivar in a class (which it must be).
1509 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1510 Target = CGF.Builder.CreateStructGEP(Target, 0);
1511 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001512 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001513 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1514 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1515 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1516 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001517 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001518 }
1519 else if (isCategoryImpl)
1520 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1521 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001522 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1523 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1524 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001525 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001526 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1527 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001528 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001529 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001530 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001531 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001532 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001533 return EmitMessageSend(CGF, Return, ResultType,
1534 EmitSelector(CGF.Builder, Sel),
1535 ObjCSuper, ObjCTypes.SuperPtrCTy,
1536 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001537}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001538
1539/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001540CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001541 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001542 QualType ResultType,
1543 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001544 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001545 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001546 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001547 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001548 return EmitMessageSend(CGF, Return, ResultType,
1549 EmitSelector(CGF.Builder, Sel),
1550 Receiver, CGF.getContext().getObjCIdType(),
1551 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001552}
1553
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001554CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001555CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1556 ReturnValueSlot Return,
1557 QualType ResultType,
1558 llvm::Value *Sel,
1559 llvm::Value *Arg0,
1560 QualType Arg0Ty,
1561 bool IsSuper,
1562 const CallArgList &CallArgs,
1563 const ObjCMethodDecl *Method,
1564 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001565 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001566 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001567 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001568 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1569 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001570 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001571
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001572 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001573 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001574 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001575 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001576 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001577
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001578 if (Method)
1579 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1580 CGM.getContext().getCanonicalType(ResultType) &&
1581 "Result type mismatch!");
1582
John McCallcba681a2011-05-14 21:12:11 +00001583 NullReturnState nullReturn;
1584
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001585 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001586 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallcba681a2011-05-14 21:12:11 +00001587 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001588 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001589 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001590 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1591 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1592 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001593 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001594 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001595 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001596 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001597 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001598 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1599 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001600}
1601
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001602static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1603 if (FQT.isObjCGCStrong())
1604 return Qualifiers::Strong;
1605
John McCallf85e1932011-06-15 23:02:42 +00001606 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001607 return Qualifiers::Weak;
1608
1609 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1610 return Qualifiers::Strong;
1611
1612 if (const PointerType *PT = FQT->getAs<PointerType>())
1613 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1614
1615 return Qualifiers::GCNone;
1616}
1617
John McCall6b5a61b2011-02-07 10:33:21 +00001618llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1619 const CGBlockInfo &blockInfo) {
1620 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001621 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001622
Douglas Gregore289d812011-09-13 17:21:33 +00001623 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001624 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001625 return nullPtr;
1626
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001627 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001628 SkipIvars.clear();
1629 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001630 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1631 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001632
Fariborz Jahanian81979822010-09-09 00:21:45 +00001633 // __isa is the first field in block descriptor and must assume by runtime's
1634 // convention that it is GC'able.
1635 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001636
1637 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1638
1639 // Calculate the basic layout of the block structure.
1640 const llvm::StructLayout *layout =
1641 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1642
1643 // Ignore the optional 'this' capture: C++ objects are not assumed
1644 // to be GC'ed.
1645
1646 // Walk the captured variables.
1647 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1648 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1649 const VarDecl *variable = ci->getVariable();
1650 QualType type = variable->getType();
1651
1652 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1653
1654 // Ignore constant captures.
1655 if (capture.isConstant()) continue;
1656
1657 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1658
1659 // __block variables are passed by their descriptor address.
1660 if (ci->isByRef()) {
1661 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001662 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001663 }
1664
1665 assert(!type->isArrayType() && "array variable should not be caught");
1666 if (const RecordType *record = type->getAs<RecordType>()) {
1667 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001668 continue;
1669 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001670
John McCall6b5a61b2011-02-07 10:33:21 +00001671 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1672 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1673
1674 if (GCAttr == Qualifiers::Strong)
1675 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1676 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001677 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001678 SkipIvars.push_back(GC_IVAR(fieldOffset,
1679 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001680 }
1681
1682 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001683 return nullPtr;
1684
1685 // Sort on byte position; captures might not be allocated in order,
1686 // and unions can do funny things.
1687 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1688 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001689
1690 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001691 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001692 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1693 printf("\n block variable layout for block: ");
1694 const unsigned char *s = (unsigned char*)BitMap.c_str();
1695 for (unsigned i = 0; i < BitMap.size(); i++)
1696 if (!(s[i] & 0xf0))
1697 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1698 else
1699 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1700 printf("\n");
1701 }
1702
1703 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001704}
1705
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001706llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001707 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001708 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001709 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001710 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1711
Owen Anderson3c4972d2009-07-29 18:54:39 +00001712 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001713 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001714}
1715
Fariborz Jahanianda320092009-01-29 19:24:30 +00001716void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001717 // FIXME: We shouldn't need this, the protocol decl should contain enough
1718 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001719 DefinedProtocols.insert(PD->getIdentifier());
1720
1721 // If we have generated a forward reference to this protocol, emit
1722 // it now. Otherwise do nothing, the protocol objects are lazily
1723 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001724 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001725 GetOrEmitProtocol(PD);
1726}
1727
Fariborz Jahanianda320092009-01-29 19:24:30 +00001728llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001729 if (DefinedProtocols.count(PD->getIdentifier()))
1730 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001731
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001732 return GetOrEmitProtocolRef(PD);
1733}
1734
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001736// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1737struct _objc_protocol {
1738struct _objc_protocol_extension *isa;
1739char *protocol_name;
1740struct _objc_protocol_list *protocol_list;
1741struct _objc__method_prototype_list *instance_methods;
1742struct _objc__method_prototype_list *class_methods
1743};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001744
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001745See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001746*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001747llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1748 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1749
1750 // Early exit if a defining object has already been generated.
1751 if (Entry && Entry->hasInitializer())
1752 return Entry;
1753
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001754 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001755 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001756 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1757
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001758 // Construct method lists.
1759 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1760 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001761 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001762 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001763 ObjCMethodDecl *MD = *i;
1764 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001765 if (!C)
1766 return GetOrEmitProtocolRef(PD);
1767
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001768 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1769 OptInstanceMethods.push_back(C);
1770 } else {
1771 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001772 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001773 }
1774
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001775 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001776 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001777 ObjCMethodDecl *MD = *i;
1778 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001779 if (!C)
1780 return GetOrEmitProtocolRef(PD);
1781
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001782 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1783 OptClassMethods.push_back(C);
1784 } else {
1785 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001786 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001787 }
1788
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001789 llvm::Constant *Values[] = {
1790 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods),
1791 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001792 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001793 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001794 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001795 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001796 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001797 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001798 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001799 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001800 ClassMethods)
1801 };
Owen Anderson08e25242009-07-27 22:29:56 +00001802 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001803 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001804
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001805 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001806 // Already created, fix the linkage and update the initializer.
1807 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001808 Entry->setInitializer(Init);
1809 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001810 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001811 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001812 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001814 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001815 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001816 // FIXME: Is this necessary? Why only for protocol?
1817 Entry->setAlignment(4);
1818 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001819 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001820
1821 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822}
1823
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001824llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001825 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1826
1827 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001828 // We use the initializer as a marker of whether this is a forward
1829 // reference or not. At module finalization we add the empty
1830 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001831 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001832 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001833 llvm::GlobalValue::ExternalLinkage,
1834 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001835 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001836 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001837 // FIXME: Is this necessary? Why only for protocol?
1838 Entry->setAlignment(4);
1839 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001840
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001841 return Entry;
1842}
1843
1844/*
1845 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 uint32_t size;
1847 struct objc_method_description_list *optional_instance_methods;
1848 struct objc_method_description_list *optional_class_methods;
1849 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001850 };
1851*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001852llvm::Constant *
1853CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1854 const ConstantVector &OptInstanceMethods,
1855 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001856 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001857 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001858 llvm::Constant *Values[] = {
1859 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001860 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001861 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001862 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001863 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001864 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001865 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001866 OptClassMethods),
1867 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
1868 ObjCTypes)
1869 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001871 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001872 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001874 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001875
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001876 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001877 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001878
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001879 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001880 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001881 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001882 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001883}
1884
1885/*
1886 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001887 struct objc_protocol_list *next;
1888 long count;
1889 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001890 };
1891*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001892llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00001893CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001894 ObjCProtocolDecl::protocol_iterator begin,
1895 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001896 std::vector<llvm::Constant*> ProtocolRefs;
1897
Daniel Dunbardbc93372008-08-21 21:57:41 +00001898 for (; begin != end; ++begin)
1899 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001900
1901 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001902 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001903 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001904
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001905 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001906 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001907
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001908 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001909 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001910 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001911 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001912 ProtocolRefs.size() - 1);
1913 Values[2] =
1914 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1915 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001916 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001917
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001918 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001919 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001920 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001921 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001922 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923}
1924
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001925void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1926 std::vector<llvm::Constant*> &Properties,
1927 const Decl *Container,
1928 const ObjCProtocolDecl *PROTO,
1929 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001930 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1931 E = PROTO->protocol_end(); P != E; ++P)
1932 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1933 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1934 E = PROTO->prop_end(); I != E; ++I) {
1935 const ObjCPropertyDecl *PD = *I;
1936 if (!PropertySet.insert(PD->getIdentifier()))
1937 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001938 llvm::Constant *Prop[] = {
1939 GetPropertyName(PD->getIdentifier()),
1940 GetPropertyTypeString(PD, Container)
1941 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001942 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1943 }
1944}
1945
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001946/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001947 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001948 const char * const name;
1949 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001950 };
1951
1952 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001953 uint32_t entsize; // sizeof (struct _objc_property)
1954 uint32_t prop_count;
1955 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001956 };
1957*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00001958llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001959 const Decl *Container,
1960 const ObjCContainerDecl *OCD,
1961 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001962 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001963 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001964 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1965 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001966 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001967 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001968 llvm::Constant *Prop[] = {
1969 GetPropertyName(PD->getIdentifier()),
1970 GetPropertyTypeString(PD, Container)
1971 };
Owen Anderson08e25242009-07-27 22:29:56 +00001972 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001973 Prop));
1974 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001975 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001976 for (ObjCInterfaceDecl::all_protocol_iterator
1977 P = OID->all_referenced_protocol_begin(),
1978 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001979 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1980 ObjCTypes);
1981 }
1982 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1983 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1984 E = CD->protocol_end(); P != E; ++P)
1985 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1986 ObjCTypes);
1987 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001988
1989 // Return null for empty list.
1990 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001991 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001992
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001993 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001994 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001995 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001996 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1997 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001998 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001999 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002000 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002001 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002002
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002003 llvm::GlobalVariable *GV =
2004 CreateMetadataVar(Name, Init,
2005 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002006 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002007 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002008 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002009 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002010}
2011
2012/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002013 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002014 int count;
2015 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002016 };
2017*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002018llvm::Constant *
2019CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002020 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002021 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002022 ObjCTypes.SelectorPtrTy),
2023 GetMethodVarType(MD)
2024 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002025 if (!Desc[1])
2026 return 0;
2027
Owen Anderson08e25242009-07-27 22:29:56 +00002028 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002029 Desc);
2030}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002031
Chris Lattner5f9e2722011-07-23 10:55:15 +00002032llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002033 const char *Section,
2034 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002035 // Return null for empty list.
2036 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002037 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002038
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002039 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002040 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002041 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002042 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002043 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002044 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002045
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002046 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002047 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002048 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002049}
2050
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002051/*
2052 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002053 char *category_name;
2054 char *class_name;
2055 struct _objc_method_list *instance_methods;
2056 struct _objc_method_list *class_methods;
2057 struct _objc_protocol_list *protocols;
2058 uint32_t size; // <rdar://4585769>
2059 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002060 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002061*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002062void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002063 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002064
Mike Stumpf5408fe2009-05-16 07:57:57 +00002065 // FIXME: This is poor design, the OCD should have a pointer to the category
2066 // decl. Additionally, note that Category can be null for the @implementation
2067 // w/o an @interface case. Sema should just create one for us as it does for
2068 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002069 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002070 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002071 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002072
2073 llvm::SmallString<256> ExtName;
2074 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2075 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002076
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002077 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002078 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002079 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002080 // Instance methods should always be defined.
2081 InstanceMethods.push_back(GetMethodConstant(*i));
2082 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002083 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002084 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002085 // Class methods should always be defined.
2086 ClassMethods.push_back(GetMethodConstant(*i));
2087 }
2088
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002089 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002090 Values[0] = GetClassName(OCD->getIdentifier());
2091 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002092 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002093 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002094 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002095 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002096 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002097 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002098 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002099 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002100 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002101 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002102 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002103 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002104 Category->protocol_begin(),
2105 Category->protocol_end());
2106 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002107 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002108 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002109 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002110
2111 // If there is no category @interface then there can be no properties.
2112 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002113 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002114 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002115 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002116 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002117 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002118
Owen Anderson08e25242009-07-27 22:29:56 +00002119 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002120 Values);
2121
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002122 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002123 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002124 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002125 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002126 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002127 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002128 // method definition entries must be clear for next implementation.
2129 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002130}
2131
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002132// FIXME: Get from somewhere?
2133enum ClassFlags {
2134 eClassFlags_Factory = 0x00001,
2135 eClassFlags_Meta = 0x00002,
2136 // <rdr://5142207>
2137 eClassFlags_HasCXXStructors = 0x02000,
2138 eClassFlags_Hidden = 0x20000,
2139 eClassFlags_ABI2_Hidden = 0x00010,
2140 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2141};
2142
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002143/*
2144 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145 Class isa;
2146 Class super_class;
2147 const char *name;
2148 long version;
2149 long info;
2150 long instance_size;
2151 struct _objc_ivar_list *ivars;
2152 struct _objc_method_list *methods;
2153 struct _objc_cache *cache;
2154 struct _objc_protocol_list *protocols;
2155 // Objective-C 1.0 extensions (<rdr://4585769>)
2156 const char *ivar_layout;
2157 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002158 };
2159
2160 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002162void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002163 DefinedSymbols.insert(ID->getIdentifier());
2164
Chris Lattner8ec03f52008-11-24 03:54:41 +00002165 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002167 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002168 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002169 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002170 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002171 Interface->all_referenced_protocol_begin(),
2172 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002174 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002175 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002176 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002177 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002178
2179 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002180 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 Flags |= eClassFlags_Hidden;
2182
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002183 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002184 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002185 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002186 // Instance methods should always be defined.
2187 InstanceMethods.push_back(GetMethodConstant(*i));
2188 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002189 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002190 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002191 // Class methods should always be defined.
2192 ClassMethods.push_back(GetMethodConstant(*i));
2193 }
2194
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002195 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002196 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002197 ObjCPropertyImplDecl *PID = *i;
2198
2199 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2200 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2201
2202 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2203 if (llvm::Constant *C = GetMethodConstant(MD))
2204 InstanceMethods.push_back(C);
2205 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2206 if (llvm::Constant *C = GetMethodConstant(MD))
2207 InstanceMethods.push_back(C);
2208 }
2209 }
2210
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002211 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002212 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002213 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002214 // Record a reference to the super class.
2215 LazySymbols.insert(Super->getIdentifier());
2216
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002217 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002218 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 ObjCTypes.ClassPtrTy);
2220 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002221 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 }
2223 Values[ 2] = GetClassName(ID->getIdentifier());
2224 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002225 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2226 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2227 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002228 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002229 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002230 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002231 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002232 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002234 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002237 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002238 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002239 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002240 std::string Name("\01L_OBJC_CLASS_");
2241 Name += ClassName;
2242 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2243 // Check for a forward reference.
2244 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2245 if (GV) {
2246 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2247 "Forward metaclass reference has incorrect type.");
2248 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2249 GV->setInitializer(Init);
2250 GV->setSection(Section);
2251 GV->setAlignment(4);
2252 CGM.AddUsedGlobal(GV);
2253 }
2254 else
2255 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002257 // method definition entries must be clear for next implementation.
2258 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002259}
2260
2261llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2262 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002263 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002264 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002265 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266
John McCall1fb0caa2010-10-22 21:05:15 +00002267 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002269
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002270 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002271 // The isa for the metaclass is the root of the hierarchy.
2272 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2273 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2274 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002275 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002276 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002278 // The super class for the metaclass is emitted as the name of the
2279 // super class. The runtime fixes this up to point to the
2280 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002281 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002282 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002283 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002284 ObjCTypes.ClassPtrTy);
2285 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002286 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 }
2288 Values[ 2] = GetClassName(ID->getIdentifier());
2289 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002290 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2291 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2292 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002293 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002294 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002295 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002296 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002297 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002298 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002299 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 Values[ 9] = Protocols;
2301 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002302 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002304 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002305 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 Values);
2307
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002308 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002309 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002310
2311 // Check for a forward reference.
2312 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2313 if (GV) {
2314 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2315 "Forward metaclass reference has incorrect type.");
2316 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2317 GV->setInitializer(Init);
2318 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002319 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002320 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002321 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002322 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002324 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002325 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326
2327 return GV;
2328}
2329
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002330llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002331 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002332
Mike Stumpf5408fe2009-05-16 07:57:57 +00002333 // FIXME: Should we look these up somewhere other than the module. Its a bit
2334 // silly since we only generate these while processing an implementation, so
2335 // exactly one pointer would work if know when we entered/exitted an
2336 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002337
2338 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002339 // Previously, metaclass with internal linkage may have been defined.
2340 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2342 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002343 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2344 "Forward metaclass reference has incorrect type.");
2345 return GV;
2346 } else {
2347 // Generate as an external reference to keep a consistent
2348 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002349 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002350 llvm::GlobalValue::ExternalLinkage,
2351 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002352 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002353 }
2354}
2355
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002356llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2357 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2358
2359 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2360 true)) {
2361 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2362 "Forward class metadata reference has incorrect type.");
2363 return GV;
2364 } else {
2365 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2366 llvm::GlobalValue::ExternalLinkage,
2367 0,
2368 Name);
2369 }
2370}
2371
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002372/*
2373 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002374 uint32_t size;
2375 const char *weak_ivar_layout;
2376 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002377 };
2378*/
2379llvm::Constant *
2380CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002381 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002382 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002383
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002384 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002385 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002386 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002387 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002388 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002389
2390 // Return null if no extension bits are used.
2391 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002392 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002394 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002395 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002396 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002398 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002399}
2400
2401/*
2402 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002403 char *ivar_name;
2404 char *ivar_type;
2405 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002406 };
2407
2408 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002409 int ivar_count;
2410 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002413llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002414 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002415 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416
2417 // When emitting the root class GCC emits ivar entries for the
2418 // actual class structure. It is not clear if we need to follow this
2419 // behavior; for now lets try and get away with not doing it. If so,
2420 // the cleanest solution would be to make up an ObjCInterfaceDecl
2421 // for the class.
2422 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002423 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002424
Jordy Rosedb8264e2011-07-22 02:08:32 +00002425 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002426
Jordy Rosedb8264e2011-07-22 02:08:32 +00002427 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002428 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002429 // Ignore unnamed bit-fields.
2430 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002431 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002432 llvm::Constant *Ivar[] = {
2433 GetMethodVarName(IVD->getIdentifier()),
2434 GetMethodVarType(IVD),
2435 llvm::ConstantInt::get(ObjCTypes.IntTy,
2436 ComputeIvarBaseOffset(CGM, OID, IVD))
2437 };
Owen Anderson08e25242009-07-27 22:29:56 +00002438 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002439 }
2440
2441 // Return null for empty list.
2442 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002443 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002445 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002446 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002447 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002449 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002450 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002452 llvm::GlobalVariable *GV;
2453 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002454 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002455 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002456 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002457 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002458 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002459 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002460 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002461 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462}
2463
2464/*
2465 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002466 SEL method_name;
2467 char *method_types;
2468 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002469 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002470
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002472 struct objc_method_list *obsolete;
2473 int count;
2474 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002475 };
2476*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002477
2478/// GetMethodConstant - Return a struct objc_method constant for the
2479/// given method if it has been defined. The result is null if the
2480/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002481llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002482 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002483 if (!Fn)
2484 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002485
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002486 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002487 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002488 ObjCTypes.SelectorPtrTy),
2489 GetMethodVarType(MD),
2490 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2491 };
Owen Anderson08e25242009-07-27 22:29:56 +00002492 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002493}
2494
Chris Lattner5f9e2722011-07-23 10:55:15 +00002495llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002496 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002497 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002498 // Return null for empty list.
2499 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002500 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002501
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002502 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002503 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002504 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002505 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002506 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002507 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002508 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002509
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002510 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002511 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002512}
2513
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002514llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002515 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002516 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002517 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002518
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002519 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002520 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002521 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002522 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002523 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002524 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002525 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002526 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002527 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002528
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002529 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002530}
2531
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002532llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002533CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002534 llvm::Constant *Init,
2535 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002536 unsigned Align,
2537 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002538 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002539 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002540 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002541 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002542 if (Section)
2543 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002544 if (Align)
2545 GV->setAlignment(Align);
2546 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002547 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002548 return GV;
2549}
2550
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002551llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002552 // Abuse this interface function as a place to finalize.
2553 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002554 return NULL;
2555}
2556
Chris Lattner74391b42009-03-22 21:03:39 +00002557llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002558 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002559}
2560
Chris Lattner74391b42009-03-22 21:03:39 +00002561llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002562 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002563}
2564
David Chisnall8fac25d2010-12-26 22:13:16 +00002565llvm::Constant *CGObjCMac::GetGetStructFunction() {
2566 return ObjCTypes.getCopyStructFn();
2567}
2568llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002569 return ObjCTypes.getCopyStructFn();
2570}
2571
Chris Lattner74391b42009-03-22 21:03:39 +00002572llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002573 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002574}
2575
John McCallf1549f62010-07-06 01:34:17 +00002576void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2577 return EmitTryOrSynchronizedStmt(CGF, S);
2578}
2579
2580void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2581 const ObjCAtSynchronizedStmt &S) {
2582 return EmitTryOrSynchronizedStmt(CGF, S);
2583}
2584
John McCallcc505292010-07-21 06:59:36 +00002585namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002586 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002587 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002588 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002589 llvm::Value *CallTryExitVar;
2590 llvm::Value *ExceptionData;
2591 ObjCTypesHelper &ObjCTypes;
2592 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002593 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002594 llvm::Value *CallTryExitVar,
2595 llvm::Value *ExceptionData,
2596 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002597 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002598 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2599
John McCallad346f42011-07-12 20:27:29 +00002600 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002601 // Check whether we need to call objc_exception_try_exit.
2602 // In optimized code, this branch will always be folded.
2603 llvm::BasicBlock *FinallyCallExit =
2604 CGF.createBasicBlock("finally.call_exit");
2605 llvm::BasicBlock *FinallyNoCallExit =
2606 CGF.createBasicBlock("finally.no_call_exit");
2607 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2608 FinallyCallExit, FinallyNoCallExit);
2609
2610 CGF.EmitBlock(FinallyCallExit);
2611 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2612 ->setDoesNotThrow();
2613
2614 CGF.EmitBlock(FinallyNoCallExit);
2615
2616 if (isa<ObjCAtTryStmt>(S)) {
2617 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002618 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2619 // Save the current cleanup destination in case there's
2620 // control flow inside the finally statement.
2621 llvm::Value *CurCleanupDest =
2622 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2623
John McCallcc505292010-07-21 06:59:36 +00002624 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2625
John McCalld96a8e72010-08-11 00:16:14 +00002626 if (CGF.HaveInsertPoint()) {
2627 CGF.Builder.CreateStore(CurCleanupDest,
2628 CGF.getNormalCleanupDestSlot());
2629 } else {
2630 // Currently, the end of the cleanup must always exist.
2631 CGF.EnsureInsertPoint();
2632 }
2633 }
John McCallcc505292010-07-21 06:59:36 +00002634 } else {
2635 // Emit objc_sync_exit(expr); as finally's sole statement for
2636 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002637 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002638 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2639 ->setDoesNotThrow();
2640 }
2641 }
2642 };
John McCall87bb5822010-07-31 23:20:56 +00002643
2644 class FragileHazards {
2645 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002646 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002647 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2648
2649 llvm::InlineAsm *ReadHazard;
2650 llvm::InlineAsm *WriteHazard;
2651
2652 llvm::FunctionType *GetAsmFnType();
2653
2654 void collectLocals();
2655 void emitReadHazard(CGBuilderTy &Builder);
2656
2657 public:
2658 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002659
John McCall87bb5822010-07-31 23:20:56 +00002660 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002661 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002662 };
2663}
2664
2665/// Create the fragile-ABI read and write hazards based on the current
2666/// state of the function, which is presumed to be immediately prior
2667/// to a @try block. These hazards are used to maintain correct
2668/// semantics in the face of optimization and the fragile ABI's
2669/// cavalier use of setjmp/longjmp.
2670FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2671 collectLocals();
2672
2673 if (Locals.empty()) return;
2674
2675 // Collect all the blocks in the function.
2676 for (llvm::Function::iterator
2677 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2678 BlocksBeforeTry.insert(&*I);
2679
2680 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2681
2682 // Create a read hazard for the allocas. This inhibits dead-store
2683 // optimizations and forces the values to memory. This hazard is
2684 // inserted before any 'throwing' calls in the protected scope to
2685 // reflect the possibility that the variables might be read from the
2686 // catch block if the call throws.
2687 {
2688 std::string Constraint;
2689 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2690 if (I) Constraint += ',';
2691 Constraint += "*m";
2692 }
2693
2694 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2695 }
2696
2697 // Create a write hazard for the allocas. This inhibits folding
2698 // loads across the hazard. This hazard is inserted at the
2699 // beginning of the catch path to reflect the possibility that the
2700 // variables might have been written within the protected scope.
2701 {
2702 std::string Constraint;
2703 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2704 if (I) Constraint += ',';
2705 Constraint += "=*m";
2706 }
2707
2708 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2709 }
2710}
2711
2712/// Emit a write hazard at the current location.
2713void FragileHazards::emitWriteHazard() {
2714 if (Locals.empty()) return;
2715
Jay Foad4c7d9f12011-07-15 08:37:34 +00002716 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002717}
2718
John McCall87bb5822010-07-31 23:20:56 +00002719void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2720 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002721 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002722}
2723
2724/// Emit read hazards in all the protected blocks, i.e. all the blocks
2725/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002726void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002727 if (Locals.empty()) return;
2728
2729 CGBuilderTy Builder(CGF.getLLVMContext());
2730
2731 // Iterate through all blocks, skipping those prior to the try.
2732 for (llvm::Function::iterator
2733 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2734 llvm::BasicBlock &BB = *FI;
2735 if (BlocksBeforeTry.count(&BB)) continue;
2736
2737 // Walk through all the calls in the block.
2738 for (llvm::BasicBlock::iterator
2739 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2740 llvm::Instruction &I = *BI;
2741
2742 // Ignore instructions that aren't non-intrinsic calls.
2743 // These are the only calls that can possibly call longjmp.
2744 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2745 if (isa<llvm::IntrinsicInst>(I))
2746 continue;
2747
2748 // Ignore call sites marked nounwind. This may be questionable,
2749 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2750 llvm::CallSite CS(&I);
2751 if (CS.doesNotThrow()) continue;
2752
John McCall0b251722010-08-04 05:59:32 +00002753 // Insert a read hazard before the call. This will ensure that
2754 // any writes to the locals are performed before making the
2755 // call. If the call throws, then this is sufficient to
2756 // guarantee correctness as long as it doesn't also write to any
2757 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002758 Builder.SetInsertPoint(&BB, BI);
2759 emitReadHazard(Builder);
2760 }
2761 }
2762}
2763
2764static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2765 if (V) S.insert(V);
2766}
2767
2768void FragileHazards::collectLocals() {
2769 // Compute a set of allocas to ignore.
2770 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2771 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2772 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002773
2774 // Collect all the allocas currently in the function. This is
2775 // probably way too aggressive.
2776 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2777 for (llvm::BasicBlock::iterator
2778 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2779 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2780 Locals.push_back(&*I);
2781}
2782
2783llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002784 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002785 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2786 tys[i] = Locals[i]->getType();
2787 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002788}
2789
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002790/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002791
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002792 Objective-C setjmp-longjmp (sjlj) Exception Handling
2793 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002794
John McCallf1549f62010-07-06 01:34:17 +00002795 A catch buffer is a setjmp buffer plus:
2796 - a pointer to the exception that was caught
2797 - a pointer to the previous exception data buffer
2798 - two pointers of reserved storage
2799 Therefore catch buffers form a stack, with a pointer to the top
2800 of the stack kept in thread-local storage.
2801
2802 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2803 objc_exception_try_exit pops the given catch buffer, which is
2804 required to be the top of the EH stack.
2805 objc_exception_throw pops the top of the EH stack, writes the
2806 thrown exception into the appropriate field, and longjmps
2807 to the setjmp buffer. It crashes the process (with a printf
2808 and an abort()) if there are no catch buffers on the stack.
2809 objc_exception_extract just reads the exception pointer out of the
2810 catch buffer.
2811
2812 There's no reason an implementation couldn't use a light-weight
2813 setjmp here --- something like __builtin_setjmp, but API-compatible
2814 with the heavyweight setjmp. This will be more important if we ever
2815 want to implement correct ObjC/C++ exception interactions for the
2816 fragile ABI.
2817
2818 Note that for this use of setjmp/longjmp to be correct, we may need
2819 to mark some local variables volatile: if a non-volatile local
2820 variable is modified between the setjmp and the longjmp, it has
2821 indeterminate value. For the purposes of LLVM IR, it may be
2822 sufficient to make loads and stores within the @try (to variables
2823 declared outside the @try) volatile. This is necessary for
2824 optimized correctness, but is not currently being done; this is
2825 being tracked as rdar://problem/8160285
2826
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002827 The basic framework for a @try-catch-finally is as follows:
2828 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002829 objc_exception_data d;
2830 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002831 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002832
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002833 objc_exception_try_enter(&d);
2834 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002835 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002837 // exception path
2838 id _caught = objc_exception_extract(&d);
2839
2840 // enter new try scope for handlers
2841 if (!setjmp(d.jmp_buf)) {
2842 ... match exception and execute catch blocks ...
2843
2844 // fell off end, rethrow.
2845 _rethrow = _caught;
2846 ... jump-through-finally to finally_rethrow ...
2847 } else {
2848 // exception in catch block
2849 _rethrow = objc_exception_extract(&d);
2850 _call_try_exit = false;
2851 ... jump-through-finally to finally_rethrow ...
2852 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002853 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002854 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002855
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002856 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002857 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002858 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002859
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002860 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002861 ... dispatch to finally destination ...
2862
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002863 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002864 objc_exception_throw(_rethrow);
2865
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002866 finally_end:
2867 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002868
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002869 This framework differs slightly from the one gcc uses, in that gcc
2870 uses _rethrow to determine if objc_exception_try_exit should be called
2871 and if the object should be rethrown. This breaks in the face of
2872 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002873
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002874 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002875
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002876 - If there are no catch blocks, then we avoid emitting the second
2877 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002878
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002879 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2880 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002881
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002882 - FIXME: If there is no @finally block we can do a few more
2883 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002884
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002885 Rethrows and Jumps-Through-Finally
2886 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002887
John McCallf1549f62010-07-06 01:34:17 +00002888 '@throw;' is supported by pushing the currently-caught exception
2889 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002890
John McCallf1549f62010-07-06 01:34:17 +00002891 Branches through the @finally block are handled with an ordinary
2892 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2893 exceptions are not compatible with C++ exceptions, and this is
2894 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002895
John McCallf1549f62010-07-06 01:34:17 +00002896 @synchronized(expr) { stmt; } is emitted as if it were:
2897 id synch_value = expr;
2898 objc_sync_enter(synch_value);
2899 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002900*/
2901
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002902void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2903 const Stmt &S) {
2904 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002905
2906 // A destination for the fall-through edges of the catch handlers to
2907 // jump to.
2908 CodeGenFunction::JumpDest FinallyEnd =
2909 CGF.getJumpDestInCurrentScope("finally.end");
2910
2911 // A destination for the rethrow edge of the catch handlers to jump
2912 // to.
2913 CodeGenFunction::JumpDest FinallyRethrow =
2914 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002915
Daniel Dunbar1c566672009-02-24 01:43:46 +00002916 // For @synchronized, call objc_sync_enter(sync.expr). The
2917 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002918 // @synchronized. We can't avoid a temp here because we need the
2919 // value to be preserved. If the backend ever does liveness
2920 // correctly after setjmp, this will be unnecessary.
2921 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002922 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002923 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002924 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2925 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002926 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2927 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002928
2929 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2930 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002931 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002932
John McCall0b251722010-08-04 05:59:32 +00002933 // Allocate memory for the setjmp buffer. This needs to be kept
2934 // live throughout the try and catch blocks.
2935 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2936 "exceptiondata.ptr");
2937
John McCall87bb5822010-07-31 23:20:56 +00002938 // Create the fragile hazards. Note that this will not capture any
2939 // of the allocas required for exception processing, but will
2940 // capture the current basic block (which extends all the way to the
2941 // setjmp call) as "before the @try".
2942 FragileHazards Hazards(CGF);
2943
John McCallf1549f62010-07-06 01:34:17 +00002944 // Create a flag indicating whether the cleanup needs to call
2945 // objc_exception_try_exit. This is true except when
2946 // - no catches match and we're branching through the cleanup
2947 // just to rethrow the exception, or
2948 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002949 // The setjmp-safety rule here is that we should always store to this
2950 // variable in a place that dominates the branch through the cleanup
2951 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002952 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002953 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002954
John McCall9e2213d2010-10-04 23:42:51 +00002955 // A slot containing the exception to rethrow. Only needed when we
2956 // have both a @catch and a @finally.
2957 llvm::Value *PropagatingExnVar = 0;
2958
John McCallf1549f62010-07-06 01:34:17 +00002959 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002960 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002961 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002962 CallTryExitVar,
2963 ExceptionData,
2964 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002965
2966 // Enter a try block:
2967 // - Call objc_exception_try_enter to push ExceptionData on top of
2968 // the EH stack.
2969 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2970 ->setDoesNotThrow();
2971
2972 // - Call setjmp on the exception data buffer.
2973 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2974 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2975 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00002976 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00002977 llvm::CallInst *SetJmpResult =
2978 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2979 SetJmpResult->setDoesNotThrow();
2980
2981 // If setjmp returned 0, enter the protected block; otherwise,
2982 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002983 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2984 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002985 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002986 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2987 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002988
John McCallf1549f62010-07-06 01:34:17 +00002989 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002990 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002991 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002992 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002993 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002994
2995 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002996
John McCallf1549f62010-07-06 01:34:17 +00002997 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002998 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002999
John McCall87bb5822010-07-31 23:20:56 +00003000 // Don't optimize loads of the in-scope locals across this point.
3001 Hazards.emitWriteHazard();
3002
John McCallf1549f62010-07-06 01:34:17 +00003003 // For a @synchronized (or a @try with no catches), just branch
3004 // through the cleanup to the rethrow block.
3005 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3006 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003007 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003008 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003009
3010 // Otherwise, we have to match against the caught exceptions.
3011 } else {
John McCall0b251722010-08-04 05:59:32 +00003012 // Retrieve the exception object. We may emit multiple blocks but
3013 // nothing can cross this so the value is already in SSA form.
3014 llvm::CallInst *Caught =
3015 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3016 ExceptionData, "caught");
3017 Caught->setDoesNotThrow();
3018
John McCallf1549f62010-07-06 01:34:17 +00003019 // Push the exception to rethrow onto the EH value stack for the
3020 // benefit of any @throws in the handlers.
3021 CGF.ObjCEHValueStack.push_back(Caught);
3022
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003023 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003024
John McCall0b251722010-08-04 05:59:32 +00003025 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003026
John McCall0b251722010-08-04 05:59:32 +00003027 llvm::BasicBlock *CatchBlock = 0;
3028 llvm::BasicBlock *CatchHandler = 0;
3029 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003030 // Save the currently-propagating exception before
3031 // objc_exception_try_enter clears the exception slot.
3032 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3033 "propagating_exception");
3034 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3035
John McCall0b251722010-08-04 05:59:32 +00003036 // Enter a new exception try block (in case a @catch block
3037 // throws an exception).
3038 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3039 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003040
John McCall0b251722010-08-04 05:59:32 +00003041 llvm::CallInst *SetJmpResult =
3042 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3043 "setjmp.result");
3044 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003045
John McCall0b251722010-08-04 05:59:32 +00003046 llvm::Value *Threw =
3047 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3048
3049 CatchBlock = CGF.createBasicBlock("catch");
3050 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3051 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3052
3053 CGF.EmitBlock(CatchBlock);
3054 }
3055
3056 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003057
Daniel Dunbar55e40722008-09-27 07:03:52 +00003058 // Handle catch list. As a special case we check if everything is
3059 // matched and avoid generating code for falling off the end if
3060 // so.
3061 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003062 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3063 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003064
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003065 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003066 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003067
Anders Carlsson80f25672008-09-09 17:59:25 +00003068 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003069 if (!CatchParam) {
3070 AllMatched = true;
3071 } else {
John McCall183700f2009-09-21 23:43:11 +00003072 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003073
John McCallf1549f62010-07-06 01:34:17 +00003074 // catch(id e) always matches under this ABI, since only
3075 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003076 // FIXME: For the time being we also match id<X>; this should
3077 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003078 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003079 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003080 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003081
John McCallf1549f62010-07-06 01:34:17 +00003082 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003083 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003084 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3085
Anders Carlssondde0a942008-09-11 09:15:33 +00003086 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003087 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003088 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003089
3090 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003091 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003092 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093
Anders Carlssondde0a942008-09-11 09:15:33 +00003094 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003095
3096 // The scope of the catch variable ends right here.
3097 CatchVarCleanups.ForceCleanup();
3098
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003099 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003100 break;
3101 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102
Steve Naroff14108da2009-07-10 23:34:53 +00003103 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003104 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003105
3106 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003107 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3108 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003109
3110 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003111 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003112
John McCallf1549f62010-07-06 01:34:17 +00003113 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003114 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3115 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003116 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003117
John McCallf1549f62010-07-06 01:34:17 +00003118 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3119 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003120
3121 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003122 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003123
Anders Carlsson80f25672008-09-09 17:59:25 +00003124 // Emit the @catch block.
3125 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003126
3127 // Collect any cleanups for the catch variable. The scope lasts until
3128 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003129 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003130
John McCallb6bbcc92010-10-15 04:57:14 +00003131 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003132 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003133
John McCallf1549f62010-07-06 01:34:17 +00003134 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003135 llvm::Value *Tmp =
3136 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003137 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003138 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003139
Anders Carlssondde0a942008-09-11 09:15:33 +00003140 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003141
3142 // We're done with the catch variable.
3143 CatchVarCleanups.ForceCleanup();
3144
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003145 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003146
Anders Carlsson80f25672008-09-09 17:59:25 +00003147 CGF.EmitBlock(NextCatchBlock);
3148 }
3149
John McCallf1549f62010-07-06 01:34:17 +00003150 CGF.ObjCEHValueStack.pop_back();
3151
John McCall0b251722010-08-04 05:59:32 +00003152 // If nothing wanted anything to do with the caught exception,
3153 // kill the extract call.
3154 if (Caught->use_empty())
3155 Caught->eraseFromParent();
3156
3157 if (!AllMatched)
3158 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3159
3160 if (HasFinally) {
3161 // Emit the exception handler for the @catch blocks.
3162 CGF.EmitBlock(CatchHandler);
3163
3164 // In theory we might now need a write hazard, but actually it's
3165 // unnecessary because there's no local-accessing code between
3166 // the try's write hazard and here.
3167 //Hazards.emitWriteHazard();
3168
John McCall9e2213d2010-10-04 23:42:51 +00003169 // Extract the new exception and save it to the
3170 // propagating-exception slot.
3171 assert(PropagatingExnVar);
3172 llvm::CallInst *NewCaught =
3173 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3174 ExceptionData, "caught");
3175 NewCaught->setDoesNotThrow();
3176 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3177
John McCall0b251722010-08-04 05:59:32 +00003178 // Don't pop the catch handler; the throw already did.
3179 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003180 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003181 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003182 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003183
John McCall87bb5822010-07-31 23:20:56 +00003184 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003185 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003186
John McCallf1549f62010-07-06 01:34:17 +00003187 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003188 CGF.Builder.restoreIP(TryFallthroughIP);
3189 if (CGF.HaveInsertPoint())
3190 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003191 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003192 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003193
John McCallf1549f62010-07-06 01:34:17 +00003194 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003195 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003196 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003197 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003198 // If we have a propagating-exception variable, check it.
3199 llvm::Value *PropagatingExn;
3200 if (PropagatingExnVar) {
3201 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003202
John McCall9e2213d2010-10-04 23:42:51 +00003203 // Otherwise, just look in the buffer for the exception to throw.
3204 } else {
3205 llvm::CallInst *Caught =
3206 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3207 ExceptionData);
3208 Caught->setDoesNotThrow();
3209 PropagatingExn = Caught;
3210 }
3211
3212 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003213 ->setDoesNotThrow();
3214 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003215 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003216
John McCall87bb5822010-07-31 23:20:56 +00003217 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003218}
3219
3220void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003221 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003222 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003223
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003224 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003225 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003226 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003227 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003228 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003229 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003230 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003231 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003232 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003233
John McCallf1549f62010-07-06 01:34:17 +00003234 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3235 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003236 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003237
3238 // Clear the insertion point to indicate we are in unreachable code.
3239 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003240}
3241
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003242/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003243/// object: objc_read_weak (id *src)
3244///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003245llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003246 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003247 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003248 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3249 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3250 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003251 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003252 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003253 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003254 return read_weak;
3255}
3256
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003257/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3258/// objc_assign_weak (id src, id *dst)
3259///
3260void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003261 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003262 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003263 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003264 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003265 assert(Size <= 8 && "does not support size > 8");
3266 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003267 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003268 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3269 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003270 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3271 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003272 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003273 src, dst, "weakassign");
3274 return;
3275}
3276
Fariborz Jahanian58626502008-11-19 00:59:10 +00003277/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3278/// objc_assign_global (id src, id *dst)
3279///
3280void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003281 llvm::Value *src, llvm::Value *dst,
3282 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003283 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003284 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003285 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003286 assert(Size <= 8 && "does not support size > 8");
3287 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003288 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003289 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3290 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003291 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3292 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003293 if (!threadlocal)
3294 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3295 src, dst, "globalassign");
3296 else
3297 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3298 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003299 return;
3300}
3301
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003302/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003303/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003304///
3305void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003306 llvm::Value *src, llvm::Value *dst,
3307 llvm::Value *ivarOffset) {
3308 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003309 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003310 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003311 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003312 assert(Size <= 8 && "does not support size > 8");
3313 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003314 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003315 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3316 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003317 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3318 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003319 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3320 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003321 return;
3322}
3323
Fariborz Jahanian58626502008-11-19 00:59:10 +00003324/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3325/// objc_assign_strongCast (id src, id *dst)
3326///
3327void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003328 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003329 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003330 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003331 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003332 assert(Size <= 8 && "does not support size > 8");
3333 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003335 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3336 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003337 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3338 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003339 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003340 src, dst, "weakassign");
3341 return;
3342}
3343
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003344void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003345 llvm::Value *DestPtr,
3346 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003347 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003348 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3349 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003350 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003351 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003352 return;
3353}
3354
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003355/// EmitObjCValueForIvar - Code Gen for ivar reference.
3356///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003357LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3358 QualType ObjectTy,
3359 llvm::Value *BaseValue,
3360 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003361 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003362 const ObjCInterfaceDecl *ID =
3363 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003364 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3365 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003366}
3367
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003368llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003369 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003370 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003371 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003372 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003373 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3374 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003375}
3376
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003377/* *** Private Interface *** */
3378
3379/// EmitImageInfo - Emit the image info marker used to encode some module
3380/// level information.
3381///
3382/// See: <rdr://4810609&4810587&4810587>
3383/// struct IMAGE_INFO {
3384/// unsigned version;
3385/// unsigned flags;
3386/// };
3387enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003388 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003389 eImageInfo_GarbageCollected = (1 << 1),
3390 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003391 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3392
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003393 // A flag indicating that the module has no instances of a @synthesize of a
3394 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003395 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003396};
3397
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003398void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003399 unsigned version = 0; // Version is unused?
3400 unsigned flags = 0;
3401
3402 // FIXME: Fix and continue?
Douglas Gregore289d812011-09-13 17:21:33 +00003403 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003404 flags |= eImageInfo_GarbageCollected;
Douglas Gregore289d812011-09-13 17:21:33 +00003405 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003406 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003407
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003408 // We never allow @synthesize of a superclass property.
3409 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003410
Chris Lattner2acc6e32011-07-18 04:24:23 +00003411 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003412
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003413 // Emitted as int[2];
3414 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003415 llvm::ConstantInt::get(Int32Ty, version),
3416 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003417 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003418 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003419
3420 const char *Section;
3421 if (ObjCABI == 1)
3422 Section = "__OBJC, __image_info,regular";
3423 else
3424 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003425 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003426 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003427 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003428 Section,
3429 0,
3430 true);
3431 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003432}
3433
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003434
3435// struct objc_module {
3436// unsigned long version;
3437// unsigned long size;
3438// const char *name;
3439// Symtab symtab;
3440// };
3441
3442// FIXME: Get from somewhere
3443static const int ModuleVersion = 7;
3444
3445void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003446 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003447
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003448 llvm::Constant *Values[] = {
3449 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3450 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3451 // This used to be the filename, now it is unused. <rdr://4327263>
3452 GetClassName(&CGM.getContext().Idents.get("")),
3453 EmitModuleSymbols()
3454 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003456 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003457 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003458 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003459}
3460
3461llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003462 unsigned NumClasses = DefinedClasses.size();
3463 unsigned NumCategories = DefinedCategories.size();
3464
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003465 // Return null if no symbols were defined.
3466 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003467 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003468
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003469 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003470 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003471 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003472 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3473 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003474
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003475 // The runtime expects exactly the list of defined classes followed
3476 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003477 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003478 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003479 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003480 ObjCTypes.Int8PtrTy);
3481 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003482 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003483 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003484 ObjCTypes.Int8PtrTy);
3485
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003486 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003487 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003488 NumClasses + NumCategories),
3489 Symbols);
3490
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003491 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003492
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003493 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003494 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3495 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003496 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003497 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003498}
3499
John McCallf85e1932011-06-15 23:02:42 +00003500llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3501 IdentifierInfo *II) {
3502 LazySymbols.insert(II);
3503
3504 llvm::GlobalVariable *&Entry = ClassReferences[II];
3505
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003506 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003507 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003508 llvm::ConstantExpr::getBitCast(GetClassName(II),
3509 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003510 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003511 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3512 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3513 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003514 }
John McCallf85e1932011-06-15 23:02:42 +00003515
Benjamin Kramer578faa82011-09-27 21:06:10 +00003516 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003517}
3518
John McCallf85e1932011-06-15 23:02:42 +00003519llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3520 const ObjCInterfaceDecl *ID) {
3521 return EmitClassRefFromId(Builder, ID->getIdentifier());
3522}
3523
3524llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3525 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3526 return EmitClassRefFromId(Builder, II);
3527}
3528
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003529llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3530 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003531 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003532
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003533 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003534 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003535 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003536 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003537 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003538 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3539 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003540 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003541 }
3542
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003543 if (lvalue)
3544 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003545 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003546}
3547
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003548llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003549 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003550
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003551 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003553 llvm::ConstantArray::get(VMContext,
3554 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003555 ((ObjCABI == 2) ?
3556 "__TEXT,__objc_classname,cstring_literals" :
3557 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003558 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003559
Owen Andersona1cf15f2009-07-14 23:10:40 +00003560 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003561}
3562
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003563llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3564 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3565 I = MethodDefinitions.find(MD);
3566 if (I != MethodDefinitions.end())
3567 return I->second;
3568
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003569 return NULL;
3570}
3571
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003572/// GetIvarLayoutName - Returns a unique constant for the given
3573/// ivar layout bitmap.
3574llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003575 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003576 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003577}
3578
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003579void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003581 bool ForStrongLayout,
3582 bool &HasUnion) {
3583 const RecordDecl *RD = RT->getDecl();
3584 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003585 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003586 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003588 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003589
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003590 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3591 ForStrongLayout, HasUnion);
3592}
3593
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003594void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003595 const llvm::StructLayout *Layout,
3596 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003597 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003598 unsigned int BytePos, bool ForStrongLayout,
3599 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003600 bool IsUnion = (RD && RD->isUnion());
3601 uint64_t MaxUnionIvarSize = 0;
3602 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003603 const FieldDecl *MaxField = 0;
3604 const FieldDecl *MaxSkippedField = 0;
3605 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003606 uint64_t MaxFieldOffset = 0;
3607 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003608 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003609 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003610
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003611 if (RecFields.empty())
3612 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003613 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3614 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003615 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003616 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003617 FirstFieldDelta =
3618 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3619 }
3620
Chris Lattnerf1690852009-03-31 08:48:01 +00003621 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003622 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003623 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003624 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003625 // Note that 'i' here is actually the field index inside RD of Field,
3626 // although this dependency is hidden.
3627 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003628 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003629 } else
John McCallf85e1932011-06-15 23:02:42 +00003630 FieldOffset =
3631 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003632
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003633 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003634 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003635 LastFieldBitfieldOrUnnamed = Field;
3636 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003637 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003638 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003639
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003640 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003641 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003642 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003643 if (FQT->isUnionType())
3644 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003645
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003646 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003647 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003648 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003649 continue;
3650 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003651
Chris Lattnerf1690852009-03-31 08:48:01 +00003652 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003653 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003654 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003655 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003656 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003657 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003658 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3659 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003660 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003661 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003662 FQT = CArray->getElementType();
3663 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003664
3665 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003666 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003667 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003668 int OldIndex = IvarsInfo.size() - 1;
3669 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003670
Ted Kremenek6217b802009-07-29 21:53:49 +00003671 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003672 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003673 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003675 // Replicate layout information for each array element. Note that
3676 // one element is already done.
3677 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003678 for (int FirstIndex = IvarsInfo.size() - 1,
3679 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003680 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003681 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3682 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3683 IvarsInfo[i].ivar_size));
3684 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3685 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3686 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003687 }
3688 continue;
3689 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003690 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003691 // At this point, we are done with Record/Union and array there of.
3692 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003693 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003694
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003695 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003696 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3697 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003698 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003699 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003700 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003701 MaxUnionIvarSize = UnionIvarSize;
3702 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003703 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003704 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003705 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003706 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003707 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003708 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003709 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003710 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3711 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003712 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003713 // FIXME: Why the asymmetry? We divide by word size in bits on other
3714 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003715 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003716 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003717 MaxSkippedUnionIvarSize = UnionIvarSize;
3718 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003719 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003720 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003721 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003722 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003723 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003724 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003725 }
3726 }
3727 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003728
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003729 if (LastFieldBitfieldOrUnnamed) {
3730 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3731 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003732 uint64_t BitFieldSize
3733 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003734 GC_IVAR skivar;
3735 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3736 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3737 + ((BitFieldSize % ByteSizeInBits) != 0);
3738 SkipIvars.push_back(skivar);
3739 } else {
3740 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3741 // Last field was unnamed. Must update skip info.
3742 unsigned FieldSize
3743 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3744 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3745 FieldSize / ByteSizeInBits));
3746 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003747 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003749 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003750 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003751 MaxUnionIvarSize));
3752 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003753 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003754 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003755}
3756
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003757/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3758/// the computations and returning the layout bitmap (for ivar or blocks) in
3759/// the given argument BitMap string container. Routine reads
3760/// two containers, IvarsInfo and SkipIvars which are assumed to be
3761/// filled already by the caller.
3762llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003763 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003764 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003765
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003766 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003767 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003768 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003769 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003770 if (IvarsInfo[0].ivar_bytepos == 0) {
3771 WordsToSkip = 0;
3772 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003773 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003774 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3775 WordsToScan = IvarsInfo[0].ivar_size;
3776 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003777 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003778 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003779 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003780 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003781 // consecutive 'scanned' object pointers.
3782 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003783 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003784 // Skip over 'gc'able object pointer which lay over each other.
3785 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3786 continue;
3787 // Must skip over 1 or more words. We save current skip/scan values
3788 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003789 SKIP_SCAN SkScan;
3790 SkScan.skip = WordsToSkip;
3791 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003792 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003793
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003794 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003795 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3796 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003797 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003798 WordsToSkip = 0;
3799 WordsToScan = IvarsInfo[i].ivar_size;
3800 }
3801 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003802 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003803 SKIP_SCAN SkScan;
3804 SkScan.skip = WordsToSkip;
3805 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003806 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003807 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003808
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003809 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003810 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003811 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003812 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003813 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003814 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003815 IvarsInfo[LastIndex].ivar_bytepos +
3816 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003817 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003818 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003819 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003820 SKIP_SCAN SkScan;
3821 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3822 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003823 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003824 }
3825 }
3826 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3827 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003828 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003829 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003830 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3831 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3832 // 0xM0 followed by 0x0N detected.
3833 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3834 for (int j = i+1; j < SkipScan; j++)
3835 SkipScanIvars[j] = SkipScanIvars[j+1];
3836 --SkipScan;
3837 }
3838 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003839
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003840 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003841 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003842 unsigned char byte;
3843 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3844 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3845 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3846 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003847
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003848 // first skip big.
3849 for (unsigned int ix = 0; ix < skip_big; ix++)
3850 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003851
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003852 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003853 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003854 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003855 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003856 byte |= 0xf;
3857 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003858 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003859 byte |= scan_small;
3860 scan_small = 0;
3861 }
3862 BitMap += byte;
3863 }
3864 // next scan big
3865 for (unsigned int ix = 0; ix < scan_big; ix++)
3866 BitMap += (unsigned char)(0x0f);
3867 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003868 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003869 byte = scan_small;
3870 BitMap += byte;
3871 }
3872 }
3873 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003874 unsigned char zero = 0;
3875 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003876
3877 llvm::GlobalVariable * Entry =
3878 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3879 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003880 ((ObjCABI == 2) ?
3881 "__TEXT,__objc_classname,cstring_literals" :
3882 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003883 1, true);
3884 return getConstantGEP(VMContext, Entry, 0, 0);
3885}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003886
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003887/// BuildIvarLayout - Builds ivar layout bitmap for the class
3888/// implementation for the __strong or __weak case.
3889/// The layout map displays which words in ivar list must be skipped
3890/// and which must be scanned by GC (see below). String is built of bytes.
3891/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3892/// of words to skip and right nibble is count of words to scan. So, each
3893/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3894/// represented by a 0x00 byte which also ends the string.
3895/// 1. when ForStrongLayout is true, following ivars are scanned:
3896/// - id, Class
3897/// - object *
3898/// - __strong anything
3899///
3900/// 2. When ForStrongLayout is false, following ivars are scanned:
3901/// - __weak anything
3902///
3903llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3904 const ObjCImplementationDecl *OMD,
3905 bool ForStrongLayout) {
3906 bool hasUnion = false;
3907
Chris Lattner2acc6e32011-07-18 04:24:23 +00003908 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregore289d812011-09-13 17:21:33 +00003909 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00003910 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003911 return llvm::Constant::getNullValue(PtrTy);
3912
Jordy Rosedb8264e2011-07-22 02:08:32 +00003913 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3914 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003915 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003916 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003917 IVD; IVD = IVD->getNextIvar())
3918 RecFields.push_back(cast<FieldDecl>(IVD));
3919 }
3920 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003921 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00003922 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003923
Jordy Rosedb8264e2011-07-22 02:08:32 +00003924 // FIXME: This is not ideal; we shouldn't have to do this copy.
3925 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003926 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003927
3928 if (RecFields.empty())
3929 return llvm::Constant::getNullValue(PtrTy);
3930
3931 SkipIvars.clear();
3932 IvarsInfo.clear();
3933
3934 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3935 if (IvarsInfo.empty())
3936 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003937 // Sort on byte position in case we encounterred a union nested in
3938 // the ivar list.
3939 if (hasUnion && !IvarsInfo.empty())
3940 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3941 if (hasUnion && !SkipIvars.empty())
3942 std::sort(SkipIvars.begin(), SkipIvars.end());
3943
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003944 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003945 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003946
3947 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003948 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003949 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003950 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003951 const unsigned char *s = (unsigned char*)BitMap.c_str();
3952 for (unsigned i = 0; i < BitMap.size(); i++)
3953 if (!(s[i] & 0xf0))
3954 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3955 else
3956 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3957 printf("\n");
3958 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003959 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003960}
3961
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003962llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003963 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3964
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003965 // FIXME: Avoid std::string copying.
3966 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003967 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003968 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003969 ((ObjCABI == 2) ?
3970 "__TEXT,__objc_methname,cstring_literals" :
3971 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003972 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003973
Owen Andersona1cf15f2009-07-14 23:10:40 +00003974 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003975}
3976
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003977// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003978llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003979 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3980}
3981
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003982llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003983 std::string TypeStr;
3984 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3985
3986 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003987
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003988 if (!Entry)
3989 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003990 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003991 ((ObjCABI == 2) ?
3992 "__TEXT,__objc_methtype,cstring_literals" :
3993 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003994 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003995
Owen Andersona1cf15f2009-07-14 23:10:40 +00003996 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003997}
3998
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003999llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004000 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00004001 if (CGM.getContext().getObjCEncodingForMethodDecl(
4002 const_cast<ObjCMethodDecl*>(D),
4003 TypeStr))
4004 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004005
4006 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4007
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004008 if (!Entry)
4009 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004010 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004011 ((ObjCABI == 2) ?
4012 "__TEXT,__objc_methtype,cstring_literals" :
4013 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004014 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004015
Owen Andersona1cf15f2009-07-14 23:10:40 +00004016 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004017}
4018
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004019// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004020llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004021 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004022
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004023 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004024 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004025 llvm::ConstantArray::get(VMContext,
4026 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004027 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004028 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004029
Owen Andersona1cf15f2009-07-14 23:10:40 +00004030 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004031}
4032
4033// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004034// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004035llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004036CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4037 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004038 std::string TypeStr;
4039 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004040 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4041}
4042
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004043void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004044 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004045 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004046 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004047 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004048 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4049 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004050 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004051 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004052 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004053 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004054}
4055
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004056void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004057 EmitModuleInfo();
4058
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004059 // Emit the dummy bodies for any protocols which were referenced but
4060 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004061 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004062 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4063 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004064 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004065
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004066 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004067 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004068 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004069 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004070 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004071 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004072 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004073 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004074 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004075 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004076 }
4077
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004078 // Add assembler directives to add lazy undefined symbol references
4079 // for classes which are referenced but not defined. This is
4080 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004081 //
4082 // FIXME: It would be nice if we had an LLVM construct for this.
4083 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4084 llvm::SmallString<256> Asm;
4085 Asm += CGM.getModule().getModuleInlineAsm();
4086 if (!Asm.empty() && Asm.back() != '\n')
4087 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004088
Daniel Dunbar33063492009-09-07 00:20:42 +00004089 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004090 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4091 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004092 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4093 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004094 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004095 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004096 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004097 }
4098
4099 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4100 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4101 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4102 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004103
Daniel Dunbar33063492009-09-07 00:20:42 +00004104 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004105 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004106}
4107
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004108CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004109 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004110 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004111 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004112 ObjCABI = 2;
4113}
4114
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004115/* *** */
4116
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004117ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004118 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004119 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4120 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004122 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004123 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004124 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004125 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004126 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004128 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004129 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004130 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131
Mike Stumpf5408fe2009-05-16 07:57:57 +00004132 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4133 // comes out a bit cleaner.
Chris Lattner2acc6e32011-07-18 04:24:23 +00004134 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004135 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004136
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004137 // I'm not sure I like this. The implicit coordination is a bit
4138 // gross. We should solve this in a reasonable fashion because this
4139 // is a pretty common task (match some runtime data structure with
4140 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004142 // FIXME: This is leaked.
4143 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004145 // struct _objc_super {
4146 // id self;
4147 // Class cls;
4148 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004149 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004150 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004151 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004153 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004154 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004155 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004156 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004157 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004158
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004159 SuperCTy = Ctx.getTagDeclType(RD);
4160 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004161
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004162 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4164
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004165 // struct _prop_t {
4166 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004168 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004169 PropertyTy = llvm::StructType::create("struct._prop_t",
4170 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004172 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004173 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004174 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004175 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004176 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004177 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004178 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4179 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004180 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004181 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004183 // struct _objc_method {
4184 // SEL _cmd;
4185 // char *method_type;
4186 // char *_imp;
4187 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004188 MethodTy = llvm::StructType::create("struct._objc_method",
4189 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4190 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004191
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004192 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004193 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004194 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004195
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004196}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004197
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004199 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004200 // struct _objc_method_description {
4201 // SEL name;
4202 // char *types;
4203 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004204 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004205 llvm::StructType::create("struct._objc_method_description",
4206 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004207
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004208 // struct _objc_method_description_list {
4209 // int count;
4210 // struct _objc_method_description[1];
4211 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004213 llvm::StructType::create("struct._objc_method_description_list",
4214 IntTy,
4215 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004216
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004217 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004218 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004219 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004220
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004221 // Protocol description structures
4222
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004223 // struct _objc_protocol_extension {
4224 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4225 // struct _objc_method_description_list *optional_instance_methods;
4226 // struct _objc_method_description_list *optional_class_methods;
4227 // struct _objc_property_list *instance_properties;
4228 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004230 llvm::StructType::create("struct._objc_protocol_extension",
4231 IntTy, MethodDescriptionListPtrTy,
4232 MethodDescriptionListPtrTy, PropertyListPtrTy,
4233 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004234
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004235 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004236 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004237
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004238 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004239
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004240 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004241 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004242
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004243 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004244 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004245 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004246 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004247 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004248 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004249
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004250 // struct _objc_protocol {
4251 // struct _objc_protocol_extension *isa;
4252 // char *protocol_name;
4253 // struct _objc_protocol **_objc_protocol_list;
4254 // struct _objc_method_description_list *instance_methods;
4255 // struct _objc_method_description_list *class_methods;
4256 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004257 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4258 llvm::PointerType::getUnqual(ProtocolListTy),
4259 MethodDescriptionListPtrTy,
4260 MethodDescriptionListPtrTy,
4261 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004262
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004263 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004264 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004265
Owen Anderson96e0fc72009-07-29 22:16:19 +00004266 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004267
4268 // Class description structures
4269
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004270 // struct _objc_ivar {
4271 // char *ivar_name;
4272 // char *ivar_type;
4273 // int ivar_offset;
4274 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004275 IvarTy = llvm::StructType::create("struct._objc_ivar",
4276 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004277
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004278 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004279 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004280 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004281 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004282
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004283 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004284 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004285 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004286 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004287
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004288 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004289 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004290 llvm::StructType::create("struct._objc_class_extension",
4291 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004292 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004293
Chris Lattnerc1c20112011-08-12 17:43:31 +00004294 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004295
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004296 // struct _objc_class {
4297 // Class isa;
4298 // Class super_class;
4299 // char *name;
4300 // long version;
4301 // long info;
4302 // long instance_size;
4303 // struct _objc_ivar_list *ivars;
4304 // struct _objc_method_list *methods;
4305 // struct _objc_cache *cache;
4306 // struct _objc_protocol_list *protocols;
4307 // char *ivar_layout;
4308 // struct _objc_class_ext *ext;
4309 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004310 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4311 llvm::PointerType::getUnqual(ClassTy),
4312 Int8PtrTy,
4313 LongTy,
4314 LongTy,
4315 LongTy,
4316 IvarListPtrTy,
4317 MethodListPtrTy,
4318 CachePtrTy,
4319 ProtocolListPtrTy,
4320 Int8PtrTy,
4321 ClassExtensionPtrTy,
4322 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004323
Owen Anderson96e0fc72009-07-29 22:16:19 +00004324 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004325
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004326 // struct _objc_category {
4327 // char *category_name;
4328 // char *class_name;
4329 // struct _objc_method_list *instance_method;
4330 // struct _objc_method_list *class_method;
4331 // uint32_t size; // sizeof(struct _objc_category)
4332 // struct _objc_property_list *instance_properties;// category's @property
4333 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004334 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004335 llvm::StructType::create("struct._objc_category",
4336 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4337 MethodListPtrTy, ProtocolListPtrTy,
4338 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004339
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004340 // Global metadata structures
4341
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004342 // struct _objc_symtab {
4343 // long sel_ref_cnt;
4344 // SEL *refs;
4345 // short cls_def_cnt;
4346 // short cat_def_cnt;
4347 // char *defs[cls_def_cnt + cat_def_cnt];
4348 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004349 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004350 llvm::StructType::create("struct._objc_symtab",
4351 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4352 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004353 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004354
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004355 // struct _objc_module {
4356 // long version;
4357 // long size; // sizeof(struct _objc_module)
4358 // char *name;
4359 // struct _objc_symtab* symtab;
4360 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004361 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004362 llvm::StructType::create("struct._objc_module",
4363 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004364
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004365
Mike Stumpf5408fe2009-05-16 07:57:57 +00004366 // FIXME: This is the size of the setjmp buffer and should be target
4367 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004368 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369
Anders Carlsson124526b2008-09-09 10:10:21 +00004370 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004371 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004372 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373
4374 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004375 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004376 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4377 SetJmpBufferSize),
4378 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004379
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004380}
4381
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004383 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004384 // struct _method_list_t {
4385 // uint32_t entsize; // sizeof(struct _objc_method)
4386 // uint32_t method_count;
4387 // struct _objc_method method_list[method_count];
4388 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004389 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004390 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4391 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004392 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004393 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004394
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395 // struct _protocol_t {
4396 // id isa; // NULL
4397 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004398 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004399 // const struct method_list_t * const instance_methods;
4400 // const struct method_list_t * const class_methods;
4401 // const struct method_list_t *optionalInstanceMethods;
4402 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004403 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004404 // const uint32_t size; // sizeof(struct _protocol_t)
4405 // const uint32_t flags; // = 0
4406 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004407
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004408 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004409 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004410 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004412 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004413 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4414 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4415 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4416 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4417 PropertyListPtrTy, IntTy, IntTy, NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004418
4419 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004420 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004421
Fariborz Jahanianda320092009-01-29 19:24:30 +00004422 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004423 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004424 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004425 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004426 ProtocolListnfABITy->setBody(LongTy,
4427 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4428 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004429
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004430 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004431 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004433 // struct _ivar_t {
4434 // unsigned long int *offset; // pointer to ivar offset location
4435 // char *name;
4436 // char *type;
4437 // uint32_t alignment;
4438 // uint32_t size;
4439 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004440 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004441 llvm::StructType::create("struct._ivar_t",
4442 llvm::PointerType::getUnqual(LongTy),
4443 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004445 // struct _ivar_list_t {
4446 // uint32 entsize; // sizeof(struct _ivar_t)
4447 // uint32 count;
4448 // struct _iver_t list[count];
4449 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004450 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004451 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4452 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004453
Owen Anderson96e0fc72009-07-29 22:16:19 +00004454 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004455
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004456 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004457 // uint32_t const flags;
4458 // uint32_t const instanceStart;
4459 // uint32_t const instanceSize;
4460 // uint32_t const reserved; // only when building for 64bit targets
4461 // const uint8_t * const ivarLayout;
4462 // const char *const name;
4463 // const struct _method_list_t * const baseMethods;
4464 // const struct _objc_protocol_list *const baseProtocols;
4465 // const struct _ivar_list_t *const ivars;
4466 // const uint8_t * const weakIvarLayout;
4467 // const struct _prop_list_t * const properties;
4468 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004469
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004470 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004471 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4472 IntTy, IntTy, IntTy, Int8PtrTy,
4473 Int8PtrTy, MethodListnfABIPtrTy,
4474 ProtocolListnfABIPtrTy,
4475 IvarListnfABIPtrTy,
4476 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004477
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004478 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004479 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004480 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4481 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004482
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004483 // struct _class_t {
4484 // struct _class_t *isa;
4485 // struct _class_t * const superclass;
4486 // void *cache;
4487 // IMP *vtable;
4488 // struct class_ro_t *ro;
4489 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004490
Chris Lattnerc1c20112011-08-12 17:43:31 +00004491 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004492 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4493 llvm::PointerType::getUnqual(ClassnfABITy),
4494 CachePtrTy,
4495 llvm::PointerType::getUnqual(ImpnfABITy),
4496 llvm::PointerType::getUnqual(ClassRonfABITy),
4497 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004499 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004500 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004501
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004502 // struct _category_t {
4503 // const char * const name;
4504 // struct _class_t *const cls;
4505 // const struct _method_list_t * const instance_methods;
4506 // const struct _method_list_t * const class_methods;
4507 // const struct _protocol_list_t * const protocols;
4508 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004509 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004510 CategorynfABITy = llvm::StructType::create("struct._category_t",
4511 Int8PtrTy, ClassnfABIPtrTy,
4512 MethodListnfABIPtrTy,
4513 MethodListnfABIPtrTy,
4514 ProtocolListnfABIPtrTy,
4515 PropertyListPtrTy,
4516 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004517
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004518 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004519 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4520 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004521
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004522 // MessageRefTy - LLVM for:
4523 // struct _message_ref_t {
4524 // IMP messenger;
4525 // SEL name;
4526 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004527
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004528 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004529 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004530 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004531 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004532 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004533 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004534 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004535 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004536 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004537 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004538
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004539 MessageRefCTy = Ctx.getTagDeclType(RD);
4540 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4541 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004542
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004543 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004544 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004546 // SuperMessageRefTy - LLVM for:
4547 // struct _super_message_ref_t {
4548 // SUPER_IMP messenger;
4549 // SEL name;
4550 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004551 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004552 llvm::StructType::create("struct._super_message_ref_t",
4553 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004554
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004555 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004556 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004557
Daniel Dunbare588b992009-03-01 04:46:24 +00004558
4559 // struct objc_typeinfo {
4560 // const void** vtable; // objc_ehtype_vtable + 2
4561 // const char* name; // c++ typeinfo string
4562 // Class cls;
4563 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004564 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004565 llvm::StructType::create("struct._objc_typeinfo",
4566 llvm::PointerType::getUnqual(Int8PtrTy),
4567 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004568 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004569}
4570
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004571llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004572 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004574 return NULL;
4575}
4576
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004577void CGObjCNonFragileABIMac::AddModuleClassList(const
4578 std::vector<llvm::GlobalValue*>
4579 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004580 const char *SymbolName,
4581 const char *SectionName) {
4582 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004583
Daniel Dunbar463b8762009-05-15 21:48:48 +00004584 if (!NumClasses)
4585 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Daniel Dunbar463b8762009-05-15 21:48:48 +00004587 std::vector<llvm::Constant*> Symbols(NumClasses);
4588 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004589 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004590 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004591 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004592 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004593 NumClasses),
4594 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004595
Daniel Dunbar463b8762009-05-15 21:48:48 +00004596 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004597 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004598 llvm::GlobalValue::InternalLinkage,
4599 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004601 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004602 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004603 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004604}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004605
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004606void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4607 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Daniel Dunbar463b8762009-05-15 21:48:48 +00004609 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004610 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004611 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004612 "\01L_OBJC_LABEL_CLASS_$",
4613 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004614
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004615 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4616 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4617 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4618 continue;
4619 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004620 }
4621
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004622 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4623 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4624 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4625 continue;
4626 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4627 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004630 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4631 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004632
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004633 // Build list of all implemented category addresses in array
4634 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004635 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004636 "\01L_OBJC_LABEL_CATEGORY_$",
4637 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004639 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4640 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004641
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004642 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004643}
4644
John McCall944c8432011-05-14 03:10:52 +00004645/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4646/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004647/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004648/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004649bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4650 // At various points we've experimented with using vtable-based
4651 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004652 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4653 default:
John McCall944c8432011-05-14 03:10:52 +00004654 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004655 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004656 return false;
John McCall944c8432011-05-14 03:10:52 +00004657 case CodeGenOptions::NonLegacy:
4658 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004659 case CodeGenOptions::Mixed:
4660 break;
4661 }
4662
4663 // If so, see whether this selector is in the white-list of things which must
4664 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004665 if (VTableDispatchMethods.empty()) {
4666 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4667 VTableDispatchMethods.insert(GetNullarySelector("class"));
4668 VTableDispatchMethods.insert(GetNullarySelector("self"));
4669 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4670 VTableDispatchMethods.insert(GetNullarySelector("length"));
4671 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004672
John McCall944c8432011-05-14 03:10:52 +00004673 // These are vtable-based if GC is disabled.
4674 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004675 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004676 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4677 VTableDispatchMethods.insert(GetNullarySelector("release"));
4678 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4679 }
4680
4681 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4682 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4683 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4684 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4685 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4686 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4687 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4688
4689 // These are vtable-based if GC is enabled.
4690 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004691 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004692 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4693 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4694
4695 // "countByEnumeratingWithState:objects:count"
4696 IdentifierInfo *KeyIdents[] = {
4697 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4698 &CGM.getContext().Idents.get("objects"),
4699 &CGM.getContext().Idents.get("count")
4700 };
4701 VTableDispatchMethods.insert(
4702 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4703 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004704 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004705
John McCall944c8432011-05-14 03:10:52 +00004706 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004707}
4708
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004709// Metadata flags
4710enum MetaDataDlags {
4711 CLS = 0x0,
4712 CLS_META = 0x1,
4713 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004714 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004715 CLS_EXCEPTION = 0x20,
4716
4717 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4718 CLS_HAS_IVAR_RELEASER = 0x40,
4719 /// class was compiled with -fobjc-arr
4720 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004721};
4722/// BuildClassRoTInitializer - generate meta-data for:
4723/// struct _class_ro_t {
4724/// uint32_t const flags;
4725/// uint32_t const instanceStart;
4726/// uint32_t const instanceSize;
4727/// uint32_t const reserved; // only when building for 64bit targets
4728/// const uint8_t * const ivarLayout;
4729/// const char *const name;
4730/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004731/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004732/// const struct _ivar_list_t *const ivars;
4733/// const uint8_t * const weakIvarLayout;
4734/// const struct _prop_list_t * const properties;
4735/// }
4736///
4737llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004738 unsigned flags,
4739 unsigned InstanceStart,
4740 unsigned InstanceSize,
4741 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004742 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004743 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004744
4745 if (CGM.getLangOptions().ObjCAutoRefCount)
4746 flags |= CLS_COMPILED_BY_ARC;
4747
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004748 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4749 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4750 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004751 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004752 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4753 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004754 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004755 // const struct _method_list_t * const baseMethods;
4756 std::vector<llvm::Constant*> Methods;
4757 std::string MethodListName("\01l_OBJC_$_");
4758 if (flags & CLS_META) {
4759 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004761 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004762 // Class methods should always be defined.
4763 Methods.push_back(GetMethodConstant(*i));
4764 }
4765 } else {
4766 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004767 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004768 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004769 // Instance methods should always be defined.
4770 Methods.push_back(GetMethodConstant(*i));
4771 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004772 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004773 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004774 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004775
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004776 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4777 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004778
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004779 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4780 if (llvm::Constant *C = GetMethodConstant(MD))
4781 Methods.push_back(C);
4782 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4783 if (llvm::Constant *C = GetMethodConstant(MD))
4784 Methods.push_back(C);
4785 }
4786 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004787 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788 Values[ 5] = EmitMethodList(MethodListName,
4789 "__DATA, __objc_const", Methods);
4790
Fariborz Jahanianda320092009-01-29 19:24:30 +00004791 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4792 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004793 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004794 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004795 OID->all_referenced_protocol_begin(),
4796 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004797
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004798 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004799 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004800 else
4801 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004802 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4803 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004804 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004805 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004806 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004807 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4808 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004809 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004810 Values);
4811 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004812 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4813 llvm::GlobalValue::InternalLinkage,
4814 Init,
4815 (flags & CLS_META) ?
4816 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4817 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004818 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004819 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004820 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004821 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004822
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004823}
4824
4825/// BuildClassMetaData - This routine defines that to-level meta-data
4826/// for the given ClassName for:
4827/// struct _class_t {
4828/// struct _class_t *isa;
4829/// struct _class_t * const superclass;
4830/// void *cache;
4831/// IMP *vtable;
4832/// struct class_ro_t *ro;
4833/// }
4834///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004835llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004836 std::string &ClassName,
4837 llvm::Constant *IsAGV,
4838 llvm::Constant *SuperClassGV,
4839 llvm::Constant *ClassRoGV,
4840 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004841 llvm::Constant *Values[] = {
4842 IsAGV,
4843 SuperClassGV,
4844 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4845 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4846 ClassRoGV // &CLASS_RO_GV
4847 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848 if (!Values[1])
4849 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004850 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004851 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004852 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4853 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004854 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004855 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004856 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004857 if (HiddenVisibility)
4858 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004859 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004860}
4861
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004863CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004864 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004865}
4866
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004867void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004868 uint32_t &InstanceStart,
4869 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004870 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004871 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004873 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004874 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004875
4876 // If there are no fields, the start is the same as the end.
4877 if (!RL.getFieldCount())
4878 InstanceStart = InstanceSize;
4879 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004880 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004881}
4882
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004883void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4884 std::string ClassName = ID->getNameAsString();
4885 if (!ObjCEmptyCacheVar) {
4886 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004887 CGM.getModule(),
4888 ObjCTypes.CacheTy,
4889 false,
4890 llvm::GlobalValue::ExternalLinkage,
4891 0,
4892 "_objc_empty_cache");
4893
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004894 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004895 CGM.getModule(),
4896 ObjCTypes.ImpnfABITy,
4897 false,
4898 llvm::GlobalValue::ExternalLinkage,
4899 0,
4900 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004901 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004902 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004903 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004904 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004905 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004906 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004907 uint32_t InstanceSize = InstanceStart;
4908 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004909 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4910 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004911
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004912 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004913
4914 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004915 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004916 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004917 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004918 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004919 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004920 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004921 // class is root
4922 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004923 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004924 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004925 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004926 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004927 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4928 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4929 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004930 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004931 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004932 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004933 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004934 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004935 ObjCMetaClassName +
4936 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004937 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004938 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004939 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004940 }
4941 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4942 InstanceStart,
4943 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004944 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004945 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004946 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4947 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004948 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004949
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004950 // Metadata for the class
4951 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004952 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004953 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004954 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004955 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004956
Douglas Gregor68584ed2009-06-18 16:11:24 +00004957 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004958 flags |= CLS_EXCEPTION;
4959
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004960 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004961 flags |= CLS_ROOT;
4962 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004963 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004964 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004965 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004966 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004967 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004968 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004969 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004970 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004971 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004972 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004973 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004974 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004975 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004976
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004977 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004978 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004979 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4980 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004981 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004982
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004983 // Determine if this class is also "non-lazy".
4984 if (ImplementationIsNonLazy(ID))
4985 DefinedNonLazyClasses.push_back(ClassMD);
4986
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004987 // Force the definition of the EHType if necessary.
4988 if (flags & CLS_EXCEPTION)
4989 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00004990 // Make sure method definition entries are all clear for next implementation.
4991 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004992}
4993
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004994/// GenerateProtocolRef - This routine is called to generate code for
4995/// a protocol reference expression; as in:
4996/// @code
4997/// @protocol(Proto1);
4998/// @endcode
4999/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5000/// which will hold address of the protocol meta-data.
5001///
5002llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005003 const ObjCProtocolDecl *PD) {
5004
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005005 // This routine is called for @protocol only. So, we must build definition
5006 // of protocol's meta-data (not a reference to it!)
5007 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 llvm::Constant *Init =
5009 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5010 ObjCTypes.ExternalProtocolPtrTy);
5011
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005012 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005013 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005015 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5016 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005017 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005018 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005019 CGM.getModule(),
5020 Init->getType(), false,
5021 llvm::GlobalValue::WeakAnyLinkage,
5022 Init,
5023 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005024 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5025 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005026 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005027 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005028}
5029
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005030/// GenerateCategory - Build metadata for a category implementation.
5031/// struct _category_t {
5032/// const char * const name;
5033/// struct _class_t *const cls;
5034/// const struct _method_list_t * const instance_methods;
5035/// const struct _method_list_t * const class_methods;
5036/// const struct _protocol_list_t * const protocols;
5037/// const struct _prop_list_t * const properties;
5038/// }
5039///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005040void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005041 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005042 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005043 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5044 "_$_" + OCD->getNameAsString());
5045 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005046 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005047
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005048 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005049 Values[0] = GetClassName(OCD->getIdentifier());
5050 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005051 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005052 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005053 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5054
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005055 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005056 std::vector<llvm::Constant*> Methods;
5057 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005058 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005059 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005060
5061 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005062 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005063 // Instance methods should always be defined.
5064 Methods.push_back(GetMethodConstant(*i));
5065 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005066
5067 Values[2] = EmitMethodList(MethodListName,
5068 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005069 Methods);
5070
5071 MethodListName = Prefix;
5072 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5073 OCD->getNameAsString();
5074 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005076 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005077 // Class methods should always be defined.
5078 Methods.push_back(GetMethodConstant(*i));
5079 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005080
5081 Values[3] = EmitMethodList(MethodListName,
5082 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005083 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005084 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005085 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005086 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005087 llvm::SmallString<256> ExtName;
5088 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5089 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005090 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005091 + Interface->getName() + "_$_"
5092 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005093 Category->protocol_begin(),
5094 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005095 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5096 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005097 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005098 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5099 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005100 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005101
5102 llvm::Constant *Init =
5103 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005104 Values);
5105 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005106 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005107 false,
5108 llvm::GlobalValue::InternalLinkage,
5109 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005110 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005111 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005112 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005113 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005114 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005115 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005116
5117 // Determine if this category is also "non-lazy".
5118 if (ImplementationIsNonLazy(OCD))
5119 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005120 // method definition entries must be clear for next implementation.
5121 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005122}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005123
5124/// GetMethodConstant - Return a struct objc_method constant for the
5125/// given method if it has been defined. The result is null if the
5126/// method has not been defined. The return value has type MethodPtrTy.
5127llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005128 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005129 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005130 if (!Fn)
5131 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005132
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005133 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005134 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005135 ObjCTypes.SelectorPtrTy),
5136 GetMethodVarType(MD),
5137 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5138 };
Owen Anderson08e25242009-07-27 22:29:56 +00005139 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005140}
5141
5142/// EmitMethodList - Build meta-data for method declarations
5143/// struct _method_list_t {
5144/// uint32_t entsize; // sizeof(struct _objc_method)
5145/// uint32_t method_count;
5146/// struct _objc_method method_list[method_count];
5147/// }
5148///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005149llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005150 const char *Section,
5151 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005152 // Return null for empty list.
5153 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005154 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005155
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005156 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005157 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005158 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005159 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005160 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005161 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005162 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005163 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005164 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005165 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005166
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005167 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005168 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005169 llvm::GlobalValue::InternalLinkage, Init, Name);
5170 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005171 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005172 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005173 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005174}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005175
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005176/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5177/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005178llvm::GlobalVariable *
5179CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5180 const ObjCIvarDecl *Ivar) {
5181 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005182 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005183 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005184 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005185 CGM.getModule().getGlobalVariable(Name);
5186 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005187 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005188 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005189 false,
5190 llvm::GlobalValue::ExternalLinkage,
5191 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005192 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005193 return IvarOffsetGV;
5194}
5195
Daniel Dunbare83be122010-04-02 21:14:02 +00005196llvm::Constant *
5197CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5198 const ObjCIvarDecl *Ivar,
5199 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005200 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005201 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005202 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005203 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005204 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005205
Mike Stumpf5408fe2009-05-16 07:57:57 +00005206 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5207 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005208 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5209 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005210 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005211 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005212 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005213 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005214 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005215 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005216}
5217
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005218/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005219/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005220/// IvarListnfABIPtrTy.
5221/// struct _ivar_t {
5222/// unsigned long int *offset; // pointer to ivar offset location
5223/// char *name;
5224/// char *type;
5225/// uint32_t alignment;
5226/// uint32_t size;
5227/// }
5228/// struct _ivar_list_t {
5229/// uint32 entsize; // sizeof(struct _ivar_t)
5230/// uint32 count;
5231/// struct _iver_t list[count];
5232/// }
5233///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005234
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005235llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005236 const ObjCImplementationDecl *ID) {
5237
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005238 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005239
Jordy Rosedb8264e2011-07-22 02:08:32 +00005240 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005241 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005243 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005244
Jordy Rosedb8264e2011-07-22 02:08:32 +00005245 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005246 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005247 // Ignore unnamed bit-fields.
5248 if (!IVD->getDeclName())
5249 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005250 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005251 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005252 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005253 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5254 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005255 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005256 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005257 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005258 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005259 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005260 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005261 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005262 // NOTE. Size of a bitfield does not match gcc's, because of the
5263 // way bitfields are treated special in each. But I am told that
5264 // 'size' for bitfield ivars is ignored by the runtime so it does
5265 // not matter. If it matters, there is enough info to get the
5266 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005267 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005268 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005269 }
5270 // Return null for empty list.
5271 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005272 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005273
5274 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005275 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005276 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5277 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005278 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005279 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005280 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005281 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005282 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5283 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005284 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005285 llvm::GlobalValue::InternalLinkage,
5286 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005287 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005288 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005289 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005290 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291
Chris Lattnerad64e022009-07-17 23:57:13 +00005292 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005293 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005294}
5295
5296llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005298 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
Fariborz Jahanianda320092009-01-29 19:24:30 +00005300 if (!Entry) {
5301 // We use the initializer as a marker of whether this is a forward
5302 // reference or not. At module finalization we add the empty
5303 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304 Entry =
5305 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5306 llvm::GlobalValue::ExternalLinkage,
5307 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005308 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005309 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311
Fariborz Jahanianda320092009-01-29 19:24:30 +00005312 return Entry;
5313}
5314
5315/// GetOrEmitProtocol - Generate the protocol meta-data:
5316/// @code
5317/// struct _protocol_t {
5318/// id isa; // NULL
5319/// const char * const protocol_name;
5320/// const struct _protocol_list_t * protocol_list; // super protocols
5321/// const struct method_list_t * const instance_methods;
5322/// const struct method_list_t * const class_methods;
5323/// const struct method_list_t *optionalInstanceMethods;
5324/// const struct method_list_t *optionalClassMethods;
5325/// const struct _prop_list_t * properties;
5326/// const uint32_t size; // sizeof(struct _protocol_t)
5327/// const uint32_t flags; // = 0
5328/// }
5329/// @endcode
5330///
5331
5332llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005333 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005334 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005335
Fariborz Jahanianda320092009-01-29 19:24:30 +00005336 // Early exit if a defining object has already been generated.
5337 if (Entry && Entry->hasInitializer())
5338 return Entry;
5339
Fariborz Jahanianda320092009-01-29 19:24:30 +00005340 // Construct method lists.
5341 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5342 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005344 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005345 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005346 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005347 if (!C)
5348 return GetOrEmitProtocolRef(PD);
5349
Fariborz Jahanianda320092009-01-29 19:24:30 +00005350 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5351 OptInstanceMethods.push_back(C);
5352 } else {
5353 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005354 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005355 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005356
5357 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005358 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005359 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005360 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005361 if (!C)
5362 return GetOrEmitProtocolRef(PD);
5363
Fariborz Jahanianda320092009-01-29 19:24:30 +00005364 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5365 OptClassMethods.push_back(C);
5366 } else {
5367 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005368 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005369 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005370
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005371 llvm::Constant *Values[10];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005372 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005373 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005374 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005375 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5376 PD->protocol_begin(),
5377 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005379 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005380 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005381 "__DATA, __objc_const",
5382 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005383 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005384 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005385 "__DATA, __objc_const",
5386 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005387 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005388 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005389 "__DATA, __objc_const",
5390 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005391 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005392 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 "__DATA, __objc_const",
5394 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005395 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005396 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005398 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005399 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005400 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005401 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005402 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 if (Entry) {
5405 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005406 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005407 Entry->setInitializer(Init);
5408 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005410 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5411 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5412 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005413 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005414 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005417 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005418 CGM.AddUsedGlobal(Entry);
5419
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005420 // Use this protocol meta-data to build protocol list table in section
5421 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005422 llvm::GlobalVariable *PTGV =
5423 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5424 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5425 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005426 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005427 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005428 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005429 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005430 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005431 return Entry;
5432}
5433
5434/// EmitProtocolList - Generate protocol list meta-data:
5435/// @code
5436/// struct _protocol_list_t {
5437/// long protocol_count; // Note, this is 32/64 bit
5438/// struct _protocol_t[protocol_count];
5439/// }
5440/// @endcode
5441///
5442llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005443CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005444 ObjCProtocolDecl::protocol_iterator begin,
5445 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005446 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005447
Fariborz Jahanianda320092009-01-29 19:24:30 +00005448 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005450 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005451
Daniel Dunbar948e2582009-02-15 07:36:20 +00005452 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005453 llvm::SmallString<256> TmpName;
5454 Name.toVector(TmpName);
5455 llvm::GlobalVariable *GV =
5456 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005458 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459
Daniel Dunbar948e2582009-02-15 07:36:20 +00005460 for (; begin != end; ++begin)
5461 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5462
Fariborz Jahanianda320092009-01-29 19:24:30 +00005463 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005464 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005465 ObjCTypes.ProtocolnfABIPtrTy));
5466
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005467 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005468 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005469 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005470 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005471 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005472 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 ProtocolRefs.size()),
5474 ProtocolRefs);
5475
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005476 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005477 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005478 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005479 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005480 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005481 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005482 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005483 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005485 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005486}
5487
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005488/// GetMethodDescriptionConstant - This routine build following meta-data:
5489/// struct _objc_method {
5490/// SEL _cmd;
5491/// char *method_type;
5492/// char *_imp;
5493/// }
5494
5495llvm::Constant *
5496CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005497 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005498 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5500 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005501 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005502 if (!Desc[1])
5503 return 0;
5504
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005505 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005506 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005507 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005508}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005509
5510/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5511/// This code gen. amounts to generating code for:
5512/// @code
5513/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5514/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005515///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005516LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005517 CodeGen::CodeGenFunction &CGF,
5518 QualType ObjectTy,
5519 llvm::Value *BaseValue,
5520 const ObjCIvarDecl *Ivar,
5521 unsigned CVRQualifiers) {
5522 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005523 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5524 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005525}
5526
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005527llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005528 CodeGen::CodeGenFunction &CGF,
5529 const ObjCInterfaceDecl *Interface,
5530 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005531 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005532}
5533
John McCallb1e81442011-05-13 23:16:18 +00005534static void appendSelectorForMessageRefTable(std::string &buffer,
5535 Selector selector) {
5536 if (selector.isUnarySelector()) {
5537 buffer += selector.getNameForSlot(0);
5538 return;
5539 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005540
John McCallb1e81442011-05-13 23:16:18 +00005541 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5542 buffer += selector.getNameForSlot(i);
5543 buffer += '_';
5544 }
5545}
5546
John McCall944c8432011-05-14 03:10:52 +00005547/// Emit a "v-table" message send. We emit a weak hidden-visibility
5548/// struct, initially containing the selector pointer and a pointer to
5549/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5550/// load and call the function pointer, passing the address of the
5551/// struct as the second parameter. The runtime determines whether
5552/// the selector is currently emitted using vtable dispatch; if so, it
5553/// substitutes a stub function which simply tail-calls through the
5554/// appropriate vtable slot, and if not, it substitues a stub function
5555/// which tail-calls objc_msgSend. Both stubs adjust the selector
5556/// argument to correctly point to the selector.
5557RValue
5558CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5559 ReturnValueSlot returnSlot,
5560 QualType resultType,
5561 Selector selector,
5562 llvm::Value *arg0,
5563 QualType arg0Type,
5564 bool isSuper,
5565 const CallArgList &formalArgs,
5566 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005567 // Compute the actual arguments.
5568 CallArgList args;
5569
John McCall944c8432011-05-14 03:10:52 +00005570 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005571 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005572 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5573 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005574
John McCall944c8432011-05-14 03:10:52 +00005575 // Second argument: a pointer to the message ref structure. Leave
5576 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005577 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5578
5579 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5580
5581 const CGFunctionInfo &fnInfo =
5582 CGM.getTypes().getFunctionInfo(resultType, args,
5583 FunctionType::ExtInfo());
5584
John McCallcba681a2011-05-14 21:12:11 +00005585 NullReturnState nullReturn;
5586
John McCall944c8432011-05-14 03:10:52 +00005587 // Find the function to call and the mangled name for the message
5588 // ref structure. Using a different mangled name wouldn't actually
5589 // be a problem; it would just be a waste.
5590 //
5591 // The runtime currently never uses vtable dispatch for anything
5592 // except normal, non-super message-sends.
5593 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005594 llvm::Constant *fn = 0;
5595 std::string messageRefName("\01l_");
5596 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005597 if (isSuper) {
5598 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5599 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005600 } else {
John McCallcba681a2011-05-14 21:12:11 +00005601 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005602 fn = ObjCTypes.getMessageSendStretFixupFn();
5603 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005604 }
John McCallb1e81442011-05-13 23:16:18 +00005605 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5606 fn = ObjCTypes.getMessageSendFpretFixupFn();
5607 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005608 } else {
John McCallb1e81442011-05-13 23:16:18 +00005609 if (isSuper) {
5610 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5611 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005612 } else {
John McCallb1e81442011-05-13 23:16:18 +00005613 fn = ObjCTypes.getMessageSendFixupFn();
5614 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005615 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005616 }
John McCallb1e81442011-05-13 23:16:18 +00005617 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5618 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005619
John McCallb1e81442011-05-13 23:16:18 +00005620 // Append the selector name, except use underscores anywhere we
5621 // would have used colons.
5622 appendSelectorForMessageRefTable(messageRefName, selector);
5623
5624 llvm::GlobalVariable *messageRef
5625 = CGM.getModule().getGlobalVariable(messageRefName);
5626 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005627 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005628 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005629 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005630 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5631 init->getType(),
5632 /*constant*/ false,
5633 llvm::GlobalValue::WeakAnyLinkage,
5634 init,
5635 messageRefName);
5636 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5637 messageRef->setAlignment(16);
5638 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5639 }
5640 llvm::Value *mref =
5641 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5642
John McCall944c8432011-05-14 03:10:52 +00005643 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005644 args[1].RV = RValue::get(mref);
5645
5646 // Load the function to call from the message ref table.
5647 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5648 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5649
5650 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005651 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005652 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5653 callee = CGF.Builder.CreateBitCast(callee,
5654 llvm::PointerType::getUnqual(fnType));
5655
John McCallcba681a2011-05-14 21:12:11 +00005656 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5657 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005658}
5659
5660/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005661CodeGen::RValue
5662CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005663 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005664 QualType ResultType,
5665 Selector Sel,
5666 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005667 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005668 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005669 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005670 return isVTableDispatchedSelector(Sel)
5671 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005672 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005673 false, CallArgs, Method)
5674 : EmitMessageSend(CGF, Return, ResultType,
5675 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005676 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005677 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005678}
5679
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005680llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005681CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005682 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5683
Daniel Dunbardfff2302009-03-02 05:18:14 +00005684 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005686 false, llvm::GlobalValue::ExternalLinkage,
5687 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005688 }
5689
5690 return GV;
5691}
5692
John McCallf85e1932011-06-15 23:02:42 +00005693llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5694 IdentifierInfo *II) {
5695 llvm::GlobalVariable *&Entry = ClassReferences[II];
5696
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005697 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005698 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005699 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005700 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005701 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5702 false, llvm::GlobalValue::InternalLinkage,
5703 ClassGV,
5704 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005705 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005706 CGM.getTargetData().getABITypeAlignment(
5707 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005708 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005709 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005710 }
John McCallf85e1932011-06-15 23:02:42 +00005711
Benjamin Kramer578faa82011-09-27 21:06:10 +00005712 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005713}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005714
John McCallf85e1932011-06-15 23:02:42 +00005715llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5716 const ObjCInterfaceDecl *ID) {
5717 return EmitClassRefFromId(Builder, ID->getIdentifier());
5718}
5719
5720llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5721 CGBuilderTy &Builder) {
5722 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5723 return EmitClassRefFromId(Builder, II);
5724}
5725
Daniel Dunbar11394522009-04-18 08:51:00 +00005726llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005727CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005728 const ObjCInterfaceDecl *ID) {
5729 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005730
Daniel Dunbar11394522009-04-18 08:51:00 +00005731 if (!Entry) {
5732 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5733 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734 Entry =
5735 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005736 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005738 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005739 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005740 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005742 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005743 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005744 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005745
Benjamin Kramer578faa82011-09-27 21:06:10 +00005746 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005747}
5748
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005749/// EmitMetaClassRef - Return a Value * of the address of _class_t
5750/// meta-data
5751///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5753 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5755 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005756 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005757
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005758 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005759 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005760 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005761 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005762 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005764 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005765 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005766 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005767 ObjCTypes.ClassnfABIPtrTy));
5768
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005769 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005770 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005771
Benjamin Kramer578faa82011-09-27 21:06:10 +00005772 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005773}
5774
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005775/// GetClass - Return a reference to the class for the given interface
5776/// decl.
5777llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5778 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005779 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005780 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5781 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5782 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5783 }
5784
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005785 return EmitClassRef(Builder, ID);
5786}
5787
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005788/// Generates a message send where the super is the receiver. This is
5789/// a message send to self with special delivery semantics indicating
5790/// which class's method should be called.
5791CodeGen::RValue
5792CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005793 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005794 QualType ResultType,
5795 Selector Sel,
5796 const ObjCInterfaceDecl *Class,
5797 bool isCategoryImpl,
5798 llvm::Value *Receiver,
5799 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005800 const CodeGen::CallArgList &CallArgs,
5801 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005802 // ...
5803 // Create and init a super structure; this is a (receiver, class)
5804 // pair we will pass to objc_msgSendSuper.
5805 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005806 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005807
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005808 llvm::Value *ReceiverAsObject =
5809 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5810 CGF.Builder.CreateStore(ReceiverAsObject,
5811 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005812
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005813 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005814 llvm::Value *Target;
5815 if (IsClassMessage) {
5816 if (isCategoryImpl) {
5817 // Message sent to "super' in a class method defined in
5818 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005819 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005820 Target = CGF.Builder.CreateStructGEP(Target, 0);
5821 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005822 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005823 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005824 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005825 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826
Mike Stumpf5408fe2009-05-16 07:57:57 +00005827 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5828 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005829 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005830 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5831 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5832 CGF.Builder.CreateStore(Target,
5833 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834
John McCall944c8432011-05-14 03:10:52 +00005835 return (isVTableDispatchedSelector(Sel))
5836 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005837 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005838 true, CallArgs, Method)
5839 : EmitMessageSend(CGF, Return, ResultType,
5840 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005841 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005842 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005843}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005844
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005845llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005846 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005847 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005848
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005849 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850 llvm::Constant *Casted =
5851 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5852 ObjCTypes.SelectorPtrTy);
5853 Entry =
5854 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5855 llvm::GlobalValue::InternalLinkage,
5856 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005857 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005858 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005859 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005860
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005861 if (lval)
5862 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00005863 return Builder.CreateLoad(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005864}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005865/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005866/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005867///
5868void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005869 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005870 llvm::Value *dst,
5871 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005872 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005873 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005874 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005875 assert(Size <= 8 && "does not support size > 8");
5876 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5877 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005878 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5879 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005880 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5881 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005882 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5883 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005884 return;
5885}
5886
5887/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5888/// objc_assign_strongCast (id src, id *dst)
5889///
5890void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005891 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005892 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005893 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005894 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005895 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005896 assert(Size <= 8 && "does not support size > 8");
5897 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005898 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005899 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5900 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005901 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5902 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005903 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005904 src, dst, "weakassign");
5905 return;
5906}
5907
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005908void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005909 CodeGen::CodeGenFunction &CGF,
5910 llvm::Value *DestPtr,
5911 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005912 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005913 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5914 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005915 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005916 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005917 return;
5918}
5919
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005920/// EmitObjCWeakRead - Code gen for loading value of a __weak
5921/// object: objc_read_weak (id *src)
5922///
5923llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005924 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005925 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005926 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005927 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5928 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005929 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005930 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005931 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005932 return read_weak;
5933}
5934
5935/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5936/// objc_assign_weak (id src, id *dst)
5937///
5938void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005939 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005940 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005941 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005942 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005943 assert(Size <= 8 && "does not support size > 8");
5944 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5945 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005946 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5947 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005948 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5949 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005950 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005951 src, dst, "weakassign");
5952 return;
5953}
5954
5955/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5956/// objc_assign_global (id src, id *dst)
5957///
5958void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005959 llvm::Value *src, llvm::Value *dst,
5960 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005961 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005962 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005963 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005964 assert(Size <= 8 && "does not support size > 8");
5965 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5966 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005967 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5968 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005969 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5970 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005971 if (!threadlocal)
5972 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5973 src, dst, "globalassign");
5974 else
5975 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5976 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005977 return;
5978}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005979
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005980void
John McCallf1549f62010-07-06 01:34:17 +00005981CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5982 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005983 EmitAtSynchronizedStmt(CGF, S,
5984 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5985 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005986}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005987
John McCall5a180392010-07-24 00:37:23 +00005988llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00005989CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00005990 // There's a particular fixed type info for 'id'.
5991 if (T->isObjCIdType() ||
5992 T->isObjCQualifiedIdType()) {
5993 llvm::Constant *IDEHType =
5994 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5995 if (!IDEHType)
5996 IDEHType =
5997 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5998 false,
5999 llvm::GlobalValue::ExternalLinkage,
6000 0, "OBJC_EHTYPE_id");
6001 return IDEHType;
6002 }
6003
6004 // All other types should be Objective-C interface pointer types.
6005 const ObjCObjectPointerType *PT =
6006 T->getAs<ObjCObjectPointerType>();
6007 assert(PT && "Invalid @catch type.");
6008 const ObjCInterfaceType *IT = PT->getInterfaceType();
6009 assert(IT && "Invalid @catch type.");
6010 return GetInterfaceEHType(IT->getDecl(), false);
6011}
6012
John McCallf1549f62010-07-06 01:34:17 +00006013void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6014 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006015 EmitTryCatchStmt(CGF, S,
6016 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6017 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6018 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006019}
6020
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006021/// EmitThrowStmt - Generate code for a throw statement.
6022void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6023 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006024 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006025 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006026 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006027 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006028 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006029 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006030 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006031 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006032 }
6033
John McCall7ec404c2010-10-16 08:21:07 +00006034 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006035 CGF.Builder.ClearInsertionPoint();
6036}
Daniel Dunbare588b992009-03-01 04:46:24 +00006037
John McCall5a180392010-07-24 00:37:23 +00006038llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006039CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006040 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006041 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006042
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006043 // If we don't need a definition, return the entry if found or check
6044 // if we use an external reference.
6045 if (!ForDefinition) {
6046 if (Entry)
6047 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006048
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006049 // If this type (or a super class) has the __objc_exception__
6050 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006051 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006052 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006053 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006054 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006055 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006056 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006057 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006058 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006059
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006060 // Otherwise we need to either make a new entry or fill in the
6061 // initializer.
6062 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006063 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006064 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006065 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006066 CGM.getModule().getGlobalVariable(VTableName);
6067 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006068 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6069 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006070 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006071 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006072
Chris Lattner77b89b82010-06-27 07:15:29 +00006073 llvm::Value *VTableIdx =
6074 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006075
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006076 llvm::Constant *Values[] = {
6077 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6078 GetClassName(ID->getIdentifier()),
6079 GetClassGlobal(ClassName)
6080 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006081 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006082 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006083
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006084 if (Entry) {
6085 Entry->setInitializer(Init);
6086 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006087 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006088 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006089 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006090 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006091 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006092 }
6093
John McCall1fb0caa2010-10-22 21:05:15 +00006094 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006095 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006096 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6097 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006098
6099 if (ForDefinition) {
6100 Entry->setSection("__DATA,__objc_const");
6101 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6102 } else {
6103 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6104 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006105
6106 return Entry;
6107}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006108
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006109/* *** */
6110
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006111CodeGen::CGObjCRuntime *
6112CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006113 if (CGM.getLangOptions().ObjCNonFragileABI)
6114 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006115 return new CGObjCMac(CGM);
6116}