blob: c0f016af57094b0a6b0310019315bcca835a43d8 [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 Dunbardbc933702008-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) {
1063 assert(false && "CGObjCMac::GetClassGlobal");
1064 return 0;
1065 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001066};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001068class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001069private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001070 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001071 llvm::GlobalVariable* ObjCEmptyCacheVar;
1072 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001073
Daniel Dunbar11394522009-04-18 08:51:00 +00001074 /// SuperClassReferences - uniqued super class references.
1075 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001076
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001077 /// MetaClassReferences - uniqued meta class references.
1078 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001079
1080 /// EHTypeReferences - uniqued class ehtype references.
1081 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001082
John McCall944c8432011-05-14 03:10:52 +00001083 /// VTableDispatchMethods - List of methods for which we generate
1084 /// vtable-based message dispatch.
1085 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001086
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001087 /// DefinedMetaClasses - List of defined meta-classes.
1088 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1089
John McCall944c8432011-05-14 03:10:52 +00001090 /// isVTableDispatchedSelector - Returns true if SEL is a
1091 /// vtable-based selector.
1092 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001093
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001094 /// FinishNonFragileABIModule - Write out global data structures at the end of
1095 /// processing a translation unit.
1096 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001097
Daniel Dunbar463b8762009-05-15 21:48:48 +00001098 /// AddModuleClassList - Add the given list of class pointers to the
1099 /// module with the provided symbol and section names.
1100 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1101 const char *SymbolName,
1102 const char *SectionName);
1103
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001104 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1105 unsigned InstanceStart,
1106 unsigned InstanceSize,
1107 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001108 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001109 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001110 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001111 llvm::Constant *ClassRoGV,
1112 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001113
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001114 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001115
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001116 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001117
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001118 /// EmitMethodList - Emit the method list for the given
1119 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001120 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001121 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001122 const ConstantVector &Methods);
1123 /// EmitIvarList - Emit the ivar list for the given
1124 /// implementation. If ForClass is true the list of class ivars
1125 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1126 /// interface ivars will be emitted. The return value has type
1127 /// IvarListnfABIPtrTy.
1128 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001129
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001130 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001131 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001132 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001133
Fariborz Jahanianda320092009-01-29 19:24:30 +00001134 /// GetOrEmitProtocol - Get the protocol object for the given
1135 /// declaration, emitting it if necessary. The return value has type
1136 /// ProtocolPtrTy.
1137 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001138
Fariborz Jahanianda320092009-01-29 19:24:30 +00001139 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1140 /// object for the given declaration, emitting it if needed. These
1141 /// forward references will be filled in with empty bodies if no
1142 /// definition is seen. The return value has type ProtocolPtrTy.
1143 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001144
Fariborz Jahanianda320092009-01-29 19:24:30 +00001145 /// EmitProtocolList - Generate the list of referenced
1146 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001147 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001148 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001149 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001150
John McCall944c8432011-05-14 03:10:52 +00001151 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1152 ReturnValueSlot Return,
1153 QualType ResultType,
1154 Selector Sel,
1155 llvm::Value *Receiver,
1156 QualType Arg0Ty,
1157 bool IsSuper,
1158 const CallArgList &CallArgs,
1159 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001160
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001161 /// GetClassGlobal - Return the global variable for the Objective-C
1162 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001163 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001164
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001165 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001166 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001167 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001168 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001169
1170 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1171 IdentifierInfo *II);
1172
1173 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174
Daniel Dunbar11394522009-04-18 08:51:00 +00001175 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1176 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001177 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1178 const ObjCInterfaceDecl *ID);
1179
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001180 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1181 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001182 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001183 const ObjCInterfaceDecl *ID);
1184
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001185 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1186 /// the given ivar.
1187 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001188 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001189 const ObjCInterfaceDecl *ID,
1190 const ObjCIvarDecl *Ivar);
1191
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001192 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1193 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001194 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1195 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001196
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001197 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001198 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001199 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001200 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001201
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001203 return "OBJC_METACLASS_$_";
1204 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001205
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001206 const char *getClassSymbolPrefix() const {
1207 return "OBJC_CLASS_$_";
1208 }
1209
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001210 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001211 uint32_t &InstanceStart,
1212 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001213
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001214 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001215 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001216 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1217 return CGM.getContext().Selectors.getSelector(0, &II);
1218 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001219
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001220 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001221 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1222 return CGM.getContext().Selectors.getSelector(1, &II);
1223 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001224
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001225 /// ImplementationIsNonLazy - Check whether the given category or
1226 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001227 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001228
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001229public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001230 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001231 // FIXME. All stubs for now!
1232 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001233
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001234 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001235 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001236 QualType ResultType,
1237 Selector Sel,
1238 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001239 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001240 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001241 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
1243 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001244 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001245 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001246 QualType ResultType,
1247 Selector Sel,
1248 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001249 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001250 llvm::Value *Receiver,
1251 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001252 const CallArgList &CallArgs,
1253 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001254
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001255 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001256 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001258 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1259 bool lvalue = false)
1260 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001261
1262 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1263 /// untyped one.
1264 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1265 const ObjCMethodDecl *Method)
1266 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001267
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001268 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001269
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001270 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001271 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001272 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001274 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001275
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001276 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001277 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001278 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279 virtual llvm::Constant *GetPropertySetFunction() {
1280 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001281 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001282
David Chisnall8fac25d2010-12-26 22:13:16 +00001283 virtual llvm::Constant *GetSetStructFunction() {
1284 return ObjCTypes.getCopyStructFn();
1285 }
1286 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001287 return ObjCTypes.getCopyStructFn();
1288 }
1289
Chris Lattner74391b42009-03-22 21:03:39 +00001290 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001291 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001292 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001293
John McCallf1549f62010-07-06 01:34:17 +00001294 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1295 const ObjCAtTryStmt &S);
1296 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1297 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001298 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001299 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001300 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001301 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001303 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001304 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001305 llvm::Value *src, llvm::Value *dest,
1306 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001307 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001308 llvm::Value *src, llvm::Value *dest,
1309 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001310 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001311 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001312 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1313 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001314 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001315 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1316 QualType ObjectTy,
1317 llvm::Value *BaseValue,
1318 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001319 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001320 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001321 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001322 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001323};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
John McCallcba681a2011-05-14 21:12:11 +00001325/// A helper class for performing the null-initialization of a return
1326/// value.
1327struct NullReturnState {
1328 llvm::BasicBlock *NullBB;
1329
1330 NullReturnState() : NullBB(0) {}
1331
1332 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1333 // Make blocks for the null-init and call edges.
1334 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1335 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1336
1337 // Check for a null receiver and, if there is one, jump to the
1338 // null-init test.
1339 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1340 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1341
1342 // Otherwise, start performing the call.
1343 CGF.EmitBlock(callBB);
1344 }
1345
1346 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1347 if (!NullBB) return result;
1348
1349 // Finish the call path.
1350 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1351 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1352
1353 // Emit the null-init block and perform the null-initialization there.
1354 CGF.EmitBlock(NullBB);
1355 assert(result.isAggregate() && "null init of non-aggregate result?");
1356 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1357
1358 // Jump to the continuation block.
1359 CGF.EmitBlock(contBB);
1360
1361 return result;
1362 }
1363};
1364
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001365} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001366
1367/* *** Helper Functions *** */
1368
1369/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001370static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001371 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001372 unsigned idx0,
1373 unsigned idx1) {
1374 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001375 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1376 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001377 };
Jay Foada5c04342011-07-21 14:31:17 +00001378 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001379}
1380
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001381/// hasObjCExceptionAttribute - Return true if this class or any super
1382/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001383static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001384 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001385 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001386 return true;
1387 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001388 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001389 return false;
1390}
1391
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001392/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001393
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001394CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001395 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001396 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001398}
1399
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001400/// GetClass - Return a reference to the class for the given interface
1401/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001402llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001403 const ObjCInterfaceDecl *ID) {
1404 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001405}
1406
1407/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001408llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1409 bool lval) {
1410 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001411}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001412llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001413 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001414 return EmitSelector(Builder, Method->getSelector());
1415}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001416
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001417llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001418 if (T->isObjCIdType() ||
1419 T->isObjCQualifiedIdType()) {
1420 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001421 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001422 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001423 if (T->isObjCClassType() ||
1424 T->isObjCQualifiedClassType()) {
1425 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001426 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001427 }
1428 if (T->isObjCObjectPointerType())
1429 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1430
John McCall5a180392010-07-24 00:37:23 +00001431 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1432 return 0;
1433}
1434
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001435/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001436/*
1437 struct __builtin_CFString {
1438 const int *isa; // point to __CFConstantStringClassReference
1439 int flags;
1440 const char *str;
1441 long length;
1442 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001443*/
1444
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001445/// or Generate a constant NSString object.
1446/*
1447 struct __builtin_NSString {
1448 const int *isa; // point to __NSConstantStringClassReference
1449 const char *str;
1450 unsigned int length;
1451 };
1452*/
1453
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001454llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001455 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001456 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1457 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001458 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001459}
1460
1461/// Generates a message send where the super is the receiver. This is
1462/// a message send to self with special delivery semantics indicating
1463/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001464CodeGen::RValue
1465CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001466 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001467 QualType ResultType,
1468 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001469 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001470 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001471 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001472 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001473 const CodeGen::CallArgList &CallArgs,
1474 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001475 // Create and init a super structure; this is a (receiver, class)
1476 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001478 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001479 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001480 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001481 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001482 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001483
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001484 // If this is a class message the metaclass is passed as the target.
1485 llvm::Value *Target;
1486 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001487 if (isCategoryImpl) {
1488 // Message sent to 'super' in a class method defined in a category
1489 // implementation requires an odd treatment.
1490 // If we are in a class method, we must retrieve the
1491 // _metaclass_ for the current class, pointed at by
1492 // the class's "isa" pointer. The following assumes that
1493 // isa" is the first ivar in a class (which it must be).
1494 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1495 Target = CGF.Builder.CreateStructGEP(Target, 0);
1496 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001497 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001498 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1499 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1500 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1501 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001502 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001503 }
1504 else if (isCategoryImpl)
1505 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1506 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001507 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1508 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1509 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001510 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001511 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1512 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001513 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001514 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001515 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001516 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001517 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001518 return EmitMessageSend(CGF, Return, ResultType,
1519 EmitSelector(CGF.Builder, Sel),
1520 ObjCSuper, ObjCTypes.SuperPtrCTy,
1521 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001522}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001523
1524/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001525CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001526 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001527 QualType ResultType,
1528 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001529 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001530 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001531 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001532 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001533 return EmitMessageSend(CGF, Return, ResultType,
1534 EmitSelector(CGF.Builder, Sel),
1535 Receiver, CGF.getContext().getObjCIdType(),
1536 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001537}
1538
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001539CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001540CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1541 ReturnValueSlot Return,
1542 QualType ResultType,
1543 llvm::Value *Sel,
1544 llvm::Value *Arg0,
1545 QualType Arg0Ty,
1546 bool IsSuper,
1547 const CallArgList &CallArgs,
1548 const ObjCMethodDecl *Method,
1549 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001550 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001551 if (!IsSuper)
1552 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman04c9a492011-05-02 17:57:46 +00001553 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1554 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001555 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001556
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001557 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001558 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001559 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001560 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001561 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001562
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001563 if (Method)
1564 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1565 CGM.getContext().getCanonicalType(ResultType) &&
1566 "Result type mismatch!");
1567
John McCallcba681a2011-05-14 21:12:11 +00001568 NullReturnState nullReturn;
1569
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001570 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001571 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallcba681a2011-05-14 21:12:11 +00001572 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001573 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001574 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001575 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1576 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1577 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001578 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001579 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001580 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001581 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001582 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001583 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1584 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001585}
1586
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001587static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1588 if (FQT.isObjCGCStrong())
1589 return Qualifiers::Strong;
1590
John McCallf85e1932011-06-15 23:02:42 +00001591 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001592 return Qualifiers::Weak;
1593
1594 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1595 return Qualifiers::Strong;
1596
1597 if (const PointerType *PT = FQT->getAs<PointerType>())
1598 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1599
1600 return Qualifiers::GCNone;
1601}
1602
John McCall6b5a61b2011-02-07 10:33:21 +00001603llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1604 const CGBlockInfo &blockInfo) {
1605 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001606 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001607
John McCallf85e1932011-06-15 23:02:42 +00001608 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
1609 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001610 return nullPtr;
1611
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001612 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001613 SkipIvars.clear();
1614 IvarsInfo.clear();
1615 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1616 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1617
Fariborz Jahanian81979822010-09-09 00:21:45 +00001618 // __isa is the first field in block descriptor and must assume by runtime's
1619 // convention that it is GC'able.
1620 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001621
1622 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1623
1624 // Calculate the basic layout of the block structure.
1625 const llvm::StructLayout *layout =
1626 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1627
1628 // Ignore the optional 'this' capture: C++ objects are not assumed
1629 // to be GC'ed.
1630
1631 // Walk the captured variables.
1632 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1633 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1634 const VarDecl *variable = ci->getVariable();
1635 QualType type = variable->getType();
1636
1637 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1638
1639 // Ignore constant captures.
1640 if (capture.isConstant()) continue;
1641
1642 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1643
1644 // __block variables are passed by their descriptor address.
1645 if (ci->isByRef()) {
1646 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001647 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001648 }
1649
1650 assert(!type->isArrayType() && "array variable should not be caught");
1651 if (const RecordType *record = type->getAs<RecordType>()) {
1652 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001653 continue;
1654 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001655
John McCall6b5a61b2011-02-07 10:33:21 +00001656 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1657 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1658
1659 if (GCAttr == Qualifiers::Strong)
1660 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1661 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001662 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001663 SkipIvars.push_back(GC_IVAR(fieldOffset,
1664 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001665 }
1666
1667 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001668 return nullPtr;
1669
1670 // Sort on byte position; captures might not be allocated in order,
1671 // and unions can do funny things.
1672 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1673 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001674
1675 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001676 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001677 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1678 printf("\n block variable layout for block: ");
1679 const unsigned char *s = (unsigned char*)BitMap.c_str();
1680 for (unsigned i = 0; i < BitMap.size(); i++)
1681 if (!(s[i] & 0xf0))
1682 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1683 else
1684 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1685 printf("\n");
1686 }
1687
1688 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001689}
1690
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001691llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001692 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001693 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001694 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001695 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1696
Owen Anderson3c4972d2009-07-29 18:54:39 +00001697 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001698 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001699}
1700
Fariborz Jahanianda320092009-01-29 19:24:30 +00001701void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001702 // FIXME: We shouldn't need this, the protocol decl should contain enough
1703 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001704 DefinedProtocols.insert(PD->getIdentifier());
1705
1706 // If we have generated a forward reference to this protocol, emit
1707 // it now. Otherwise do nothing, the protocol objects are lazily
1708 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001709 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001710 GetOrEmitProtocol(PD);
1711}
1712
Fariborz Jahanianda320092009-01-29 19:24:30 +00001713llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001714 if (DefinedProtocols.count(PD->getIdentifier()))
1715 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001716
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001717 return GetOrEmitProtocolRef(PD);
1718}
1719
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001720/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001721// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1722struct _objc_protocol {
1723struct _objc_protocol_extension *isa;
1724char *protocol_name;
1725struct _objc_protocol_list *protocol_list;
1726struct _objc__method_prototype_list *instance_methods;
1727struct _objc__method_prototype_list *class_methods
1728};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001729
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001730See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001731*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001732llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1733 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1734
1735 // Early exit if a defining object has already been generated.
1736 if (Entry && Entry->hasInitializer())
1737 return Entry;
1738
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001739 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001740 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001741 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1742
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001743 // Construct method lists.
1744 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1745 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001746 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001747 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001748 ObjCMethodDecl *MD = *i;
1749 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001750 if (!C)
1751 return GetOrEmitProtocolRef(PD);
1752
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001753 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1754 OptInstanceMethods.push_back(C);
1755 } else {
1756 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001757 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001758 }
1759
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001760 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001761 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001762 ObjCMethodDecl *MD = *i;
1763 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001764 if (!C)
1765 return GetOrEmitProtocolRef(PD);
1766
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001767 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1768 OptClassMethods.push_back(C);
1769 } else {
1770 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001771 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001772 }
1773
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001774 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001775 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001777 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001778 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001779 PD->protocol_begin(),
1780 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001782 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001783 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1784 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001785 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001786 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001787 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1788 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001789 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001790 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001791
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001792 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001793 // Already created, fix the linkage and update the initializer.
1794 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001795 Entry->setInitializer(Init);
1796 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001797 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001798 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001799 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001800 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001801 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001802 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001803 // FIXME: Is this necessary? Why only for protocol?
1804 Entry->setAlignment(4);
1805 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001806 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001807
1808 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001809}
1810
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001811llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001812 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1813
1814 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001815 // We use the initializer as a marker of whether this is a forward
1816 // reference or not. At module finalization we add the empty
1817 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001818 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001819 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001820 llvm::GlobalValue::ExternalLinkage,
1821 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001822 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001823 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001824 // FIXME: Is this necessary? Why only for protocol?
1825 Entry->setAlignment(4);
1826 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001827
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001828 return Entry;
1829}
1830
1831/*
1832 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001833 uint32_t size;
1834 struct objc_method_description_list *optional_instance_methods;
1835 struct objc_method_description_list *optional_class_methods;
1836 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001837 };
1838*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001839llvm::Constant *
1840CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1841 const ConstantVector &OptInstanceMethods,
1842 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001843 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001844 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001845 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001846 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001847 Values[1] =
1848 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001849 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001850 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1851 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001852 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001853 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001854 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1855 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001856 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001857 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001858
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001859 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001860 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001861 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001862 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001863
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001864 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001865 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001866
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001867 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001868 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001869 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001870 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001871}
1872
1873/*
1874 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001875 struct objc_protocol_list *next;
1876 long count;
1877 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001878 };
1879*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001880llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00001881CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001882 ObjCProtocolDecl::protocol_iterator begin,
1883 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 std::vector<llvm::Constant*> ProtocolRefs;
1885
Daniel Dunbardbc933702008-08-21 21:57:41 +00001886 for (; begin != end; ++begin)
1887 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001888
1889 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001890 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001891 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001892
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001893 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001894 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001895
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001896 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001897 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001898 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001899 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001900 ProtocolRefs.size() - 1);
1901 Values[2] =
1902 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1903 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001904 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001905
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001906 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001908 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001909 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001910 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001911}
1912
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001913void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1914 std::vector<llvm::Constant*> &Properties,
1915 const Decl *Container,
1916 const ObjCProtocolDecl *PROTO,
1917 const ObjCCommonTypesHelper &ObjCTypes) {
1918 std::vector<llvm::Constant*> Prop(2);
1919 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1920 E = PROTO->protocol_end(); P != E; ++P)
1921 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1922 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1923 E = PROTO->prop_end(); I != E; ++I) {
1924 const ObjCPropertyDecl *PD = *I;
1925 if (!PropertySet.insert(PD->getIdentifier()))
1926 continue;
1927 Prop[0] = GetPropertyName(PD->getIdentifier());
1928 Prop[1] = GetPropertyTypeString(PD, Container);
1929 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1930 }
1931}
1932
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001933/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001934 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001935 const char * const name;
1936 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001937 };
1938
1939 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001940 uint32_t entsize; // sizeof (struct _objc_property)
1941 uint32_t prop_count;
1942 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001943 };
1944*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00001945llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001946 const Decl *Container,
1947 const ObjCContainerDecl *OCD,
1948 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001949 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001950 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1952 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001953 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001954 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001955 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001956 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001957 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001958 Prop));
1959 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001960 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001961 for (ObjCInterfaceDecl::all_protocol_iterator
1962 P = OID->all_referenced_protocol_begin(),
1963 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001964 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1965 ObjCTypes);
1966 }
1967 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1968 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1969 E = CD->protocol_end(); P != E; ++P)
1970 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1971 ObjCTypes);
1972 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001973
1974 // Return null for empty list.
1975 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001976 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001977
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001978 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001979 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001980 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001981 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1982 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001983 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001984 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001985 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001986 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001987
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001988 llvm::GlobalVariable *GV =
1989 CreateMetadataVar(Name, Init,
1990 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001991 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001992 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001993 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001994 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001995}
1996
1997/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001998 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001999 int count;
2000 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002001 };
2002*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002003llvm::Constant *
2004CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2005 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002006 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002007 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2008 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002009 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002010 if (!Desc[1])
2011 return 0;
2012
Owen Anderson08e25242009-07-27 22:29:56 +00002013 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002014 Desc);
2015}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002016
Chris Lattner5f9e2722011-07-23 10:55:15 +00002017llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002018 const char *Section,
2019 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002020 // Return null for empty list.
2021 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002022 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002023
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002024 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002025 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002026 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002027 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002028 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002029 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002030
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002031 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002032 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002033 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002034}
2035
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002036/*
2037 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002038 char *category_name;
2039 char *class_name;
2040 struct _objc_method_list *instance_methods;
2041 struct _objc_method_list *class_methods;
2042 struct _objc_protocol_list *protocols;
2043 uint32_t size; // <rdar://4585769>
2044 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002045 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002046*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002047void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002048 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002049
Mike Stumpf5408fe2009-05-16 07:57:57 +00002050 // FIXME: This is poor design, the OCD should have a pointer to the category
2051 // decl. Additionally, note that Category can be null for the @implementation
2052 // w/o an @interface case. Sema should just create one for us as it does for
2053 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002054 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002055 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002056 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002057
2058 llvm::SmallString<256> ExtName;
2059 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2060 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002061
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002062 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002063 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002064 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002065 // Instance methods should always be defined.
2066 InstanceMethods.push_back(GetMethodConstant(*i));
2067 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002069 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002070 // Class methods should always be defined.
2071 ClassMethods.push_back(GetMethodConstant(*i));
2072 }
2073
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002074 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002075 Values[0] = GetClassName(OCD->getIdentifier());
2076 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002077 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002078 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002079 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002080 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002081 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002082 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002083 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002084 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002085 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002086 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002087 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002088 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002089 Category->protocol_begin(),
2090 Category->protocol_end());
2091 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002092 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002093 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002094 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002095
2096 // If there is no category @interface then there can be no properties.
2097 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002098 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002099 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002100 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002101 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002102 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103
Owen Anderson08e25242009-07-27 22:29:56 +00002104 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002105 Values);
2106
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002107 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002108 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002109 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002110 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002111 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002112 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002113 // method definition entries must be clear for next implementation.
2114 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002115}
2116
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002117// FIXME: Get from somewhere?
2118enum ClassFlags {
2119 eClassFlags_Factory = 0x00001,
2120 eClassFlags_Meta = 0x00002,
2121 // <rdr://5142207>
2122 eClassFlags_HasCXXStructors = 0x02000,
2123 eClassFlags_Hidden = 0x20000,
2124 eClassFlags_ABI2_Hidden = 0x00010,
2125 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2126};
2127
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002128/*
2129 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002130 Class isa;
2131 Class super_class;
2132 const char *name;
2133 long version;
2134 long info;
2135 long instance_size;
2136 struct _objc_ivar_list *ivars;
2137 struct _objc_method_list *methods;
2138 struct _objc_cache *cache;
2139 struct _objc_protocol_list *protocols;
2140 // Objective-C 1.0 extensions (<rdr://4585769>)
2141 const char *ivar_layout;
2142 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002143 };
2144
2145 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002146*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002147void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002148 DefinedSymbols.insert(ID->getIdentifier());
2149
Chris Lattner8ec03f52008-11-24 03:54:41 +00002150 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002151 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002152 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002154 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002155 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002156 Interface->all_referenced_protocol_begin(),
2157 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002158 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002159 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002160 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002162 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002163
2164 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002165 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 Flags |= eClassFlags_Hidden;
2167
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002168 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002169 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002170 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002171 // Instance methods should always be defined.
2172 InstanceMethods.push_back(GetMethodConstant(*i));
2173 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002174 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002175 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002176 // Class methods should always be defined.
2177 ClassMethods.push_back(GetMethodConstant(*i));
2178 }
2179
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002180 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002181 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002182 ObjCPropertyImplDecl *PID = *i;
2183
2184 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2185 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2186
2187 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2188 if (llvm::Constant *C = GetMethodConstant(MD))
2189 InstanceMethods.push_back(C);
2190 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2191 if (llvm::Constant *C = GetMethodConstant(MD))
2192 InstanceMethods.push_back(C);
2193 }
2194 }
2195
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002196 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002197 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002198 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002199 // Record a reference to the super class.
2200 LazySymbols.insert(Super->getIdentifier());
2201
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002202 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002203 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 ObjCTypes.ClassPtrTy);
2205 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002206 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 }
2208 Values[ 2] = GetClassName(ID->getIdentifier());
2209 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002210 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2211 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2212 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002213 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002214 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002215 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002216 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002217 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002218 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002219 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002220 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002221 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002223 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002225 std::string Name("\01L_OBJC_CLASS_");
2226 Name += ClassName;
2227 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2228 // Check for a forward reference.
2229 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2230 if (GV) {
2231 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2232 "Forward metaclass reference has incorrect type.");
2233 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2234 GV->setInitializer(Init);
2235 GV->setSection(Section);
2236 GV->setAlignment(4);
2237 CGM.AddUsedGlobal(GV);
2238 }
2239 else
2240 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002241 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002242 // method definition entries must be clear for next implementation.
2243 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244}
2245
2246llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2247 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002248 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002249 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002250 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251
John McCall1fb0caa2010-10-22 21:05:15 +00002252 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002253 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002254
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002255 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 // The isa for the metaclass is the root of the hierarchy.
2257 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2258 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2259 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002260 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002261 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002262 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002263 // The super class for the metaclass is emitted as the name of the
2264 // super class. The runtime fixes this up to point to the
2265 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002267 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002268 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002269 ObjCTypes.ClassPtrTy);
2270 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002271 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002272 }
2273 Values[ 2] = GetClassName(ID->getIdentifier());
2274 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002275 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2276 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2277 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002278 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002279 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002280 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002281 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002282 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002284 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002285 Values[ 9] = Protocols;
2286 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002287 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002288 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002289 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002290 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 Values);
2292
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002293 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002294 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002295
2296 // Check for a forward reference.
2297 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2298 if (GV) {
2299 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2300 "Forward metaclass reference has incorrect type.");
2301 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2302 GV->setInitializer(Init);
2303 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002304 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002305 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002306 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002307 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002309 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002310 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002311
2312 return GV;
2313}
2314
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002315llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002316 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002317
Mike Stumpf5408fe2009-05-16 07:57:57 +00002318 // FIXME: Should we look these up somewhere other than the module. Its a bit
2319 // silly since we only generate these while processing an implementation, so
2320 // exactly one pointer would work if know when we entered/exitted an
2321 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002322
2323 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002324 // Previously, metaclass with internal linkage may have been defined.
2325 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002326 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2327 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002328 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2329 "Forward metaclass reference has incorrect type.");
2330 return GV;
2331 } else {
2332 // Generate as an external reference to keep a consistent
2333 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002334 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002335 llvm::GlobalValue::ExternalLinkage,
2336 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002337 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002338 }
2339}
2340
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002341llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2342 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2343
2344 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2345 true)) {
2346 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2347 "Forward class metadata reference has incorrect type.");
2348 return GV;
2349 } else {
2350 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2351 llvm::GlobalValue::ExternalLinkage,
2352 0,
2353 Name);
2354 }
2355}
2356
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357/*
2358 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002359 uint32_t size;
2360 const char *weak_ivar_layout;
2361 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002362 };
2363*/
2364llvm::Constant *
2365CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002366 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002367 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002368
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002369 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002370 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002371 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002372 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002373 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002374
2375 // Return null if no extension bits are used.
2376 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002377 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002378
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002379 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002380 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002381 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002383 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002384}
2385
2386/*
2387 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002388 char *ivar_name;
2389 char *ivar_type;
2390 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391 };
2392
2393 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002394 int ivar_count;
2395 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002398llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002399 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 std::vector<llvm::Constant*> Ivars, Ivar(3);
2401
2402 // When emitting the root class GCC emits ivar entries for the
2403 // actual class structure. It is not clear if we need to follow this
2404 // behavior; for now lets try and get away with not doing it. If so,
2405 // the cleanest solution would be to make up an ObjCInterfaceDecl
2406 // for the class.
2407 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002408 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002409
Jordy Rosedb8264e2011-07-22 02:08:32 +00002410 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002411
Jordy Rosedb8264e2011-07-22 02:08:32 +00002412 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002413 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002414 // Ignore unnamed bit-fields.
2415 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002416 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002417 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2418 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002419 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002420 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002421 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422 }
2423
2424 // Return null for empty list.
2425 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002426 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002428 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002429 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002430 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002432 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002433 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002435 llvm::GlobalVariable *GV;
2436 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002437 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002438 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002439 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002440 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002441 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002442 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002443 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002444 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445}
2446
2447/*
2448 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002449 SEL method_name;
2450 char *method_types;
2451 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002453
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002455 struct objc_method_list *obsolete;
2456 int count;
2457 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 };
2459*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002460
2461/// GetMethodConstant - Return a struct objc_method constant for the
2462/// given method if it has been defined. The result is null if the
2463/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002464llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002465 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002466 if (!Fn)
2467 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002469 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002470 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002471 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002472 ObjCTypes.SelectorPtrTy);
2473 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002474 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002475 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002476}
2477
Chris Lattner5f9e2722011-07-23 10:55:15 +00002478llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002479 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002480 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481 // Return null for empty list.
2482 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002483 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002485 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002486 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002487 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002488 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002489 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002490 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002491 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002493 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002494 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002495}
2496
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002497llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002498 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002499 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002500 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002501
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002502 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002503 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002504 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002505 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002506 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002507 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002508 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002509 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002510 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002511
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002512 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002513}
2514
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002515llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002516CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002517 llvm::Constant *Init,
2518 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002519 unsigned Align,
2520 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002521 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002522 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002523 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002524 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002525 if (Section)
2526 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002527 if (Align)
2528 GV->setAlignment(Align);
2529 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002530 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002531 return GV;
2532}
2533
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002534llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002535 // Abuse this interface function as a place to finalize.
2536 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002537 return NULL;
2538}
2539
Chris Lattner74391b42009-03-22 21:03:39 +00002540llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002541 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002542}
2543
Chris Lattner74391b42009-03-22 21:03:39 +00002544llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002545 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002546}
2547
David Chisnall8fac25d2010-12-26 22:13:16 +00002548llvm::Constant *CGObjCMac::GetGetStructFunction() {
2549 return ObjCTypes.getCopyStructFn();
2550}
2551llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002552 return ObjCTypes.getCopyStructFn();
2553}
2554
Chris Lattner74391b42009-03-22 21:03:39 +00002555llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002556 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002557}
2558
John McCallf1549f62010-07-06 01:34:17 +00002559void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2560 return EmitTryOrSynchronizedStmt(CGF, S);
2561}
2562
2563void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2564 const ObjCAtSynchronizedStmt &S) {
2565 return EmitTryOrSynchronizedStmt(CGF, S);
2566}
2567
John McCallcc505292010-07-21 06:59:36 +00002568namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002569 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002570 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002571 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002572 llvm::Value *CallTryExitVar;
2573 llvm::Value *ExceptionData;
2574 ObjCTypesHelper &ObjCTypes;
2575 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002576 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002577 llvm::Value *CallTryExitVar,
2578 llvm::Value *ExceptionData,
2579 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002580 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002581 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2582
John McCallad346f42011-07-12 20:27:29 +00002583 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002584 // Check whether we need to call objc_exception_try_exit.
2585 // In optimized code, this branch will always be folded.
2586 llvm::BasicBlock *FinallyCallExit =
2587 CGF.createBasicBlock("finally.call_exit");
2588 llvm::BasicBlock *FinallyNoCallExit =
2589 CGF.createBasicBlock("finally.no_call_exit");
2590 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2591 FinallyCallExit, FinallyNoCallExit);
2592
2593 CGF.EmitBlock(FinallyCallExit);
2594 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2595 ->setDoesNotThrow();
2596
2597 CGF.EmitBlock(FinallyNoCallExit);
2598
2599 if (isa<ObjCAtTryStmt>(S)) {
2600 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002601 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2602 // Save the current cleanup destination in case there's
2603 // control flow inside the finally statement.
2604 llvm::Value *CurCleanupDest =
2605 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2606
John McCallcc505292010-07-21 06:59:36 +00002607 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2608
John McCalld96a8e72010-08-11 00:16:14 +00002609 if (CGF.HaveInsertPoint()) {
2610 CGF.Builder.CreateStore(CurCleanupDest,
2611 CGF.getNormalCleanupDestSlot());
2612 } else {
2613 // Currently, the end of the cleanup must always exist.
2614 CGF.EnsureInsertPoint();
2615 }
2616 }
John McCallcc505292010-07-21 06:59:36 +00002617 } else {
2618 // Emit objc_sync_exit(expr); as finally's sole statement for
2619 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002620 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002621 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2622 ->setDoesNotThrow();
2623 }
2624 }
2625 };
John McCall87bb5822010-07-31 23:20:56 +00002626
2627 class FragileHazards {
2628 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002629 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002630 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2631
2632 llvm::InlineAsm *ReadHazard;
2633 llvm::InlineAsm *WriteHazard;
2634
2635 llvm::FunctionType *GetAsmFnType();
2636
2637 void collectLocals();
2638 void emitReadHazard(CGBuilderTy &Builder);
2639
2640 public:
2641 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002642
John McCall87bb5822010-07-31 23:20:56 +00002643 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002644 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002645 };
2646}
2647
2648/// Create the fragile-ABI read and write hazards based on the current
2649/// state of the function, which is presumed to be immediately prior
2650/// to a @try block. These hazards are used to maintain correct
2651/// semantics in the face of optimization and the fragile ABI's
2652/// cavalier use of setjmp/longjmp.
2653FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2654 collectLocals();
2655
2656 if (Locals.empty()) return;
2657
2658 // Collect all the blocks in the function.
2659 for (llvm::Function::iterator
2660 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2661 BlocksBeforeTry.insert(&*I);
2662
2663 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2664
2665 // Create a read hazard for the allocas. This inhibits dead-store
2666 // optimizations and forces the values to memory. This hazard is
2667 // inserted before any 'throwing' calls in the protected scope to
2668 // reflect the possibility that the variables might be read from the
2669 // catch block if the call throws.
2670 {
2671 std::string Constraint;
2672 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2673 if (I) Constraint += ',';
2674 Constraint += "*m";
2675 }
2676
2677 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2678 }
2679
2680 // Create a write hazard for the allocas. This inhibits folding
2681 // loads across the hazard. This hazard is inserted at the
2682 // beginning of the catch path to reflect the possibility that the
2683 // variables might have been written within the protected scope.
2684 {
2685 std::string Constraint;
2686 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2687 if (I) Constraint += ',';
2688 Constraint += "=*m";
2689 }
2690
2691 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2692 }
2693}
2694
2695/// Emit a write hazard at the current location.
2696void FragileHazards::emitWriteHazard() {
2697 if (Locals.empty()) return;
2698
Jay Foad4c7d9f12011-07-15 08:37:34 +00002699 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002700}
2701
John McCall87bb5822010-07-31 23:20:56 +00002702void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2703 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002704 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002705}
2706
2707/// Emit read hazards in all the protected blocks, i.e. all the blocks
2708/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002709void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002710 if (Locals.empty()) return;
2711
2712 CGBuilderTy Builder(CGF.getLLVMContext());
2713
2714 // Iterate through all blocks, skipping those prior to the try.
2715 for (llvm::Function::iterator
2716 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2717 llvm::BasicBlock &BB = *FI;
2718 if (BlocksBeforeTry.count(&BB)) continue;
2719
2720 // Walk through all the calls in the block.
2721 for (llvm::BasicBlock::iterator
2722 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2723 llvm::Instruction &I = *BI;
2724
2725 // Ignore instructions that aren't non-intrinsic calls.
2726 // These are the only calls that can possibly call longjmp.
2727 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2728 if (isa<llvm::IntrinsicInst>(I))
2729 continue;
2730
2731 // Ignore call sites marked nounwind. This may be questionable,
2732 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2733 llvm::CallSite CS(&I);
2734 if (CS.doesNotThrow()) continue;
2735
John McCall0b251722010-08-04 05:59:32 +00002736 // Insert a read hazard before the call. This will ensure that
2737 // any writes to the locals are performed before making the
2738 // call. If the call throws, then this is sufficient to
2739 // guarantee correctness as long as it doesn't also write to any
2740 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002741 Builder.SetInsertPoint(&BB, BI);
2742 emitReadHazard(Builder);
2743 }
2744 }
2745}
2746
2747static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2748 if (V) S.insert(V);
2749}
2750
2751void FragileHazards::collectLocals() {
2752 // Compute a set of allocas to ignore.
2753 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2754 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2755 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002756
2757 // Collect all the allocas currently in the function. This is
2758 // probably way too aggressive.
2759 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2760 for (llvm::BasicBlock::iterator
2761 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2762 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2763 Locals.push_back(&*I);
2764}
2765
2766llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002767 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002768 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2769 tys[i] = Locals[i]->getType();
2770 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002771}
2772
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002773/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002774
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002775 Objective-C setjmp-longjmp (sjlj) Exception Handling
2776 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002777
John McCallf1549f62010-07-06 01:34:17 +00002778 A catch buffer is a setjmp buffer plus:
2779 - a pointer to the exception that was caught
2780 - a pointer to the previous exception data buffer
2781 - two pointers of reserved storage
2782 Therefore catch buffers form a stack, with a pointer to the top
2783 of the stack kept in thread-local storage.
2784
2785 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2786 objc_exception_try_exit pops the given catch buffer, which is
2787 required to be the top of the EH stack.
2788 objc_exception_throw pops the top of the EH stack, writes the
2789 thrown exception into the appropriate field, and longjmps
2790 to the setjmp buffer. It crashes the process (with a printf
2791 and an abort()) if there are no catch buffers on the stack.
2792 objc_exception_extract just reads the exception pointer out of the
2793 catch buffer.
2794
2795 There's no reason an implementation couldn't use a light-weight
2796 setjmp here --- something like __builtin_setjmp, but API-compatible
2797 with the heavyweight setjmp. This will be more important if we ever
2798 want to implement correct ObjC/C++ exception interactions for the
2799 fragile ABI.
2800
2801 Note that for this use of setjmp/longjmp to be correct, we may need
2802 to mark some local variables volatile: if a non-volatile local
2803 variable is modified between the setjmp and the longjmp, it has
2804 indeterminate value. For the purposes of LLVM IR, it may be
2805 sufficient to make loads and stores within the @try (to variables
2806 declared outside the @try) volatile. This is necessary for
2807 optimized correctness, but is not currently being done; this is
2808 being tracked as rdar://problem/8160285
2809
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002810 The basic framework for a @try-catch-finally is as follows:
2811 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002812 objc_exception_data d;
2813 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002814 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002815
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002816 objc_exception_try_enter(&d);
2817 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002818 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002819 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002820 // exception path
2821 id _caught = objc_exception_extract(&d);
2822
2823 // enter new try scope for handlers
2824 if (!setjmp(d.jmp_buf)) {
2825 ... match exception and execute catch blocks ...
2826
2827 // fell off end, rethrow.
2828 _rethrow = _caught;
2829 ... jump-through-finally to finally_rethrow ...
2830 } else {
2831 // exception in catch block
2832 _rethrow = objc_exception_extract(&d);
2833 _call_try_exit = false;
2834 ... jump-through-finally to finally_rethrow ...
2835 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002837 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002838
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002839 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002840 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002841 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002842
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002843 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002844 ... dispatch to finally destination ...
2845
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002846 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002847 objc_exception_throw(_rethrow);
2848
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002849 finally_end:
2850 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002851
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002852 This framework differs slightly from the one gcc uses, in that gcc
2853 uses _rethrow to determine if objc_exception_try_exit should be called
2854 and if the object should be rethrown. This breaks in the face of
2855 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002856
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002857 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002858
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002859 - If there are no catch blocks, then we avoid emitting the second
2860 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002861
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002862 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2863 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002864
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002865 - FIXME: If there is no @finally block we can do a few more
2866 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002867
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002868 Rethrows and Jumps-Through-Finally
2869 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002870
John McCallf1549f62010-07-06 01:34:17 +00002871 '@throw;' is supported by pushing the currently-caught exception
2872 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002873
John McCallf1549f62010-07-06 01:34:17 +00002874 Branches through the @finally block are handled with an ordinary
2875 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2876 exceptions are not compatible with C++ exceptions, and this is
2877 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002878
John McCallf1549f62010-07-06 01:34:17 +00002879 @synchronized(expr) { stmt; } is emitted as if it were:
2880 id synch_value = expr;
2881 objc_sync_enter(synch_value);
2882 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002883*/
2884
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002885void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2886 const Stmt &S) {
2887 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002888
2889 // A destination for the fall-through edges of the catch handlers to
2890 // jump to.
2891 CodeGenFunction::JumpDest FinallyEnd =
2892 CGF.getJumpDestInCurrentScope("finally.end");
2893
2894 // A destination for the rethrow edge of the catch handlers to jump
2895 // to.
2896 CodeGenFunction::JumpDest FinallyRethrow =
2897 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002898
Daniel Dunbar1c566672009-02-24 01:43:46 +00002899 // For @synchronized, call objc_sync_enter(sync.expr). The
2900 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002901 // @synchronized. We can't avoid a temp here because we need the
2902 // value to be preserved. If the backend ever does liveness
2903 // correctly after setjmp, this will be unnecessary.
2904 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002905 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002906 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002907 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2908 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002909 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2910 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002911
2912 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2913 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002914 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002915
John McCall0b251722010-08-04 05:59:32 +00002916 // Allocate memory for the setjmp buffer. This needs to be kept
2917 // live throughout the try and catch blocks.
2918 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2919 "exceptiondata.ptr");
2920
John McCall87bb5822010-07-31 23:20:56 +00002921 // Create the fragile hazards. Note that this will not capture any
2922 // of the allocas required for exception processing, but will
2923 // capture the current basic block (which extends all the way to the
2924 // setjmp call) as "before the @try".
2925 FragileHazards Hazards(CGF);
2926
John McCallf1549f62010-07-06 01:34:17 +00002927 // Create a flag indicating whether the cleanup needs to call
2928 // objc_exception_try_exit. This is true except when
2929 // - no catches match and we're branching through the cleanup
2930 // just to rethrow the exception, or
2931 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002932 // The setjmp-safety rule here is that we should always store to this
2933 // variable in a place that dominates the branch through the cleanup
2934 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002935 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002936 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002937
John McCall9e2213d2010-10-04 23:42:51 +00002938 // A slot containing the exception to rethrow. Only needed when we
2939 // have both a @catch and a @finally.
2940 llvm::Value *PropagatingExnVar = 0;
2941
John McCallf1549f62010-07-06 01:34:17 +00002942 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002943 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002944 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002945 CallTryExitVar,
2946 ExceptionData,
2947 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002948
2949 // Enter a try block:
2950 // - Call objc_exception_try_enter to push ExceptionData on top of
2951 // the EH stack.
2952 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2953 ->setDoesNotThrow();
2954
2955 // - Call setjmp on the exception data buffer.
2956 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2957 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2958 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00002959 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00002960 llvm::CallInst *SetJmpResult =
2961 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2962 SetJmpResult->setDoesNotThrow();
2963
2964 // If setjmp returned 0, enter the protected block; otherwise,
2965 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002966 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2967 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002968 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002969 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2970 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002971
John McCallf1549f62010-07-06 01:34:17 +00002972 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002973 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002974 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002975 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002976 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002977
2978 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002979
John McCallf1549f62010-07-06 01:34:17 +00002980 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002981 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002982
John McCall87bb5822010-07-31 23:20:56 +00002983 // Don't optimize loads of the in-scope locals across this point.
2984 Hazards.emitWriteHazard();
2985
John McCallf1549f62010-07-06 01:34:17 +00002986 // For a @synchronized (or a @try with no catches), just branch
2987 // through the cleanup to the rethrow block.
2988 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2989 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002990 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002991 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002992
2993 // Otherwise, we have to match against the caught exceptions.
2994 } else {
John McCall0b251722010-08-04 05:59:32 +00002995 // Retrieve the exception object. We may emit multiple blocks but
2996 // nothing can cross this so the value is already in SSA form.
2997 llvm::CallInst *Caught =
2998 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2999 ExceptionData, "caught");
3000 Caught->setDoesNotThrow();
3001
John McCallf1549f62010-07-06 01:34:17 +00003002 // Push the exception to rethrow onto the EH value stack for the
3003 // benefit of any @throws in the handlers.
3004 CGF.ObjCEHValueStack.push_back(Caught);
3005
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003006 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003007
John McCall0b251722010-08-04 05:59:32 +00003008 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003009
John McCall0b251722010-08-04 05:59:32 +00003010 llvm::BasicBlock *CatchBlock = 0;
3011 llvm::BasicBlock *CatchHandler = 0;
3012 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003013 // Save the currently-propagating exception before
3014 // objc_exception_try_enter clears the exception slot.
3015 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3016 "propagating_exception");
3017 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3018
John McCall0b251722010-08-04 05:59:32 +00003019 // Enter a new exception try block (in case a @catch block
3020 // throws an exception).
3021 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3022 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003023
John McCall0b251722010-08-04 05:59:32 +00003024 llvm::CallInst *SetJmpResult =
3025 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3026 "setjmp.result");
3027 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003028
John McCall0b251722010-08-04 05:59:32 +00003029 llvm::Value *Threw =
3030 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3031
3032 CatchBlock = CGF.createBasicBlock("catch");
3033 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3034 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3035
3036 CGF.EmitBlock(CatchBlock);
3037 }
3038
3039 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003040
Daniel Dunbar55e40722008-09-27 07:03:52 +00003041 // Handle catch list. As a special case we check if everything is
3042 // matched and avoid generating code for falling off the end if
3043 // so.
3044 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003045 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3046 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003047
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003048 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003049 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003050
Anders Carlsson80f25672008-09-09 17:59:25 +00003051 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003052 if (!CatchParam) {
3053 AllMatched = true;
3054 } else {
John McCall183700f2009-09-21 23:43:11 +00003055 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003056
John McCallf1549f62010-07-06 01:34:17 +00003057 // catch(id e) always matches under this ABI, since only
3058 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003059 // FIXME: For the time being we also match id<X>; this should
3060 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003061 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003062 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003063 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003064
John McCallf1549f62010-07-06 01:34:17 +00003065 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003066 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003067 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3068
Anders Carlssondde0a942008-09-11 09:15:33 +00003069 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003070 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003071 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003072
3073 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003074 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003075 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003076
Anders Carlssondde0a942008-09-11 09:15:33 +00003077 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003078
3079 // The scope of the catch variable ends right here.
3080 CatchVarCleanups.ForceCleanup();
3081
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003082 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003083 break;
3084 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085
Steve Naroff14108da2009-07-10 23:34:53 +00003086 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003087 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003088
3089 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003090 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3091 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003092
3093 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003094 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003095
John McCallf1549f62010-07-06 01:34:17 +00003096 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003097 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3098 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003099 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003100
John McCallf1549f62010-07-06 01:34:17 +00003101 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3102 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003103
3104 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003105 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003106
Anders Carlsson80f25672008-09-09 17:59:25 +00003107 // Emit the @catch block.
3108 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003109
3110 // Collect any cleanups for the catch variable. The scope lasts until
3111 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003112 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003113
John McCallb6bbcc92010-10-15 04:57:14 +00003114 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003115 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003116
John McCallf1549f62010-07-06 01:34:17 +00003117 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003118 llvm::Value *Tmp =
3119 CGF.Builder.CreateBitCast(Caught,
3120 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003121 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003122 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003123
Anders Carlssondde0a942008-09-11 09:15:33 +00003124 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003125
3126 // We're done with the catch variable.
3127 CatchVarCleanups.ForceCleanup();
3128
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003129 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003130
Anders Carlsson80f25672008-09-09 17:59:25 +00003131 CGF.EmitBlock(NextCatchBlock);
3132 }
3133
John McCallf1549f62010-07-06 01:34:17 +00003134 CGF.ObjCEHValueStack.pop_back();
3135
John McCall0b251722010-08-04 05:59:32 +00003136 // If nothing wanted anything to do with the caught exception,
3137 // kill the extract call.
3138 if (Caught->use_empty())
3139 Caught->eraseFromParent();
3140
3141 if (!AllMatched)
3142 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3143
3144 if (HasFinally) {
3145 // Emit the exception handler for the @catch blocks.
3146 CGF.EmitBlock(CatchHandler);
3147
3148 // In theory we might now need a write hazard, but actually it's
3149 // unnecessary because there's no local-accessing code between
3150 // the try's write hazard and here.
3151 //Hazards.emitWriteHazard();
3152
John McCall9e2213d2010-10-04 23:42:51 +00003153 // Extract the new exception and save it to the
3154 // propagating-exception slot.
3155 assert(PropagatingExnVar);
3156 llvm::CallInst *NewCaught =
3157 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3158 ExceptionData, "caught");
3159 NewCaught->setDoesNotThrow();
3160 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3161
John McCall0b251722010-08-04 05:59:32 +00003162 // Don't pop the catch handler; the throw already did.
3163 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003164 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003165 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003166 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003167
John McCall87bb5822010-07-31 23:20:56 +00003168 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003169 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003170
John McCallf1549f62010-07-06 01:34:17 +00003171 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003172 CGF.Builder.restoreIP(TryFallthroughIP);
3173 if (CGF.HaveInsertPoint())
3174 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003175 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003176 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003177
John McCallf1549f62010-07-06 01:34:17 +00003178 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003179 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003180 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003181 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003182 // If we have a propagating-exception variable, check it.
3183 llvm::Value *PropagatingExn;
3184 if (PropagatingExnVar) {
3185 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003186
John McCall9e2213d2010-10-04 23:42:51 +00003187 // Otherwise, just look in the buffer for the exception to throw.
3188 } else {
3189 llvm::CallInst *Caught =
3190 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3191 ExceptionData);
3192 Caught->setDoesNotThrow();
3193 PropagatingExn = Caught;
3194 }
3195
3196 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003197 ->setDoesNotThrow();
3198 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003199 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003200
John McCall87bb5822010-07-31 23:20:56 +00003201 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003202}
3203
3204void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003205 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003206 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003207
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003208 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3209 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003210 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003211 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3212 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003214 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003215 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003216 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003217
John McCallf1549f62010-07-06 01:34:17 +00003218 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3219 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003220 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003221
3222 // Clear the insertion point to indicate we are in unreachable code.
3223 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003224}
3225
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003226/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003227/// object: objc_read_weak (id *src)
3228///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003229llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003230 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003231 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003232 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3233 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3234 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003235 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003236 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003237 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003238 return read_weak;
3239}
3240
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003241/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3242/// objc_assign_weak (id src, id *dst)
3243///
3244void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003245 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003246 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003247 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003248 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003249 assert(Size <= 8 && "does not support size > 8");
3250 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003251 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003252 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3253 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003254 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3255 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003256 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003257 src, dst, "weakassign");
3258 return;
3259}
3260
Fariborz Jahanian58626502008-11-19 00:59:10 +00003261/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3262/// objc_assign_global (id src, id *dst)
3263///
3264void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003265 llvm::Value *src, llvm::Value *dst,
3266 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003267 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003268 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003269 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003270 assert(Size <= 8 && "does not support size > 8");
3271 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003272 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003273 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3274 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003275 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3276 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003277 if (!threadlocal)
3278 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3279 src, dst, "globalassign");
3280 else
3281 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3282 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003283 return;
3284}
3285
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003286/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003287/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003288///
3289void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003290 llvm::Value *src, llvm::Value *dst,
3291 llvm::Value *ivarOffset) {
3292 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003293 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003294 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003295 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003296 assert(Size <= 8 && "does not support size > 8");
3297 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003298 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003299 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3300 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003301 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3302 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003303 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3304 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003305 return;
3306}
3307
Fariborz Jahanian58626502008-11-19 00:59:10 +00003308/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3309/// objc_assign_strongCast (id src, id *dst)
3310///
3311void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003312 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003313 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003314 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003315 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003316 assert(Size <= 8 && "does not support size > 8");
3317 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003318 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003319 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3320 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003321 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3322 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003323 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003324 src, dst, "weakassign");
3325 return;
3326}
3327
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003328void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003329 llvm::Value *DestPtr,
3330 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003331 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003332 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3333 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003334 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003335 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003336 return;
3337}
3338
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003339/// EmitObjCValueForIvar - Code Gen for ivar reference.
3340///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003341LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3342 QualType ObjectTy,
3343 llvm::Value *BaseValue,
3344 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003345 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003346 const ObjCInterfaceDecl *ID =
3347 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003348 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3349 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003350}
3351
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003352llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003353 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003354 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003355 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003356 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003357 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3358 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003359}
3360
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003361/* *** Private Interface *** */
3362
3363/// EmitImageInfo - Emit the image info marker used to encode some module
3364/// level information.
3365///
3366/// See: <rdr://4810609&4810587&4810587>
3367/// struct IMAGE_INFO {
3368/// unsigned version;
3369/// unsigned flags;
3370/// };
3371enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003372 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003373 eImageInfo_GarbageCollected = (1 << 1),
3374 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003375 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3376
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003377 // A flag indicating that the module has no instances of a @synthesize of a
3378 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003379 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003380};
3381
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003382void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003383 unsigned version = 0; // Version is unused?
3384 unsigned flags = 0;
3385
3386 // FIXME: Fix and continue?
3387 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3388 flags |= eImageInfo_GarbageCollected;
3389 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3390 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003391
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003392 // We never allow @synthesize of a superclass property.
3393 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003394
Chris Lattner2acc6e32011-07-18 04:24:23 +00003395 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003396
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003397 // Emitted as int[2];
3398 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003399 llvm::ConstantInt::get(Int32Ty, version),
3400 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003401 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003402 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003403
3404 const char *Section;
3405 if (ObjCABI == 1)
3406 Section = "__OBJC, __image_info,regular";
3407 else
3408 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003409 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003410 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003411 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003412 Section,
3413 0,
3414 true);
3415 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003416}
3417
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003418
3419// struct objc_module {
3420// unsigned long version;
3421// unsigned long size;
3422// const char *name;
3423// Symtab symtab;
3424// };
3425
3426// FIXME: Get from somewhere
3427static const int ModuleVersion = 7;
3428
3429void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003430 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003431
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003432 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003433 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3434 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003435 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003436 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003437 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003438 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003439 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003440 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003441 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003442}
3443
3444llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003445 unsigned NumClasses = DefinedClasses.size();
3446 unsigned NumCategories = DefinedCategories.size();
3447
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003448 // Return null if no symbols were defined.
3449 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003450 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003451
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003452 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003453 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003454 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003455 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3456 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003457
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003458 // The runtime expects exactly the list of defined classes followed
3459 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003461 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003462 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003463 ObjCTypes.Int8PtrTy);
3464 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003465 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003466 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003467 ObjCTypes.Int8PtrTy);
3468
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003469 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003470 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003471 NumClasses + NumCategories),
3472 Symbols);
3473
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003474 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003475
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003476 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003477 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3478 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003479 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003480 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003481}
3482
John McCallf85e1932011-06-15 23:02:42 +00003483llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3484 IdentifierInfo *II) {
3485 LazySymbols.insert(II);
3486
3487 llvm::GlobalVariable *&Entry = ClassReferences[II];
3488
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003489 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003490 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003491 llvm::ConstantExpr::getBitCast(GetClassName(II),
3492 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003494 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3495 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3496 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003497 }
John McCallf85e1932011-06-15 23:02:42 +00003498
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003499 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003500}
3501
John McCallf85e1932011-06-15 23:02:42 +00003502llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3503 const ObjCInterfaceDecl *ID) {
3504 return EmitClassRefFromId(Builder, ID->getIdentifier());
3505}
3506
3507llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3508 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3509 return EmitClassRefFromId(Builder, II);
3510}
3511
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003512llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3513 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003514 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003515
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003516 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003517 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003518 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003519 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003520 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003521 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3522 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003523 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003524 }
3525
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003526 if (lvalue)
3527 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003528 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003529}
3530
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003531llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003532 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003533
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003534 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003535 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003536 llvm::ConstantArray::get(VMContext,
3537 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003538 ((ObjCABI == 2) ?
3539 "__TEXT,__objc_classname,cstring_literals" :
3540 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003541 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003542
Owen Andersona1cf15f2009-07-14 23:10:40 +00003543 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003544}
3545
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003546llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3547 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3548 I = MethodDefinitions.find(MD);
3549 if (I != MethodDefinitions.end())
3550 return I->second;
3551
3552 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3553 // MD isn't emitted yet because it comes from PCH.
3554 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3555 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3556 return MethodDefinitions[MD];
3557 }
3558
3559 return NULL;
3560}
3561
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003562/// GetIvarLayoutName - Returns a unique constant for the given
3563/// ivar layout bitmap.
3564llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003565 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003566 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003567}
3568
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003569void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003570 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003571 bool ForStrongLayout,
3572 bool &HasUnion) {
3573 const RecordDecl *RD = RT->getDecl();
3574 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003575 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003576 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003577 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003578 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003579
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003580 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3581 ForStrongLayout, HasUnion);
3582}
3583
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003584void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585 const llvm::StructLayout *Layout,
3586 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003587 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003588 unsigned int BytePos, bool ForStrongLayout,
3589 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003590 bool IsUnion = (RD && RD->isUnion());
3591 uint64_t MaxUnionIvarSize = 0;
3592 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003593 const FieldDecl *MaxField = 0;
3594 const FieldDecl *MaxSkippedField = 0;
3595 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003596 uint64_t MaxFieldOffset = 0;
3597 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003598 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003599 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003600
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003601 if (RecFields.empty())
3602 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003603 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3604 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003605 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003606 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003607 FirstFieldDelta =
3608 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3609 }
3610
Chris Lattnerf1690852009-03-31 08:48:01 +00003611 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003612 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003613 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003614 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003615 // Note that 'i' here is actually the field index inside RD of Field,
3616 // although this dependency is hidden.
3617 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003618 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003619 } else
John McCallf85e1932011-06-15 23:02:42 +00003620 FieldOffset =
3621 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003622
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003623 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003624 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003625 LastFieldBitfieldOrUnnamed = Field;
3626 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003627 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003628 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003629
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003630 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003631 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003632 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003633 if (FQT->isUnionType())
3634 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003635
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003636 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003637 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003638 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003639 continue;
3640 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003641
Chris Lattnerf1690852009-03-31 08:48:01 +00003642 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003643 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003644 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003645 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003646 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003647 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003648 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3649 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003650 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003651 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003652 FQT = CArray->getElementType();
3653 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003654
3655 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003656 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003657 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003658 int OldIndex = IvarsInfo.size() - 1;
3659 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003660
Ted Kremenek6217b802009-07-29 21:53:49 +00003661 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003662 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003663 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003664
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003665 // Replicate layout information for each array element. Note that
3666 // one element is already done.
3667 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003668 for (int FirstIndex = IvarsInfo.size() - 1,
3669 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003670 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003671 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3672 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3673 IvarsInfo[i].ivar_size));
3674 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3675 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3676 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003677 }
3678 continue;
3679 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003680 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003681 // At this point, we are done with Record/Union and array there of.
3682 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003683 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003684
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003685 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003686 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3687 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003688 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003689 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003690 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003691 MaxUnionIvarSize = UnionIvarSize;
3692 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003693 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003694 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003695 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003696 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003697 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003698 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003699 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003700 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3701 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003702 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003703 // FIXME: Why the asymmetry? We divide by word size in bits on other
3704 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003705 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003706 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003707 MaxSkippedUnionIvarSize = UnionIvarSize;
3708 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003709 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003710 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003711 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003712 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003713 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003714 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003715 }
3716 }
3717 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003718
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003719 if (LastFieldBitfieldOrUnnamed) {
3720 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3721 // Last field was a bitfield. Must update skip info.
3722 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3723 uint64_t BitFieldSize =
3724 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3725 GC_IVAR skivar;
3726 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3727 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3728 + ((BitFieldSize % ByteSizeInBits) != 0);
3729 SkipIvars.push_back(skivar);
3730 } else {
3731 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3732 // Last field was unnamed. Must update skip info.
3733 unsigned FieldSize
3734 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3735 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3736 FieldSize / ByteSizeInBits));
3737 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003738 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003739
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003740 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003741 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003742 MaxUnionIvarSize));
3743 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003744 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003745 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003746}
3747
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003748/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3749/// the computations and returning the layout bitmap (for ivar or blocks) in
3750/// the given argument BitMap string container. Routine reads
3751/// two containers, IvarsInfo and SkipIvars which are assumed to be
3752/// filled already by the caller.
3753llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003754 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003755 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003756
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003757 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003758 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003759 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003760 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003761 if (IvarsInfo[0].ivar_bytepos == 0) {
3762 WordsToSkip = 0;
3763 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003764 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003765 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3766 WordsToScan = IvarsInfo[0].ivar_size;
3767 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003768 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003769 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003770 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003771 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003772 // consecutive 'scanned' object pointers.
3773 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003774 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003775 // Skip over 'gc'able object pointer which lay over each other.
3776 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3777 continue;
3778 // Must skip over 1 or more words. We save current skip/scan values
3779 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003780 SKIP_SCAN SkScan;
3781 SkScan.skip = WordsToSkip;
3782 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003783 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003784
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003785 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003786 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3787 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003788 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 WordsToSkip = 0;
3790 WordsToScan = IvarsInfo[i].ivar_size;
3791 }
3792 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003793 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003794 SKIP_SCAN SkScan;
3795 SkScan.skip = WordsToSkip;
3796 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003797 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003798 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003799
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003800 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003801 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003802 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003803 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003804 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003805 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003806 IvarsInfo[LastIndex].ivar_bytepos +
3807 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003808 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003809 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003810 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003811 SKIP_SCAN SkScan;
3812 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3813 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003814 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003815 }
3816 }
3817 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3818 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003819 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003820 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003821 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3822 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3823 // 0xM0 followed by 0x0N detected.
3824 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3825 for (int j = i+1; j < SkipScan; j++)
3826 SkipScanIvars[j] = SkipScanIvars[j+1];
3827 --SkipScan;
3828 }
3829 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003830
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003831 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003832 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003833 unsigned char byte;
3834 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3835 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3836 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3837 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003838
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003839 // first skip big.
3840 for (unsigned int ix = 0; ix < skip_big; ix++)
3841 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003842
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003843 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003844 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003845 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003846 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003847 byte |= 0xf;
3848 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003849 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003850 byte |= scan_small;
3851 scan_small = 0;
3852 }
3853 BitMap += byte;
3854 }
3855 // next scan big
3856 for (unsigned int ix = 0; ix < scan_big; ix++)
3857 BitMap += (unsigned char)(0x0f);
3858 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003859 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003860 byte = scan_small;
3861 BitMap += byte;
3862 }
3863 }
3864 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003865 unsigned char zero = 0;
3866 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003867
3868 llvm::GlobalVariable * Entry =
3869 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3870 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003871 ((ObjCABI == 2) ?
3872 "__TEXT,__objc_classname,cstring_literals" :
3873 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003874 1, true);
3875 return getConstantGEP(VMContext, Entry, 0, 0);
3876}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003877
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003878/// BuildIvarLayout - Builds ivar layout bitmap for the class
3879/// implementation for the __strong or __weak case.
3880/// The layout map displays which words in ivar list must be skipped
3881/// and which must be scanned by GC (see below). String is built of bytes.
3882/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3883/// of words to skip and right nibble is count of words to scan. So, each
3884/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3885/// represented by a 0x00 byte which also ends the string.
3886/// 1. when ForStrongLayout is true, following ivars are scanned:
3887/// - id, Class
3888/// - object *
3889/// - __strong anything
3890///
3891/// 2. When ForStrongLayout is false, following ivars are scanned:
3892/// - __weak anything
3893///
3894llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3895 const ObjCImplementationDecl *OMD,
3896 bool ForStrongLayout) {
3897 bool hasUnion = false;
3898
Chris Lattner2acc6e32011-07-18 04:24:23 +00003899 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
John McCallf85e1932011-06-15 23:02:42 +00003900 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
3901 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003902 return llvm::Constant::getNullValue(PtrTy);
3903
Jordy Rosedb8264e2011-07-22 02:08:32 +00003904 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3905 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003906 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003907 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003908 IVD; IVD = IVD->getNextIvar())
3909 RecFields.push_back(cast<FieldDecl>(IVD));
3910 }
3911 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003912 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00003913 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003914
Jordy Rosedb8264e2011-07-22 02:08:32 +00003915 // FIXME: This is not ideal; we shouldn't have to do this copy.
3916 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003917 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003918
3919 if (RecFields.empty())
3920 return llvm::Constant::getNullValue(PtrTy);
3921
3922 SkipIvars.clear();
3923 IvarsInfo.clear();
3924
3925 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3926 if (IvarsInfo.empty())
3927 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003928 // Sort on byte position in case we encounterred a union nested in
3929 // the ivar list.
3930 if (hasUnion && !IvarsInfo.empty())
3931 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3932 if (hasUnion && !SkipIvars.empty())
3933 std::sort(SkipIvars.begin(), SkipIvars.end());
3934
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003935 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003936 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003937
3938 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003939 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003940 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003941 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003942 const unsigned char *s = (unsigned char*)BitMap.c_str();
3943 for (unsigned i = 0; i < BitMap.size(); i++)
3944 if (!(s[i] & 0xf0))
3945 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3946 else
3947 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3948 printf("\n");
3949 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003950 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003951}
3952
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003953llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003954 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3955
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003956 // FIXME: Avoid std::string copying.
3957 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003958 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003959 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003960 ((ObjCABI == 2) ?
3961 "__TEXT,__objc_methname,cstring_literals" :
3962 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003963 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003964
Owen Andersona1cf15f2009-07-14 23:10:40 +00003965 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003966}
3967
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003968// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003969llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003970 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3971}
3972
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003973llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003974 std::string TypeStr;
3975 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3976
3977 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003978
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003979 if (!Entry)
3980 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003981 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003982 ((ObjCABI == 2) ?
3983 "__TEXT,__objc_methtype,cstring_literals" :
3984 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003985 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003986
Owen Andersona1cf15f2009-07-14 23:10:40 +00003987 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003988}
3989
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003990llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003991 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00003992 if (CGM.getContext().getObjCEncodingForMethodDecl(
3993 const_cast<ObjCMethodDecl*>(D),
3994 TypeStr))
3995 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00003996
3997 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3998
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003999 if (!Entry)
4000 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004001 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004002 ((ObjCABI == 2) ?
4003 "__TEXT,__objc_methtype,cstring_literals" :
4004 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004005 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004006
Owen Andersona1cf15f2009-07-14 23:10:40 +00004007 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004008}
4009
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004010// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004011llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004012 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004013
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004014 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004015 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004016 llvm::ConstantArray::get(VMContext,
4017 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004018 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004019 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004020
Owen Andersona1cf15f2009-07-14 23:10:40 +00004021 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004022}
4023
4024// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004025// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004026llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004027CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4028 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004029 std::string TypeStr;
4030 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004031 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4032}
4033
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004034void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004035 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004036 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004037 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004038 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004039 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4040 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004041 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004042 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004043 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004044 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004045}
4046
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004047void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004048 EmitModuleInfo();
4049
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004050 // Emit the dummy bodies for any protocols which were referenced but
4051 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004053 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4054 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004055 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004056
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004057 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004058 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004059 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004060 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004061 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004062 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004063 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004064 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004065 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004066 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004067 }
4068
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004069 // Add assembler directives to add lazy undefined symbol references
4070 // for classes which are referenced but not defined. This is
4071 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004072 //
4073 // FIXME: It would be nice if we had an LLVM construct for this.
4074 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4075 llvm::SmallString<256> Asm;
4076 Asm += CGM.getModule().getModuleInlineAsm();
4077 if (!Asm.empty() && Asm.back() != '\n')
4078 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004079
Daniel Dunbar33063492009-09-07 00:20:42 +00004080 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004081 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4082 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004083 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4084 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004085 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004086 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004087 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004088 }
4089
4090 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4091 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4092 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4093 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004094
Daniel Dunbar33063492009-09-07 00:20:42 +00004095 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004096 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004097}
4098
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004099CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004100 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004101 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004102 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004103 ObjCABI = 2;
4104}
4105
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004106/* *** */
4107
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004108ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004109 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004110 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4111 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004112
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004113 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004114 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004115 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004116 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004117 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004118
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004119 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004120 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004121 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004122
Mike Stumpf5408fe2009-05-16 07:57:57 +00004123 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4124 // comes out a bit cleaner.
Chris Lattner2acc6e32011-07-18 04:24:23 +00004125 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004126 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004128 // I'm not sure I like this. The implicit coordination is a bit
4129 // gross. We should solve this in a reasonable fashion because this
4130 // is a pretty common task (match some runtime data structure with
4131 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004132
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004133 // FIXME: This is leaked.
4134 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004135
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004136 // struct _objc_super {
4137 // id self;
4138 // Class cls;
4139 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004140 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004141 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004142 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004143 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004144 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004145 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004146 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004147 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004148 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004149
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004150 SuperCTy = Ctx.getTagDeclType(RD);
4151 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004153 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004154 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4155
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004156 // struct _prop_t {
4157 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004158 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004159 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004160 PropertyTy = llvm::StructType::create("struct._prop_t",
4161 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004162
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004163 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004164 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004165 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004166 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004167 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004168 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004169 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4170 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004171 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004172 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004174 // struct _objc_method {
4175 // SEL _cmd;
4176 // char *method_type;
4177 // char *_imp;
4178 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004179 MethodTy = llvm::StructType::create("struct._objc_method",
4180 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4181 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004183 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004184 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004185 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004186
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004187}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004188
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004189ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004190 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004191 // struct _objc_method_description {
4192 // SEL name;
4193 // char *types;
4194 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004195 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004196 llvm::StructType::create("struct._objc_method_description",
4197 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004198
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004199 // struct _objc_method_description_list {
4200 // int count;
4201 // struct _objc_method_description[1];
4202 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004203 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004204 llvm::StructType::create("struct._objc_method_description_list",
4205 IntTy,
4206 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004207
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004208 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004209 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004210 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004211
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004212 // Protocol description structures
4213
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004214 // struct _objc_protocol_extension {
4215 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4216 // struct _objc_method_description_list *optional_instance_methods;
4217 // struct _objc_method_description_list *optional_class_methods;
4218 // struct _objc_property_list *instance_properties;
4219 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004221 llvm::StructType::create("struct._objc_protocol_extension",
4222 IntTy, MethodDescriptionListPtrTy,
4223 MethodDescriptionListPtrTy, PropertyListPtrTy,
4224 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004226 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004227 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004228
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004229 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004231 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004232 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004233
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004234 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004235 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004236 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004237 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004238 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004239 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004240
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004241 // struct _objc_protocol {
4242 // struct _objc_protocol_extension *isa;
4243 // char *protocol_name;
4244 // struct _objc_protocol **_objc_protocol_list;
4245 // struct _objc_method_description_list *instance_methods;
4246 // struct _objc_method_description_list *class_methods;
4247 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004248 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4249 llvm::PointerType::getUnqual(ProtocolListTy),
4250 MethodDescriptionListPtrTy,
4251 MethodDescriptionListPtrTy,
4252 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004253
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004254 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004255 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004256
Owen Anderson96e0fc72009-07-29 22:16:19 +00004257 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004258
4259 // Class description structures
4260
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004261 // struct _objc_ivar {
4262 // char *ivar_name;
4263 // char *ivar_type;
4264 // int ivar_offset;
4265 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004266 IvarTy = llvm::StructType::create("struct._objc_ivar",
4267 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004268
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004269 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004270 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004271 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004272 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004273
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004274 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004275 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004276 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004277 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004278
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004279 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004280 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004281 llvm::StructType::create("struct._objc_class_extension",
4282 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004283 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004284
Chris Lattnerc1c20112011-08-12 17:43:31 +00004285 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004286
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004287 // struct _objc_class {
4288 // Class isa;
4289 // Class super_class;
4290 // char *name;
4291 // long version;
4292 // long info;
4293 // long instance_size;
4294 // struct _objc_ivar_list *ivars;
4295 // struct _objc_method_list *methods;
4296 // struct _objc_cache *cache;
4297 // struct _objc_protocol_list *protocols;
4298 // char *ivar_layout;
4299 // struct _objc_class_ext *ext;
4300 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004301 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4302 llvm::PointerType::getUnqual(ClassTy),
4303 Int8PtrTy,
4304 LongTy,
4305 LongTy,
4306 LongTy,
4307 IvarListPtrTy,
4308 MethodListPtrTy,
4309 CachePtrTy,
4310 ProtocolListPtrTy,
4311 Int8PtrTy,
4312 ClassExtensionPtrTy,
4313 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314
Owen Anderson96e0fc72009-07-29 22:16:19 +00004315 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004317 // struct _objc_category {
4318 // char *category_name;
4319 // char *class_name;
4320 // struct _objc_method_list *instance_method;
4321 // struct _objc_method_list *class_method;
4322 // uint32_t size; // sizeof(struct _objc_category)
4323 // struct _objc_property_list *instance_properties;// category's @property
4324 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004325 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004326 llvm::StructType::create("struct._objc_category",
4327 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4328 MethodListPtrTy, ProtocolListPtrTy,
4329 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004330
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004331 // Global metadata structures
4332
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004333 // struct _objc_symtab {
4334 // long sel_ref_cnt;
4335 // SEL *refs;
4336 // short cls_def_cnt;
4337 // short cat_def_cnt;
4338 // char *defs[cls_def_cnt + cat_def_cnt];
4339 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004340 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004341 llvm::StructType::create("struct._objc_symtab",
4342 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4343 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004344 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004345
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004346 // struct _objc_module {
4347 // long version;
4348 // long size; // sizeof(struct _objc_module)
4349 // char *name;
4350 // struct _objc_symtab* symtab;
4351 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004352 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004353 llvm::StructType::create("struct._objc_module",
4354 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004355
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004356
Mike Stumpf5408fe2009-05-16 07:57:57 +00004357 // FIXME: This is the size of the setjmp buffer and should be target
4358 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004359 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004360
Anders Carlsson124526b2008-09-09 10:10:21 +00004361 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004362 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004363 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004364
4365 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004366 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004367 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4368 SetJmpBufferSize),
4369 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004370
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004371}
4372
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004374 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004375 // struct _method_list_t {
4376 // uint32_t entsize; // sizeof(struct _objc_method)
4377 // uint32_t method_count;
4378 // struct _objc_method method_list[method_count];
4379 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004380 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004381 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4382 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004383 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004384 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004386 // struct _protocol_t {
4387 // id isa; // NULL
4388 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004389 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004390 // const struct method_list_t * const instance_methods;
4391 // const struct method_list_t * const class_methods;
4392 // const struct method_list_t *optionalInstanceMethods;
4393 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004394 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004395 // const uint32_t size; // sizeof(struct _protocol_t)
4396 // const uint32_t flags; // = 0
4397 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004398
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004399 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004400 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004401 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004402
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004403 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004404 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4405 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4406 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4407 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4408 PropertyListPtrTy, IntTy, IntTy, NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004409
4410 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004411 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Fariborz Jahanianda320092009-01-29 19:24:30 +00004413 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004414 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004415 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004416 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004417 ProtocolListnfABITy->setBody(LongTy,
4418 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4419 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004420
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004421 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004422 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004424 // struct _ivar_t {
4425 // unsigned long int *offset; // pointer to ivar offset location
4426 // char *name;
4427 // char *type;
4428 // uint32_t alignment;
4429 // uint32_t size;
4430 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004431 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004432 llvm::StructType::create("struct._ivar_t",
4433 llvm::PointerType::getUnqual(LongTy),
4434 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004435
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004436 // struct _ivar_list_t {
4437 // uint32 entsize; // sizeof(struct _ivar_t)
4438 // uint32 count;
4439 // struct _iver_t list[count];
4440 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004441 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004442 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4443 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444
Owen Anderson96e0fc72009-07-29 22:16:19 +00004445 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004446
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004447 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004448 // uint32_t const flags;
4449 // uint32_t const instanceStart;
4450 // uint32_t const instanceSize;
4451 // uint32_t const reserved; // only when building for 64bit targets
4452 // const uint8_t * const ivarLayout;
4453 // const char *const name;
4454 // const struct _method_list_t * const baseMethods;
4455 // const struct _objc_protocol_list *const baseProtocols;
4456 // const struct _ivar_list_t *const ivars;
4457 // const uint8_t * const weakIvarLayout;
4458 // const struct _prop_list_t * const properties;
4459 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004460
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004461 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004462 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4463 IntTy, IntTy, IntTy, Int8PtrTy,
4464 Int8PtrTy, MethodListnfABIPtrTy,
4465 ProtocolListnfABIPtrTy,
4466 IvarListnfABIPtrTy,
4467 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004469 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004470 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004471 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4472 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004474 // struct _class_t {
4475 // struct _class_t *isa;
4476 // struct _class_t * const superclass;
4477 // void *cache;
4478 // IMP *vtable;
4479 // struct class_ro_t *ro;
4480 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004481
Chris Lattnerc1c20112011-08-12 17:43:31 +00004482 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004483 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4484 llvm::PointerType::getUnqual(ClassnfABITy),
4485 CachePtrTy,
4486 llvm::PointerType::getUnqual(ImpnfABITy),
4487 llvm::PointerType::getUnqual(ClassRonfABITy),
4488 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004489
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004490 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004491 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004493 // struct _category_t {
4494 // const char * const name;
4495 // struct _class_t *const cls;
4496 // const struct _method_list_t * const instance_methods;
4497 // const struct _method_list_t * const class_methods;
4498 // const struct _protocol_list_t * const protocols;
4499 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004500 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004501 CategorynfABITy = llvm::StructType::create("struct._category_t",
4502 Int8PtrTy, ClassnfABIPtrTy,
4503 MethodListnfABIPtrTy,
4504 MethodListnfABIPtrTy,
4505 ProtocolListnfABIPtrTy,
4506 PropertyListPtrTy,
4507 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004508
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004509 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004510 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4511 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004512
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004513 // MessageRefTy - LLVM for:
4514 // struct _message_ref_t {
4515 // IMP messenger;
4516 // SEL name;
4517 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004518
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004519 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004520 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004521 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004522 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004523 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004524 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004525 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004526 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004527 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004528 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004529
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004530 MessageRefCTy = Ctx.getTagDeclType(RD);
4531 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4532 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004533
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004534 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004535 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004537 // SuperMessageRefTy - LLVM for:
4538 // struct _super_message_ref_t {
4539 // SUPER_IMP messenger;
4540 // SEL name;
4541 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004542 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004543 llvm::StructType::create("struct._super_message_ref_t",
4544 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004546 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004547 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004548
Daniel Dunbare588b992009-03-01 04:46:24 +00004549
4550 // struct objc_typeinfo {
4551 // const void** vtable; // objc_ehtype_vtable + 2
4552 // const char* name; // c++ typeinfo string
4553 // Class cls;
4554 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004555 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004556 llvm::StructType::create("struct._objc_typeinfo",
4557 llvm::PointerType::getUnqual(Int8PtrTy),
4558 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004559 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004560}
4561
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004562llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004563 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004564
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004565 return NULL;
4566}
4567
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004568void CGObjCNonFragileABIMac::AddModuleClassList(const
4569 std::vector<llvm::GlobalValue*>
4570 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004571 const char *SymbolName,
4572 const char *SectionName) {
4573 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004574
Daniel Dunbar463b8762009-05-15 21:48:48 +00004575 if (!NumClasses)
4576 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004577
Daniel Dunbar463b8762009-05-15 21:48:48 +00004578 std::vector<llvm::Constant*> Symbols(NumClasses);
4579 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004580 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004581 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004582 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004583 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004584 NumClasses),
4585 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Daniel Dunbar463b8762009-05-15 21:48:48 +00004587 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004588 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004589 llvm::GlobalValue::InternalLinkage,
4590 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004591 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004592 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004593 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004594 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004595}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004597void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4598 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Daniel Dunbar463b8762009-05-15 21:48:48 +00004600 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004601 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004603 "\01L_OBJC_LABEL_CLASS_$",
4604 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004605
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004606 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4607 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4608 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4609 continue;
4610 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004611 }
4612
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004613 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4614 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4615 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4616 continue;
4617 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4618 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004619
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004621 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4622 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004623
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004624 // Build list of all implemented category addresses in array
4625 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004626 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004627 "\01L_OBJC_LABEL_CATEGORY_$",
4628 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004630 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4631 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004632
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004633 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004634}
4635
John McCall944c8432011-05-14 03:10:52 +00004636/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4637/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004639/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004640bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4641 // At various points we've experimented with using vtable-based
4642 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004643 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4644 default:
John McCall944c8432011-05-14 03:10:52 +00004645 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004646 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004647 return false;
John McCall944c8432011-05-14 03:10:52 +00004648 case CodeGenOptions::NonLegacy:
4649 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004650 case CodeGenOptions::Mixed:
4651 break;
4652 }
4653
4654 // If so, see whether this selector is in the white-list of things which must
4655 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004656 if (VTableDispatchMethods.empty()) {
4657 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4658 VTableDispatchMethods.insert(GetNullarySelector("class"));
4659 VTableDispatchMethods.insert(GetNullarySelector("self"));
4660 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4661 VTableDispatchMethods.insert(GetNullarySelector("length"));
4662 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663
John McCall944c8432011-05-14 03:10:52 +00004664 // These are vtable-based if GC is disabled.
4665 // Optimistically use vtable dispatch for hybrid compiles.
4666 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
4667 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4668 VTableDispatchMethods.insert(GetNullarySelector("release"));
4669 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4670 }
4671
4672 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4673 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4674 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4675 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4676 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4677 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4678 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4679
4680 // These are vtable-based if GC is enabled.
4681 // Optimistically use vtable dispatch for hybrid compiles.
4682 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
4683 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4684 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4685
4686 // "countByEnumeratingWithState:objects:count"
4687 IdentifierInfo *KeyIdents[] = {
4688 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4689 &CGM.getContext().Idents.get("objects"),
4690 &CGM.getContext().Idents.get("count")
4691 };
4692 VTableDispatchMethods.insert(
4693 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4694 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004695 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004696
John McCall944c8432011-05-14 03:10:52 +00004697 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004698}
4699
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004700// Metadata flags
4701enum MetaDataDlags {
4702 CLS = 0x0,
4703 CLS_META = 0x1,
4704 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004705 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004706 CLS_EXCEPTION = 0x20,
4707
4708 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4709 CLS_HAS_IVAR_RELEASER = 0x40,
4710 /// class was compiled with -fobjc-arr
4711 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004712};
4713/// BuildClassRoTInitializer - generate meta-data for:
4714/// struct _class_ro_t {
4715/// uint32_t const flags;
4716/// uint32_t const instanceStart;
4717/// uint32_t const instanceSize;
4718/// uint32_t const reserved; // only when building for 64bit targets
4719/// const uint8_t * const ivarLayout;
4720/// const char *const name;
4721/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004722/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004723/// const struct _ivar_list_t *const ivars;
4724/// const uint8_t * const weakIvarLayout;
4725/// const struct _prop_list_t * const properties;
4726/// }
4727///
4728llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004729 unsigned flags,
4730 unsigned InstanceStart,
4731 unsigned InstanceSize,
4732 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004733 std::string ClassName = ID->getNameAsString();
4734 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004735
4736 if (CGM.getLangOptions().ObjCAutoRefCount)
4737 flags |= CLS_COMPILED_BY_ARC;
4738
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004739 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4740 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4741 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004742 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4744 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004745 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004746 // const struct _method_list_t * const baseMethods;
4747 std::vector<llvm::Constant*> Methods;
4748 std::string MethodListName("\01l_OBJC_$_");
4749 if (flags & CLS_META) {
4750 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004751 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004752 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004753 // Class methods should always be defined.
4754 Methods.push_back(GetMethodConstant(*i));
4755 }
4756 } else {
4757 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004758 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004759 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004760 // Instance methods should always be defined.
4761 Methods.push_back(GetMethodConstant(*i));
4762 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004763 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004764 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004765 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004766
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004767 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4768 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004769
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004770 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4771 if (llvm::Constant *C = GetMethodConstant(MD))
4772 Methods.push_back(C);
4773 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4774 if (llvm::Constant *C = GetMethodConstant(MD))
4775 Methods.push_back(C);
4776 }
4777 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004778 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004779 Values[ 5] = EmitMethodList(MethodListName,
4780 "__DATA, __objc_const", Methods);
4781
Fariborz Jahanianda320092009-01-29 19:24:30 +00004782 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4783 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004784 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004785 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004786 OID->all_referenced_protocol_begin(),
4787 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004789 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004790 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004791 else
4792 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004793 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4794 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004795 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004796 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004797 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004798 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4799 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004800 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004801 Values);
4802 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004803 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4804 llvm::GlobalValue::InternalLinkage,
4805 Init,
4806 (flags & CLS_META) ?
4807 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4808 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004809 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004810 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004811 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004812 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004813
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004814}
4815
4816/// BuildClassMetaData - This routine defines that to-level meta-data
4817/// for the given ClassName for:
4818/// struct _class_t {
4819/// struct _class_t *isa;
4820/// struct _class_t * const superclass;
4821/// void *cache;
4822/// IMP *vtable;
4823/// struct class_ro_t *ro;
4824/// }
4825///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004826llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004827 std::string &ClassName,
4828 llvm::Constant *IsAGV,
4829 llvm::Constant *SuperClassGV,
4830 llvm::Constant *ClassRoGV,
4831 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004832 std::vector<llvm::Constant*> Values(5);
4833 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004834 Values[1] = SuperClassGV;
4835 if (!Values[1])
4836 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004837 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4838 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4839 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004840 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004841 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004842 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4843 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004844 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004845 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004846 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004847 if (HiddenVisibility)
4848 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004849 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004850}
4851
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004852bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004853CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004854 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004855}
4856
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004857void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004858 uint32_t &InstanceStart,
4859 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004860 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004861 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004863 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004864 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004865
4866 // If there are no fields, the start is the same as the end.
4867 if (!RL.getFieldCount())
4868 InstanceStart = InstanceSize;
4869 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004870 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004871}
4872
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004873void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4874 std::string ClassName = ID->getNameAsString();
4875 if (!ObjCEmptyCacheVar) {
4876 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 CGM.getModule(),
4878 ObjCTypes.CacheTy,
4879 false,
4880 llvm::GlobalValue::ExternalLinkage,
4881 0,
4882 "_objc_empty_cache");
4883
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004884 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004885 CGM.getModule(),
4886 ObjCTypes.ImpnfABITy,
4887 false,
4888 llvm::GlobalValue::ExternalLinkage,
4889 0,
4890 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004891 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004892 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004893 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004894 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004895 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004896 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004897 uint32_t InstanceSize = InstanceStart;
4898 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004899 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4900 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004901
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004902 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004903
4904 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004905 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004906 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004907 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004908 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004909 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004910 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004911 // class is root
4912 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004913 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004914 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004915 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004916 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004917 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4918 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4919 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004920 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004921 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004922 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004923 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004924 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004925 ObjCMetaClassName +
4926 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004927 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004928 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004929 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004930 }
4931 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4932 InstanceStart,
4933 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004934 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004935 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004936 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4937 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004938 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004939
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004940 // Metadata for the class
4941 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004942 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004943 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004944 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004945 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004946
Douglas Gregor68584ed2009-06-18 16:11:24 +00004947 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004948 flags |= CLS_EXCEPTION;
4949
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004950 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004951 flags |= CLS_ROOT;
4952 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004953 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004954 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004955 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004956 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004957 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004958 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004959 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004960 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004961 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004962 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004963 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004964 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004965 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004966
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004967 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004969 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4970 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004971 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004972
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004973 // Determine if this class is also "non-lazy".
4974 if (ImplementationIsNonLazy(ID))
4975 DefinedNonLazyClasses.push_back(ClassMD);
4976
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004977 // Force the definition of the EHType if necessary.
4978 if (flags & CLS_EXCEPTION)
4979 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00004980 // Make sure method definition entries are all clear for next implementation.
4981 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004982}
4983
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004984/// GenerateProtocolRef - This routine is called to generate code for
4985/// a protocol reference expression; as in:
4986/// @code
4987/// @protocol(Proto1);
4988/// @endcode
4989/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4990/// which will hold address of the protocol meta-data.
4991///
4992llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993 const ObjCProtocolDecl *PD) {
4994
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004995 // This routine is called for @protocol only. So, we must build definition
4996 // of protocol's meta-data (not a reference to it!)
4997 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004998 llvm::Constant *Init =
4999 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5000 ObjCTypes.ExternalProtocolPtrTy);
5001
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005002 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005003 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005004
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005005 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5006 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005007 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005008 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009 CGM.getModule(),
5010 Init->getType(), false,
5011 llvm::GlobalValue::WeakAnyLinkage,
5012 Init,
5013 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005014 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5015 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005016 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005017 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005018}
5019
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005020/// GenerateCategory - Build metadata for a category implementation.
5021/// struct _category_t {
5022/// const char * const name;
5023/// struct _class_t *const cls;
5024/// const struct _method_list_t * const instance_methods;
5025/// const struct _method_list_t * const class_methods;
5026/// const struct _protocol_list_t * const protocols;
5027/// const struct _prop_list_t * const properties;
5028/// }
5029///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005030void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005031 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005032 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005033 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5034 "_$_" + OCD->getNameAsString());
5035 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005036 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005037
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005038 std::vector<llvm::Constant*> Values(6);
5039 Values[0] = GetClassName(OCD->getIdentifier());
5040 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005041 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005042 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005043 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5044
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005045 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005046 std::vector<llvm::Constant*> Methods;
5047 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005048 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005049 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005050
5051 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005052 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005053 // Instance methods should always be defined.
5054 Methods.push_back(GetMethodConstant(*i));
5055 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005056
5057 Values[2] = EmitMethodList(MethodListName,
5058 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005059 Methods);
5060
5061 MethodListName = Prefix;
5062 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5063 OCD->getNameAsString();
5064 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005065 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005066 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005067 // Class methods should always be defined.
5068 Methods.push_back(GetMethodConstant(*i));
5069 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005070
5071 Values[3] = EmitMethodList(MethodListName,
5072 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005073 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005074 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005075 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005076 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005077 llvm::SmallString<256> ExtName;
5078 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5079 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005080 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005081 + Interface->getName() + "_$_"
5082 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005083 Category->protocol_begin(),
5084 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005085 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5086 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005087 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005088 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5089 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005090 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005091
5092 llvm::Constant *Init =
5093 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005094 Values);
5095 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005097 false,
5098 llvm::GlobalValue::InternalLinkage,
5099 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005100 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005101 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005102 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005103 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005104 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005105 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005106
5107 // Determine if this category is also "non-lazy".
5108 if (ImplementationIsNonLazy(OCD))
5109 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005110 // method definition entries must be clear for next implementation.
5111 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005112}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005113
5114/// GetMethodConstant - Return a struct objc_method constant for the
5115/// given method if it has been defined. The result is null if the
5116/// method has not been defined. The return value has type MethodPtrTy.
5117llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005118 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005119 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005120 if (!Fn)
5121 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005122
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005123 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005124 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005125 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005126 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005127 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005128 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005129 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005130}
5131
5132/// EmitMethodList - Build meta-data for method declarations
5133/// struct _method_list_t {
5134/// uint32_t entsize; // sizeof(struct _objc_method)
5135/// uint32_t method_count;
5136/// struct _objc_method method_list[method_count];
5137/// }
5138///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005139llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005140 const char *Section,
5141 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005142 // Return null for empty list.
5143 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005144 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005145
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005146 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005147 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005148 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005149 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005150 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005151 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005152 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005153 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005154 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005155 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005156
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005157 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005158 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005159 llvm::GlobalValue::InternalLinkage, Init, Name);
5160 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005161 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005162 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005163 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005164}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005165
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005166/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5167/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005168llvm::GlobalVariable *
5169CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5170 const ObjCIvarDecl *Ivar) {
5171 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005172 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005173 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005174 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005175 CGM.getModule().getGlobalVariable(Name);
5176 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005177 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005178 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005179 false,
5180 llvm::GlobalValue::ExternalLinkage,
5181 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005182 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005183 return IvarOffsetGV;
5184}
5185
Daniel Dunbare83be122010-04-02 21:14:02 +00005186llvm::Constant *
5187CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5188 const ObjCIvarDecl *Ivar,
5189 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005190 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005191 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005192 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005193 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005194 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005195
Mike Stumpf5408fe2009-05-16 07:57:57 +00005196 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5197 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005198 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5199 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005200 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005201 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005202 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005203 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005204 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005205 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005206}
5207
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005208/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005209/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005210/// IvarListnfABIPtrTy.
5211/// struct _ivar_t {
5212/// unsigned long int *offset; // pointer to ivar offset location
5213/// char *name;
5214/// char *type;
5215/// uint32_t alignment;
5216/// uint32_t size;
5217/// }
5218/// struct _ivar_list_t {
5219/// uint32 entsize; // sizeof(struct _ivar_t)
5220/// uint32 count;
5221/// struct _iver_t list[count];
5222/// }
5223///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005224
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005225llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005226 const ObjCImplementationDecl *ID) {
5227
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005228 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005229
Jordy Rosedb8264e2011-07-22 02:08:32 +00005230 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005231 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005232
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005233 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005234
Jordy Rosedb8264e2011-07-22 02:08:32 +00005235 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005236 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005237 // Ignore unnamed bit-fields.
5238 if (!IVD->getDeclName())
5239 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005240 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005241 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005242 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5243 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005244 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005245 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005246 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005247 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005248 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005249 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005250 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005251 // NOTE. Size of a bitfield does not match gcc's, because of the
5252 // way bitfields are treated special in each. But I am told that
5253 // 'size' for bitfield ivars is ignored by the runtime so it does
5254 // not matter. If it matters, there is enough info to get the
5255 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005256 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005257 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005258 }
5259 // Return null for empty list.
5260 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005261 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005262
5263 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005264 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005265 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5266 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005267 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005268 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005269 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005270 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005271 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5272 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005273 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005274 llvm::GlobalValue::InternalLinkage,
5275 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005276 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005277 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005278 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005279 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280
Chris Lattnerad64e022009-07-17 23:57:13 +00005281 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005282 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005283}
5284
5285llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005286 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005287 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005288
Fariborz Jahanianda320092009-01-29 19:24:30 +00005289 if (!Entry) {
5290 // We use the initializer as a marker of whether this is a forward
5291 // reference or not. At module finalization we add the empty
5292 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005293 Entry =
5294 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5295 llvm::GlobalValue::ExternalLinkage,
5296 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005297 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005298 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005299 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005300
Fariborz Jahanianda320092009-01-29 19:24:30 +00005301 return Entry;
5302}
5303
5304/// GetOrEmitProtocol - Generate the protocol meta-data:
5305/// @code
5306/// struct _protocol_t {
5307/// id isa; // NULL
5308/// const char * const protocol_name;
5309/// const struct _protocol_list_t * protocol_list; // super protocols
5310/// const struct method_list_t * const instance_methods;
5311/// const struct method_list_t * const class_methods;
5312/// const struct method_list_t *optionalInstanceMethods;
5313/// const struct method_list_t *optionalClassMethods;
5314/// const struct _prop_list_t * properties;
5315/// const uint32_t size; // sizeof(struct _protocol_t)
5316/// const uint32_t flags; // = 0
5317/// }
5318/// @endcode
5319///
5320
5321llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005323 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324
Fariborz Jahanianda320092009-01-29 19:24:30 +00005325 // Early exit if a defining object has already been generated.
5326 if (Entry && Entry->hasInitializer())
5327 return Entry;
5328
Fariborz Jahanianda320092009-01-29 19:24:30 +00005329 // Construct method lists.
5330 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5331 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005332 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005333 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005334 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005335 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005336 if (!C)
5337 return GetOrEmitProtocolRef(PD);
5338
Fariborz Jahanianda320092009-01-29 19:24:30 +00005339 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5340 OptInstanceMethods.push_back(C);
5341 } else {
5342 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005344 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005345
5346 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005347 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005348 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005349 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005350 if (!C)
5351 return GetOrEmitProtocolRef(PD);
5352
Fariborz Jahanianda320092009-01-29 19:24:30 +00005353 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5354 OptClassMethods.push_back(C);
5355 } else {
5356 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005358 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005359
Fariborz Jahanianda320092009-01-29 19:24:30 +00005360 std::vector<llvm::Constant*> Values(10);
5361 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005362 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005363 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005364 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5365 PD->protocol_begin(),
5366 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005368 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005370 "__DATA, __objc_const",
5371 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005373 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005374 "__DATA, __objc_const",
5375 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005376 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005377 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005378 "__DATA, __objc_const",
5379 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005380 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005381 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005382 "__DATA, __objc_const",
5383 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005384 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005385 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005387 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005388 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005389 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005390 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005392
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 if (Entry) {
5394 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005395 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005396 Entry->setInitializer(Init);
5397 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005398 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005399 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5400 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5401 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005402 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005403 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005405 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005406 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005407 CGM.AddUsedGlobal(Entry);
5408
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005409 // Use this protocol meta-data to build protocol list table in section
5410 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005411 llvm::GlobalVariable *PTGV =
5412 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5413 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5414 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005415 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005416 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005417 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005418 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005419 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005420 return Entry;
5421}
5422
5423/// EmitProtocolList - Generate protocol list meta-data:
5424/// @code
5425/// struct _protocol_list_t {
5426/// long protocol_count; // Note, this is 32/64 bit
5427/// struct _protocol_t[protocol_count];
5428/// }
5429/// @endcode
5430///
5431llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005432CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005433 ObjCProtocolDecl::protocol_iterator begin,
5434 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005435 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005436
Fariborz Jahanianda320092009-01-29 19:24:30 +00005437 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005438 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005439 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005440
Daniel Dunbar948e2582009-02-15 07:36:20 +00005441 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005442 llvm::SmallString<256> TmpName;
5443 Name.toVector(TmpName);
5444 llvm::GlobalVariable *GV =
5445 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005446 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005447 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005448
Daniel Dunbar948e2582009-02-15 07:36:20 +00005449 for (; begin != end; ++begin)
5450 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5451
Fariborz Jahanianda320092009-01-29 19:24:30 +00005452 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005453 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454 ObjCTypes.ProtocolnfABIPtrTy));
5455
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005456 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005457 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005458 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005460 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005461 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462 ProtocolRefs.size()),
5463 ProtocolRefs);
5464
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005465 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005466 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005467 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005468 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005469 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005470 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005471 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005472 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005474 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005475}
5476
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005477/// GetMethodDescriptionConstant - This routine build following meta-data:
5478/// struct _objc_method {
5479/// SEL _cmd;
5480/// char *method_type;
5481/// char *_imp;
5482/// }
5483
5484llvm::Constant *
5485CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5486 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005487 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005488 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5489 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005490 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005491 if (!Desc[1])
5492 return 0;
5493
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005494 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005495 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005496 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005497}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005498
5499/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5500/// This code gen. amounts to generating code for:
5501/// @code
5502/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5503/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005504///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005505LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005506 CodeGen::CodeGenFunction &CGF,
5507 QualType ObjectTy,
5508 llvm::Value *BaseValue,
5509 const ObjCIvarDecl *Ivar,
5510 unsigned CVRQualifiers) {
5511 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005512 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5513 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005514}
5515
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005516llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005517 CodeGen::CodeGenFunction &CGF,
5518 const ObjCInterfaceDecl *Interface,
5519 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005520 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005521}
5522
John McCallb1e81442011-05-13 23:16:18 +00005523static void appendSelectorForMessageRefTable(std::string &buffer,
5524 Selector selector) {
5525 if (selector.isUnarySelector()) {
5526 buffer += selector.getNameForSlot(0);
5527 return;
5528 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005529
John McCallb1e81442011-05-13 23:16:18 +00005530 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5531 buffer += selector.getNameForSlot(i);
5532 buffer += '_';
5533 }
5534}
5535
John McCall944c8432011-05-14 03:10:52 +00005536/// Emit a "v-table" message send. We emit a weak hidden-visibility
5537/// struct, initially containing the selector pointer and a pointer to
5538/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5539/// load and call the function pointer, passing the address of the
5540/// struct as the second parameter. The runtime determines whether
5541/// the selector is currently emitted using vtable dispatch; if so, it
5542/// substitutes a stub function which simply tail-calls through the
5543/// appropriate vtable slot, and if not, it substitues a stub function
5544/// which tail-calls objc_msgSend. Both stubs adjust the selector
5545/// argument to correctly point to the selector.
5546RValue
5547CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5548 ReturnValueSlot returnSlot,
5549 QualType resultType,
5550 Selector selector,
5551 llvm::Value *arg0,
5552 QualType arg0Type,
5553 bool isSuper,
5554 const CallArgList &formalArgs,
5555 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005556 // Compute the actual arguments.
5557 CallArgList args;
5558
John McCall944c8432011-05-14 03:10:52 +00005559 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005560 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005561 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5562 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005563
John McCall944c8432011-05-14 03:10:52 +00005564 // Second argument: a pointer to the message ref structure. Leave
5565 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005566 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5567
5568 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5569
5570 const CGFunctionInfo &fnInfo =
5571 CGM.getTypes().getFunctionInfo(resultType, args,
5572 FunctionType::ExtInfo());
5573
John McCallcba681a2011-05-14 21:12:11 +00005574 NullReturnState nullReturn;
5575
John McCall944c8432011-05-14 03:10:52 +00005576 // Find the function to call and the mangled name for the message
5577 // ref structure. Using a different mangled name wouldn't actually
5578 // be a problem; it would just be a waste.
5579 //
5580 // The runtime currently never uses vtable dispatch for anything
5581 // except normal, non-super message-sends.
5582 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005583 llvm::Constant *fn = 0;
5584 std::string messageRefName("\01l_");
5585 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005586 if (isSuper) {
5587 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5588 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005589 } else {
John McCallcba681a2011-05-14 21:12:11 +00005590 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005591 fn = ObjCTypes.getMessageSendStretFixupFn();
5592 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005593 }
John McCallb1e81442011-05-13 23:16:18 +00005594 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5595 fn = ObjCTypes.getMessageSendFpretFixupFn();
5596 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005597 } else {
John McCallb1e81442011-05-13 23:16:18 +00005598 if (isSuper) {
5599 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5600 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005601 } else {
John McCallb1e81442011-05-13 23:16:18 +00005602 fn = ObjCTypes.getMessageSendFixupFn();
5603 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005604 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005605 }
John McCallb1e81442011-05-13 23:16:18 +00005606 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5607 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005608
John McCallb1e81442011-05-13 23:16:18 +00005609 // Append the selector name, except use underscores anywhere we
5610 // would have used colons.
5611 appendSelectorForMessageRefTable(messageRefName, selector);
5612
5613 llvm::GlobalVariable *messageRef
5614 = CGM.getModule().getGlobalVariable(messageRefName);
5615 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005616 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005617 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005618 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005619 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5620 init->getType(),
5621 /*constant*/ false,
5622 llvm::GlobalValue::WeakAnyLinkage,
5623 init,
5624 messageRefName);
5625 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5626 messageRef->setAlignment(16);
5627 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5628 }
5629 llvm::Value *mref =
5630 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5631
John McCall944c8432011-05-14 03:10:52 +00005632 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005633 args[1].RV = RValue::get(mref);
5634
5635 // Load the function to call from the message ref table.
5636 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5637 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5638
5639 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005640 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005641 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5642 callee = CGF.Builder.CreateBitCast(callee,
5643 llvm::PointerType::getUnqual(fnType));
5644
John McCallcba681a2011-05-14 21:12:11 +00005645 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5646 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005647}
5648
5649/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005650CodeGen::RValue
5651CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005652 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005653 QualType ResultType,
5654 Selector Sel,
5655 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005656 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005657 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005658 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005659 return isVTableDispatchedSelector(Sel)
5660 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005661 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005662 false, CallArgs, Method)
5663 : EmitMessageSend(CGF, Return, ResultType,
5664 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005665 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005666 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005667}
5668
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005669llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005670CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005671 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5672
Daniel Dunbardfff2302009-03-02 05:18:14 +00005673 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005674 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005675 false, llvm::GlobalValue::ExternalLinkage,
5676 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005677 }
5678
5679 return GV;
5680}
5681
John McCallf85e1932011-06-15 23:02:42 +00005682llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5683 IdentifierInfo *II) {
5684 llvm::GlobalVariable *&Entry = ClassReferences[II];
5685
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005686 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005687 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005688 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005689 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005690 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5691 false, llvm::GlobalValue::InternalLinkage,
5692 ClassGV,
5693 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005694 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005695 CGM.getTargetData().getABITypeAlignment(
5696 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005697 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005698 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005699 }
John McCallf85e1932011-06-15 23:02:42 +00005700
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005701 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005702}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005703
John McCallf85e1932011-06-15 23:02:42 +00005704llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5705 const ObjCInterfaceDecl *ID) {
5706 return EmitClassRefFromId(Builder, ID->getIdentifier());
5707}
5708
5709llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5710 CGBuilderTy &Builder) {
5711 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5712 return EmitClassRefFromId(Builder, II);
5713}
5714
Daniel Dunbar11394522009-04-18 08:51:00 +00005715llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005717 const ObjCInterfaceDecl *ID) {
5718 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719
Daniel Dunbar11394522009-04-18 08:51:00 +00005720 if (!Entry) {
5721 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5722 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723 Entry =
5724 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005725 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005727 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005728 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005729 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005730 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005731 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005732 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005733 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005735 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005736}
5737
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005738/// EmitMetaClassRef - Return a Value * of the address of _class_t
5739/// meta-data
5740///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5742 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005743 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5744 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005745 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005746
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005747 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005748 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005749 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005750 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005751 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005753 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005755 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005756 ObjCTypes.ClassnfABIPtrTy));
5757
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005758 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005759 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005760
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005761 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005762}
5763
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005764/// GetClass - Return a reference to the class for the given interface
5765/// decl.
5766llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5767 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005768 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005769 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5770 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5771 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5772 }
5773
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005774 return EmitClassRef(Builder, ID);
5775}
5776
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005777/// Generates a message send where the super is the receiver. This is
5778/// a message send to self with special delivery semantics indicating
5779/// which class's method should be called.
5780CodeGen::RValue
5781CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005782 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005783 QualType ResultType,
5784 Selector Sel,
5785 const ObjCInterfaceDecl *Class,
5786 bool isCategoryImpl,
5787 llvm::Value *Receiver,
5788 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005789 const CodeGen::CallArgList &CallArgs,
5790 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005791 // ...
5792 // Create and init a super structure; this is a (receiver, class)
5793 // pair we will pass to objc_msgSendSuper.
5794 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005795 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005796
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005797 llvm::Value *ReceiverAsObject =
5798 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5799 CGF.Builder.CreateStore(ReceiverAsObject,
5800 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005801
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005802 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005803 llvm::Value *Target;
5804 if (IsClassMessage) {
5805 if (isCategoryImpl) {
5806 // Message sent to "super' in a class method defined in
5807 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005808 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005809 Target = CGF.Builder.CreateStructGEP(Target, 0);
5810 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005811 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005812 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005813 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005814 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005815
Mike Stumpf5408fe2009-05-16 07:57:57 +00005816 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5817 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005818 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005819 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5820 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5821 CGF.Builder.CreateStore(Target,
5822 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005823
John McCall944c8432011-05-14 03:10:52 +00005824 return (isVTableDispatchedSelector(Sel))
5825 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005827 true, CallArgs, Method)
5828 : EmitMessageSend(CGF, Return, ResultType,
5829 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005830 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005831 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005832}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005833
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005835 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005836 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005837
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005838 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005839 llvm::Constant *Casted =
5840 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5841 ObjCTypes.SelectorPtrTy);
5842 Entry =
5843 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5844 llvm::GlobalValue::InternalLinkage,
5845 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005846 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005847 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005848 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005850 if (lval)
5851 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005852 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005853}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005854/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005855/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005856///
5857void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005858 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005859 llvm::Value *dst,
5860 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005861 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005862 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005863 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005864 assert(Size <= 8 && "does not support size > 8");
5865 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5866 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005867 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5868 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005869 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5870 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005871 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5872 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873 return;
5874}
5875
5876/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5877/// objc_assign_strongCast (id src, id *dst)
5878///
5879void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005880 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005881 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005882 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005883 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005884 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005885 assert(Size <= 8 && "does not support size > 8");
5886 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005887 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005888 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5889 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005890 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5891 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005892 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005893 src, dst, "weakassign");
5894 return;
5895}
5896
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005897void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005898 CodeGen::CodeGenFunction &CGF,
5899 llvm::Value *DestPtr,
5900 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005901 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005902 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5903 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005904 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005905 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005906 return;
5907}
5908
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005909/// EmitObjCWeakRead - Code gen for loading value of a __weak
5910/// object: objc_read_weak (id *src)
5911///
5912llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005913 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005914 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005915 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005916 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5917 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005918 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005919 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005920 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005921 return read_weak;
5922}
5923
5924/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5925/// objc_assign_weak (id src, id *dst)
5926///
5927void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005928 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005929 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005930 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005931 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005932 assert(Size <= 8 && "does not support size > 8");
5933 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5934 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005935 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5936 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005937 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5938 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005939 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005940 src, dst, "weakassign");
5941 return;
5942}
5943
5944/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5945/// objc_assign_global (id src, id *dst)
5946///
5947void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005948 llvm::Value *src, llvm::Value *dst,
5949 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005950 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005951 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005952 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005953 assert(Size <= 8 && "does not support size > 8");
5954 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5955 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005956 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5957 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005958 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5959 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005960 if (!threadlocal)
5961 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5962 src, dst, "globalassign");
5963 else
5964 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5965 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005966 return;
5967}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005968
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005969void
John McCallf1549f62010-07-06 01:34:17 +00005970CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5971 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005972 EmitAtSynchronizedStmt(CGF, S,
5973 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5974 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005975}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005976
John McCall5a180392010-07-24 00:37:23 +00005977llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00005978CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00005979 // There's a particular fixed type info for 'id'.
5980 if (T->isObjCIdType() ||
5981 T->isObjCQualifiedIdType()) {
5982 llvm::Constant *IDEHType =
5983 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5984 if (!IDEHType)
5985 IDEHType =
5986 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5987 false,
5988 llvm::GlobalValue::ExternalLinkage,
5989 0, "OBJC_EHTYPE_id");
5990 return IDEHType;
5991 }
5992
5993 // All other types should be Objective-C interface pointer types.
5994 const ObjCObjectPointerType *PT =
5995 T->getAs<ObjCObjectPointerType>();
5996 assert(PT && "Invalid @catch type.");
5997 const ObjCInterfaceType *IT = PT->getInterfaceType();
5998 assert(IT && "Invalid @catch type.");
5999 return GetInterfaceEHType(IT->getDecl(), false);
6000}
6001
John McCallf1549f62010-07-06 01:34:17 +00006002void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6003 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006004 EmitTryCatchStmt(CGF, S,
6005 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6006 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6007 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006008}
6009
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006010/// EmitThrowStmt - Generate code for a throw statement.
6011void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6012 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006013 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006014 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006015 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6016 "tmp");
Jay Foad4c7d9f12011-07-15 08:37:34 +00006017 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006018 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006019 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006020 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006021 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006022 }
6023
John McCall7ec404c2010-10-16 08:21:07 +00006024 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006025 CGF.Builder.ClearInsertionPoint();
6026}
Daniel Dunbare588b992009-03-01 04:46:24 +00006027
John McCall5a180392010-07-24 00:37:23 +00006028llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006029CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006030 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006031 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006032
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006033 // If we don't need a definition, return the entry if found or check
6034 // if we use an external reference.
6035 if (!ForDefinition) {
6036 if (Entry)
6037 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006038
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006039 // If this type (or a super class) has the __objc_exception__
6040 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006041 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006042 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006043 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006044 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006045 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006046 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006047 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006048 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006049
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006050 // Otherwise we need to either make a new entry or fill in the
6051 // initializer.
6052 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006053 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006054 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006055 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006056 CGM.getModule().getGlobalVariable(VTableName);
6057 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006058 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6059 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006060 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006061 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006062
Chris Lattner77b89b82010-06-27 07:15:29 +00006063 llvm::Value *VTableIdx =
6064 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006065
6066 std::vector<llvm::Constant*> Values(3);
Jay Foada5c04342011-07-21 14:31:17 +00006067 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx);
Daniel Dunbare588b992009-03-01 04:46:24 +00006068 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006069 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006070 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006071 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006072
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006073 if (Entry) {
6074 Entry->setInitializer(Init);
6075 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006076 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006077 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006078 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006079 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006080 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006081 }
6082
John McCall1fb0caa2010-10-22 21:05:15 +00006083 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006084 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006085 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6086 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006087
6088 if (ForDefinition) {
6089 Entry->setSection("__DATA,__objc_const");
6090 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6091 } else {
6092 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6093 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006094
6095 return Entry;
6096}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006097
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006098/* *** */
6099
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006100CodeGen::CGObjCRuntime *
6101CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006102 if (CGM.getLangOptions().ObjCNonFragileABI)
6103 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006104 return new CGObjCMac(CGM);
6105}