blob: 1058886e61233b0e0989859cb1b0d1505ed5a2fd [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-04-22 07:32:20 +000045
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000046namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000047
Daniel Dunbar6bff2512009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +000049
Daniel Dunbar6bff2512009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000052
Fariborz Jahanianee0af742009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000056
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000057private:
John McCall0774cb82011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCallf85e1932011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000074
John McCall0774cb82011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000086 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000087
John McCall0774cb82011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson0032b272009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall0774cb82011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000099
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000101
John McCall0774cb82011-05-15 01:53:33 +0000102 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
103 ///
104 /// The messenger used for super calls, which have different dispatch
105 /// semantics. The class passed is the superclass of the current
106 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000107 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000108 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000110 params, true),
111 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000112 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000113
John McCall0774cb82011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
115 ///
116 /// A slightly different messenger used for super calls. The class
117 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000118 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000119 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000121 params, true),
122 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000123 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000124
John McCall0774cb82011-05-15 01:53:33 +0000125 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
126 /// SEL op, ...)
127 ///
128 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000129 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000130 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000131 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000132 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000133 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000134 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000135
John McCall0774cb82011-05-15 01:53:33 +0000136 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
137 /// SEL op, ...)
138 ///
139 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000140 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000141 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000142 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000143 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000144 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 llvm::Constant *getMessageSendSuperFpretFn() const {
148 // There is no objc_msgSendSuper_fpret? How can that work?
149 return getMessageSendSuperFn();
150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000151
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperFpretFn2() const {
153 // There is no objc_msgSendSuper_fpret? How can that work?
154 return getMessageSendSuperFn2();
155 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000156
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000157protected:
158 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000160public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000161 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
162 llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000164 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000165 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000166
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000167 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000168 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000170 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000171 llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000172 /// ProtocolPtrTy - LLVM type for external protocol handles
173 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000176 // SuperCTy - clang type for struct objc_super.
177 QualType SuperCTy;
178 // SuperPtrCTy - clang type for struct objc_super *.
179 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000180
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000181 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000182 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000183 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000185
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000186 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
187 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000188 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000189
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000190 /// PropertyListTy - LLVM type for struct objc_property_list
191 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000192 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000193 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000194 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000195
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000196 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000197 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000198
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000199 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000200 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000201 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000202 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000203
Chris Lattner72db6c32009-04-22 02:44:54 +0000204 llvm::Constant *getGetPropertyFn() {
205 CodeGen::CodeGenTypes &Types = CGM.getTypes();
206 ASTContext &Ctx = CGM.getContext();
207 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000209 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
210 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000211 Params.push_back(IdType);
212 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000213 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000214 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000215 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000216 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000217 FunctionType::ExtInfo()),
218 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000219 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
220 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000221
Chris Lattner72db6c32009-04-22 02:44:54 +0000222 llvm::Constant *getSetPropertyFn() {
223 CodeGen::CodeGenTypes &Types = CGM.getTypes();
224 ASTContext &Ctx = CGM.getContext();
225 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000227 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
228 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000229 Params.push_back(IdType);
230 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000231 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000232 Params.push_back(IdType);
233 Params.push_back(Ctx.BoolTy);
234 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000235 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000236 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000237 FunctionType::ExtInfo()),
238 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000239 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
240 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000241
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000242
243 llvm::Constant *getCopyStructFn() {
244 CodeGen::CodeGenTypes &Types = CGM.getTypes();
245 ASTContext &Ctx = CGM.getContext();
246 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000247 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000248 Params.push_back(Ctx.VoidPtrTy);
249 Params.push_back(Ctx.VoidPtrTy);
250 Params.push_back(Ctx.LongTy);
251 Params.push_back(Ctx.BoolTy);
252 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000253 llvm::FunctionType *FTy =
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000254 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
255 FunctionType::ExtInfo()),
256 false);
257 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
258 }
259
Chris Lattner72db6c32009-04-22 02:44:54 +0000260 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000261 CodeGen::CodeGenTypes &Types = CGM.getTypes();
262 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000263 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000265 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000266 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000267 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000268 FunctionType::ExtInfo()),
269 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
271 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000273 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000274 llvm::Constant *getGcReadWeakFn() {
275 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000276 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000277 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000278 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000280 }
281
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000282 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000283 llvm::Constant *getGcAssignWeakFn() {
284 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000285 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000286 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000287 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000288 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
289 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000290
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000291 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000292 llvm::Constant *getGcAssignGlobalFn() {
293 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000294 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000295 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000296 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000297 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
298 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000299
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000300 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
301 llvm::Constant *getGcAssignThreadLocalFn() {
302 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000303 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000304 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000305 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000306 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
307 }
308
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000309 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000310 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000311 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000312 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
313 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000314 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000315 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000316 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
317 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000318
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000319 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
320 llvm::Constant *GcMemmoveCollectableFn() {
321 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000322 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000323 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000324 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000326
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000327 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000328 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000329 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000330 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000331 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000332 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000333 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
334 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000335
336 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000337 llvm::Constant *getExceptionThrowFn() {
338 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000339 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000340 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000341 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
343 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000344
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000345 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
346 llvm::Constant *getExceptionRethrowFn() {
347 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000348 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
350 }
351
Daniel Dunbar1c566672009-02-24 01:43:46 +0000352 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000353 llvm::Constant *getSyncEnterFn() {
354 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000355 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000356 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
359 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360
Daniel Dunbar1c566672009-02-24 01:43:46 +0000361 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000362 llvm::Constant *getSyncExitFn() {
363 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000364 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000365 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000366 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
368 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000369
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000370 llvm::Constant *getSendFn(bool IsSuper) const {
371 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000374 llvm::Constant *getSendFn2(bool IsSuper) const {
375 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000377
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000378 llvm::Constant *getSendStretFn(bool IsSuper) const {
379 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000381
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000382 llvm::Constant *getSendStretFn2(bool IsSuper) const {
383 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000385
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFpretFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
388 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000389
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000393
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000394 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
395 ~ObjCCommonTypesHelper(){}
396};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000397
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000398/// ObjCTypesHelper - Helper class that encapsulates lazy
399/// construction of varies types used during ObjC generation.
400class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000401public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000403 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000404 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000406 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000407 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000408
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000409 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000410 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000411 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000412 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000415 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000416 /// ProtocolExtensionTy - LLVM type for struct
417 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000418 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000419 /// MethodDescriptionTy - LLVM type for struct
420 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000421 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000422 /// MethodDescriptionListTy - LLVM type for struct
423 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000424 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000425 /// MethodDescriptionListPtrTy - LLVM type for struct
426 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000427 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000428 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000429 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000430 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000431 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000432 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000433 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000435 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000436 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000437 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000438 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000439 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000440 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000441 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000442 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000443 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000444 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000445 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000446 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000448 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000449 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000450 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000451 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Anders Carlsson124526b2008-09-09 10:10:21 +0000453 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000454 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000455
Anders Carlsson124526b2008-09-09 10:10:21 +0000456 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000457 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000458 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000459 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000460 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000461 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000462 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000463
464 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000465 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000466 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000467 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000468 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000469 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000470 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000471
472 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000473 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000474 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000475 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000476 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000477 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000478 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000479
Anders Carlsson124526b2008-09-09 10:10:21 +0000480 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000482 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000483 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485 "objc_exception_match");
486
Chris Lattner34b02a12009-04-22 02:26:14 +0000487 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000488
Anders Carlsson124526b2008-09-09 10:10:21 +0000489 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000490 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000491 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000493 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
494 params, false),
495 "_setjmp");
Chris Lattner34b02a12009-04-22 02:26:14 +0000496 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000497
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000501};
502
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000503/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000504/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000505class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000506public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000508 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000510
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000513
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000514 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000516
Daniel Dunbar948e2582009-02-15 07:36:20 +0000517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000519
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000522
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000525
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000526 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000527 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000528
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000529 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000531
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000532 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000533 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000534
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000535 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000537
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000539 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000541 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000546
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000547 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000548 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000549
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000550 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000551
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
554 // IMP messenger;
555 // SEL name;
556 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000562 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000565
Fariborz Jahanianef163782009-02-05 01:13:09 +0000566 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
572 // SEL name;
573 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000577 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000578
Chris Lattner1c02f862009-04-22 02:53:24 +0000579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000581 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000583 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000584 "objc_msgSend_fixup");
585 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000586
Chris Lattner1c02f862009-04-22 02:53:24 +0000587 llvm::Constant *getMessageSendFpretFixupFn() {
588 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000589 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000590 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000591 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000592 "objc_msgSend_fpret_fixup");
593 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000594
Chris Lattner1c02f862009-04-22 02:53:24 +0000595 llvm::Constant *getMessageSendStretFixupFn() {
596 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000597 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000599 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000600 "objc_msgSend_stret_fixup");
601 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Chris Lattner1c02f862009-04-22 02:53:24 +0000603 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000605 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000608 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000609 "objc_msgSendSuper2_fixup");
610 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Chris Lattner1c02f862009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000614 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000615 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000616 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000617 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000618 "objc_msgSendSuper2_stret_fixup");
619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Chris Lattner8a569112009-04-22 02:15:23 +0000621 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000623 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Chris Lattner8a569112009-04-22 02:15:23 +0000625 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Chris Lattner8a569112009-04-22 02:15:23 +0000627 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000628 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000629 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000630 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000631 "objc_begin_catch");
632 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000633
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000634 llvm::StructType *EHTypeTy;
635 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000636
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000637 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
638 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000639};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000641class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000642public:
643 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000644 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000645 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000646 unsigned ivar_bytepos;
647 unsigned ivar_size;
648 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000650
651 // Allow sorting based on byte pos.
652 bool operator<(const GC_IVAR &b) const {
653 return ivar_bytepos < b.ivar_bytepos;
654 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000655 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000657 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000658 public:
659 unsigned skip;
660 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000662 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000663 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000665protected:
666 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000667 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000668 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000669 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000671 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000672 SmallVector<GC_IVAR, 16> SkipIvars;
673 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000675 /// LazySymbols - Symbols to generate a lazy reference for. See
676 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000677 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000678
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000679 /// DefinedSymbols - External symbols which are defined by this
680 /// module. The symbols in this list and LazySymbols are used to add
681 /// special linker symbols which ensure that Objective-C modules are
682 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000683 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000685 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000686 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000687
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000688 /// MethodVarNames - uniqued method variable names.
689 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000690
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000691 /// DefinedCategoryNames - list of category names in form Class_Category.
692 llvm::SetVector<std::string> DefinedCategoryNames;
693
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000694 /// MethodVarTypes - uniqued method type signatures. We have to use
695 /// a StringMap here because have no other unique reference.
696 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000698 /// MethodDefinitions - map of methods which have been defined in
699 /// this translation unit.
700 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000701
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000702 /// PropertyNames - uniqued method variable names.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000705 /// ClassReferences - uniqued class references.
706 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000707
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000708 /// SelectorReferences - uniqued selector references.
709 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000710
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000711 /// Protocols - Protocols for which an objc_protocol structure has
712 /// been emitted. Forward declarations are handled by creating an
713 /// empty structure whose initializer is filled in when/if defined.
714 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000716 /// DefinedProtocols - Protocols which have actually been
717 /// defined. We should not need this, see FIXME in GenerateProtocol.
718 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000720 /// DefinedClasses - List of defined classes.
721 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000722
723 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
724 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000725
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000726 /// DefinedCategories - List of defined categories.
727 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000728
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000729 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
730 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000732 /// GetNameForMethod - Return a name for the given method.
733 /// \param[out] NameOut - The return value.
734 void GetNameForMethod(const ObjCMethodDecl *OMD,
735 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000736 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000737
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000738 /// GetMethodVarName - Return a unique constant for the given
739 /// selector's name. The return value has type char *.
740 llvm::Constant *GetMethodVarName(Selector Sel);
741 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000743 /// GetMethodVarType - Return a unique constant for the given
744 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000745
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000746 // FIXME: This is a horrible name.
747 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000748 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000750 /// GetPropertyName - Return a unique constant for the given
751 /// name. The return value has type char *.
752 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000754 // FIXME: This can be dropped once string functions are unified.
755 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
756 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000758 /// GetClassName - Return a unique constant for the given selector's
759 /// name. The return value has type char *.
760 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000762 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
763
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000764 /// BuildIvarLayout - Builds ivar layout bitmap for the class
765 /// implementation for the __strong or __weak case.
766 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000767 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
768 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000769
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000770 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000771
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000772 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773 unsigned int BytePos, bool ForStrongLayout,
774 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000775 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000776 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000777 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000778 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000779 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000780 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000781
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000782 /// GetIvarLayoutName - Returns a unique constant for the given
783 /// ivar layout bitmap.
784 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
785 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000786
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000787 /// EmitPropertyList - Emit the given property list. The return
788 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000789 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000790 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000791 const ObjCContainerDecl *OCD,
792 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000794 /// PushProtocolProperties - Push protocol's property on the input stack.
795 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
796 std::vector<llvm::Constant*> &Properties,
797 const Decl *Container,
798 const ObjCProtocolDecl *PROTO,
799 const ObjCCommonTypesHelper &ObjCTypes);
800
Fariborz Jahanianda320092009-01-29 19:24:30 +0000801 /// GetProtocolRef - Return a reference to the internal protocol
802 /// description, creating an empty one if it has not been
803 /// defined. The return value has type ProtocolPtrTy.
804 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000805
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000806 /// CreateMetadataVar - Create a global variable with internal
807 /// linkage for use by the Objective-C runtime.
808 ///
809 /// This is a convenience wrapper which not only creates the
810 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000811 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000812 ///
813 /// \param Name - The variable name.
814 /// \param Init - The variable initializer; this is also used to
815 /// define the type of the variable.
816 /// \param Section - The section the variable should go into, or 0.
817 /// \param Align - The alignment for the variable, or 0.
818 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000819 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000820 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000821 llvm::Constant *Init,
822 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000823 unsigned Align,
824 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000825
John McCall944c8432011-05-14 03:10:52 +0000826 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
827 ReturnValueSlot Return,
828 QualType ResultType,
829 llvm::Value *Sel,
830 llvm::Value *Arg0,
831 QualType Arg0Ty,
832 bool IsSuper,
833 const CallArgList &CallArgs,
834 const ObjCMethodDecl *OMD,
835 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000836
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000837 /// EmitImageInfo - Emit the image info marker used to encode some module
838 /// level information.
839 void EmitImageInfo();
840
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000841public:
Owen Anderson69243822009-07-13 04:10:07 +0000842 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000843 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
David Chisnall0d13f6f2010-01-23 02:40:42 +0000845 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000847 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
848 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Fariborz Jahanianda320092009-01-29 19:24:30 +0000850 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Fariborz Jahanianda320092009-01-29 19:24:30 +0000852 /// GetOrEmitProtocol - Get the protocol object for the given
853 /// declaration, emitting it if necessary. The return value has type
854 /// ProtocolPtrTy.
855 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000856
Fariborz Jahanianda320092009-01-29 19:24:30 +0000857 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
858 /// object for the given declaration, emitting it if needed. These
859 /// forward references will be filled in with empty bodies if no
860 /// definition is seen. The return value has type ProtocolPtrTy.
861 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000862 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
863 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000864
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000865};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000867class CGObjCMac : public CGObjCCommonMac {
868private:
869 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000870
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000871 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000873 void EmitModuleInfo();
874
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000875 /// EmitModuleSymols - Emit module symbols, the list of defined
876 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000877 llvm::Constant *EmitModuleSymbols();
878
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000879 /// FinishModule - Write out global data structures at the end of
880 /// processing a translation unit.
881 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000882
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000883 /// EmitClassExtension - Generate the class extension structure used
884 /// to store the weak ivar layout and properties. The return value
885 /// has type ClassExtensionPtrTy.
886 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
887
888 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
889 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000891 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000892
John McCallf85e1932011-06-15 23:02:42 +0000893 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
894 IdentifierInfo *II);
895
896 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
897
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000898 /// EmitSuperClassRef - Emits reference to class's main metadata class.
899 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000900
901 /// EmitIvarList - Emit the ivar list for the given
902 /// implementation. If ForClass is true the list of class ivars
903 /// (i.e. metaclass ivars) is emitted, otherwise the list of
904 /// interface ivars will be emitted. The return value has type
905 /// IvarListPtrTy.
906 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000907 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000909 /// EmitMetaClass - Emit a forward reference to the class structure
910 /// for the metaclass of the given interface. The return value has
911 /// type ClassPtrTy.
912 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
913
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000914 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000915 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000916 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
917 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000918 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000920 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000921
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000922 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000923
924 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000925 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000926 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000927 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000928 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000929
930 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000932 /// - TypeName: The name for the type containing the methods.
933 /// - IsProtocol: True iff these methods are for a protocol.
934 /// - ClassMethds: True iff these are class methods.
935 /// - Required: When true, only "required" methods are
936 /// listed. Similarly, when false only "optional" methods are
937 /// listed. For classes this should always be true.
938 /// - begin, end: The method list to output.
939 ///
940 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000941 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000942 const char *Section,
943 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000944
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000945 /// GetOrEmitProtocol - Get the protocol object for the given
946 /// declaration, emitting it if necessary. The return value has type
947 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000948 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000949
950 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
951 /// object for the given declaration, emitting it if needed. These
952 /// forward references will be filled in with empty bodies if no
953 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000954 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000955
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000956 /// EmitProtocolExtension - Generate the protocol extension
957 /// structure used to store optional instance and class methods, and
958 /// protocol properties. The return value has type
959 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000960 llvm::Constant *
961 EmitProtocolExtension(const ObjCProtocolDecl *PD,
962 const ConstantVector &OptInstanceMethods,
963 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000964
965 /// EmitProtocolList - Generate the list of referenced
966 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000967 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +0000968 ObjCProtocolDecl::protocol_iterator begin,
969 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000970
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000971 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
972 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000973 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
974 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000975
976public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000977 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000978
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000979 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000980
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000981 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000982 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000983 QualType ResultType,
984 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000985 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000986 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000987 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000988 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000989
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000990 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000991 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000992 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000993 QualType ResultType,
994 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000995 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000996 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000997 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000998 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000999 const CallArgList &CallArgs,
1000 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001001
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001002 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001003 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001004
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001005 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1006 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001007
1008 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1009 /// untyped one.
1010 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1011 const ObjCMethodDecl *Method);
1012
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001013 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001014
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001015 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001016
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001017 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001019 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001020 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001021
Chris Lattner74391b42009-03-22 21:03:39 +00001022 virtual llvm::Constant *GetPropertyGetFunction();
1023 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001024 virtual llvm::Constant *GetGetStructFunction();
1025 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001026 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027
John McCallf1549f62010-07-06 01:34:17 +00001028 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1029 const ObjCAtTryStmt &S);
1030 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1031 const ObjCAtSynchronizedStmt &S);
1032 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001033 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1034 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001035 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001036 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001037 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001038 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001039 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001040 llvm::Value *src, llvm::Value *dest,
1041 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001042 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001043 llvm::Value *src, llvm::Value *dest,
1044 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001045 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001047 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1048 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001049 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001050
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001051 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1052 QualType ObjectTy,
1053 llvm::Value *BaseValue,
1054 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001055 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001056 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001057 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001058 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001059
1060 /// GetClassGlobal - Return the global variable for the Objective-C
1061 /// class of the given name.
1062 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001063 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001064 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001065};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001066
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001067class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001068private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001069 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001070 llvm::GlobalVariable* ObjCEmptyCacheVar;
1071 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001072
Daniel Dunbar11394522009-04-18 08:51:00 +00001073 /// SuperClassReferences - uniqued super class references.
1074 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001075
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001076 /// MetaClassReferences - uniqued meta class references.
1077 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001078
1079 /// EHTypeReferences - uniqued class ehtype references.
1080 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001081
John McCall944c8432011-05-14 03:10:52 +00001082 /// VTableDispatchMethods - List of methods for which we generate
1083 /// vtable-based message dispatch.
1084 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001085
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001086 /// DefinedMetaClasses - List of defined meta-classes.
1087 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1088
John McCall944c8432011-05-14 03:10:52 +00001089 /// isVTableDispatchedSelector - Returns true if SEL is a
1090 /// vtable-based selector.
1091 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001092
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001093 /// FinishNonFragileABIModule - Write out global data structures at the end of
1094 /// processing a translation unit.
1095 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001096
Daniel Dunbar463b8762009-05-15 21:48:48 +00001097 /// AddModuleClassList - Add the given list of class pointers to the
1098 /// module with the provided symbol and section names.
1099 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1100 const char *SymbolName,
1101 const char *SectionName);
1102
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1104 unsigned InstanceStart,
1105 unsigned InstanceSize,
1106 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001107 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001108 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001109 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001110 llvm::Constant *ClassRoGV,
1111 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001113 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001114
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001115 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001117 /// EmitMethodList - Emit the method list for the given
1118 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001119 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001120 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001121 const ConstantVector &Methods);
1122 /// EmitIvarList - Emit the ivar list for the given
1123 /// implementation. If ForClass is true the list of class ivars
1124 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1125 /// interface ivars will be emitted. The return value has type
1126 /// IvarListnfABIPtrTy.
1127 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001128
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001129 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001130 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001131 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001132
Fariborz Jahanianda320092009-01-29 19:24:30 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
1136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001137
Fariborz Jahanianda320092009-01-29 19:24:30 +00001138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
1142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001143
Fariborz Jahanianda320092009-01-29 19:24:30 +00001144 /// EmitProtocolList - Generate the list of referenced
1145 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001146 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001147 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001148 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
John McCall944c8432011-05-14 03:10:52 +00001150 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1151 ReturnValueSlot Return,
1152 QualType ResultType,
1153 Selector Sel,
1154 llvm::Value *Receiver,
1155 QualType Arg0Ty,
1156 bool IsSuper,
1157 const CallArgList &CallArgs,
1158 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001159
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001160 /// GetClassGlobal - Return the global variable for the Objective-C
1161 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001162 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001163
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001164 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001165 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001166 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001167 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001168
1169 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1170 IdentifierInfo *II);
1171
1172 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001173
Daniel Dunbar11394522009-04-18 08:51:00 +00001174 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1175 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1177 const ObjCInterfaceDecl *ID);
1178
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001179 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1180 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001182 const ObjCInterfaceDecl *ID);
1183
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001184 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1185 /// the given ivar.
1186 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001187 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001188 const ObjCInterfaceDecl *ID,
1189 const ObjCIvarDecl *Ivar);
1190
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001191 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1192 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001193 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1194 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001195
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001196 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001197 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001198 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001199 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001200
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001201 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001202 return "OBJC_METACLASS_$_";
1203 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001204
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001205 const char *getClassSymbolPrefix() const {
1206 return "OBJC_CLASS_$_";
1207 }
1208
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001209 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001210 uint32_t &InstanceStart,
1211 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001212
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001213 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001214 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001215 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1216 return CGM.getContext().Selectors.getSelector(0, &II);
1217 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001219 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001220 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1221 return CGM.getContext().Selectors.getSelector(1, &II);
1222 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001223
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001224 /// ImplementationIsNonLazy - Check whether the given category or
1225 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001226 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001227
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001228public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001229 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001230 // FIXME. All stubs for now!
1231 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001232
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001233 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001234 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001235 QualType ResultType,
1236 Selector Sel,
1237 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001238 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001239 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001240 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
1242 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001243 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001244 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001245 QualType ResultType,
1246 Selector Sel,
1247 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001248 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001249 llvm::Value *Receiver,
1250 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001251 const CallArgList &CallArgs,
1252 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001253
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001254 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001255 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001257 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1258 bool lvalue = false)
1259 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001260
1261 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1262 /// untyped one.
1263 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1264 const ObjCMethodDecl *Method)
1265 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001266
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001267 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001269 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001270 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001271 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001272
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001273 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001274
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001275 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001276 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001277 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001278 virtual llvm::Constant *GetPropertySetFunction() {
1279 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001280 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001281
David Chisnall8fac25d2010-12-26 22:13:16 +00001282 virtual llvm::Constant *GetSetStructFunction() {
1283 return ObjCTypes.getCopyStructFn();
1284 }
1285 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001286 return ObjCTypes.getCopyStructFn();
1287 }
1288
Chris Lattner74391b42009-03-22 21:03:39 +00001289 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001290 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001291 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
John McCallf1549f62010-07-06 01:34:17 +00001293 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1294 const ObjCAtTryStmt &S);
1295 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1296 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001297 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001298 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001299 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001300 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001301 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001302 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001303 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001304 llvm::Value *src, llvm::Value *dest,
1305 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001306 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001307 llvm::Value *src, llvm::Value *dest,
1308 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001309 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001310 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001311 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1312 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001313 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001314 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1315 QualType ObjectTy,
1316 llvm::Value *BaseValue,
1317 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001318 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001319 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001320 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001321 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001322};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001323
John McCallcba681a2011-05-14 21:12:11 +00001324/// A helper class for performing the null-initialization of a return
1325/// value.
1326struct NullReturnState {
1327 llvm::BasicBlock *NullBB;
1328
1329 NullReturnState() : NullBB(0) {}
1330
1331 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1332 // Make blocks for the null-init and call edges.
1333 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1334 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1335
1336 // Check for a null receiver and, if there is one, jump to the
1337 // null-init test.
1338 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1339 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1340
1341 // Otherwise, start performing the call.
1342 CGF.EmitBlock(callBB);
1343 }
1344
1345 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1346 if (!NullBB) return result;
1347
1348 // Finish the call path.
1349 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1350 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1351
1352 // Emit the null-init block and perform the null-initialization there.
1353 CGF.EmitBlock(NullBB);
1354 assert(result.isAggregate() && "null init of non-aggregate result?");
1355 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1356
1357 // Jump to the continuation block.
1358 CGF.EmitBlock(contBB);
1359
1360 return result;
1361 }
1362};
1363
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001364} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001365
1366/* *** Helper Functions *** */
1367
1368/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001369static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001370 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001371 unsigned idx0,
1372 unsigned idx1) {
1373 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001374 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1375 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001376 };
Jay Foada5c04342011-07-21 14:31:17 +00001377 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001378}
1379
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001380/// hasObjCExceptionAttribute - Return true if this class or any super
1381/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001382static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001383 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001384 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001385 return true;
1386 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001387 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001388 return false;
1389}
1390
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001391/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001392
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001393CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001394 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001395 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001396 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001397}
1398
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001399/// GetClass - Return a reference to the class for the given interface
1400/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001401llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001402 const ObjCInterfaceDecl *ID) {
1403 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001404}
1405
1406/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001407llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1408 bool lval) {
1409 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001410}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001411llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001412 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001413 return EmitSelector(Builder, Method->getSelector());
1414}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001415
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001416llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001417 if (T->isObjCIdType() ||
1418 T->isObjCQualifiedIdType()) {
1419 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001420 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001421 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001422 if (T->isObjCClassType() ||
1423 T->isObjCQualifiedClassType()) {
1424 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001425 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001426 }
1427 if (T->isObjCObjectPointerType())
1428 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1429
John McCall5a180392010-07-24 00:37:23 +00001430 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1431 return 0;
1432}
1433
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001434/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001435/*
1436 struct __builtin_CFString {
1437 const int *isa; // point to __CFConstantStringClassReference
1438 int flags;
1439 const char *str;
1440 long length;
1441 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001442*/
1443
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001444/// or Generate a constant NSString object.
1445/*
1446 struct __builtin_NSString {
1447 const int *isa; // point to __NSConstantStringClassReference
1448 const char *str;
1449 unsigned int length;
1450 };
1451*/
1452
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001453llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001454 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001455 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1456 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001457 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001458}
1459
1460/// Generates a message send where the super is the receiver. This is
1461/// a message send to self with special delivery semantics indicating
1462/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001463CodeGen::RValue
1464CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001465 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001466 QualType ResultType,
1467 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001468 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001469 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001470 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001471 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001472 const CodeGen::CallArgList &CallArgs,
1473 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001474 // Create and init a super structure; this is a (receiver, class)
1475 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001476 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001477 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001478 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001479 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001480 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001481 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001482
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001483 // If this is a class message the metaclass is passed as the target.
1484 llvm::Value *Target;
1485 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001486 if (isCategoryImpl) {
1487 // Message sent to 'super' in a class method defined in a category
1488 // implementation requires an odd treatment.
1489 // If we are in a class method, we must retrieve the
1490 // _metaclass_ for the current class, pointed at by
1491 // the class's "isa" pointer. The following assumes that
1492 // isa" is the first ivar in a class (which it must be).
1493 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1494 Target = CGF.Builder.CreateStructGEP(Target, 0);
1495 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001496 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001497 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1498 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1499 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1500 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001501 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001502 }
1503 else if (isCategoryImpl)
1504 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1505 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001506 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1507 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1508 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001509 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001510 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1511 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001512 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001513 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001514 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001515 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001516 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001517 return EmitMessageSend(CGF, Return, ResultType,
1518 EmitSelector(CGF.Builder, Sel),
1519 ObjCSuper, ObjCTypes.SuperPtrCTy,
1520 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001521}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001522
1523/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001524CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001525 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001526 QualType ResultType,
1527 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001528 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001529 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001530 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001531 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001532 return EmitMessageSend(CGF, Return, ResultType,
1533 EmitSelector(CGF.Builder, Sel),
1534 Receiver, CGF.getContext().getObjCIdType(),
1535 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001536}
1537
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001538CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001539CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1540 ReturnValueSlot Return,
1541 QualType ResultType,
1542 llvm::Value *Sel,
1543 llvm::Value *Arg0,
1544 QualType Arg0Ty,
1545 bool IsSuper,
1546 const CallArgList &CallArgs,
1547 const ObjCMethodDecl *Method,
1548 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001549 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001550 if (!IsSuper)
1551 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman04c9a492011-05-02 17:57:46 +00001552 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1553 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001554 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001555
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001556 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001557 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001558 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001559 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001560 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001561
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001562 if (Method)
1563 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1564 CGM.getContext().getCanonicalType(ResultType) &&
1565 "Result type mismatch!");
1566
John McCallcba681a2011-05-14 21:12:11 +00001567 NullReturnState nullReturn;
1568
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001569 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001570 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallcba681a2011-05-14 21:12:11 +00001571 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001572 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001573 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001574 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1575 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1576 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001577 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001578 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001579 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001580 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001581 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001582 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1583 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001584}
1585
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001586static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1587 if (FQT.isObjCGCStrong())
1588 return Qualifiers::Strong;
1589
John McCallf85e1932011-06-15 23:02:42 +00001590 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001591 return Qualifiers::Weak;
1592
1593 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1594 return Qualifiers::Strong;
1595
1596 if (const PointerType *PT = FQT->getAs<PointerType>())
1597 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1598
1599 return Qualifiers::GCNone;
1600}
1601
John McCall6b5a61b2011-02-07 10:33:21 +00001602llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1603 const CGBlockInfo &blockInfo) {
1604 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001605 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001606
Douglas Gregore289d812011-09-13 17:21:33 +00001607 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001608 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001609 return nullPtr;
1610
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001611 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001612 SkipIvars.clear();
1613 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001614 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1615 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001616
Fariborz Jahanian81979822010-09-09 00:21:45 +00001617 // __isa is the first field in block descriptor and must assume by runtime's
1618 // convention that it is GC'able.
1619 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001620
1621 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1622
1623 // Calculate the basic layout of the block structure.
1624 const llvm::StructLayout *layout =
1625 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1626
1627 // Ignore the optional 'this' capture: C++ objects are not assumed
1628 // to be GC'ed.
1629
1630 // Walk the captured variables.
1631 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1632 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1633 const VarDecl *variable = ci->getVariable();
1634 QualType type = variable->getType();
1635
1636 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1637
1638 // Ignore constant captures.
1639 if (capture.isConstant()) continue;
1640
1641 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1642
1643 // __block variables are passed by their descriptor address.
1644 if (ci->isByRef()) {
1645 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001646 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001647 }
1648
1649 assert(!type->isArrayType() && "array variable should not be caught");
1650 if (const RecordType *record = type->getAs<RecordType>()) {
1651 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001652 continue;
1653 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001654
John McCall6b5a61b2011-02-07 10:33:21 +00001655 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1656 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1657
1658 if (GCAttr == Qualifiers::Strong)
1659 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1660 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001661 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001662 SkipIvars.push_back(GC_IVAR(fieldOffset,
1663 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001664 }
1665
1666 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001667 return nullPtr;
1668
1669 // Sort on byte position; captures might not be allocated in order,
1670 // and unions can do funny things.
1671 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1672 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001673
1674 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001675 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001676 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1677 printf("\n block variable layout for block: ");
1678 const unsigned char *s = (unsigned char*)BitMap.c_str();
1679 for (unsigned i = 0; i < BitMap.size(); i++)
1680 if (!(s[i] & 0xf0))
1681 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1682 else
1683 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1684 printf("\n");
1685 }
1686
1687 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001688}
1689
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001690llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001691 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001692 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001693 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001694 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1695
Owen Anderson3c4972d2009-07-29 18:54:39 +00001696 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001697 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001698}
1699
Fariborz Jahanianda320092009-01-29 19:24:30 +00001700void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001701 // FIXME: We shouldn't need this, the protocol decl should contain enough
1702 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001703 DefinedProtocols.insert(PD->getIdentifier());
1704
1705 // If we have generated a forward reference to this protocol, emit
1706 // it now. Otherwise do nothing, the protocol objects are lazily
1707 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001708 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001709 GetOrEmitProtocol(PD);
1710}
1711
Fariborz Jahanianda320092009-01-29 19:24:30 +00001712llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001713 if (DefinedProtocols.count(PD->getIdentifier()))
1714 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001715
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001716 return GetOrEmitProtocolRef(PD);
1717}
1718
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001719/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001720// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1721struct _objc_protocol {
1722struct _objc_protocol_extension *isa;
1723char *protocol_name;
1724struct _objc_protocol_list *protocol_list;
1725struct _objc__method_prototype_list *instance_methods;
1726struct _objc__method_prototype_list *class_methods
1727};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001728
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001729See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001731llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1732 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1733
1734 // Early exit if a defining object has already been generated.
1735 if (Entry && Entry->hasInitializer())
1736 return Entry;
1737
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001738 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001739 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001740 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1741
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001742 // Construct method lists.
1743 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1744 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001745 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001746 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001747 ObjCMethodDecl *MD = *i;
1748 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001749 if (!C)
1750 return GetOrEmitProtocolRef(PD);
1751
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001752 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1753 OptInstanceMethods.push_back(C);
1754 } else {
1755 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001756 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001757 }
1758
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001759 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001760 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001761 ObjCMethodDecl *MD = *i;
1762 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001763 if (!C)
1764 return GetOrEmitProtocolRef(PD);
1765
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001766 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1767 OptClassMethods.push_back(C);
1768 } else {
1769 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001770 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001771 }
1772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001774 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001775 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001776 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001777 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001778 PD->protocol_begin(),
1779 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001780 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001781 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001782 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1783 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001784 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001785 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001786 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1787 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001788 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001789 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001790
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001791 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001792 // Already created, fix the linkage and update the initializer.
1793 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001794 Entry->setInitializer(Init);
1795 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001796 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001797 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001799 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001800 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001801 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001802 // FIXME: Is this necessary? Why only for protocol?
1803 Entry->setAlignment(4);
1804 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001805 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001806
1807 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001808}
1809
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001810llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001811 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1812
1813 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001814 // We use the initializer as a marker of whether this is a forward
1815 // reference or not. At module finalization we add the empty
1816 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001817 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001818 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001819 llvm::GlobalValue::ExternalLinkage,
1820 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001821 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001823 // FIXME: Is this necessary? Why only for protocol?
1824 Entry->setAlignment(4);
1825 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001826
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827 return Entry;
1828}
1829
1830/*
1831 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001832 uint32_t size;
1833 struct objc_method_description_list *optional_instance_methods;
1834 struct objc_method_description_list *optional_class_methods;
1835 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001836 };
1837*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001838llvm::Constant *
1839CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1840 const ConstantVector &OptInstanceMethods,
1841 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001843 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001844 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001845 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001846 Values[1] =
1847 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001848 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001849 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1850 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001851 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001852 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001853 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1854 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001855 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001856 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001857
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001858 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001859 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001861 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001862
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001864 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001866 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001867 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001868 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001869 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870}
1871
1872/*
1873 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001874 struct objc_protocol_list *next;
1875 long count;
1876 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001877 };
1878*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001879llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00001880CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001881 ObjCProtocolDecl::protocol_iterator begin,
1882 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001883 std::vector<llvm::Constant*> ProtocolRefs;
1884
Daniel Dunbardbc93372008-08-21 21:57:41 +00001885 for (; begin != end; ++begin)
1886 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001887
1888 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001889 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001890 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001891
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001892 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001893 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001895 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001896 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001897 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001898 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001899 ProtocolRefs.size() - 1);
1900 Values[2] =
1901 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1902 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001903 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001904
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001905 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001906 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001907 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001908 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001909 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001910}
1911
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001912void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1913 std::vector<llvm::Constant*> &Properties,
1914 const Decl *Container,
1915 const ObjCProtocolDecl *PROTO,
1916 const ObjCCommonTypesHelper &ObjCTypes) {
1917 std::vector<llvm::Constant*> Prop(2);
1918 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1919 E = PROTO->protocol_end(); P != E; ++P)
1920 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1921 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1922 E = PROTO->prop_end(); I != E; ++I) {
1923 const ObjCPropertyDecl *PD = *I;
1924 if (!PropertySet.insert(PD->getIdentifier()))
1925 continue;
1926 Prop[0] = GetPropertyName(PD->getIdentifier());
1927 Prop[1] = GetPropertyTypeString(PD, Container);
1928 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1929 }
1930}
1931
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001932/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001933 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001934 const char * const name;
1935 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001936 };
1937
1938 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001939 uint32_t entsize; // sizeof (struct _objc_property)
1940 uint32_t prop_count;
1941 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001942 };
1943*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00001944llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001945 const Decl *Container,
1946 const ObjCContainerDecl *OCD,
1947 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001948 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001949 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1951 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001952 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001953 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001954 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001955 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001956 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001957 Prop));
1958 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001959 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001960 for (ObjCInterfaceDecl::all_protocol_iterator
1961 P = OID->all_referenced_protocol_begin(),
1962 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001963 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1964 ObjCTypes);
1965 }
1966 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1967 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1968 E = CD->protocol_end(); P != E; ++P)
1969 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1970 ObjCTypes);
1971 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001972
1973 // Return null for empty list.
1974 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001975 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001976
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001977 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001978 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001979 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001980 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1981 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001982 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001983 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001984 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001985 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001986
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001987 llvm::GlobalVariable *GV =
1988 CreateMetadataVar(Name, Init,
1989 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001990 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001991 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001992 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001993 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001994}
1995
1996/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001997 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001998 int count;
1999 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002000 };
2001*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002002llvm::Constant *
2003CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2004 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002005 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002006 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2007 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002008 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002009 if (!Desc[1])
2010 return 0;
2011
Owen Anderson08e25242009-07-27 22:29:56 +00002012 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002013 Desc);
2014}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002015
Chris Lattner5f9e2722011-07-23 10:55:15 +00002016llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002017 const char *Section,
2018 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002019 // Return null for empty list.
2020 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002021 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002023 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002024 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002025 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002026 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002027 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002028 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002029
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002030 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002031 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002032 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002033}
2034
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002035/*
2036 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002037 char *category_name;
2038 char *class_name;
2039 struct _objc_method_list *instance_methods;
2040 struct _objc_method_list *class_methods;
2041 struct _objc_protocol_list *protocols;
2042 uint32_t size; // <rdar://4585769>
2043 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002044 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002045*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002046void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002047 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002048
Mike Stumpf5408fe2009-05-16 07:57:57 +00002049 // FIXME: This is poor design, the OCD should have a pointer to the category
2050 // decl. Additionally, note that Category can be null for the @implementation
2051 // w/o an @interface case. Sema should just create one for us as it does for
2052 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002053 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002054 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002055 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002056
2057 llvm::SmallString<256> ExtName;
2058 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2059 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002060
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002061 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002062 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002063 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002064 // Instance methods should always be defined.
2065 InstanceMethods.push_back(GetMethodConstant(*i));
2066 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002067 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002068 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002069 // Class methods should always be defined.
2070 ClassMethods.push_back(GetMethodConstant(*i));
2071 }
2072
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002073 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002074 Values[0] = GetClassName(OCD->getIdentifier());
2075 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002076 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002077 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002078 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002079 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002080 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002081 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002082 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002083 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002084 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002085 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002086 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002087 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002088 Category->protocol_begin(),
2089 Category->protocol_end());
2090 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002091 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002092 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002093 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002094
2095 // If there is no category @interface then there can be no properties.
2096 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002097 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002098 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002099 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002100 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002101 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002102
Owen Anderson08e25242009-07-27 22:29:56 +00002103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002104 Values);
2105
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002107 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002108 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002109 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002110 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002111 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002112 // method definition entries must be clear for next implementation.
2113 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002114}
2115
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002116// FIXME: Get from somewhere?
2117enum ClassFlags {
2118 eClassFlags_Factory = 0x00001,
2119 eClassFlags_Meta = 0x00002,
2120 // <rdr://5142207>
2121 eClassFlags_HasCXXStructors = 0x02000,
2122 eClassFlags_Hidden = 0x20000,
2123 eClassFlags_ABI2_Hidden = 0x00010,
2124 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2125};
2126
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002127/*
2128 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002129 Class isa;
2130 Class super_class;
2131 const char *name;
2132 long version;
2133 long info;
2134 long instance_size;
2135 struct _objc_ivar_list *ivars;
2136 struct _objc_method_list *methods;
2137 struct _objc_cache *cache;
2138 struct _objc_protocol_list *protocols;
2139 // Objective-C 1.0 extensions (<rdr://4585769>)
2140 const char *ivar_layout;
2141 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002142 };
2143
2144 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002146void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002147 DefinedSymbols.insert(ID->getIdentifier());
2148
Chris Lattner8ec03f52008-11-24 03:54:41 +00002149 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002151 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002152 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002153 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002154 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002155 Interface->all_referenced_protocol_begin(),
2156 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002157 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002158 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002159 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002160 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002161 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002162
2163 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002164 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002165 Flags |= eClassFlags_Hidden;
2166
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002167 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002168 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002169 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002170 // Instance methods should always be defined.
2171 InstanceMethods.push_back(GetMethodConstant(*i));
2172 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002173 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002174 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002175 // Class methods should always be defined.
2176 ClassMethods.push_back(GetMethodConstant(*i));
2177 }
2178
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002179 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002180 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002181 ObjCPropertyImplDecl *PID = *i;
2182
2183 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2184 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2185
2186 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2187 if (llvm::Constant *C = GetMethodConstant(MD))
2188 InstanceMethods.push_back(C);
2189 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2190 if (llvm::Constant *C = GetMethodConstant(MD))
2191 InstanceMethods.push_back(C);
2192 }
2193 }
2194
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002195 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002196 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002198 // Record a reference to the super class.
2199 LazySymbols.insert(Super->getIdentifier());
2200
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002201 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002202 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203 ObjCTypes.ClassPtrTy);
2204 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002205 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 }
2207 Values[ 2] = GetClassName(ID->getIdentifier());
2208 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002209 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2210 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2211 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002212 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002213 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002214 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002215 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002216 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002218 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002220 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002222 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002224 std::string Name("\01L_OBJC_CLASS_");
2225 Name += ClassName;
2226 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2227 // Check for a forward reference.
2228 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2229 if (GV) {
2230 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2231 "Forward metaclass reference has incorrect type.");
2232 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2233 GV->setInitializer(Init);
2234 GV->setSection(Section);
2235 GV->setAlignment(4);
2236 CGM.AddUsedGlobal(GV);
2237 }
2238 else
2239 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002240 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002241 // method definition entries must be clear for next implementation.
2242 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243}
2244
2245llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2246 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002247 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002248 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002249 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250
John McCall1fb0caa2010-10-22 21:05:15 +00002251 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002252 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002253
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002254 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002255 // The isa for the metaclass is the root of the hierarchy.
2256 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2257 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2258 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002259 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002260 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002261 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002262 // The super class for the metaclass is emitted as the name of the
2263 // super class. The runtime fixes this up to point to the
2264 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002265 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002266 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002267 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268 ObjCTypes.ClassPtrTy);
2269 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002270 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002271 }
2272 Values[ 2] = GetClassName(ID->getIdentifier());
2273 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002274 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2275 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2276 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002277 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002278 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002279 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002280 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002281 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002283 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002284 Values[ 9] = Protocols;
2285 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002286 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002288 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002289 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 Values);
2291
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002292 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002293 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002294
2295 // Check for a forward reference.
2296 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2297 if (GV) {
2298 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2299 "Forward metaclass reference has incorrect type.");
2300 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2301 GV->setInitializer(Init);
2302 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002303 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002304 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002305 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002306 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002308 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002309 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002310
2311 return GV;
2312}
2313
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002314llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002315 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002316
Mike Stumpf5408fe2009-05-16 07:57:57 +00002317 // FIXME: Should we look these up somewhere other than the module. Its a bit
2318 // silly since we only generate these while processing an implementation, so
2319 // exactly one pointer would work if know when we entered/exitted an
2320 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002321
2322 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002323 // Previously, metaclass with internal linkage may have been defined.
2324 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002325 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2326 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002327 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2328 "Forward metaclass reference has incorrect type.");
2329 return GV;
2330 } else {
2331 // Generate as an external reference to keep a consistent
2332 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002333 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002334 llvm::GlobalValue::ExternalLinkage,
2335 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002336 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002337 }
2338}
2339
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002340llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2341 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2342
2343 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2344 true)) {
2345 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2346 "Forward class metadata reference has incorrect type.");
2347 return GV;
2348 } else {
2349 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2350 llvm::GlobalValue::ExternalLinkage,
2351 0,
2352 Name);
2353 }
2354}
2355
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002356/*
2357 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002358 uint32_t size;
2359 const char *weak_ivar_layout;
2360 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002361 };
2362*/
2363llvm::Constant *
2364CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002365 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002366 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002367
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002368 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002369 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002370 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002371 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002372 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373
2374 // Return null if no extension bits are used.
2375 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002376 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002377
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002378 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002379 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002380 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002381 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002382 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002383}
2384
2385/*
2386 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002387 char *ivar_name;
2388 char *ivar_type;
2389 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002390 };
2391
2392 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002393 int ivar_count;
2394 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002396*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002397llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002398 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002399 std::vector<llvm::Constant*> Ivars, Ivar(3);
2400
2401 // When emitting the root class GCC emits ivar entries for the
2402 // actual class structure. It is not clear if we need to follow this
2403 // behavior; for now lets try and get away with not doing it. If so,
2404 // the cleanest solution would be to make up an ObjCInterfaceDecl
2405 // for the class.
2406 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002407 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002408
Jordy Rosedb8264e2011-07-22 02:08:32 +00002409 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002410
Jordy Rosedb8264e2011-07-22 02:08:32 +00002411 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002412 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002413 // Ignore unnamed bit-fields.
2414 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002415 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002416 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2417 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002418 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002419 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002420 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 }
2422
2423 // Return null for empty list.
2424 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002425 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002427 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002428 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002429 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002431 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002432 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002434 llvm::GlobalVariable *GV;
2435 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002436 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002437 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002438 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002439 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002440 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002441 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002442 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444}
2445
2446/*
2447 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 SEL method_name;
2449 char *method_types;
2450 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002452
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002454 struct objc_method_list *obsolete;
2455 int count;
2456 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002457 };
2458*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002459
2460/// GetMethodConstant - Return a struct objc_method constant for the
2461/// given method if it has been defined. The result is null if the
2462/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002463llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002464 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002465 if (!Fn)
2466 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002467
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002468 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002469 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002470 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002471 ObjCTypes.SelectorPtrTy);
2472 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002473 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002474 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002475}
2476
Chris Lattner5f9e2722011-07-23 10:55:15 +00002477llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002478 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002479 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 // Return null for empty list.
2481 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002482 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002483
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002484 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002485 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002486 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002487 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002489 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002490 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002491
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002492 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002493 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002494}
2495
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002496llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002498 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002499 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002500
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002501 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002502 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002503 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002504 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002505 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002506 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002507 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002508 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002509 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002510
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002511 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002512}
2513
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002514llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002515CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002516 llvm::Constant *Init,
2517 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002518 unsigned Align,
2519 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002520 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002521 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002522 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002523 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002524 if (Section)
2525 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002526 if (Align)
2527 GV->setAlignment(Align);
2528 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002529 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002530 return GV;
2531}
2532
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002533llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002534 // Abuse this interface function as a place to finalize.
2535 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002536 return NULL;
2537}
2538
Chris Lattner74391b42009-03-22 21:03:39 +00002539llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002540 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002541}
2542
Chris Lattner74391b42009-03-22 21:03:39 +00002543llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002544 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002545}
2546
David Chisnall8fac25d2010-12-26 22:13:16 +00002547llvm::Constant *CGObjCMac::GetGetStructFunction() {
2548 return ObjCTypes.getCopyStructFn();
2549}
2550llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002551 return ObjCTypes.getCopyStructFn();
2552}
2553
Chris Lattner74391b42009-03-22 21:03:39 +00002554llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002555 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002556}
2557
John McCallf1549f62010-07-06 01:34:17 +00002558void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2559 return EmitTryOrSynchronizedStmt(CGF, S);
2560}
2561
2562void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2563 const ObjCAtSynchronizedStmt &S) {
2564 return EmitTryOrSynchronizedStmt(CGF, S);
2565}
2566
John McCallcc505292010-07-21 06:59:36 +00002567namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002568 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002569 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002570 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002571 llvm::Value *CallTryExitVar;
2572 llvm::Value *ExceptionData;
2573 ObjCTypesHelper &ObjCTypes;
2574 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002575 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002576 llvm::Value *CallTryExitVar,
2577 llvm::Value *ExceptionData,
2578 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002579 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002580 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2581
John McCallad346f42011-07-12 20:27:29 +00002582 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002583 // Check whether we need to call objc_exception_try_exit.
2584 // In optimized code, this branch will always be folded.
2585 llvm::BasicBlock *FinallyCallExit =
2586 CGF.createBasicBlock("finally.call_exit");
2587 llvm::BasicBlock *FinallyNoCallExit =
2588 CGF.createBasicBlock("finally.no_call_exit");
2589 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2590 FinallyCallExit, FinallyNoCallExit);
2591
2592 CGF.EmitBlock(FinallyCallExit);
2593 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2594 ->setDoesNotThrow();
2595
2596 CGF.EmitBlock(FinallyNoCallExit);
2597
2598 if (isa<ObjCAtTryStmt>(S)) {
2599 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002600 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2601 // Save the current cleanup destination in case there's
2602 // control flow inside the finally statement.
2603 llvm::Value *CurCleanupDest =
2604 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2605
John McCallcc505292010-07-21 06:59:36 +00002606 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2607
John McCalld96a8e72010-08-11 00:16:14 +00002608 if (CGF.HaveInsertPoint()) {
2609 CGF.Builder.CreateStore(CurCleanupDest,
2610 CGF.getNormalCleanupDestSlot());
2611 } else {
2612 // Currently, the end of the cleanup must always exist.
2613 CGF.EnsureInsertPoint();
2614 }
2615 }
John McCallcc505292010-07-21 06:59:36 +00002616 } else {
2617 // Emit objc_sync_exit(expr); as finally's sole statement for
2618 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002619 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002620 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2621 ->setDoesNotThrow();
2622 }
2623 }
2624 };
John McCall87bb5822010-07-31 23:20:56 +00002625
2626 class FragileHazards {
2627 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002628 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002629 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2630
2631 llvm::InlineAsm *ReadHazard;
2632 llvm::InlineAsm *WriteHazard;
2633
2634 llvm::FunctionType *GetAsmFnType();
2635
2636 void collectLocals();
2637 void emitReadHazard(CGBuilderTy &Builder);
2638
2639 public:
2640 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002641
John McCall87bb5822010-07-31 23:20:56 +00002642 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002643 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002644 };
2645}
2646
2647/// Create the fragile-ABI read and write hazards based on the current
2648/// state of the function, which is presumed to be immediately prior
2649/// to a @try block. These hazards are used to maintain correct
2650/// semantics in the face of optimization and the fragile ABI's
2651/// cavalier use of setjmp/longjmp.
2652FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2653 collectLocals();
2654
2655 if (Locals.empty()) return;
2656
2657 // Collect all the blocks in the function.
2658 for (llvm::Function::iterator
2659 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2660 BlocksBeforeTry.insert(&*I);
2661
2662 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2663
2664 // Create a read hazard for the allocas. This inhibits dead-store
2665 // optimizations and forces the values to memory. This hazard is
2666 // inserted before any 'throwing' calls in the protected scope to
2667 // reflect the possibility that the variables might be read from the
2668 // catch block if the call throws.
2669 {
2670 std::string Constraint;
2671 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2672 if (I) Constraint += ',';
2673 Constraint += "*m";
2674 }
2675
2676 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2677 }
2678
2679 // Create a write hazard for the allocas. This inhibits folding
2680 // loads across the hazard. This hazard is inserted at the
2681 // beginning of the catch path to reflect the possibility that the
2682 // variables might have been written within the protected scope.
2683 {
2684 std::string Constraint;
2685 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2686 if (I) Constraint += ',';
2687 Constraint += "=*m";
2688 }
2689
2690 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2691 }
2692}
2693
2694/// Emit a write hazard at the current location.
2695void FragileHazards::emitWriteHazard() {
2696 if (Locals.empty()) return;
2697
Jay Foad4c7d9f12011-07-15 08:37:34 +00002698 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002699}
2700
John McCall87bb5822010-07-31 23:20:56 +00002701void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2702 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002703 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002704}
2705
2706/// Emit read hazards in all the protected blocks, i.e. all the blocks
2707/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002708void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002709 if (Locals.empty()) return;
2710
2711 CGBuilderTy Builder(CGF.getLLVMContext());
2712
2713 // Iterate through all blocks, skipping those prior to the try.
2714 for (llvm::Function::iterator
2715 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2716 llvm::BasicBlock &BB = *FI;
2717 if (BlocksBeforeTry.count(&BB)) continue;
2718
2719 // Walk through all the calls in the block.
2720 for (llvm::BasicBlock::iterator
2721 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2722 llvm::Instruction &I = *BI;
2723
2724 // Ignore instructions that aren't non-intrinsic calls.
2725 // These are the only calls that can possibly call longjmp.
2726 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2727 if (isa<llvm::IntrinsicInst>(I))
2728 continue;
2729
2730 // Ignore call sites marked nounwind. This may be questionable,
2731 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2732 llvm::CallSite CS(&I);
2733 if (CS.doesNotThrow()) continue;
2734
John McCall0b251722010-08-04 05:59:32 +00002735 // Insert a read hazard before the call. This will ensure that
2736 // any writes to the locals are performed before making the
2737 // call. If the call throws, then this is sufficient to
2738 // guarantee correctness as long as it doesn't also write to any
2739 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002740 Builder.SetInsertPoint(&BB, BI);
2741 emitReadHazard(Builder);
2742 }
2743 }
2744}
2745
2746static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2747 if (V) S.insert(V);
2748}
2749
2750void FragileHazards::collectLocals() {
2751 // Compute a set of allocas to ignore.
2752 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2753 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2754 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002755
2756 // Collect all the allocas currently in the function. This is
2757 // probably way too aggressive.
2758 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2759 for (llvm::BasicBlock::iterator
2760 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2761 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2762 Locals.push_back(&*I);
2763}
2764
2765llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002766 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002767 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2768 tys[i] = Locals[i]->getType();
2769 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002770}
2771
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002772/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002773
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002774 Objective-C setjmp-longjmp (sjlj) Exception Handling
2775 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002776
John McCallf1549f62010-07-06 01:34:17 +00002777 A catch buffer is a setjmp buffer plus:
2778 - a pointer to the exception that was caught
2779 - a pointer to the previous exception data buffer
2780 - two pointers of reserved storage
2781 Therefore catch buffers form a stack, with a pointer to the top
2782 of the stack kept in thread-local storage.
2783
2784 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2785 objc_exception_try_exit pops the given catch buffer, which is
2786 required to be the top of the EH stack.
2787 objc_exception_throw pops the top of the EH stack, writes the
2788 thrown exception into the appropriate field, and longjmps
2789 to the setjmp buffer. It crashes the process (with a printf
2790 and an abort()) if there are no catch buffers on the stack.
2791 objc_exception_extract just reads the exception pointer out of the
2792 catch buffer.
2793
2794 There's no reason an implementation couldn't use a light-weight
2795 setjmp here --- something like __builtin_setjmp, but API-compatible
2796 with the heavyweight setjmp. This will be more important if we ever
2797 want to implement correct ObjC/C++ exception interactions for the
2798 fragile ABI.
2799
2800 Note that for this use of setjmp/longjmp to be correct, we may need
2801 to mark some local variables volatile: if a non-volatile local
2802 variable is modified between the setjmp and the longjmp, it has
2803 indeterminate value. For the purposes of LLVM IR, it may be
2804 sufficient to make loads and stores within the @try (to variables
2805 declared outside the @try) volatile. This is necessary for
2806 optimized correctness, but is not currently being done; this is
2807 being tracked as rdar://problem/8160285
2808
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002809 The basic framework for a @try-catch-finally is as follows:
2810 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002811 objc_exception_data d;
2812 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002813 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002814
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002815 objc_exception_try_enter(&d);
2816 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002817 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002818 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002819 // exception path
2820 id _caught = objc_exception_extract(&d);
2821
2822 // enter new try scope for handlers
2823 if (!setjmp(d.jmp_buf)) {
2824 ... match exception and execute catch blocks ...
2825
2826 // fell off end, rethrow.
2827 _rethrow = _caught;
2828 ... jump-through-finally to finally_rethrow ...
2829 } else {
2830 // exception in catch block
2831 _rethrow = objc_exception_extract(&d);
2832 _call_try_exit = false;
2833 ... jump-through-finally to finally_rethrow ...
2834 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002835 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002836 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002837
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002838 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002839 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002840 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002841
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002842 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002843 ... dispatch to finally destination ...
2844
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002845 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002846 objc_exception_throw(_rethrow);
2847
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002848 finally_end:
2849 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002850
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002851 This framework differs slightly from the one gcc uses, in that gcc
2852 uses _rethrow to determine if objc_exception_try_exit should be called
2853 and if the object should be rethrown. This breaks in the face of
2854 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002855
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002856 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002857
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002858 - If there are no catch blocks, then we avoid emitting the second
2859 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002860
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002861 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2862 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002863
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864 - FIXME: If there is no @finally block we can do a few more
2865 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002866
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002867 Rethrows and Jumps-Through-Finally
2868 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002869
John McCallf1549f62010-07-06 01:34:17 +00002870 '@throw;' is supported by pushing the currently-caught exception
2871 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002872
John McCallf1549f62010-07-06 01:34:17 +00002873 Branches through the @finally block are handled with an ordinary
2874 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2875 exceptions are not compatible with C++ exceptions, and this is
2876 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002877
John McCallf1549f62010-07-06 01:34:17 +00002878 @synchronized(expr) { stmt; } is emitted as if it were:
2879 id synch_value = expr;
2880 objc_sync_enter(synch_value);
2881 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002882*/
2883
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002884void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2885 const Stmt &S) {
2886 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002887
2888 // A destination for the fall-through edges of the catch handlers to
2889 // jump to.
2890 CodeGenFunction::JumpDest FinallyEnd =
2891 CGF.getJumpDestInCurrentScope("finally.end");
2892
2893 // A destination for the rethrow edge of the catch handlers to jump
2894 // to.
2895 CodeGenFunction::JumpDest FinallyRethrow =
2896 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002897
Daniel Dunbar1c566672009-02-24 01:43:46 +00002898 // For @synchronized, call objc_sync_enter(sync.expr). The
2899 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002900 // @synchronized. We can't avoid a temp here because we need the
2901 // value to be preserved. If the backend ever does liveness
2902 // correctly after setjmp, this will be unnecessary.
2903 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002904 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002905 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002906 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2907 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002908 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2909 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002910
2911 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2912 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002913 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002914
John McCall0b251722010-08-04 05:59:32 +00002915 // Allocate memory for the setjmp buffer. This needs to be kept
2916 // live throughout the try and catch blocks.
2917 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2918 "exceptiondata.ptr");
2919
John McCall87bb5822010-07-31 23:20:56 +00002920 // Create the fragile hazards. Note that this will not capture any
2921 // of the allocas required for exception processing, but will
2922 // capture the current basic block (which extends all the way to the
2923 // setjmp call) as "before the @try".
2924 FragileHazards Hazards(CGF);
2925
John McCallf1549f62010-07-06 01:34:17 +00002926 // Create a flag indicating whether the cleanup needs to call
2927 // objc_exception_try_exit. This is true except when
2928 // - no catches match and we're branching through the cleanup
2929 // just to rethrow the exception, or
2930 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002931 // The setjmp-safety rule here is that we should always store to this
2932 // variable in a place that dominates the branch through the cleanup
2933 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002934 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002935 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002936
John McCall9e2213d2010-10-04 23:42:51 +00002937 // A slot containing the exception to rethrow. Only needed when we
2938 // have both a @catch and a @finally.
2939 llvm::Value *PropagatingExnVar = 0;
2940
John McCallf1549f62010-07-06 01:34:17 +00002941 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002942 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002943 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002944 CallTryExitVar,
2945 ExceptionData,
2946 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002947
2948 // Enter a try block:
2949 // - Call objc_exception_try_enter to push ExceptionData on top of
2950 // the EH stack.
2951 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2952 ->setDoesNotThrow();
2953
2954 // - Call setjmp on the exception data buffer.
2955 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2956 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2957 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00002958 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00002959 llvm::CallInst *SetJmpResult =
2960 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2961 SetJmpResult->setDoesNotThrow();
2962
2963 // If setjmp returned 0, enter the protected block; otherwise,
2964 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002965 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2966 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002967 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002968 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2969 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002970
John McCallf1549f62010-07-06 01:34:17 +00002971 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002972 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002973 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002974 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002975 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002976
2977 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002978
John McCallf1549f62010-07-06 01:34:17 +00002979 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002980 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002981
John McCall87bb5822010-07-31 23:20:56 +00002982 // Don't optimize loads of the in-scope locals across this point.
2983 Hazards.emitWriteHazard();
2984
John McCallf1549f62010-07-06 01:34:17 +00002985 // For a @synchronized (or a @try with no catches), just branch
2986 // through the cleanup to the rethrow block.
2987 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2988 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002989 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002990 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002991
2992 // Otherwise, we have to match against the caught exceptions.
2993 } else {
John McCall0b251722010-08-04 05:59:32 +00002994 // Retrieve the exception object. We may emit multiple blocks but
2995 // nothing can cross this so the value is already in SSA form.
2996 llvm::CallInst *Caught =
2997 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2998 ExceptionData, "caught");
2999 Caught->setDoesNotThrow();
3000
John McCallf1549f62010-07-06 01:34:17 +00003001 // Push the exception to rethrow onto the EH value stack for the
3002 // benefit of any @throws in the handlers.
3003 CGF.ObjCEHValueStack.push_back(Caught);
3004
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003005 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003006
John McCall0b251722010-08-04 05:59:32 +00003007 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003008
John McCall0b251722010-08-04 05:59:32 +00003009 llvm::BasicBlock *CatchBlock = 0;
3010 llvm::BasicBlock *CatchHandler = 0;
3011 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003012 // Save the currently-propagating exception before
3013 // objc_exception_try_enter clears the exception slot.
3014 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3015 "propagating_exception");
3016 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3017
John McCall0b251722010-08-04 05:59:32 +00003018 // Enter a new exception try block (in case a @catch block
3019 // throws an exception).
3020 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3021 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003022
John McCall0b251722010-08-04 05:59:32 +00003023 llvm::CallInst *SetJmpResult =
3024 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3025 "setjmp.result");
3026 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027
John McCall0b251722010-08-04 05:59:32 +00003028 llvm::Value *Threw =
3029 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3030
3031 CatchBlock = CGF.createBasicBlock("catch");
3032 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3033 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3034
3035 CGF.EmitBlock(CatchBlock);
3036 }
3037
3038 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003039
Daniel Dunbar55e40722008-09-27 07:03:52 +00003040 // Handle catch list. As a special case we check if everything is
3041 // matched and avoid generating code for falling off the end if
3042 // so.
3043 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003044 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3045 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003046
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003047 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003048 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003049
Anders Carlsson80f25672008-09-09 17:59:25 +00003050 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003051 if (!CatchParam) {
3052 AllMatched = true;
3053 } else {
John McCall183700f2009-09-21 23:43:11 +00003054 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003055
John McCallf1549f62010-07-06 01:34:17 +00003056 // catch(id e) always matches under this ABI, since only
3057 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003058 // FIXME: For the time being we also match id<X>; this should
3059 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003060 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003061 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003062 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003063
John McCallf1549f62010-07-06 01:34:17 +00003064 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003065 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003066 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3067
Anders Carlssondde0a942008-09-11 09:15:33 +00003068 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003069 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003070 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003071
3072 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003073 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003074 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003075
Anders Carlssondde0a942008-09-11 09:15:33 +00003076 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003077
3078 // The scope of the catch variable ends right here.
3079 CatchVarCleanups.ForceCleanup();
3080
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003081 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003082 break;
3083 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003084
Steve Naroff14108da2009-07-10 23:34:53 +00003085 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003086 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003087
3088 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003089 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3090 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003091
3092 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003093 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003094
John McCallf1549f62010-07-06 01:34:17 +00003095 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003096 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3097 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003098 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003099
John McCallf1549f62010-07-06 01:34:17 +00003100 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3101 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102
3103 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003104 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105
Anders Carlsson80f25672008-09-09 17:59:25 +00003106 // Emit the @catch block.
3107 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003108
3109 // Collect any cleanups for the catch variable. The scope lasts until
3110 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003111 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003112
John McCallb6bbcc92010-10-15 04:57:14 +00003113 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003114 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003115
John McCallf1549f62010-07-06 01:34:17 +00003116 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003117 llvm::Value *Tmp =
3118 CGF.Builder.CreateBitCast(Caught,
3119 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003120 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003121 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003122
Anders Carlssondde0a942008-09-11 09:15:33 +00003123 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003124
3125 // We're done with the catch variable.
3126 CatchVarCleanups.ForceCleanup();
3127
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003128 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003129
Anders Carlsson80f25672008-09-09 17:59:25 +00003130 CGF.EmitBlock(NextCatchBlock);
3131 }
3132
John McCallf1549f62010-07-06 01:34:17 +00003133 CGF.ObjCEHValueStack.pop_back();
3134
John McCall0b251722010-08-04 05:59:32 +00003135 // If nothing wanted anything to do with the caught exception,
3136 // kill the extract call.
3137 if (Caught->use_empty())
3138 Caught->eraseFromParent();
3139
3140 if (!AllMatched)
3141 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3142
3143 if (HasFinally) {
3144 // Emit the exception handler for the @catch blocks.
3145 CGF.EmitBlock(CatchHandler);
3146
3147 // In theory we might now need a write hazard, but actually it's
3148 // unnecessary because there's no local-accessing code between
3149 // the try's write hazard and here.
3150 //Hazards.emitWriteHazard();
3151
John McCall9e2213d2010-10-04 23:42:51 +00003152 // Extract the new exception and save it to the
3153 // propagating-exception slot.
3154 assert(PropagatingExnVar);
3155 llvm::CallInst *NewCaught =
3156 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3157 ExceptionData, "caught");
3158 NewCaught->setDoesNotThrow();
3159 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3160
John McCall0b251722010-08-04 05:59:32 +00003161 // Don't pop the catch handler; the throw already did.
3162 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003163 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003164 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003165 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003166
John McCall87bb5822010-07-31 23:20:56 +00003167 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003168 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003169
John McCallf1549f62010-07-06 01:34:17 +00003170 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003171 CGF.Builder.restoreIP(TryFallthroughIP);
3172 if (CGF.HaveInsertPoint())
3173 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003174 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003175 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003176
John McCallf1549f62010-07-06 01:34:17 +00003177 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003178 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003179 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003180 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003181 // If we have a propagating-exception variable, check it.
3182 llvm::Value *PropagatingExn;
3183 if (PropagatingExnVar) {
3184 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003185
John McCall9e2213d2010-10-04 23:42:51 +00003186 // Otherwise, just look in the buffer for the exception to throw.
3187 } else {
3188 llvm::CallInst *Caught =
3189 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3190 ExceptionData);
3191 Caught->setDoesNotThrow();
3192 PropagatingExn = Caught;
3193 }
3194
3195 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003196 ->setDoesNotThrow();
3197 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003198 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003199
John McCall87bb5822010-07-31 23:20:56 +00003200 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003201}
3202
3203void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003204 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003205 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003206
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003207 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3208 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003209 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003210 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3211 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003212 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003213 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003214 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003215 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003216
John McCallf1549f62010-07-06 01:34:17 +00003217 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3218 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003219 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003220
3221 // Clear the insertion point to indicate we are in unreachable code.
3222 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003223}
3224
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003225/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003226/// object: objc_read_weak (id *src)
3227///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003228llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003229 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003230 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003231 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3232 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3233 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003234 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003235 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003236 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003237 return read_weak;
3238}
3239
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003240/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3241/// objc_assign_weak (id src, id *dst)
3242///
3243void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003244 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003245 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003246 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003247 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003248 assert(Size <= 8 && "does not support size > 8");
3249 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003250 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003251 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3252 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003253 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3254 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003255 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003256 src, dst, "weakassign");
3257 return;
3258}
3259
Fariborz Jahanian58626502008-11-19 00:59:10 +00003260/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3261/// objc_assign_global (id src, id *dst)
3262///
3263void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003264 llvm::Value *src, llvm::Value *dst,
3265 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003266 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003267 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003268 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003269 assert(Size <= 8 && "does not support size > 8");
3270 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003271 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003272 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3273 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003274 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3275 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003276 if (!threadlocal)
3277 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3278 src, dst, "globalassign");
3279 else
3280 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3281 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003282 return;
3283}
3284
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003285/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003286/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003287///
3288void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003289 llvm::Value *src, llvm::Value *dst,
3290 llvm::Value *ivarOffset) {
3291 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003292 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003293 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003294 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003295 assert(Size <= 8 && "does not support size > 8");
3296 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003297 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003298 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3299 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003300 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3301 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003302 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3303 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003304 return;
3305}
3306
Fariborz Jahanian58626502008-11-19 00:59:10 +00003307/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3308/// objc_assign_strongCast (id src, id *dst)
3309///
3310void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003311 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003312 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003313 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003314 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003315 assert(Size <= 8 && "does not support size > 8");
3316 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003317 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003318 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3319 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003320 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3321 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003322 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003323 src, dst, "weakassign");
3324 return;
3325}
3326
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003327void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003328 llvm::Value *DestPtr,
3329 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003330 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003331 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3332 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003333 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003334 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003335 return;
3336}
3337
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003338/// EmitObjCValueForIvar - Code Gen for ivar reference.
3339///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003340LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3341 QualType ObjectTy,
3342 llvm::Value *BaseValue,
3343 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003344 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003345 const ObjCInterfaceDecl *ID =
3346 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003347 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3348 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003349}
3350
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003351llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003352 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003353 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003354 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003355 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003356 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3357 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003358}
3359
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003360/* *** Private Interface *** */
3361
3362/// EmitImageInfo - Emit the image info marker used to encode some module
3363/// level information.
3364///
3365/// See: <rdr://4810609&4810587&4810587>
3366/// struct IMAGE_INFO {
3367/// unsigned version;
3368/// unsigned flags;
3369/// };
3370enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003371 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003372 eImageInfo_GarbageCollected = (1 << 1),
3373 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003374 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3375
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003376 // A flag indicating that the module has no instances of a @synthesize of a
3377 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003378 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003379};
3380
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003381void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003382 unsigned version = 0; // Version is unused?
3383 unsigned flags = 0;
3384
3385 // FIXME: Fix and continue?
Douglas Gregore289d812011-09-13 17:21:33 +00003386 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003387 flags |= eImageInfo_GarbageCollected;
Douglas Gregore289d812011-09-13 17:21:33 +00003388 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003389 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003390
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003391 // We never allow @synthesize of a superclass property.
3392 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003393
Chris Lattner2acc6e32011-07-18 04:24:23 +00003394 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003395
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003396 // Emitted as int[2];
3397 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003398 llvm::ConstantInt::get(Int32Ty, version),
3399 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003400 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003401 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003402
3403 const char *Section;
3404 if (ObjCABI == 1)
3405 Section = "__OBJC, __image_info,regular";
3406 else
3407 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003408 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003409 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003410 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003411 Section,
3412 0,
3413 true);
3414 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003415}
3416
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003417
3418// struct objc_module {
3419// unsigned long version;
3420// unsigned long size;
3421// const char *name;
3422// Symtab symtab;
3423// };
3424
3425// FIXME: Get from somewhere
3426static const int ModuleVersion = 7;
3427
3428void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003429 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003430
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003431 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003432 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3433 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003434 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003435 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003436 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003437 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003438 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003439 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003440 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003441}
3442
3443llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444 unsigned NumClasses = DefinedClasses.size();
3445 unsigned NumCategories = DefinedCategories.size();
3446
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003447 // Return null if no symbols were defined.
3448 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003449 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003450
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003451 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003452 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003453 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003454 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3455 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003456
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003457 // The runtime expects exactly the list of defined classes followed
3458 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003459 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003460 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003461 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003462 ObjCTypes.Int8PtrTy);
3463 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003464 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003465 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003466 ObjCTypes.Int8PtrTy);
3467
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003469 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003470 NumClasses + NumCategories),
3471 Symbols);
3472
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003473 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003474
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003475 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003476 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3477 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003478 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003479 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003480}
3481
John McCallf85e1932011-06-15 23:02:42 +00003482llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3483 IdentifierInfo *II) {
3484 LazySymbols.insert(II);
3485
3486 llvm::GlobalVariable *&Entry = ClassReferences[II];
3487
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003488 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003489 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003490 llvm::ConstantExpr::getBitCast(GetClassName(II),
3491 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003492 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003493 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3494 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3495 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003496 }
John McCallf85e1932011-06-15 23:02:42 +00003497
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003498 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003499}
3500
John McCallf85e1932011-06-15 23:02:42 +00003501llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3502 const ObjCInterfaceDecl *ID) {
3503 return EmitClassRefFromId(Builder, ID->getIdentifier());
3504}
3505
3506llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3507 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3508 return EmitClassRefFromId(Builder, II);
3509}
3510
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003511llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3512 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003513 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003514
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003515 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003516 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003517 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003518 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003519 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003520 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3521 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003522 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003523 }
3524
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003525 if (lvalue)
3526 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003527 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003528}
3529
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003530llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003531 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003532
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003533 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003534 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003535 llvm::ConstantArray::get(VMContext,
3536 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003537 ((ObjCABI == 2) ?
3538 "__TEXT,__objc_classname,cstring_literals" :
3539 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003540 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003541
Owen Andersona1cf15f2009-07-14 23:10:40 +00003542 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003543}
3544
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003545llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3546 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3547 I = MethodDefinitions.find(MD);
3548 if (I != MethodDefinitions.end())
3549 return I->second;
3550
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003551 return NULL;
3552}
3553
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003554/// GetIvarLayoutName - Returns a unique constant for the given
3555/// ivar layout bitmap.
3556llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003557 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003558 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003559}
3560
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003561void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003562 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003563 bool ForStrongLayout,
3564 bool &HasUnion) {
3565 const RecordDecl *RD = RT->getDecl();
3566 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003567 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003568 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003570 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003572 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3573 ForStrongLayout, HasUnion);
3574}
3575
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003576void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003577 const llvm::StructLayout *Layout,
3578 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003579 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 unsigned int BytePos, bool ForStrongLayout,
3581 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003582 bool IsUnion = (RD && RD->isUnion());
3583 uint64_t MaxUnionIvarSize = 0;
3584 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003585 const FieldDecl *MaxField = 0;
3586 const FieldDecl *MaxSkippedField = 0;
3587 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003588 uint64_t MaxFieldOffset = 0;
3589 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003590 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003591 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003592
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003593 if (RecFields.empty())
3594 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003595 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3596 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003597 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003598 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003599 FirstFieldDelta =
3600 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3601 }
3602
Chris Lattnerf1690852009-03-31 08:48:01 +00003603 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003604 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003605 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003606 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003607 // Note that 'i' here is actually the field index inside RD of Field,
3608 // although this dependency is hidden.
3609 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003610 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003611 } else
John McCallf85e1932011-06-15 23:02:42 +00003612 FieldOffset =
3613 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003614
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003615 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003616 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003617 LastFieldBitfieldOrUnnamed = Field;
3618 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003619 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003620 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003621
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003622 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003623 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003624 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003625 if (FQT->isUnionType())
3626 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003627
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003628 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003629 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003630 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003631 continue;
3632 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003633
Chris Lattnerf1690852009-03-31 08:48:01 +00003634 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003635 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003636 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003637 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003638 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003639 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003640 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3641 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003642 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003643 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003644 FQT = CArray->getElementType();
3645 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003646
3647 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003648 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003649 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003650 int OldIndex = IvarsInfo.size() - 1;
3651 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003652
Ted Kremenek6217b802009-07-29 21:53:49 +00003653 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003654 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003655 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003656
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003657 // Replicate layout information for each array element. Note that
3658 // one element is already done.
3659 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003660 for (int FirstIndex = IvarsInfo.size() - 1,
3661 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003662 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003663 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3664 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3665 IvarsInfo[i].ivar_size));
3666 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3667 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3668 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003669 }
3670 continue;
3671 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003672 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003673 // At this point, we are done with Record/Union and array there of.
3674 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003675 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003676
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003677 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003678 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3679 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003680 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003681 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003682 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003683 MaxUnionIvarSize = UnionIvarSize;
3684 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003685 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003686 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003687 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003688 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003689 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003690 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003691 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003692 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3693 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003694 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003695 // FIXME: Why the asymmetry? We divide by word size in bits on other
3696 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003697 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003698 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003699 MaxSkippedUnionIvarSize = UnionIvarSize;
3700 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003701 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003702 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003703 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003704 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003705 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003706 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003707 }
3708 }
3709 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003710
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003711 if (LastFieldBitfieldOrUnnamed) {
3712 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3713 // Last field was a bitfield. Must update skip info.
3714 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3715 uint64_t BitFieldSize =
3716 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3717 GC_IVAR skivar;
3718 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3719 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3720 + ((BitFieldSize % ByteSizeInBits) != 0);
3721 SkipIvars.push_back(skivar);
3722 } else {
3723 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3724 // Last field was unnamed. Must update skip info.
3725 unsigned FieldSize
3726 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3727 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3728 FieldSize / ByteSizeInBits));
3729 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003730 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003731
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003732 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003733 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003734 MaxUnionIvarSize));
3735 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003736 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003737 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003738}
3739
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003740/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3741/// the computations and returning the layout bitmap (for ivar or blocks) in
3742/// the given argument BitMap string container. Routine reads
3743/// two containers, IvarsInfo and SkipIvars which are assumed to be
3744/// filled already by the caller.
3745llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003746 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003747 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003748
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003749 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003750 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003751 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003752 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003753 if (IvarsInfo[0].ivar_bytepos == 0) {
3754 WordsToSkip = 0;
3755 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003756 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003757 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3758 WordsToScan = IvarsInfo[0].ivar_size;
3759 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003760 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003761 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003762 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003763 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003764 // consecutive 'scanned' object pointers.
3765 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003766 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003767 // Skip over 'gc'able object pointer which lay over each other.
3768 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3769 continue;
3770 // Must skip over 1 or more words. We save current skip/scan values
3771 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003772 SKIP_SCAN SkScan;
3773 SkScan.skip = WordsToSkip;
3774 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003775 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003776
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003777 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003778 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3779 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003780 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003781 WordsToSkip = 0;
3782 WordsToScan = IvarsInfo[i].ivar_size;
3783 }
3784 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003785 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003786 SKIP_SCAN SkScan;
3787 SkScan.skip = WordsToSkip;
3788 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003789 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003790 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003791
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003792 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003793 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003794 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003795 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003796 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003797 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003798 IvarsInfo[LastIndex].ivar_bytepos +
3799 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003800 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003801 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003802 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003803 SKIP_SCAN SkScan;
3804 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3805 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003806 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003807 }
3808 }
3809 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3810 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003811 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003812 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003813 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3814 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3815 // 0xM0 followed by 0x0N detected.
3816 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3817 for (int j = i+1; j < SkipScan; j++)
3818 SkipScanIvars[j] = SkipScanIvars[j+1];
3819 --SkipScan;
3820 }
3821 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003822
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003823 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003824 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003825 unsigned char byte;
3826 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3827 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3828 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3829 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003830
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003831 // first skip big.
3832 for (unsigned int ix = 0; ix < skip_big; ix++)
3833 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003834
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003835 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003836 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003837 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003838 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003839 byte |= 0xf;
3840 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003841 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003842 byte |= scan_small;
3843 scan_small = 0;
3844 }
3845 BitMap += byte;
3846 }
3847 // next scan big
3848 for (unsigned int ix = 0; ix < scan_big; ix++)
3849 BitMap += (unsigned char)(0x0f);
3850 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003851 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003852 byte = scan_small;
3853 BitMap += byte;
3854 }
3855 }
3856 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003857 unsigned char zero = 0;
3858 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003859
3860 llvm::GlobalVariable * Entry =
3861 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3862 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003863 ((ObjCABI == 2) ?
3864 "__TEXT,__objc_classname,cstring_literals" :
3865 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003866 1, true);
3867 return getConstantGEP(VMContext, Entry, 0, 0);
3868}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003869
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003870/// BuildIvarLayout - Builds ivar layout bitmap for the class
3871/// implementation for the __strong or __weak case.
3872/// The layout map displays which words in ivar list must be skipped
3873/// and which must be scanned by GC (see below). String is built of bytes.
3874/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3875/// of words to skip and right nibble is count of words to scan. So, each
3876/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3877/// represented by a 0x00 byte which also ends the string.
3878/// 1. when ForStrongLayout is true, following ivars are scanned:
3879/// - id, Class
3880/// - object *
3881/// - __strong anything
3882///
3883/// 2. When ForStrongLayout is false, following ivars are scanned:
3884/// - __weak anything
3885///
3886llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3887 const ObjCImplementationDecl *OMD,
3888 bool ForStrongLayout) {
3889 bool hasUnion = false;
3890
Chris Lattner2acc6e32011-07-18 04:24:23 +00003891 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregore289d812011-09-13 17:21:33 +00003892 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00003893 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003894 return llvm::Constant::getNullValue(PtrTy);
3895
Jordy Rosedb8264e2011-07-22 02:08:32 +00003896 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3897 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003898 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003899 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003900 IVD; IVD = IVD->getNextIvar())
3901 RecFields.push_back(cast<FieldDecl>(IVD));
3902 }
3903 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003904 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00003905 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003906
Jordy Rosedb8264e2011-07-22 02:08:32 +00003907 // FIXME: This is not ideal; we shouldn't have to do this copy.
3908 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003909 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003910
3911 if (RecFields.empty())
3912 return llvm::Constant::getNullValue(PtrTy);
3913
3914 SkipIvars.clear();
3915 IvarsInfo.clear();
3916
3917 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3918 if (IvarsInfo.empty())
3919 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003920 // Sort on byte position in case we encounterred a union nested in
3921 // the ivar list.
3922 if (hasUnion && !IvarsInfo.empty())
3923 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3924 if (hasUnion && !SkipIvars.empty())
3925 std::sort(SkipIvars.begin(), SkipIvars.end());
3926
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003927 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003928 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003929
3930 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003931 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003932 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003933 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003934 const unsigned char *s = (unsigned char*)BitMap.c_str();
3935 for (unsigned i = 0; i < BitMap.size(); i++)
3936 if (!(s[i] & 0xf0))
3937 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3938 else
3939 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3940 printf("\n");
3941 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003942 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003943}
3944
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003945llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003946 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3947
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003948 // FIXME: Avoid std::string copying.
3949 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003950 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003951 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003952 ((ObjCABI == 2) ?
3953 "__TEXT,__objc_methname,cstring_literals" :
3954 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003955 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003956
Owen Andersona1cf15f2009-07-14 23:10:40 +00003957 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003958}
3959
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003960// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003961llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003962 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3963}
3964
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003965llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003966 std::string TypeStr;
3967 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3968
3969 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003970
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003971 if (!Entry)
3972 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003973 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003974 ((ObjCABI == 2) ?
3975 "__TEXT,__objc_methtype,cstring_literals" :
3976 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003977 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003978
Owen Andersona1cf15f2009-07-14 23:10:40 +00003979 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003980}
3981
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003982llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003983 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00003984 if (CGM.getContext().getObjCEncodingForMethodDecl(
3985 const_cast<ObjCMethodDecl*>(D),
3986 TypeStr))
3987 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00003988
3989 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3990
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003991 if (!Entry)
3992 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003993 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003994 ((ObjCABI == 2) ?
3995 "__TEXT,__objc_methtype,cstring_literals" :
3996 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003997 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003998
Owen Andersona1cf15f2009-07-14 23:10:40 +00003999 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004000}
4001
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004002// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004003llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004004 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004005
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004006 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004007 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004008 llvm::ConstantArray::get(VMContext,
4009 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004010 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004011 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004012
Owen Andersona1cf15f2009-07-14 23:10:40 +00004013 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004014}
4015
4016// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004017// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004018llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004019CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4020 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004021 std::string TypeStr;
4022 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004023 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4024}
4025
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004026void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004027 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004028 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004029 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004030 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004031 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4032 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004033 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004034 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004035 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004036 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004037}
4038
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004039void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004040 EmitModuleInfo();
4041
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004042 // Emit the dummy bodies for any protocols which were referenced but
4043 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004044 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004045 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4046 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004047 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004048
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004049 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004050 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004051 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004052 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004053 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004054 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004055 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004056 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004057 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004058 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004059 }
4060
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004061 // Add assembler directives to add lazy undefined symbol references
4062 // for classes which are referenced but not defined. This is
4063 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004064 //
4065 // FIXME: It would be nice if we had an LLVM construct for this.
4066 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4067 llvm::SmallString<256> Asm;
4068 Asm += CGM.getModule().getModuleInlineAsm();
4069 if (!Asm.empty() && Asm.back() != '\n')
4070 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004071
Daniel Dunbar33063492009-09-07 00:20:42 +00004072 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004073 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4074 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004075 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4076 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004077 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004078 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004079 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004080 }
4081
4082 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4083 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4084 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4085 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004086
Daniel Dunbar33063492009-09-07 00:20:42 +00004087 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004088 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004089}
4090
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004091CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004092 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004093 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004094 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004095 ObjCABI = 2;
4096}
4097
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004098/* *** */
4099
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004100ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004101 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004102 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4103 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004104
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004105 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004106 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004107 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004108 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004109 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004110
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004111 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004112 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004113 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004114
Mike Stumpf5408fe2009-05-16 07:57:57 +00004115 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4116 // comes out a bit cleaner.
Chris Lattner2acc6e32011-07-18 04:24:23 +00004117 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004118 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004119
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004120 // I'm not sure I like this. The implicit coordination is a bit
4121 // gross. We should solve this in a reasonable fashion because this
4122 // is a pretty common task (match some runtime data structure with
4123 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004125 // FIXME: This is leaked.
4126 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004128 // struct _objc_super {
4129 // id self;
4130 // Class cls;
4131 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004132 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004133 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004134 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004135 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004136 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004137 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004138 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004139 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004140 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004142 SuperCTy = Ctx.getTagDeclType(RD);
4143 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004145 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004146 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4147
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004148 // struct _prop_t {
4149 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004150 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004151 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004152 PropertyTy = llvm::StructType::create("struct._prop_t",
4153 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004154
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004155 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004156 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004157 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004158 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004159 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004160 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004161 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4162 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004163 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004164 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004165
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004166 // struct _objc_method {
4167 // SEL _cmd;
4168 // char *method_type;
4169 // char *_imp;
4170 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004171 MethodTy = llvm::StructType::create("struct._objc_method",
4172 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4173 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004175 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004176 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004177 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004178
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004179}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004180
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004181ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004182 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004183 // struct _objc_method_description {
4184 // SEL name;
4185 // char *types;
4186 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004188 llvm::StructType::create("struct._objc_method_description",
4189 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004190
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004191 // struct _objc_method_description_list {
4192 // int count;
4193 // struct _objc_method_description[1];
4194 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004195 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004196 llvm::StructType::create("struct._objc_method_description_list",
4197 IntTy,
4198 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004199
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004200 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004202 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004203
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004204 // Protocol description structures
4205
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004206 // struct _objc_protocol_extension {
4207 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4208 // struct _objc_method_description_list *optional_instance_methods;
4209 // struct _objc_method_description_list *optional_class_methods;
4210 // struct _objc_property_list *instance_properties;
4211 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004213 llvm::StructType::create("struct._objc_protocol_extension",
4214 IntTy, MethodDescriptionListPtrTy,
4215 MethodDescriptionListPtrTy, PropertyListPtrTy,
4216 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004217
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004218 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004219 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004220
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004221 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004222
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004223 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004224 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004225
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004226 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004227 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004228 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004229 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004230 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004231 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004232
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004233 // struct _objc_protocol {
4234 // struct _objc_protocol_extension *isa;
4235 // char *protocol_name;
4236 // struct _objc_protocol **_objc_protocol_list;
4237 // struct _objc_method_description_list *instance_methods;
4238 // struct _objc_method_description_list *class_methods;
4239 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004240 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4241 llvm::PointerType::getUnqual(ProtocolListTy),
4242 MethodDescriptionListPtrTy,
4243 MethodDescriptionListPtrTy,
4244 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004245
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004246 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004247 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004248
Owen Anderson96e0fc72009-07-29 22:16:19 +00004249 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004250
4251 // Class description structures
4252
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004253 // struct _objc_ivar {
4254 // char *ivar_name;
4255 // char *ivar_type;
4256 // int ivar_offset;
4257 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004258 IvarTy = llvm::StructType::create("struct._objc_ivar",
4259 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004260
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004261 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004262 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004263 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004264 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004265
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004266 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004267 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004268 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004269 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004270
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004271 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004272 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004273 llvm::StructType::create("struct._objc_class_extension",
4274 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004275 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004276
Chris Lattnerc1c20112011-08-12 17:43:31 +00004277 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004278
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004279 // struct _objc_class {
4280 // Class isa;
4281 // Class super_class;
4282 // char *name;
4283 // long version;
4284 // long info;
4285 // long instance_size;
4286 // struct _objc_ivar_list *ivars;
4287 // struct _objc_method_list *methods;
4288 // struct _objc_cache *cache;
4289 // struct _objc_protocol_list *protocols;
4290 // char *ivar_layout;
4291 // struct _objc_class_ext *ext;
4292 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004293 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4294 llvm::PointerType::getUnqual(ClassTy),
4295 Int8PtrTy,
4296 LongTy,
4297 LongTy,
4298 LongTy,
4299 IvarListPtrTy,
4300 MethodListPtrTy,
4301 CachePtrTy,
4302 ProtocolListPtrTy,
4303 Int8PtrTy,
4304 ClassExtensionPtrTy,
4305 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004306
Owen Anderson96e0fc72009-07-29 22:16:19 +00004307 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004308
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004309 // struct _objc_category {
4310 // char *category_name;
4311 // char *class_name;
4312 // struct _objc_method_list *instance_method;
4313 // struct _objc_method_list *class_method;
4314 // uint32_t size; // sizeof(struct _objc_category)
4315 // struct _objc_property_list *instance_properties;// category's @property
4316 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004317 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004318 llvm::StructType::create("struct._objc_category",
4319 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4320 MethodListPtrTy, ProtocolListPtrTy,
4321 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004322
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323 // Global metadata structures
4324
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004325 // struct _objc_symtab {
4326 // long sel_ref_cnt;
4327 // SEL *refs;
4328 // short cls_def_cnt;
4329 // short cat_def_cnt;
4330 // char *defs[cls_def_cnt + cat_def_cnt];
4331 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004332 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004333 llvm::StructType::create("struct._objc_symtab",
4334 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4335 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004336 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004337
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004338 // struct _objc_module {
4339 // long version;
4340 // long size; // sizeof(struct _objc_module)
4341 // char *name;
4342 // struct _objc_symtab* symtab;
4343 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004344 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004345 llvm::StructType::create("struct._objc_module",
4346 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004347
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004348
Mike Stumpf5408fe2009-05-16 07:57:57 +00004349 // FIXME: This is the size of the setjmp buffer and should be target
4350 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004351 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004352
Anders Carlsson124526b2008-09-09 10:10:21 +00004353 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004354 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004355 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004356
4357 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004358 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004359 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4360 SetJmpBufferSize),
4361 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004362
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004363}
4364
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004365ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004366 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004367 // struct _method_list_t {
4368 // uint32_t entsize; // sizeof(struct _objc_method)
4369 // uint32_t method_count;
4370 // struct _objc_method method_list[method_count];
4371 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004372 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004373 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4374 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004375 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004376 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004377
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004378 // struct _protocol_t {
4379 // id isa; // NULL
4380 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004381 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004382 // const struct method_list_t * const instance_methods;
4383 // const struct method_list_t * const class_methods;
4384 // const struct method_list_t *optionalInstanceMethods;
4385 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004386 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004387 // const uint32_t size; // sizeof(struct _protocol_t)
4388 // const uint32_t flags; // = 0
4389 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004390
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004391 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004392 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004393 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004394
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004395 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004396 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4397 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4398 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4399 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4400 PropertyListPtrTy, IntTy, IntTy, NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004401
4402 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004403 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004404
Fariborz Jahanianda320092009-01-29 19:24:30 +00004405 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004406 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004407 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004408 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004409 ProtocolListnfABITy->setBody(LongTy,
4410 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4411 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004413 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004414 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004415
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004416 // struct _ivar_t {
4417 // unsigned long int *offset; // pointer to ivar offset location
4418 // char *name;
4419 // char *type;
4420 // uint32_t alignment;
4421 // uint32_t size;
4422 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004423 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004424 llvm::StructType::create("struct._ivar_t",
4425 llvm::PointerType::getUnqual(LongTy),
4426 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004427
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004428 // struct _ivar_list_t {
4429 // uint32 entsize; // sizeof(struct _ivar_t)
4430 // uint32 count;
4431 // struct _iver_t list[count];
4432 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004433 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004434 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4435 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004436
Owen Anderson96e0fc72009-07-29 22:16:19 +00004437 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004439 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004440 // uint32_t const flags;
4441 // uint32_t const instanceStart;
4442 // uint32_t const instanceSize;
4443 // uint32_t const reserved; // only when building for 64bit targets
4444 // const uint8_t * const ivarLayout;
4445 // const char *const name;
4446 // const struct _method_list_t * const baseMethods;
4447 // const struct _objc_protocol_list *const baseProtocols;
4448 // const struct _ivar_list_t *const ivars;
4449 // const uint8_t * const weakIvarLayout;
4450 // const struct _prop_list_t * const properties;
4451 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004452
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004453 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004454 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4455 IntTy, IntTy, IntTy, Int8PtrTy,
4456 Int8PtrTy, MethodListnfABIPtrTy,
4457 ProtocolListnfABIPtrTy,
4458 IvarListnfABIPtrTy,
4459 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004460
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004461 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004462 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004463 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4464 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004465
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004466 // struct _class_t {
4467 // struct _class_t *isa;
4468 // struct _class_t * const superclass;
4469 // void *cache;
4470 // IMP *vtable;
4471 // struct class_ro_t *ro;
4472 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Chris Lattnerc1c20112011-08-12 17:43:31 +00004474 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004475 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4476 llvm::PointerType::getUnqual(ClassnfABITy),
4477 CachePtrTy,
4478 llvm::PointerType::getUnqual(ImpnfABITy),
4479 llvm::PointerType::getUnqual(ClassRonfABITy),
4480 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004481
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004482 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004483 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004484
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004485 // struct _category_t {
4486 // const char * const name;
4487 // struct _class_t *const cls;
4488 // const struct _method_list_t * const instance_methods;
4489 // const struct _method_list_t * const class_methods;
4490 // const struct _protocol_list_t * const protocols;
4491 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004492 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004493 CategorynfABITy = llvm::StructType::create("struct._category_t",
4494 Int8PtrTy, ClassnfABIPtrTy,
4495 MethodListnfABIPtrTy,
4496 MethodListnfABIPtrTy,
4497 ProtocolListnfABIPtrTy,
4498 PropertyListPtrTy,
4499 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004500
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004501 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004502 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4503 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004504
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004505 // MessageRefTy - LLVM for:
4506 // struct _message_ref_t {
4507 // IMP messenger;
4508 // SEL name;
4509 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004510
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004511 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004512 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004513 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004514 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004515 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004516 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004517 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004518 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004519 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004520 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004521
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004522 MessageRefCTy = Ctx.getTagDeclType(RD);
4523 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4524 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004525
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004526 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004527 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004528
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004529 // SuperMessageRefTy - LLVM for:
4530 // struct _super_message_ref_t {
4531 // SUPER_IMP messenger;
4532 // SEL name;
4533 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004534 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004535 llvm::StructType::create("struct._super_message_ref_t",
4536 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004537
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004538 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004539 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004540
Daniel Dunbare588b992009-03-01 04:46:24 +00004541
4542 // struct objc_typeinfo {
4543 // const void** vtable; // objc_ehtype_vtable + 2
4544 // const char* name; // c++ typeinfo string
4545 // Class cls;
4546 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004547 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004548 llvm::StructType::create("struct._objc_typeinfo",
4549 llvm::PointerType::getUnqual(Int8PtrTy),
4550 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004551 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004552}
4553
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004554llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004555 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004556
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004557 return NULL;
4558}
4559
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004560void CGObjCNonFragileABIMac::AddModuleClassList(const
4561 std::vector<llvm::GlobalValue*>
4562 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004563 const char *SymbolName,
4564 const char *SectionName) {
4565 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Daniel Dunbar463b8762009-05-15 21:48:48 +00004567 if (!NumClasses)
4568 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004569
Daniel Dunbar463b8762009-05-15 21:48:48 +00004570 std::vector<llvm::Constant*> Symbols(NumClasses);
4571 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004572 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004573 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004574 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004575 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004576 NumClasses),
4577 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004578
Daniel Dunbar463b8762009-05-15 21:48:48 +00004579 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004580 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004581 llvm::GlobalValue::InternalLinkage,
4582 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004583 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004584 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004585 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004586 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004587}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004588
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004589void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4590 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004591
Daniel Dunbar463b8762009-05-15 21:48:48 +00004592 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004593 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004595 "\01L_OBJC_LABEL_CLASS_$",
4596 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004597
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004598 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4599 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4600 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4601 continue;
4602 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004603 }
4604
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004605 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4606 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4607 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4608 continue;
4609 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4610 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004611
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004612 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004613 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4614 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004615
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004616 // Build list of all implemented category addresses in array
4617 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004619 "\01L_OBJC_LABEL_CATEGORY_$",
4620 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004622 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4623 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004624
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004625 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004626}
4627
John McCall944c8432011-05-14 03:10:52 +00004628/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4629/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004630/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004631/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004632bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4633 // At various points we've experimented with using vtable-based
4634 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004635 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4636 default:
John McCall944c8432011-05-14 03:10:52 +00004637 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004638 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004639 return false;
John McCall944c8432011-05-14 03:10:52 +00004640 case CodeGenOptions::NonLegacy:
4641 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004642 case CodeGenOptions::Mixed:
4643 break;
4644 }
4645
4646 // If so, see whether this selector is in the white-list of things which must
4647 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004648 if (VTableDispatchMethods.empty()) {
4649 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4650 VTableDispatchMethods.insert(GetNullarySelector("class"));
4651 VTableDispatchMethods.insert(GetNullarySelector("self"));
4652 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4653 VTableDispatchMethods.insert(GetNullarySelector("length"));
4654 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004655
John McCall944c8432011-05-14 03:10:52 +00004656 // These are vtable-based if GC is disabled.
4657 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004658 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004659 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4660 VTableDispatchMethods.insert(GetNullarySelector("release"));
4661 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4662 }
4663
4664 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4665 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4666 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4667 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4668 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4669 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4670 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4671
4672 // These are vtable-based if GC is enabled.
4673 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004674 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004675 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4676 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4677
4678 // "countByEnumeratingWithState:objects:count"
4679 IdentifierInfo *KeyIdents[] = {
4680 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4681 &CGM.getContext().Idents.get("objects"),
4682 &CGM.getContext().Idents.get("count")
4683 };
4684 VTableDispatchMethods.insert(
4685 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4686 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004687 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004688
John McCall944c8432011-05-14 03:10:52 +00004689 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004690}
4691
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004692// Metadata flags
4693enum MetaDataDlags {
4694 CLS = 0x0,
4695 CLS_META = 0x1,
4696 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004697 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004698 CLS_EXCEPTION = 0x20,
4699
4700 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4701 CLS_HAS_IVAR_RELEASER = 0x40,
4702 /// class was compiled with -fobjc-arr
4703 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004704};
4705/// BuildClassRoTInitializer - generate meta-data for:
4706/// struct _class_ro_t {
4707/// uint32_t const flags;
4708/// uint32_t const instanceStart;
4709/// uint32_t const instanceSize;
4710/// uint32_t const reserved; // only when building for 64bit targets
4711/// const uint8_t * const ivarLayout;
4712/// const char *const name;
4713/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004714/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004715/// const struct _ivar_list_t *const ivars;
4716/// const uint8_t * const weakIvarLayout;
4717/// const struct _prop_list_t * const properties;
4718/// }
4719///
4720llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004721 unsigned flags,
4722 unsigned InstanceStart,
4723 unsigned InstanceSize,
4724 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004725 std::string ClassName = ID->getNameAsString();
4726 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004727
4728 if (CGM.getLangOptions().ObjCAutoRefCount)
4729 flags |= CLS_COMPILED_BY_ARC;
4730
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004731 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4732 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4733 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004734 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4736 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004737 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004738 // const struct _method_list_t * const baseMethods;
4739 std::vector<llvm::Constant*> Methods;
4740 std::string MethodListName("\01l_OBJC_$_");
4741 if (flags & CLS_META) {
4742 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004743 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004744 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004745 // Class methods should always be defined.
4746 Methods.push_back(GetMethodConstant(*i));
4747 }
4748 } else {
4749 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004750 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004751 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004752 // Instance methods should always be defined.
4753 Methods.push_back(GetMethodConstant(*i));
4754 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004755 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004756 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004757 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004758
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004759 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4760 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004761
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004762 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4763 if (llvm::Constant *C = GetMethodConstant(MD))
4764 Methods.push_back(C);
4765 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4766 if (llvm::Constant *C = GetMethodConstant(MD))
4767 Methods.push_back(C);
4768 }
4769 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004770 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004771 Values[ 5] = EmitMethodList(MethodListName,
4772 "__DATA, __objc_const", Methods);
4773
Fariborz Jahanianda320092009-01-29 19:24:30 +00004774 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4775 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004777 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004778 OID->all_referenced_protocol_begin(),
4779 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004780
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004781 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004782 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004783 else
4784 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4786 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004787 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004788 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004789 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004790 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4791 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004792 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004793 Values);
4794 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004795 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4796 llvm::GlobalValue::InternalLinkage,
4797 Init,
4798 (flags & CLS_META) ?
4799 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4800 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004801 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004802 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004803 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004804 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004805
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004806}
4807
4808/// BuildClassMetaData - This routine defines that to-level meta-data
4809/// for the given ClassName for:
4810/// struct _class_t {
4811/// struct _class_t *isa;
4812/// struct _class_t * const superclass;
4813/// void *cache;
4814/// IMP *vtable;
4815/// struct class_ro_t *ro;
4816/// }
4817///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004818llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004819 std::string &ClassName,
4820 llvm::Constant *IsAGV,
4821 llvm::Constant *SuperClassGV,
4822 llvm::Constant *ClassRoGV,
4823 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004824 std::vector<llvm::Constant*> Values(5);
4825 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004826 Values[1] = SuperClassGV;
4827 if (!Values[1])
4828 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004829 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4830 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4831 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004833 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004834 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4835 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004836 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004837 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004838 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004839 if (HiddenVisibility)
4840 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004841 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004842}
4843
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004845CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004846 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004847}
4848
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004849void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004850 uint32_t &InstanceStart,
4851 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004852 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004853 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004854
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004855 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004856 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004857
4858 // If there are no fields, the start is the same as the end.
4859 if (!RL.getFieldCount())
4860 InstanceStart = InstanceSize;
4861 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004862 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004863}
4864
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004865void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4866 std::string ClassName = ID->getNameAsString();
4867 if (!ObjCEmptyCacheVar) {
4868 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004869 CGM.getModule(),
4870 ObjCTypes.CacheTy,
4871 false,
4872 llvm::GlobalValue::ExternalLinkage,
4873 0,
4874 "_objc_empty_cache");
4875
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004876 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 CGM.getModule(),
4878 ObjCTypes.ImpnfABITy,
4879 false,
4880 llvm::GlobalValue::ExternalLinkage,
4881 0,
4882 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004883 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004884 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004885 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004886 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004887 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004888 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004889 uint32_t InstanceSize = InstanceStart;
4890 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004891 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4892 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004893
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004894 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004895
4896 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004897 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004898 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004899 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004900 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004901 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004902 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004903 // class is root
4904 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004905 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004906 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004907 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004908 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004909 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4910 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4911 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004912 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004913 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004914 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004915 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004916 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004917 ObjCMetaClassName +
4918 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004919 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004920 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004921 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004922 }
4923 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4924 InstanceStart,
4925 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004926 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004927 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004928 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4929 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004930 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004931
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004932 // Metadata for the class
4933 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004934 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004935 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004936 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004937 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004938
Douglas Gregor68584ed2009-06-18 16:11:24 +00004939 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004940 flags |= CLS_EXCEPTION;
4941
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004942 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004943 flags |= CLS_ROOT;
4944 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004945 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004946 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004947 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004948 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004949 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004950 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004951 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004952 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004953 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004954 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004955 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004957 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004958
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004959 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004961 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4962 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004963 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004964
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004965 // Determine if this class is also "non-lazy".
4966 if (ImplementationIsNonLazy(ID))
4967 DefinedNonLazyClasses.push_back(ClassMD);
4968
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004969 // Force the definition of the EHType if necessary.
4970 if (flags & CLS_EXCEPTION)
4971 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00004972 // Make sure method definition entries are all clear for next implementation.
4973 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004974}
4975
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004976/// GenerateProtocolRef - This routine is called to generate code for
4977/// a protocol reference expression; as in:
4978/// @code
4979/// @protocol(Proto1);
4980/// @endcode
4981/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4982/// which will hold address of the protocol meta-data.
4983///
4984llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004985 const ObjCProtocolDecl *PD) {
4986
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004987 // This routine is called for @protocol only. So, we must build definition
4988 // of protocol's meta-data (not a reference to it!)
4989 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004990 llvm::Constant *Init =
4991 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4992 ObjCTypes.ExternalProtocolPtrTy);
4993
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004994 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00004995 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004996
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004997 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4998 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004999 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005000 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005001 CGM.getModule(),
5002 Init->getType(), false,
5003 llvm::GlobalValue::WeakAnyLinkage,
5004 Init,
5005 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005006 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5007 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005008 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005009 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005010}
5011
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005012/// GenerateCategory - Build metadata for a category implementation.
5013/// struct _category_t {
5014/// const char * const name;
5015/// struct _class_t *const cls;
5016/// const struct _method_list_t * const instance_methods;
5017/// const struct _method_list_t * const class_methods;
5018/// const struct _protocol_list_t * const protocols;
5019/// const struct _prop_list_t * const properties;
5020/// }
5021///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005022void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005023 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005024 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5026 "_$_" + OCD->getNameAsString());
5027 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005028 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005030 std::vector<llvm::Constant*> Values(6);
5031 Values[0] = GetClassName(OCD->getIdentifier());
5032 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005033 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005034 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005035 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5036
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005037 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005038 std::vector<llvm::Constant*> Methods;
5039 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005041 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042
5043 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005044 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005045 // Instance methods should always be defined.
5046 Methods.push_back(GetMethodConstant(*i));
5047 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005048
5049 Values[2] = EmitMethodList(MethodListName,
5050 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005051 Methods);
5052
5053 MethodListName = Prefix;
5054 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5055 OCD->getNameAsString();
5056 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005057 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005058 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005059 // Class methods should always be defined.
5060 Methods.push_back(GetMethodConstant(*i));
5061 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005062
5063 Values[3] = EmitMethodList(MethodListName,
5064 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005065 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005066 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005067 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005068 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005069 llvm::SmallString<256> ExtName;
5070 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5071 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005072 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005073 + Interface->getName() + "_$_"
5074 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005075 Category->protocol_begin(),
5076 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005077 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5078 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005079 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005080 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5081 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005082 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005083
5084 llvm::Constant *Init =
5085 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005086 Values);
5087 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005088 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005089 false,
5090 llvm::GlobalValue::InternalLinkage,
5091 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005092 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005093 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005094 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005095 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005096 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005097 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005098
5099 // Determine if this category is also "non-lazy".
5100 if (ImplementationIsNonLazy(OCD))
5101 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005102 // method definition entries must be clear for next implementation.
5103 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005104}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005105
5106/// GetMethodConstant - Return a struct objc_method constant for the
5107/// given method if it has been defined. The result is null if the
5108/// method has not been defined. The return value has type MethodPtrTy.
5109llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005110 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005111 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005112 if (!Fn)
5113 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005114
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005115 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005116 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005117 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005118 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005119 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005120 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005121 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005122}
5123
5124/// EmitMethodList - Build meta-data for method declarations
5125/// struct _method_list_t {
5126/// uint32_t entsize; // sizeof(struct _objc_method)
5127/// uint32_t method_count;
5128/// struct _objc_method method_list[method_count];
5129/// }
5130///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005131llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005132 const char *Section,
5133 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005134 // Return null for empty list.
5135 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005136 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005138 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005139 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005140 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005141 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005142 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005143 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005144 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005145 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005146 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005147 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005148
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005149 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005150 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005151 llvm::GlobalValue::InternalLinkage, Init, Name);
5152 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005153 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005154 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005155 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005156}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005157
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005158/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5159/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005160llvm::GlobalVariable *
5161CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5162 const ObjCIvarDecl *Ivar) {
5163 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005164 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005165 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005166 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005167 CGM.getModule().getGlobalVariable(Name);
5168 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005169 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005170 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005171 false,
5172 llvm::GlobalValue::ExternalLinkage,
5173 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005174 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005175 return IvarOffsetGV;
5176}
5177
Daniel Dunbare83be122010-04-02 21:14:02 +00005178llvm::Constant *
5179CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5180 const ObjCIvarDecl *Ivar,
5181 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005182 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005183 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005184 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005185 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005186 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005187
Mike Stumpf5408fe2009-05-16 07:57:57 +00005188 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5189 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005190 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5191 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005192 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005193 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005194 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005195 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005196 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005197 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005198}
5199
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005200/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005201/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005202/// IvarListnfABIPtrTy.
5203/// struct _ivar_t {
5204/// unsigned long int *offset; // pointer to ivar offset location
5205/// char *name;
5206/// char *type;
5207/// uint32_t alignment;
5208/// uint32_t size;
5209/// }
5210/// struct _ivar_list_t {
5211/// uint32 entsize; // sizeof(struct _ivar_t)
5212/// uint32 count;
5213/// struct _iver_t list[count];
5214/// }
5215///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005216
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005217llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005218 const ObjCImplementationDecl *ID) {
5219
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005220 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005221
Jordy Rosedb8264e2011-07-22 02:08:32 +00005222 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005223 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005224
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005225 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005226
Jordy Rosedb8264e2011-07-22 02:08:32 +00005227 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005228 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005229 // Ignore unnamed bit-fields.
5230 if (!IVD->getDeclName())
5231 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005232 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005233 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005234 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5235 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005236 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005237 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005238 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005239 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005240 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005241 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005242 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005243 // NOTE. Size of a bitfield does not match gcc's, because of the
5244 // way bitfields are treated special in each. But I am told that
5245 // 'size' for bitfield ivars is ignored by the runtime so it does
5246 // not matter. If it matters, there is enough info to get the
5247 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005248 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005249 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005250 }
5251 // Return null for empty list.
5252 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005253 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005254
5255 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005256 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005257 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5258 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005259 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005260 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005261 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005262 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005263 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5264 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005265 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005266 llvm::GlobalValue::InternalLinkage,
5267 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005268 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005269 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005270 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005271 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005272
Chris Lattnerad64e022009-07-17 23:57:13 +00005273 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005274 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005275}
5276
5277llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005279 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280
Fariborz Jahanianda320092009-01-29 19:24:30 +00005281 if (!Entry) {
5282 // We use the initializer as a marker of whether this is a forward
5283 // reference or not. At module finalization we add the empty
5284 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005285 Entry =
5286 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5287 llvm::GlobalValue::ExternalLinkage,
5288 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005289 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005290 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005291 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005292
Fariborz Jahanianda320092009-01-29 19:24:30 +00005293 return Entry;
5294}
5295
5296/// GetOrEmitProtocol - Generate the protocol meta-data:
5297/// @code
5298/// struct _protocol_t {
5299/// id isa; // NULL
5300/// const char * const protocol_name;
5301/// const struct _protocol_list_t * protocol_list; // super protocols
5302/// const struct method_list_t * const instance_methods;
5303/// const struct method_list_t * const class_methods;
5304/// const struct method_list_t *optionalInstanceMethods;
5305/// const struct method_list_t *optionalClassMethods;
5306/// const struct _prop_list_t * properties;
5307/// const uint32_t size; // sizeof(struct _protocol_t)
5308/// const uint32_t flags; // = 0
5309/// }
5310/// @endcode
5311///
5312
5313llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005314 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005315 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316
Fariborz Jahanianda320092009-01-29 19:24:30 +00005317 // Early exit if a defining object has already been generated.
5318 if (Entry && Entry->hasInitializer())
5319 return Entry;
5320
Fariborz Jahanianda320092009-01-29 19:24:30 +00005321 // Construct method lists.
5322 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5323 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005325 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005326 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005327 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005328 if (!C)
5329 return GetOrEmitProtocolRef(PD);
5330
Fariborz Jahanianda320092009-01-29 19:24:30 +00005331 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5332 OptInstanceMethods.push_back(C);
5333 } else {
5334 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005335 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005336 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005337
5338 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005339 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005340 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005341 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005342 if (!C)
5343 return GetOrEmitProtocolRef(PD);
5344
Fariborz Jahanianda320092009-01-29 19:24:30 +00005345 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5346 OptClassMethods.push_back(C);
5347 } else {
5348 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005349 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005350 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005351
Fariborz Jahanianda320092009-01-29 19:24:30 +00005352 std::vector<llvm::Constant*> Values(10);
5353 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005354 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005355 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005356 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5357 PD->protocol_begin(),
5358 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005359
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005360 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005361 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005362 "__DATA, __objc_const",
5363 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005364 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005365 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 "__DATA, __objc_const",
5367 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005368 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005370 "__DATA, __objc_const",
5371 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005373 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005374 "__DATA, __objc_const",
5375 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005376 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005377 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005379 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005380 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005381 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005382 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005383 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005384
Fariborz Jahanianda320092009-01-29 19:24:30 +00005385 if (Entry) {
5386 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005387 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005388 Entry->setInitializer(Init);
5389 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005390 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005391 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5392 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5393 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005394 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005395 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005396 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005397 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005398 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005399 CGM.AddUsedGlobal(Entry);
5400
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005401 // Use this protocol meta-data to build protocol list table in section
5402 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005403 llvm::GlobalVariable *PTGV =
5404 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5405 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5406 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005407 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005408 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005409 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005410 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005411 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005412 return Entry;
5413}
5414
5415/// EmitProtocolList - Generate protocol list meta-data:
5416/// @code
5417/// struct _protocol_list_t {
5418/// long protocol_count; // Note, this is 32/64 bit
5419/// struct _protocol_t[protocol_count];
5420/// }
5421/// @endcode
5422///
5423llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005424CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005425 ObjCProtocolDecl::protocol_iterator begin,
5426 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005427 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005428
Fariborz Jahanianda320092009-01-29 19:24:30 +00005429 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005430 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005431 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432
Daniel Dunbar948e2582009-02-15 07:36:20 +00005433 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005434 llvm::SmallString<256> TmpName;
5435 Name.toVector(TmpName);
5436 llvm::GlobalVariable *GV =
5437 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005439 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005440
Daniel Dunbar948e2582009-02-15 07:36:20 +00005441 for (; begin != end; ++begin)
5442 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5443
Fariborz Jahanianda320092009-01-29 19:24:30 +00005444 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005445 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446 ObjCTypes.ProtocolnfABIPtrTy));
5447
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005448 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005449 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005450 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005451 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005452 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005453 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454 ProtocolRefs.size()),
5455 ProtocolRefs);
5456
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005457 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005458 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005459 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005460 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005461 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005462 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005463 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005464 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005465 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005466 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005467}
5468
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005469/// GetMethodDescriptionConstant - This routine build following meta-data:
5470/// struct _objc_method {
5471/// SEL _cmd;
5472/// char *method_type;
5473/// char *_imp;
5474/// }
5475
5476llvm::Constant *
5477CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5478 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005479 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5481 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005482 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005483 if (!Desc[1])
5484 return 0;
5485
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005486 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005487 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005488 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005489}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005490
5491/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5492/// This code gen. amounts to generating code for:
5493/// @code
5494/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5495/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005496///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005497LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005498 CodeGen::CodeGenFunction &CGF,
5499 QualType ObjectTy,
5500 llvm::Value *BaseValue,
5501 const ObjCIvarDecl *Ivar,
5502 unsigned CVRQualifiers) {
5503 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005504 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5505 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005506}
5507
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005508llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005509 CodeGen::CodeGenFunction &CGF,
5510 const ObjCInterfaceDecl *Interface,
5511 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005512 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005513}
5514
John McCallb1e81442011-05-13 23:16:18 +00005515static void appendSelectorForMessageRefTable(std::string &buffer,
5516 Selector selector) {
5517 if (selector.isUnarySelector()) {
5518 buffer += selector.getNameForSlot(0);
5519 return;
5520 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005521
John McCallb1e81442011-05-13 23:16:18 +00005522 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5523 buffer += selector.getNameForSlot(i);
5524 buffer += '_';
5525 }
5526}
5527
John McCall944c8432011-05-14 03:10:52 +00005528/// Emit a "v-table" message send. We emit a weak hidden-visibility
5529/// struct, initially containing the selector pointer and a pointer to
5530/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5531/// load and call the function pointer, passing the address of the
5532/// struct as the second parameter. The runtime determines whether
5533/// the selector is currently emitted using vtable dispatch; if so, it
5534/// substitutes a stub function which simply tail-calls through the
5535/// appropriate vtable slot, and if not, it substitues a stub function
5536/// which tail-calls objc_msgSend. Both stubs adjust the selector
5537/// argument to correctly point to the selector.
5538RValue
5539CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5540 ReturnValueSlot returnSlot,
5541 QualType resultType,
5542 Selector selector,
5543 llvm::Value *arg0,
5544 QualType arg0Type,
5545 bool isSuper,
5546 const CallArgList &formalArgs,
5547 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005548 // Compute the actual arguments.
5549 CallArgList args;
5550
John McCall944c8432011-05-14 03:10:52 +00005551 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005552 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005553 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5554 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005555
John McCall944c8432011-05-14 03:10:52 +00005556 // Second argument: a pointer to the message ref structure. Leave
5557 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005558 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5559
5560 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5561
5562 const CGFunctionInfo &fnInfo =
5563 CGM.getTypes().getFunctionInfo(resultType, args,
5564 FunctionType::ExtInfo());
5565
John McCallcba681a2011-05-14 21:12:11 +00005566 NullReturnState nullReturn;
5567
John McCall944c8432011-05-14 03:10:52 +00005568 // Find the function to call and the mangled name for the message
5569 // ref structure. Using a different mangled name wouldn't actually
5570 // be a problem; it would just be a waste.
5571 //
5572 // The runtime currently never uses vtable dispatch for anything
5573 // except normal, non-super message-sends.
5574 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005575 llvm::Constant *fn = 0;
5576 std::string messageRefName("\01l_");
5577 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005578 if (isSuper) {
5579 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5580 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005581 } else {
John McCallcba681a2011-05-14 21:12:11 +00005582 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005583 fn = ObjCTypes.getMessageSendStretFixupFn();
5584 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005585 }
John McCallb1e81442011-05-13 23:16:18 +00005586 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5587 fn = ObjCTypes.getMessageSendFpretFixupFn();
5588 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005589 } else {
John McCallb1e81442011-05-13 23:16:18 +00005590 if (isSuper) {
5591 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5592 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005593 } else {
John McCallb1e81442011-05-13 23:16:18 +00005594 fn = ObjCTypes.getMessageSendFixupFn();
5595 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005596 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005597 }
John McCallb1e81442011-05-13 23:16:18 +00005598 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5599 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600
John McCallb1e81442011-05-13 23:16:18 +00005601 // Append the selector name, except use underscores anywhere we
5602 // would have used colons.
5603 appendSelectorForMessageRefTable(messageRefName, selector);
5604
5605 llvm::GlobalVariable *messageRef
5606 = CGM.getModule().getGlobalVariable(messageRefName);
5607 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005608 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005609 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005610 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005611 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5612 init->getType(),
5613 /*constant*/ false,
5614 llvm::GlobalValue::WeakAnyLinkage,
5615 init,
5616 messageRefName);
5617 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5618 messageRef->setAlignment(16);
5619 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5620 }
5621 llvm::Value *mref =
5622 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5623
John McCall944c8432011-05-14 03:10:52 +00005624 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005625 args[1].RV = RValue::get(mref);
5626
5627 // Load the function to call from the message ref table.
5628 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5629 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5630
5631 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005632 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005633 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5634 callee = CGF.Builder.CreateBitCast(callee,
5635 llvm::PointerType::getUnqual(fnType));
5636
John McCallcba681a2011-05-14 21:12:11 +00005637 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5638 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005639}
5640
5641/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005642CodeGen::RValue
5643CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005644 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005645 QualType ResultType,
5646 Selector Sel,
5647 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005648 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005649 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005650 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005651 return isVTableDispatchedSelector(Sel)
5652 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005653 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005654 false, CallArgs, Method)
5655 : EmitMessageSend(CGF, Return, ResultType,
5656 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005658 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005659}
5660
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005661llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005662CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005663 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5664
Daniel Dunbardfff2302009-03-02 05:18:14 +00005665 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005666 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005667 false, llvm::GlobalValue::ExternalLinkage,
5668 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005669 }
5670
5671 return GV;
5672}
5673
John McCallf85e1932011-06-15 23:02:42 +00005674llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5675 IdentifierInfo *II) {
5676 llvm::GlobalVariable *&Entry = ClassReferences[II];
5677
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005678 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005679 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005680 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005681 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005682 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5683 false, llvm::GlobalValue::InternalLinkage,
5684 ClassGV,
5685 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005686 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005687 CGM.getTargetData().getABITypeAlignment(
5688 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005689 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005690 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005691 }
John McCallf85e1932011-06-15 23:02:42 +00005692
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005693 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005694}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005695
John McCallf85e1932011-06-15 23:02:42 +00005696llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5697 const ObjCInterfaceDecl *ID) {
5698 return EmitClassRefFromId(Builder, ID->getIdentifier());
5699}
5700
5701llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5702 CGBuilderTy &Builder) {
5703 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5704 return EmitClassRefFromId(Builder, II);
5705}
5706
Daniel Dunbar11394522009-04-18 08:51:00 +00005707llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005708CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005709 const ObjCInterfaceDecl *ID) {
5710 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005711
Daniel Dunbar11394522009-04-18 08:51:00 +00005712 if (!Entry) {
5713 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5714 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005715 Entry =
5716 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005717 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005718 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005719 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005720 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005721 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005722 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005723 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005724 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005725 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005727 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005728}
5729
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005730/// EmitMetaClassRef - Return a Value * of the address of _class_t
5731/// meta-data
5732///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005733llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5734 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005735 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5736 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005737 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005738
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005739 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005740 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005742 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005743 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005744 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005745 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005746 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005747 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005748 ObjCTypes.ClassnfABIPtrTy));
5749
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005750 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005751 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005753 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754}
5755
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005756/// GetClass - Return a reference to the class for the given interface
5757/// decl.
5758llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5759 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005760 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005761 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5762 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5763 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5764 }
5765
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005766 return EmitClassRef(Builder, ID);
5767}
5768
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005769/// Generates a message send where the super is the receiver. This is
5770/// a message send to self with special delivery semantics indicating
5771/// which class's method should be called.
5772CodeGen::RValue
5773CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005774 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005775 QualType ResultType,
5776 Selector Sel,
5777 const ObjCInterfaceDecl *Class,
5778 bool isCategoryImpl,
5779 llvm::Value *Receiver,
5780 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005781 const CodeGen::CallArgList &CallArgs,
5782 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005783 // ...
5784 // Create and init a super structure; this is a (receiver, class)
5785 // pair we will pass to objc_msgSendSuper.
5786 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005787 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005789 llvm::Value *ReceiverAsObject =
5790 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5791 CGF.Builder.CreateStore(ReceiverAsObject,
5792 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005793
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005794 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005795 llvm::Value *Target;
5796 if (IsClassMessage) {
5797 if (isCategoryImpl) {
5798 // Message sent to "super' in a class method defined in
5799 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005800 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005801 Target = CGF.Builder.CreateStructGEP(Target, 0);
5802 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005803 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005804 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005805 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005806 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005807
Mike Stumpf5408fe2009-05-16 07:57:57 +00005808 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5809 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005810 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005811 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5812 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5813 CGF.Builder.CreateStore(Target,
5814 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005815
John McCall944c8432011-05-14 03:10:52 +00005816 return (isVTableDispatchedSelector(Sel))
5817 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005818 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005819 true, CallArgs, Method)
5820 : EmitMessageSend(CGF, Return, ResultType,
5821 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005822 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005823 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005824}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005825
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005827 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005828 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005829
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005830 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005831 llvm::Constant *Casted =
5832 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5833 ObjCTypes.SelectorPtrTy);
5834 Entry =
5835 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5836 llvm::GlobalValue::InternalLinkage,
5837 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005838 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005839 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005840 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005841
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005842 if (lval)
5843 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005844 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005845}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005846/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005847/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005848///
5849void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005850 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005851 llvm::Value *dst,
5852 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005853 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005854 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005855 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005856 assert(Size <= 8 && "does not support size > 8");
5857 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5858 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005859 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5860 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005861 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5862 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005863 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5864 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005865 return;
5866}
5867
5868/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5869/// objc_assign_strongCast (id src, id *dst)
5870///
5871void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005872 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005873 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005874 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005875 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005876 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005877 assert(Size <= 8 && "does not support size > 8");
5878 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005880 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5881 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005882 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5883 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005884 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005885 src, dst, "weakassign");
5886 return;
5887}
5888
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005889void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005890 CodeGen::CodeGenFunction &CGF,
5891 llvm::Value *DestPtr,
5892 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005893 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005894 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5895 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005896 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005897 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005898 return;
5899}
5900
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005901/// EmitObjCWeakRead - Code gen for loading value of a __weak
5902/// object: objc_read_weak (id *src)
5903///
5904llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005906 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005907 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005908 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5909 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005910 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005911 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005912 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005913 return read_weak;
5914}
5915
5916/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5917/// objc_assign_weak (id src, id *dst)
5918///
5919void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005920 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005921 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005922 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005923 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005924 assert(Size <= 8 && "does not support size > 8");
5925 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5926 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005927 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5928 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005929 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5930 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005931 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005932 src, dst, "weakassign");
5933 return;
5934}
5935
5936/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5937/// objc_assign_global (id src, id *dst)
5938///
5939void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005940 llvm::Value *src, llvm::Value *dst,
5941 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005942 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005943 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005944 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005945 assert(Size <= 8 && "does not support size > 8");
5946 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5947 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005948 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5949 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005950 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5951 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005952 if (!threadlocal)
5953 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5954 src, dst, "globalassign");
5955 else
5956 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5957 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005958 return;
5959}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005960
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005961void
John McCallf1549f62010-07-06 01:34:17 +00005962CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5963 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005964 EmitAtSynchronizedStmt(CGF, S,
5965 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5966 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005967}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005968
John McCall5a180392010-07-24 00:37:23 +00005969llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00005970CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00005971 // There's a particular fixed type info for 'id'.
5972 if (T->isObjCIdType() ||
5973 T->isObjCQualifiedIdType()) {
5974 llvm::Constant *IDEHType =
5975 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5976 if (!IDEHType)
5977 IDEHType =
5978 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5979 false,
5980 llvm::GlobalValue::ExternalLinkage,
5981 0, "OBJC_EHTYPE_id");
5982 return IDEHType;
5983 }
5984
5985 // All other types should be Objective-C interface pointer types.
5986 const ObjCObjectPointerType *PT =
5987 T->getAs<ObjCObjectPointerType>();
5988 assert(PT && "Invalid @catch type.");
5989 const ObjCInterfaceType *IT = PT->getInterfaceType();
5990 assert(IT && "Invalid @catch type.");
5991 return GetInterfaceEHType(IT->getDecl(), false);
5992}
5993
John McCallf1549f62010-07-06 01:34:17 +00005994void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5995 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005996 EmitTryCatchStmt(CGF, S,
5997 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5998 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5999 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006000}
6001
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006002/// EmitThrowStmt - Generate code for a throw statement.
6003void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6004 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006005 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006006 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006007 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6008 "tmp");
Jay Foad4c7d9f12011-07-15 08:37:34 +00006009 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006010 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006011 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006012 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006013 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006014 }
6015
John McCall7ec404c2010-10-16 08:21:07 +00006016 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006017 CGF.Builder.ClearInsertionPoint();
6018}
Daniel Dunbare588b992009-03-01 04:46:24 +00006019
John McCall5a180392010-07-24 00:37:23 +00006020llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006021CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006022 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006023 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006024
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006025 // If we don't need a definition, return the entry if found or check
6026 // if we use an external reference.
6027 if (!ForDefinition) {
6028 if (Entry)
6029 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006030
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006031 // If this type (or a super class) has the __objc_exception__
6032 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006033 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006034 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006035 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006036 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006037 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006038 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006039 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006040 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006041
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006042 // Otherwise we need to either make a new entry or fill in the
6043 // initializer.
6044 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006045 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006046 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006047 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006048 CGM.getModule().getGlobalVariable(VTableName);
6049 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006050 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6051 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006052 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006053 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006054
Chris Lattner77b89b82010-06-27 07:15:29 +00006055 llvm::Value *VTableIdx =
6056 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006057
6058 std::vector<llvm::Constant*> Values(3);
Jay Foada5c04342011-07-21 14:31:17 +00006059 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx);
Daniel Dunbare588b992009-03-01 04:46:24 +00006060 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006061 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006062 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006063 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006064
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006065 if (Entry) {
6066 Entry->setInitializer(Init);
6067 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006068 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006069 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006070 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006071 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006072 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006073 }
6074
John McCall1fb0caa2010-10-22 21:05:15 +00006075 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006076 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006077 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6078 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006079
6080 if (ForDefinition) {
6081 Entry->setSection("__DATA,__objc_const");
6082 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6083 } else {
6084 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6085 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006086
6087 return Entry;
6088}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006089
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006090/* *** */
6091
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006092CodeGen::CGObjCRuntime *
6093CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006094 if (CGM.getLangOptions().ObjCNonFragileABI)
6095 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006096 return new CGObjCMac(CGM);
6097}