blob: 0675f0df05969f7b74d7bbb1d36a5043a4a7db2d [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
John McCall448d2cd2011-02-26 09:48:59 +000046static void EmitNullReturnInitialization(CodeGenFunction &CGF,
47 ReturnValueSlot &returnSlot,
48 QualType resultType) {
49 // Force the return slot to exist.
50 if (!returnSlot.getValue())
51 returnSlot = ReturnValueSlot(CGF.CreateMemTemp(resultType), false);
52 CGF.EmitNullInitialization(returnSlot.getValue(), resultType);
53}
54
Daniel Dunbar97776872009-04-22 07:32:20 +000055
56///
57
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000058namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000059
Daniel Dunbar6bff2512009-08-03 17:06:42 +000060typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +000061
Daniel Dunbar6bff2512009-08-03 17:06:42 +000062// FIXME: We should find a nicer way to make the labels for metadata, string
63// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000064
Fariborz Jahanianee0af742009-01-21 22:04:16 +000065class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000066protected:
67 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000068
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000069private:
70 llvm::Constant *getMessageSendFn() const {
71 // id objc_msgSend (id, SEL, ...)
72 std::vector<const llvm::Type*> Params;
73 Params.push_back(ObjectPtrTy);
74 Params.push_back(SelectorPtrTy);
75 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +000076 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +000077 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +000078 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000079 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000080
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000081 llvm::Constant *getMessageSendStretFn() const {
82 // id objc_msgSend_stret (id, SEL, ...)
83 std::vector<const llvm::Type*> Params;
84 Params.push_back(ObjectPtrTy);
85 Params.push_back(SelectorPtrTy);
86 return
Owen Anderson0032b272009-08-13 21:57:51 +000087 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +000088 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +000089 "objc_msgSend_stret");
90
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000092
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
94 // FIXME: This should be long double on x86_64?
95 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
96 std::vector<const llvm::Type*> Params;
97 Params.push_back(ObjectPtrTy);
98 Params.push_back(SelectorPtrTy);
99 return
Owen Anderson0032b272009-08-13 21:57:51 +0000100 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
101 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000102 Params,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000103 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000104 "objc_msgSend_fpret");
105
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000106 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000107
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000108 llvm::Constant *getMessageSendSuperFn() const {
109 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
110 const char *SuperName = "objc_msgSendSuper";
111 std::vector<const llvm::Type*> Params;
112 Params.push_back(SuperPtrTy);
113 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000114 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000115 Params, true),
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000116 SuperName);
117 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000118
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn2() const {
120 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
121 const char *SuperName = "objc_msgSendSuper2";
122 std::vector<const llvm::Type*> Params;
123 Params.push_back(SuperPtrTy);
124 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000126 Params, true),
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000127 SuperName);
128 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000129
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperStretFn() const {
131 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
132 // SEL op, ...)
133 std::vector<const llvm::Type*> Params;
134 Params.push_back(Int8PtrTy);
135 Params.push_back(SuperPtrTy);
136 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000137 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000138 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000139 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000140 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000142
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000143 llvm::Constant *getMessageSendSuperStretFn2() const {
144 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
145 // SEL op, ...)
146 std::vector<const llvm::Type*> Params;
147 Params.push_back(Int8PtrTy);
148 Params.push_back(SuperPtrTy);
149 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000150 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000151 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000152 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000153 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000154 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperFpretFn() const {
157 // There is no objc_msgSendSuper_fpret? How can that work?
158 return getMessageSendSuperFn();
159 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000161 llvm::Constant *getMessageSendSuperFpretFn2() const {
162 // There is no objc_msgSendSuper_fpret? How can that work?
163 return getMessageSendSuperFn2();
164 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000165
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000166protected:
167 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000168
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000169public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000170 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000171 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000173 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
174 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000176 /// PtrObjectPtrTy - LLVM type for id *
177 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000179 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000180 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000181 /// ProtocolPtrTy - LLVM type for external protocol handles
182 /// (typeof(Protocol))
183 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000184
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000185 // SuperCTy - clang type for struct objc_super.
186 QualType SuperCTy;
187 // SuperPtrCTy - clang type for struct objc_super *.
188 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000189
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000190 /// SuperTy - LLVM type for struct objc_super.
191 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000192 /// SuperPtrTy - LLVM type for struct objc_super *.
193 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000194
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000195 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
196 /// in GCC parlance).
197 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000198
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000199 /// PropertyListTy - LLVM type for struct objc_property_list
200 /// (_prop_list_t in GCC parlance).
201 const llvm::StructType *PropertyListTy;
202 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
203 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000204
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000205 // MethodTy - LLVM type for struct objc_method.
206 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000207
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000208 /// CacheTy - LLVM type for struct objc_cache.
209 const llvm::Type *CacheTy;
210 /// CachePtrTy - LLVM type for struct objc_cache *.
211 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Chris Lattner72db6c32009-04-22 02:44:54 +0000213 llvm::Constant *getGetPropertyFn() {
214 CodeGen::CodeGenTypes &Types = CGM.getTypes();
215 ASTContext &Ctx = CGM.getContext();
216 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000217 llvm::SmallVector<CanQualType,4> Params;
218 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
219 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000220 Params.push_back(IdType);
221 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000222 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000223 Params.push_back(Ctx.BoolTy);
224 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000225 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000226 FunctionType::ExtInfo()),
227 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000228 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000230
Chris Lattner72db6c32009-04-22 02:44:54 +0000231 llvm::Constant *getSetPropertyFn() {
232 CodeGen::CodeGenTypes &Types = CGM.getTypes();
233 ASTContext &Ctx = CGM.getContext();
234 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000235 llvm::SmallVector<CanQualType,6> Params;
236 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
237 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000238 Params.push_back(IdType);
239 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000240 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000241 Params.push_back(IdType);
242 Params.push_back(Ctx.BoolTy);
243 Params.push_back(Ctx.BoolTy);
244 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000245 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000246 FunctionType::ExtInfo()),
247 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000251
252 llvm::Constant *getCopyStructFn() {
253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
254 ASTContext &Ctx = CGM.getContext();
255 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
256 llvm::SmallVector<CanQualType,5> Params;
257 Params.push_back(Ctx.VoidPtrTy);
258 Params.push_back(Ctx.VoidPtrTy);
259 Params.push_back(Ctx.LongTy);
260 Params.push_back(Ctx.BoolTy);
261 Params.push_back(Ctx.BoolTy);
262 const llvm::FunctionType *FTy =
263 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
264 FunctionType::ExtInfo()),
265 false);
266 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
267 }
268
Chris Lattner72db6c32009-04-22 02:44:54 +0000269 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
271 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000272 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000273 llvm::SmallVector<CanQualType,1> Params;
274 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000275 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000276 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000277 FunctionType::ExtInfo()),
278 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
280 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000281
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000282 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000283 llvm::Constant *getGcReadWeakFn() {
284 // id objc_read_weak (id *)
285 std::vector<const llvm::Type*> Args;
286 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000287 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000288 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000289 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000290 }
291
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000292 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000293 llvm::Constant *getGcAssignWeakFn() {
294 // id objc_assign_weak (id, id *)
295 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
296 Args.push_back(ObjectPtrTy->getPointerTo());
297 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000298 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000299 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
300 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000301
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000302 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000303 llvm::Constant *getGcAssignGlobalFn() {
304 // id objc_assign_global(id, id *)
305 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
306 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000307 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000308 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000309 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000311
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000312 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
313 llvm::Constant *getGcAssignThreadLocalFn() {
314 // id objc_assign_threadlocal(id src, id * dest)
315 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
316 Args.push_back(ObjectPtrTy->getPointerTo());
317 llvm::FunctionType *FTy =
318 llvm::FunctionType::get(ObjectPtrTy, Args, false);
319 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
320 }
321
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000322 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000323 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000324 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000325 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
326 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000327 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000328 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000329 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
331 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000332
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000333 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
334 llvm::Constant *GcMemmoveCollectableFn() {
335 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
336 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
337 Args.push_back(Int8PtrTy);
338 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000339 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
341 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000342
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000343 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000344 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000345 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000346 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
347 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000348 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000349 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000350 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
351 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000352
353 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000354 llvm::Constant *getExceptionThrowFn() {
355 // void objc_exception_throw(id)
356 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
357 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000358 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
360 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000361
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000362 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
363 llvm::Constant *getExceptionRethrowFn() {
364 // void objc_exception_rethrow(void)
365 std::vector<const llvm::Type*> Args;
366 llvm::FunctionType *FTy =
John McCall7ec404c2010-10-16 08:21:07 +0000367 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
369 }
370
Daniel Dunbar1c566672009-02-24 01:43:46 +0000371 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000372 llvm::Constant *getSyncEnterFn() {
373 // void objc_sync_enter (id)
374 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
375 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000376 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000379
Daniel Dunbar1c566672009-02-24 01:43:46 +0000380 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000381 llvm::Constant *getSyncExitFn() {
382 // void objc_sync_exit (id)
383 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
384 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000385 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000386 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
387 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000388
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000389 llvm::Constant *getSendFn(bool IsSuper) const {
390 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
391 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000392
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000393 llvm::Constant *getSendFn2(bool IsSuper) const {
394 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
395 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000396
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000397 llvm::Constant *getSendStretFn(bool IsSuper) const {
398 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000400
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000401 llvm::Constant *getSendStretFn2(bool IsSuper) const {
402 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000404
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendFpretFn(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
407 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000408
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
411 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000412
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000413 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
414 ~ObjCCommonTypesHelper(){}
415};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000416
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000417/// ObjCTypesHelper - Helper class that encapsulates lazy
418/// construction of varies types used during ObjC generation.
419class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000420public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000421 /// SymtabTy - LLVM type for struct objc_symtab.
422 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000423 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
424 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000425 /// ModuleTy - LLVM type for struct objc_module.
426 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000427
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000428 /// ProtocolTy - LLVM type for struct objc_protocol.
429 const llvm::StructType *ProtocolTy;
430 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
431 const llvm::Type *ProtocolPtrTy;
432 /// ProtocolExtensionTy - LLVM type for struct
433 /// objc_protocol_extension.
434 const llvm::StructType *ProtocolExtensionTy;
435 /// ProtocolExtensionTy - LLVM type for struct
436 /// objc_protocol_extension *.
437 const llvm::Type *ProtocolExtensionPtrTy;
438 /// MethodDescriptionTy - LLVM type for struct
439 /// objc_method_description.
440 const llvm::StructType *MethodDescriptionTy;
441 /// MethodDescriptionListTy - LLVM type for struct
442 /// objc_method_description_list.
443 const llvm::StructType *MethodDescriptionListTy;
444 /// MethodDescriptionListPtrTy - LLVM type for struct
445 /// objc_method_description_list *.
446 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000447 /// ProtocolListTy - LLVM type for struct objc_property_list.
448 const llvm::Type *ProtocolListTy;
449 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
450 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000451 /// CategoryTy - LLVM type for struct objc_category.
452 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000453 /// ClassTy - LLVM type for struct objc_class.
454 const llvm::StructType *ClassTy;
455 /// ClassPtrTy - LLVM type for struct objc_class *.
456 const llvm::Type *ClassPtrTy;
457 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
458 const llvm::StructType *ClassExtensionTy;
459 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
460 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000461 // IvarTy - LLVM type for struct objc_ivar.
462 const llvm::StructType *IvarTy;
463 /// IvarListTy - LLVM type for struct objc_ivar_list.
464 const llvm::Type *IvarListTy;
465 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
466 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000467 /// MethodListTy - LLVM type for struct objc_method_list.
468 const llvm::Type *MethodListTy;
469 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
470 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000471
Anders Carlsson124526b2008-09-09 10:10:21 +0000472 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
473 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000474
Anders Carlsson124526b2008-09-09 10:10:21 +0000475 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000476 llvm::Constant *getExceptionTryEnterFn() {
477 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000478 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000479 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000480 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000481 Params, false),
482 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000483 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000484
485 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000486 llvm::Constant *getExceptionTryExitFn() {
487 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000488 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000489 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000490 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000491 Params, false),
492 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000493 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000494
495 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000496 llvm::Constant *getExceptionExtractFn() {
497 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000498 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
499 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000500 Params, false),
501 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Chris Lattner34b02a12009-04-22 02:26:14 +0000503 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000504
Anders Carlsson124526b2008-09-09 10:10:21 +0000505 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000506 llvm::Constant *getExceptionMatchFn() {
507 std::vector<const llvm::Type*> Params;
508 Params.push_back(ClassPtrTy);
509 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000510 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000511 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000512 Params, false),
513 "objc_exception_match");
514
Chris Lattner34b02a12009-04-22 02:26:14 +0000515 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000516
Anders Carlsson124526b2008-09-09 10:10:21 +0000517 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000518 llvm::Constant *getSetJmpFn() {
519 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000520 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000521 return
Owen Anderson0032b272009-08-13 21:57:51 +0000522 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000523 Params, false),
524 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000525
Chris Lattner34b02a12009-04-22 02:26:14 +0000526 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000527
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000528public:
529 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000530 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000531};
532
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000533/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000534/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000535class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000536public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000537
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000538 // MethodListnfABITy - LLVM for struct _method_list_t
539 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000541 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
542 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000544 // ProtocolnfABITy = LLVM for struct _protocol_t
545 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000546
Daniel Dunbar948e2582009-02-15 07:36:20 +0000547 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
548 const llvm::Type *ProtocolnfABIPtrTy;
549
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000550 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
551 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000553 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
554 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000555
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000556 // ClassnfABITy - LLVM for struct _class_t
557 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000558
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000559 // ClassnfABIPtrTy - LLVM for struct _class_t*
560 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000561
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000562 // IvarnfABITy - LLVM for struct _ivar_t
563 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000564
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000565 // IvarListnfABITy - LLVM for struct _ivar_list_t
566 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000568 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
569 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000571 // ClassRonfABITy - LLVM for struct _class_ro_t
572 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000573
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000574 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
575 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000577 // CategorynfABITy - LLVM for struct _category_t
578 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000580 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000582 // MessageRefTy - LLVM for:
583 // struct _message_ref_t {
584 // IMP messenger;
585 // SEL name;
586 // };
587 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000588 // MessageRefCTy - clang type for struct _message_ref_t
589 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000591 // MessageRefPtrTy - LLVM for struct _message_ref_t*
592 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000593 // MessageRefCPtrTy - clang type for struct _message_ref_t*
594 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000595
Fariborz Jahanianef163782009-02-05 01:13:09 +0000596 // MessengerTy - Type of the messenger (shown as IMP above)
597 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000599 // SuperMessageRefTy - LLVM for:
600 // struct _super_message_ref_t {
601 // SUPER_IMP messenger;
602 // SEL name;
603 // };
604 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000606 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
607 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000608
Chris Lattner1c02f862009-04-22 02:53:24 +0000609 llvm::Constant *getMessageSendFixupFn() {
610 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
611 std::vector<const llvm::Type*> Params;
612 Params.push_back(ObjectPtrTy);
613 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000614 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000615 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000616 "objc_msgSend_fixup");
617 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Chris Lattner1c02f862009-04-22 02:53:24 +0000619 llvm::Constant *getMessageSendFpretFixupFn() {
620 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
621 std::vector<const llvm::Type*> Params;
622 Params.push_back(ObjectPtrTy);
623 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000624 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000625 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000626 "objc_msgSend_fpret_fixup");
627 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Chris Lattner1c02f862009-04-22 02:53:24 +0000629 llvm::Constant *getMessageSendStretFixupFn() {
630 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
631 std::vector<const llvm::Type*> Params;
632 Params.push_back(ObjectPtrTy);
633 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000635 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000636 "objc_msgSend_stret_fixup");
637 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Chris Lattner1c02f862009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000641 // struct _super_message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(SuperPtrTy);
644 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000646 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000647 "objc_msgSendSuper2_fixup");
648 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Chris Lattner1c02f862009-04-22 02:53:24 +0000650 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000652 // struct _super_message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(SuperPtrTy);
655 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000656 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000657 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000658 "objc_msgSendSuper2_stret_fixup");
659 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Chris Lattner8a569112009-04-22 02:15:23 +0000661 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000662 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000663 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000664 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Chris Lattner8a569112009-04-22 02:15:23 +0000666 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Chris Lattner8a569112009-04-22 02:15:23 +0000668 llvm::Constant *getObjCBeginCatchFn() {
669 std::vector<const llvm::Type*> Params;
670 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000671 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000672 Params, false),
673 "objc_begin_catch");
674 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000675
676 const llvm::StructType *EHTypeTy;
677 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000678
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000679 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
680 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000681};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000682
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000683class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000684public:
685 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000686 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000687 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000688 unsigned ivar_bytepos;
689 unsigned ivar_size;
690 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000692
693 // Allow sorting based on byte pos.
694 bool operator<(const GC_IVAR &b) const {
695 return ivar_bytepos < b.ivar_bytepos;
696 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000697 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000698
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000699 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000700 public:
701 unsigned skip;
702 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000703 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000704 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000705 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000707protected:
708 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000709 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000710 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000711 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000713 // gc ivar layout bitmap calculation helper caches.
714 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
715 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000716
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000717 /// LazySymbols - Symbols to generate a lazy reference for. See
718 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000719 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000720
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000721 /// DefinedSymbols - External symbols which are defined by this
722 /// module. The symbols in this list and LazySymbols are used to add
723 /// special linker symbols which ensure that Objective-C modules are
724 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000725 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000726
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000727 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000728 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000729
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000730 /// MethodVarNames - uniqued method variable names.
731 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000733 /// DefinedCategoryNames - list of category names in form Class_Category.
734 llvm::SetVector<std::string> DefinedCategoryNames;
735
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000736 /// MethodVarTypes - uniqued method type signatures. We have to use
737 /// a StringMap here because have no other unique reference.
738 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000739
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000740 /// MethodDefinitions - map of methods which have been defined in
741 /// this translation unit.
742 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000743
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000744 /// PropertyNames - uniqued method variable names.
745 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000746
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000747 /// ClassReferences - uniqued class references.
748 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000750 /// SelectorReferences - uniqued selector references.
751 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000753 /// Protocols - Protocols for which an objc_protocol structure has
754 /// been emitted. Forward declarations are handled by creating an
755 /// empty structure whose initializer is filled in when/if defined.
756 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000758 /// DefinedProtocols - Protocols which have actually been
759 /// defined. We should not need this, see FIXME in GenerateProtocol.
760 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000762 /// DefinedClasses - List of defined classes.
763 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000764
765 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
766 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000767
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000768 /// DefinedCategories - List of defined categories.
769 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000771 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
772 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000774 /// GetNameForMethod - Return a name for the given method.
775 /// \param[out] NameOut - The return value.
776 void GetNameForMethod(const ObjCMethodDecl *OMD,
777 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000778 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000780 /// GetMethodVarName - Return a unique constant for the given
781 /// selector's name. The return value has type char *.
782 llvm::Constant *GetMethodVarName(Selector Sel);
783 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000784
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000785 /// GetMethodVarType - Return a unique constant for the given
786 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000788 // FIXME: This is a horrible name.
789 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000790 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000792 /// GetPropertyName - Return a unique constant for the given
793 /// name. The return value has type char *.
794 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000796 // FIXME: This can be dropped once string functions are unified.
797 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
798 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000800 /// GetClassName - Return a unique constant for the given selector's
801 /// name. The return value has type char *.
802 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000803
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000804 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
805
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000806 /// BuildIvarLayout - Builds ivar layout bitmap for the class
807 /// implementation for the __strong or __weak case.
808 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000809 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
810 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000811
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000812 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000813
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000814 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815 unsigned int BytePos, bool ForStrongLayout,
816 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000817 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000818 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000819 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000820 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000821 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000822 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000823
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000824 /// GetIvarLayoutName - Returns a unique constant for the given
825 /// ivar layout bitmap.
826 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
827 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000829 /// EmitPropertyList - Emit the given property list. The return
830 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000831 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000833 const ObjCContainerDecl *OCD,
834 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000835
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000836 /// PushProtocolProperties - Push protocol's property on the input stack.
837 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
838 std::vector<llvm::Constant*> &Properties,
839 const Decl *Container,
840 const ObjCProtocolDecl *PROTO,
841 const ObjCCommonTypesHelper &ObjCTypes);
842
Fariborz Jahanianda320092009-01-29 19:24:30 +0000843 /// GetProtocolRef - Return a reference to the internal protocol
844 /// description, creating an empty one if it has not been
845 /// defined. The return value has type ProtocolPtrTy.
846 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000847
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000848 /// CreateMetadataVar - Create a global variable with internal
849 /// linkage for use by the Objective-C runtime.
850 ///
851 /// This is a convenience wrapper which not only creates the
852 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000853 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000854 ///
855 /// \param Name - The variable name.
856 /// \param Init - The variable initializer; this is also used to
857 /// define the type of the variable.
858 /// \param Section - The section the variable should go into, or 0.
859 /// \param Align - The alignment for the variable, or 0.
860 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000861 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000862 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000863 llvm::Constant *Init,
864 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000865 unsigned Align,
866 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000867
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000868 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000869 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000870 QualType ResultType,
871 llvm::Value *Sel,
872 llvm::Value *Arg0,
873 QualType Arg0Ty,
874 bool IsSuper,
875 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000876 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000877 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000878
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000879 /// EmitImageInfo - Emit the image info marker used to encode some module
880 /// level information.
881 void EmitImageInfo();
882
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000883public:
Owen Anderson69243822009-07-13 04:10:07 +0000884 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000885 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
David Chisnall0d13f6f2010-01-23 02:40:42 +0000887 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000888
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000889 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
890 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000891
Fariborz Jahanianda320092009-01-29 19:24:30 +0000892 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Fariborz Jahanianda320092009-01-29 19:24:30 +0000894 /// GetOrEmitProtocol - Get the protocol object for the given
895 /// declaration, emitting it if necessary. The return value has type
896 /// ProtocolPtrTy.
897 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanianda320092009-01-29 19:24:30 +0000899 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
900 /// object for the given declaration, emitting it if needed. These
901 /// forward references will be filled in with empty bodies if no
902 /// definition is seen. The return value has type ProtocolPtrTy.
903 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000904 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
905 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000906
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000907};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000909class CGObjCMac : public CGObjCCommonMac {
910private:
911 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000912
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000913 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000915 void EmitModuleInfo();
916
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000917 /// EmitModuleSymols - Emit module symbols, the list of defined
918 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000919 llvm::Constant *EmitModuleSymbols();
920
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000921 /// FinishModule - Write out global data structures at the end of
922 /// processing a translation unit.
923 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000924
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000925 /// EmitClassExtension - Generate the class extension structure used
926 /// to store the weak ivar layout and properties. The return value
927 /// has type ClassExtensionPtrTy.
928 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
929
930 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
931 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000932 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000933 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000934
935 /// EmitSuperClassRef - Emits reference to class's main metadata class.
936 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000937
938 /// EmitIvarList - Emit the ivar list for the given
939 /// implementation. If ForClass is true the list of class ivars
940 /// (i.e. metaclass ivars) is emitted, otherwise the list of
941 /// interface ivars will be emitted. The return value has type
942 /// IvarListPtrTy.
943 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000944 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000945
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000946 /// EmitMetaClass - Emit a forward reference to the class structure
947 /// for the metaclass of the given interface. The return value has
948 /// type ClassPtrTy.
949 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
950
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000951 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000952 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000953 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
954 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000955 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000956
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000957 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000958
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960
961 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000962 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000963 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000964 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000965 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000966
967 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000968 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969 /// - TypeName: The name for the type containing the methods.
970 /// - IsProtocol: True iff these methods are for a protocol.
971 /// - ClassMethds: True iff these are class methods.
972 /// - Required: When true, only "required" methods are
973 /// listed. Similarly, when false only "optional" methods are
974 /// listed. For classes this should always be true.
975 /// - begin, end: The method list to output.
976 ///
977 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000978 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000979 const char *Section,
980 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000981
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000982 /// GetOrEmitProtocol - Get the protocol object for the given
983 /// declaration, emitting it if necessary. The return value has type
984 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000985 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000986
987 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
988 /// object for the given declaration, emitting it if needed. These
989 /// forward references will be filled in with empty bodies if no
990 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000991 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000992
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993 /// EmitProtocolExtension - Generate the protocol extension
994 /// structure used to store optional instance and class methods, and
995 /// protocol properties. The return value has type
996 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000997 llvm::Constant *
998 EmitProtocolExtension(const ObjCProtocolDecl *PD,
999 const ConstantVector &OptInstanceMethods,
1000 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001
1002 /// EmitProtocolList - Generate the list of referenced
1003 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001004 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001005 ObjCProtocolDecl::protocol_iterator begin,
1006 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001007
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001008 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1009 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001010 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1011 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001012
1013public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001014 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001015
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001016 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001017
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001018 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001019 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001020 QualType ResultType,
1021 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001022 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001023 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001024 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001025 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001026
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001028 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001029 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001030 QualType ResultType,
1031 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001032 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001033 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001034 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001035 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001036 const CallArgList &CallArgs,
1037 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001038
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001039 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001040 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001041
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001042 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1043 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001044
1045 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1046 /// untyped one.
1047 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1048 const ObjCMethodDecl *Method);
1049
John McCall5a180392010-07-24 00:37:23 +00001050 virtual llvm::Constant *GetEHType(QualType T);
1051
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001052 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001053
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001054 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001055
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001056 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001057 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001058
Chris Lattner74391b42009-03-22 21:03:39 +00001059 virtual llvm::Constant *GetPropertyGetFunction();
1060 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001061 virtual llvm::Constant *GetGetStructFunction();
1062 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001063 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001064
John McCallf1549f62010-07-06 01:34:17 +00001065 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1066 const ObjCAtTryStmt &S);
1067 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1068 const ObjCAtSynchronizedStmt &S);
1069 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001070 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1071 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001072 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001073 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001074 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001075 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001076 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001077 llvm::Value *src, llvm::Value *dest,
1078 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001079 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001080 llvm::Value *src, llvm::Value *dest,
1081 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001082 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1083 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001084 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1085 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001086 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001087
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001088 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1089 QualType ObjectTy,
1090 llvm::Value *BaseValue,
1091 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001092 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001093 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001094 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001095 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001096};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001097
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001098class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001099private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001100 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001101 llvm::GlobalVariable* ObjCEmptyCacheVar;
1102 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103
Daniel Dunbar11394522009-04-18 08:51:00 +00001104 /// SuperClassReferences - uniqued super class references.
1105 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001106
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001107 /// MetaClassReferences - uniqued meta class references.
1108 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001109
1110 /// EHTypeReferences - uniqued class ehtype references.
1111 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001113 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001114 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001115 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001117 /// DefinedMetaClasses - List of defined meta-classes.
1118 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1119
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001120 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001121 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001122 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001123
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001124 /// FinishNonFragileABIModule - Write out global data structures at the end of
1125 /// processing a translation unit.
1126 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001127
Daniel Dunbar463b8762009-05-15 21:48:48 +00001128 /// AddModuleClassList - Add the given list of class pointers to the
1129 /// module with the provided symbol and section names.
1130 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1131 const char *SymbolName,
1132 const char *SectionName);
1133
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001134 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1135 unsigned InstanceStart,
1136 unsigned InstanceSize,
1137 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001138 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001139 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001140 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001141 llvm::Constant *ClassRoGV,
1142 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001143
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001144 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001145
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001146 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001147
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001148 /// EmitMethodList - Emit the method list for the given
1149 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001150 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001151 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001152 const ConstantVector &Methods);
1153 /// EmitIvarList - Emit the ivar list for the given
1154 /// implementation. If ForClass is true the list of class ivars
1155 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1156 /// interface ivars will be emitted. The return value has type
1157 /// IvarListnfABIPtrTy.
1158 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001159
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001160 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001161 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001162 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001163
Fariborz Jahanianda320092009-01-29 19:24:30 +00001164 /// GetOrEmitProtocol - Get the protocol object for the given
1165 /// declaration, emitting it if necessary. The return value has type
1166 /// ProtocolPtrTy.
1167 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001168
Fariborz Jahanianda320092009-01-29 19:24:30 +00001169 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1170 /// object for the given declaration, emitting it if needed. These
1171 /// forward references will be filled in with empty bodies if no
1172 /// definition is seen. The return value has type ProtocolPtrTy.
1173 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174
Fariborz Jahanianda320092009-01-29 19:24:30 +00001175 /// EmitProtocolList - Generate the list of referenced
1176 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001177 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001178 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001179 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001180
Fariborz Jahanian46551122009-02-04 00:22:57 +00001181 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001182 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001183 QualType ResultType,
1184 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001185 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001186 QualType Arg0Ty,
1187 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001188 const CallArgList &CallArgs,
1189 const ObjCMethodDecl *Method);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001190
1191 /// GetClassGlobal - Return the global variable for the Objective-C
1192 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001193 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001194
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001195 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001196 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001198 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001199
Daniel Dunbar11394522009-04-18 08:51:00 +00001200 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1201 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1203 const ObjCInterfaceDecl *ID);
1204
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001205 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1206 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001207 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001208 const ObjCInterfaceDecl *ID);
1209
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001210 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1211 /// the given ivar.
1212 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001213 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214 const ObjCInterfaceDecl *ID,
1215 const ObjCIvarDecl *Ivar);
1216
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001217 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1218 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001219 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1220 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001221
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001222 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001223 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001224 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001225 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001226
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001227 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001228 return "OBJC_METACLASS_$_";
1229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001230
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001231 const char *getClassSymbolPrefix() const {
1232 return "OBJC_CLASS_$_";
1233 }
1234
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001235 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001236 uint32_t &InstanceStart,
1237 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001239 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001240 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001241 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1242 return CGM.getContext().Selectors.getSelector(0, &II);
1243 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001245 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001246 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1247 return CGM.getContext().Selectors.getSelector(1, &II);
1248 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001249
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001250 /// ImplementationIsNonLazy - Check whether the given category or
1251 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001252 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001253
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001254public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001255 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001256 // FIXME. All stubs for now!
1257 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001258
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001259 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001260 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001261 QualType ResultType,
1262 Selector Sel,
1263 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001264 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001265 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001266 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001267
1268 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001269 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001270 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001271 QualType ResultType,
1272 Selector Sel,
1273 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001274 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001275 llvm::Value *Receiver,
1276 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001277 const CallArgList &CallArgs,
1278 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001280 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001281 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001282
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001283 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1284 bool lvalue = false)
1285 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001286
1287 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1288 /// untyped one.
1289 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1290 const ObjCMethodDecl *Method)
1291 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001293 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001295 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001296 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001297 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001298
John McCall5a180392010-07-24 00:37:23 +00001299 virtual llvm::Constant *GetEHType(QualType T);
1300
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001302 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001303 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001304 virtual llvm::Constant *GetPropertySetFunction() {
1305 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001306 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001307
David Chisnall8fac25d2010-12-26 22:13:16 +00001308 virtual llvm::Constant *GetSetStructFunction() {
1309 return ObjCTypes.getCopyStructFn();
1310 }
1311 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001312 return ObjCTypes.getCopyStructFn();
1313 }
1314
Chris Lattner74391b42009-03-22 21:03:39 +00001315 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001316 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001317 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001318
John McCallf1549f62010-07-06 01:34:17 +00001319 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1320 const ObjCAtTryStmt &S);
1321 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1322 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001323 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001324 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001325 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001326 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001327 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001328 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001329 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001330 llvm::Value *src, llvm::Value *dest,
1331 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001333 llvm::Value *src, llvm::Value *dest,
1334 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001335 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001336 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001337 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1338 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001339 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001340 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1341 QualType ObjectTy,
1342 llvm::Value *BaseValue,
1343 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001344 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001345 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001346 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001347 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001348};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001350} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001351
1352/* *** Helper Functions *** */
1353
1354/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001355static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001356 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001357 unsigned idx0,
1358 unsigned idx1) {
1359 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001360 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1361 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001362 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001363 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001364}
1365
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001366/// hasObjCExceptionAttribute - Return true if this class or any super
1367/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001368static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001369 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001370 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001371 return true;
1372 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001373 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001374 return false;
1375}
1376
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001377/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001379CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001380 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001381 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001382 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001383}
1384
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001385/// GetClass - Return a reference to the class for the given interface
1386/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001387llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001388 const ObjCInterfaceDecl *ID) {
1389 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001390}
1391
1392/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001393llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1394 bool lval) {
1395 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001396}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001397llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001398 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001399 return EmitSelector(Builder, Method->getSelector());
1400}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001401
John McCall5a180392010-07-24 00:37:23 +00001402llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1403 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1404 return 0;
1405}
1406
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001407/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408/*
1409 struct __builtin_CFString {
1410 const int *isa; // point to __CFConstantStringClassReference
1411 int flags;
1412 const char *str;
1413 long length;
1414 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001415*/
1416
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001417/// or Generate a constant NSString object.
1418/*
1419 struct __builtin_NSString {
1420 const int *isa; // point to __NSConstantStringClassReference
1421 const char *str;
1422 unsigned int length;
1423 };
1424*/
1425
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001427 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001428 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1429 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001430 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001431}
1432
1433/// Generates a message send where the super is the receiver. This is
1434/// a message send to self with special delivery semantics indicating
1435/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001436CodeGen::RValue
1437CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001438 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001439 QualType ResultType,
1440 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001441 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001442 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001443 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001444 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001445 const CodeGen::CallArgList &CallArgs,
1446 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001447 // Create and init a super structure; this is a (receiver, class)
1448 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001450 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001451 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001452 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001453 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001454 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001455
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001456 // If this is a class message the metaclass is passed as the target.
1457 llvm::Value *Target;
1458 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001459 if (isCategoryImpl) {
1460 // Message sent to 'super' in a class method defined in a category
1461 // implementation requires an odd treatment.
1462 // If we are in a class method, we must retrieve the
1463 // _metaclass_ for the current class, pointed at by
1464 // the class's "isa" pointer. The following assumes that
1465 // isa" is the first ivar in a class (which it must be).
1466 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1467 Target = CGF.Builder.CreateStructGEP(Target, 0);
1468 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001469 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001470 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1471 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1472 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1473 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001474 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001475 }
1476 else if (isCategoryImpl)
1477 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1478 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001479 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1480 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1481 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001482 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001483 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1484 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001485 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001486 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001487 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001488 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001489 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001490 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001491 EmitSelector(CGF.Builder, Sel),
1492 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001493 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001495
1496/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001497CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001498 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001499 QualType ResultType,
1500 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001501 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001502 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001503 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001504 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001505 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001506 EmitSelector(CGF.Builder, Sel),
1507 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001508 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001509}
1510
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001511CodeGen::RValue
1512CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001513 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001514 QualType ResultType,
1515 llvm::Value *Sel,
1516 llvm::Value *Arg0,
1517 QualType Arg0Ty,
1518 bool IsSuper,
1519 const CallArgList &CallArgs,
1520 const ObjCMethodDecl *Method,
1521 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001522 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001523 if (!IsSuper)
1524 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman04c9a492011-05-02 17:57:46 +00001525 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1526 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001527 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001528
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001529 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001530 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001531 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001532 const llvm::FunctionType *FTy =
1533 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001534
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001535 if (Method)
1536 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1537 CGM.getContext().getCanonicalType(ResultType) &&
1538 "Result type mismatch!");
1539
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001540 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001541 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00001542 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001543 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001544 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001545 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1546 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1547 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001548 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001549 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001550 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001551 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001552 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001553 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001554}
1555
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001556static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1557 if (FQT.isObjCGCStrong())
1558 return Qualifiers::Strong;
1559
1560 if (FQT.isObjCGCWeak())
1561 return Qualifiers::Weak;
1562
1563 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1564 return Qualifiers::Strong;
1565
1566 if (const PointerType *PT = FQT->getAs<PointerType>())
1567 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1568
1569 return Qualifiers::GCNone;
1570}
1571
John McCall6b5a61b2011-02-07 10:33:21 +00001572llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1573 const CGBlockInfo &blockInfo) {
1574 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001575 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001576
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001577 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall6b5a61b2011-02-07 10:33:21 +00001578 return nullPtr;
1579
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001580 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001581 SkipIvars.clear();
1582 IvarsInfo.clear();
1583 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1584 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1585
Fariborz Jahanian81979822010-09-09 00:21:45 +00001586 // __isa is the first field in block descriptor and must assume by runtime's
1587 // convention that it is GC'able.
1588 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001589
1590 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1591
1592 // Calculate the basic layout of the block structure.
1593 const llvm::StructLayout *layout =
1594 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1595
1596 // Ignore the optional 'this' capture: C++ objects are not assumed
1597 // to be GC'ed.
1598
1599 // Walk the captured variables.
1600 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1601 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1602 const VarDecl *variable = ci->getVariable();
1603 QualType type = variable->getType();
1604
1605 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1606
1607 // Ignore constant captures.
1608 if (capture.isConstant()) continue;
1609
1610 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1611
1612 // __block variables are passed by their descriptor address.
1613 if (ci->isByRef()) {
1614 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001615 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001616 }
1617
1618 assert(!type->isArrayType() && "array variable should not be caught");
1619 if (const RecordType *record = type->getAs<RecordType>()) {
1620 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001621 continue;
1622 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001623
John McCall6b5a61b2011-02-07 10:33:21 +00001624 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1625 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1626
1627 if (GCAttr == Qualifiers::Strong)
1628 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1629 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001630 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001631 SkipIvars.push_back(GC_IVAR(fieldOffset,
1632 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001633 }
1634
1635 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001636 return nullPtr;
1637
1638 // Sort on byte position; captures might not be allocated in order,
1639 // and unions can do funny things.
1640 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1641 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001642
1643 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001644 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001645 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1646 printf("\n block variable layout for block: ");
1647 const unsigned char *s = (unsigned char*)BitMap.c_str();
1648 for (unsigned i = 0; i < BitMap.size(); i++)
1649 if (!(s[i] & 0xf0))
1650 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1651 else
1652 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1653 printf("\n");
1654 }
1655
1656 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001657}
1658
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001659llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001660 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001661 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001662 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001663 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1664
Owen Anderson3c4972d2009-07-29 18:54:39 +00001665 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001666 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001667}
1668
Fariborz Jahanianda320092009-01-29 19:24:30 +00001669void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001670 // FIXME: We shouldn't need this, the protocol decl should contain enough
1671 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001672 DefinedProtocols.insert(PD->getIdentifier());
1673
1674 // If we have generated a forward reference to this protocol, emit
1675 // it now. Otherwise do nothing, the protocol objects are lazily
1676 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001677 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001678 GetOrEmitProtocol(PD);
1679}
1680
Fariborz Jahanianda320092009-01-29 19:24:30 +00001681llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001682 if (DefinedProtocols.count(PD->getIdentifier()))
1683 return GetOrEmitProtocol(PD);
1684 return GetOrEmitProtocolRef(PD);
1685}
1686
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001687/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001688// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1689struct _objc_protocol {
1690struct _objc_protocol_extension *isa;
1691char *protocol_name;
1692struct _objc_protocol_list *protocol_list;
1693struct _objc__method_prototype_list *instance_methods;
1694struct _objc__method_prototype_list *class_methods
1695};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001696
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001697See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001698*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001699llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1700 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1701
1702 // Early exit if a defining object has already been generated.
1703 if (Entry && Entry->hasInitializer())
1704 return Entry;
1705
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001706 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001707 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001708 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1709
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001710 // Construct method lists.
1711 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1712 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001713 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001714 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001715 ObjCMethodDecl *MD = *i;
1716 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1717 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1718 OptInstanceMethods.push_back(C);
1719 } else {
1720 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001721 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001722 }
1723
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001724 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001725 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001726 ObjCMethodDecl *MD = *i;
1727 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1728 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1729 OptClassMethods.push_back(C);
1730 } else {
1731 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001732 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001733 }
1734
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001736 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001737 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001738 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001739 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001740 PD->protocol_begin(),
1741 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001742 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001743 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001744 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1745 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001746 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001747 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001748 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1749 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001750 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001751 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001752
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001753 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001754 // Already created, fix the linkage and update the initializer.
1755 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001756 Entry->setInitializer(Init);
1757 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001758 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001759 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001760 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001761 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001762 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001763 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001764 // FIXME: Is this necessary? Why only for protocol?
1765 Entry->setAlignment(4);
1766 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001767 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001768
1769 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001770}
1771
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001772llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1774
1775 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001776 // We use the initializer as a marker of whether this is a forward
1777 // reference or not. At module finalization we add the empty
1778 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001779 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001780 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001781 llvm::GlobalValue::ExternalLinkage,
1782 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001783 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001784 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001785 // FIXME: Is this necessary? Why only for protocol?
1786 Entry->setAlignment(4);
1787 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001788
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001789 return Entry;
1790}
1791
1792/*
1793 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001794 uint32_t size;
1795 struct objc_method_description_list *optional_instance_methods;
1796 struct objc_method_description_list *optional_class_methods;
1797 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001798 };
1799*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001800llvm::Constant *
1801CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1802 const ConstantVector &OptInstanceMethods,
1803 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001804 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001805 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001806 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001807 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001808 Values[1] =
1809 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001810 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001811 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1812 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001813 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001814 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001815 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1816 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001817 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001818 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001819
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001820 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001821 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001823 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001825 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001826 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001827
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001828 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001829 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001830 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001831 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001832}
1833
1834/*
1835 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001836 struct objc_protocol_list *next;
1837 long count;
1838 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001839 };
1840*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001841llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001842CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001843 ObjCProtocolDecl::protocol_iterator begin,
1844 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001845 std::vector<llvm::Constant*> ProtocolRefs;
1846
Daniel Dunbardbc933702008-08-21 21:57:41 +00001847 for (; begin != end; ++begin)
1848 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001849
1850 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001851 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001852 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001854 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001855 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001856
1857 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001858 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001859 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001860 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001861 ProtocolRefs.size() - 1);
1862 Values[2] =
1863 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1864 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001866
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001867 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001868 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001869 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001870 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001871 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001872}
1873
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001874void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1875 std::vector<llvm::Constant*> &Properties,
1876 const Decl *Container,
1877 const ObjCProtocolDecl *PROTO,
1878 const ObjCCommonTypesHelper &ObjCTypes) {
1879 std::vector<llvm::Constant*> Prop(2);
1880 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1881 E = PROTO->protocol_end(); P != E; ++P)
1882 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1883 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1884 E = PROTO->prop_end(); I != E; ++I) {
1885 const ObjCPropertyDecl *PD = *I;
1886 if (!PropertySet.insert(PD->getIdentifier()))
1887 continue;
1888 Prop[0] = GetPropertyName(PD->getIdentifier());
1889 Prop[1] = GetPropertyTypeString(PD, Container);
1890 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1891 }
1892}
1893
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001895 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001896 const char * const name;
1897 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001898 };
1899
1900 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001901 uint32_t entsize; // sizeof (struct _objc_property)
1902 uint32_t prop_count;
1903 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001904 };
1905*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001906llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 const Decl *Container,
1908 const ObjCContainerDecl *OCD,
1909 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001910 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001911 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001912 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1913 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001914 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001915 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001916 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001917 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001918 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001919 Prop));
1920 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001921 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001922 for (ObjCInterfaceDecl::all_protocol_iterator
1923 P = OID->all_referenced_protocol_begin(),
1924 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001925 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1926 ObjCTypes);
1927 }
1928 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1929 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1930 E = CD->protocol_end(); P != E; ++P)
1931 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1932 ObjCTypes);
1933 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001934
1935 // Return null for empty list.
1936 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001937 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001938
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001939 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001940 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001941 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001942 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1943 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001944 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001945 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001946 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001947 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001948
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001949 llvm::GlobalVariable *GV =
1950 CreateMetadataVar(Name, Init,
1951 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001952 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001953 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001954 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001955 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001956}
1957
1958/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001959 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001960 int count;
1961 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001962 };
1963*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001964llvm::Constant *
1965CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1966 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001967 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1969 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001970 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001971 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001972 Desc);
1973}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001974
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001975llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001976 const char *Section,
1977 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001978 // Return null for empty list.
1979 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001980 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001981
1982 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001983 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001984 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001985 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001986 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001987 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001988
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001989 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001990 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001991 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001992}
1993
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001994/*
1995 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 char *category_name;
1997 char *class_name;
1998 struct _objc_method_list *instance_methods;
1999 struct _objc_method_list *class_methods;
2000 struct _objc_protocol_list *protocols;
2001 uint32_t size; // <rdar://4585769>
2002 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002003 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002004*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002005void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002006 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002007
Mike Stumpf5408fe2009-05-16 07:57:57 +00002008 // FIXME: This is poor design, the OCD should have a pointer to the category
2009 // decl. Additionally, note that Category can be null for the @implementation
2010 // w/o an @interface case. Sema should just create one for us as it does for
2011 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002012 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002013 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002014 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002015
2016 llvm::SmallString<256> ExtName;
2017 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2018 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002019
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002020 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002021 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002022 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002023 // Instance methods should always be defined.
2024 InstanceMethods.push_back(GetMethodConstant(*i));
2025 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002026 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002027 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002028 // Class methods should always be defined.
2029 ClassMethods.push_back(GetMethodConstant(*i));
2030 }
2031
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002032 std::vector<llvm::Constant*> Values(7);
2033 Values[0] = GetClassName(OCD->getIdentifier());
2034 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002035 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002036 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002037 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002038 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002039 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002040 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002041 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002042 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002043 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002044 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002045 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002046 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002047 Category->protocol_begin(),
2048 Category->protocol_end());
2049 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002050 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002051 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002052 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002053
2054 // If there is no category @interface then there can be no properties.
2055 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002056 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002057 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002058 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002059 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002060 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002061
Owen Anderson08e25242009-07-27 22:29:56 +00002062 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002063 Values);
2064
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002065 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002066 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002067 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002068 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002069 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002070 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002071 // method definition entries must be clear for next implementation.
2072 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002073}
2074
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002075// FIXME: Get from somewhere?
2076enum ClassFlags {
2077 eClassFlags_Factory = 0x00001,
2078 eClassFlags_Meta = 0x00002,
2079 // <rdr://5142207>
2080 eClassFlags_HasCXXStructors = 0x02000,
2081 eClassFlags_Hidden = 0x20000,
2082 eClassFlags_ABI2_Hidden = 0x00010,
2083 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2084};
2085
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002086/*
2087 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002088 Class isa;
2089 Class super_class;
2090 const char *name;
2091 long version;
2092 long info;
2093 long instance_size;
2094 struct _objc_ivar_list *ivars;
2095 struct _objc_method_list *methods;
2096 struct _objc_cache *cache;
2097 struct _objc_protocol_list *protocols;
2098 // Objective-C 1.0 extensions (<rdr://4585769>)
2099 const char *ivar_layout;
2100 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101 };
2102
2103 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002105void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002106 DefinedSymbols.insert(ID->getIdentifier());
2107
Chris Lattner8ec03f52008-11-24 03:54:41 +00002108 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002109 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002110 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002111 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002112 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002113 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002114 Interface->all_referenced_protocol_begin(),
2115 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002116 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002117 if (ID->getNumIvarInitializers())
2118 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002119 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002120 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002121
2122 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002123 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002124 Flags |= eClassFlags_Hidden;
2125
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002126 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002127 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002128 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002129 // Instance methods should always be defined.
2130 InstanceMethods.push_back(GetMethodConstant(*i));
2131 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002132 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002133 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002134 // Class methods should always be defined.
2135 ClassMethods.push_back(GetMethodConstant(*i));
2136 }
2137
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002138 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002139 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002140 ObjCPropertyImplDecl *PID = *i;
2141
2142 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2143 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2144
2145 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2146 if (llvm::Constant *C = GetMethodConstant(MD))
2147 InstanceMethods.push_back(C);
2148 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2149 if (llvm::Constant *C = GetMethodConstant(MD))
2150 InstanceMethods.push_back(C);
2151 }
2152 }
2153
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002154 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002155 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002156 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002157 // Record a reference to the super class.
2158 LazySymbols.insert(Super->getIdentifier());
2159
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002160 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002161 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002162 ObjCTypes.ClassPtrTy);
2163 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002164 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002165 }
2166 Values[ 2] = GetClassName(ID->getIdentifier());
2167 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002168 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2169 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2170 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002171 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002172 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002173 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002174 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002175 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002176 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002177 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002178 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002179 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002180 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002181 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002183 std::string Name("\01L_OBJC_CLASS_");
2184 Name += ClassName;
2185 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2186 // Check for a forward reference.
2187 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2188 if (GV) {
2189 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2190 "Forward metaclass reference has incorrect type.");
2191 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2192 GV->setInitializer(Init);
2193 GV->setSection(Section);
2194 GV->setAlignment(4);
2195 CGM.AddUsedGlobal(GV);
2196 }
2197 else
2198 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002200 // method definition entries must be clear for next implementation.
2201 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002202}
2203
2204llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2205 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002206 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002208 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002209
John McCall1fb0caa2010-10-22 21:05:15 +00002210 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002211 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002212
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002213 std::vector<llvm::Constant*> Values(12);
2214 // The isa for the metaclass is the root of the hierarchy.
2215 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2216 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2217 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002218 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002219 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002220 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002221 // The super class for the metaclass is emitted as the name of the
2222 // super class. The runtime fixes this up to point to the
2223 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002225 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002226 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 ObjCTypes.ClassPtrTy);
2228 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002229 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230 }
2231 Values[ 2] = GetClassName(ID->getIdentifier());
2232 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002233 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2234 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2235 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002236 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002237 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002238 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002239 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002240 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002241 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002242 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243 Values[ 9] = Protocols;
2244 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002245 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002247 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002248 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002249 Values);
2250
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002251 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002252 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002253
2254 // Check for a forward reference.
2255 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2256 if (GV) {
2257 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2258 "Forward metaclass reference has incorrect type.");
2259 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2260 GV->setInitializer(Init);
2261 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002262 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002263 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002264 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002265 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002267 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002268 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002269
2270 return GV;
2271}
2272
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002273llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002274 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002275
Mike Stumpf5408fe2009-05-16 07:57:57 +00002276 // FIXME: Should we look these up somewhere other than the module. Its a bit
2277 // silly since we only generate these while processing an implementation, so
2278 // exactly one pointer would work if know when we entered/exitted an
2279 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002280
2281 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002282 // Previously, metaclass with internal linkage may have been defined.
2283 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002284 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2285 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002286 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2287 "Forward metaclass reference has incorrect type.");
2288 return GV;
2289 } else {
2290 // Generate as an external reference to keep a consistent
2291 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002292 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002293 llvm::GlobalValue::ExternalLinkage,
2294 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002295 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002296 }
2297}
2298
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002299llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2300 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2301
2302 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2303 true)) {
2304 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2305 "Forward class metadata reference has incorrect type.");
2306 return GV;
2307 } else {
2308 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2309 llvm::GlobalValue::ExternalLinkage,
2310 0,
2311 Name);
2312 }
2313}
2314
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315/*
2316 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002317 uint32_t size;
2318 const char *weak_ivar_layout;
2319 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002320 };
2321*/
2322llvm::Constant *
2323CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002324 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002325 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326
2327 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002328 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002329 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002330 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002331 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332
2333 // Return null if no extension bits are used.
2334 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002335 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002338 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002339 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002340 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002341 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002342}
2343
2344/*
2345 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002346 char *ivar_name;
2347 char *ivar_type;
2348 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002349 };
2350
2351 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002352 int ivar_count;
2353 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002355*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002356llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002357 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358 std::vector<llvm::Constant*> Ivars, Ivar(3);
2359
2360 // When emitting the root class GCC emits ivar entries for the
2361 // actual class structure. It is not clear if we need to follow this
2362 // behavior; for now lets try and get away with not doing it. If so,
2363 // the cleanest solution would be to make up an ObjCInterfaceDecl
2364 // for the class.
2365 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002366 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002367
2368 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002369 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002371 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002372 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002373
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002374 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2375 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002376 // Ignore unnamed bit-fields.
2377 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002378 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002379 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2380 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002381 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002382 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002383 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002384 }
2385
2386 // Return null for empty list.
2387 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002388 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002389
2390 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002391 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002392 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002394 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002395 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002397 llvm::GlobalVariable *GV;
2398 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002399 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002400 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002401 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002402 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002403 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002404 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002405 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002406 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002407}
2408
2409/*
2410 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002411 SEL method_name;
2412 char *method_types;
2413 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002414 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002415
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002417 struct objc_method_list *obsolete;
2418 int count;
2419 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002420 };
2421*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002422
2423/// GetMethodConstant - Return a struct objc_method constant for the
2424/// given method if it has been defined. The result is null if the
2425/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002426llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002427 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002428 if (!Fn)
2429 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002430
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002431 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002432 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002433 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002434 ObjCTypes.SelectorPtrTy);
2435 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002436 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002437 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002438}
2439
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002440llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002441 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002442 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002443 // Return null for empty list.
2444 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002445 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446
2447 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002448 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002449 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002450 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002452 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002453 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002455 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002456 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002457 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002458}
2459
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002460llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002461 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002462 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002463 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002464
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002465 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002466 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002467 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002469 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002470 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002471 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002472 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002473 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002474
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002475 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002476}
2477
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002478llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002479CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002480 llvm::Constant *Init,
2481 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002482 unsigned Align,
2483 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002484 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002485 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002486 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002487 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002488 if (Section)
2489 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002490 if (Align)
2491 GV->setAlignment(Align);
2492 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002493 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002494 return GV;
2495}
2496
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002498 // Abuse this interface function as a place to finalize.
2499 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002500 return NULL;
2501}
2502
Chris Lattner74391b42009-03-22 21:03:39 +00002503llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002504 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002505}
2506
Chris Lattner74391b42009-03-22 21:03:39 +00002507llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002508 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002509}
2510
David Chisnall8fac25d2010-12-26 22:13:16 +00002511llvm::Constant *CGObjCMac::GetGetStructFunction() {
2512 return ObjCTypes.getCopyStructFn();
2513}
2514llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002515 return ObjCTypes.getCopyStructFn();
2516}
2517
Chris Lattner74391b42009-03-22 21:03:39 +00002518llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002519 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002520}
2521
John McCallf1549f62010-07-06 01:34:17 +00002522void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2523 return EmitTryOrSynchronizedStmt(CGF, S);
2524}
2525
2526void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2527 const ObjCAtSynchronizedStmt &S) {
2528 return EmitTryOrSynchronizedStmt(CGF, S);
2529}
2530
John McCallcc505292010-07-21 06:59:36 +00002531namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002532 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002533 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002534 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002535 llvm::Value *CallTryExitVar;
2536 llvm::Value *ExceptionData;
2537 ObjCTypesHelper &ObjCTypes;
2538 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002539 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002540 llvm::Value *CallTryExitVar,
2541 llvm::Value *ExceptionData,
2542 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002543 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002544 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2545
2546 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2547 // Check whether we need to call objc_exception_try_exit.
2548 // In optimized code, this branch will always be folded.
2549 llvm::BasicBlock *FinallyCallExit =
2550 CGF.createBasicBlock("finally.call_exit");
2551 llvm::BasicBlock *FinallyNoCallExit =
2552 CGF.createBasicBlock("finally.no_call_exit");
2553 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2554 FinallyCallExit, FinallyNoCallExit);
2555
2556 CGF.EmitBlock(FinallyCallExit);
2557 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2558 ->setDoesNotThrow();
2559
2560 CGF.EmitBlock(FinallyNoCallExit);
2561
2562 if (isa<ObjCAtTryStmt>(S)) {
2563 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002564 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2565 // Save the current cleanup destination in case there's
2566 // control flow inside the finally statement.
2567 llvm::Value *CurCleanupDest =
2568 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2569
John McCallcc505292010-07-21 06:59:36 +00002570 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2571
John McCalld96a8e72010-08-11 00:16:14 +00002572 if (CGF.HaveInsertPoint()) {
2573 CGF.Builder.CreateStore(CurCleanupDest,
2574 CGF.getNormalCleanupDestSlot());
2575 } else {
2576 // Currently, the end of the cleanup must always exist.
2577 CGF.EnsureInsertPoint();
2578 }
2579 }
John McCallcc505292010-07-21 06:59:36 +00002580 } else {
2581 // Emit objc_sync_exit(expr); as finally's sole statement for
2582 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002583 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002584 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2585 ->setDoesNotThrow();
2586 }
2587 }
2588 };
John McCall87bb5822010-07-31 23:20:56 +00002589
2590 class FragileHazards {
2591 CodeGenFunction &CGF;
2592 llvm::SmallVector<llvm::Value*, 20> Locals;
2593 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2594
2595 llvm::InlineAsm *ReadHazard;
2596 llvm::InlineAsm *WriteHazard;
2597
2598 llvm::FunctionType *GetAsmFnType();
2599
2600 void collectLocals();
2601 void emitReadHazard(CGBuilderTy &Builder);
2602
2603 public:
2604 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002605
John McCall87bb5822010-07-31 23:20:56 +00002606 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002607 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002608 };
2609}
2610
2611/// Create the fragile-ABI read and write hazards based on the current
2612/// state of the function, which is presumed to be immediately prior
2613/// to a @try block. These hazards are used to maintain correct
2614/// semantics in the face of optimization and the fragile ABI's
2615/// cavalier use of setjmp/longjmp.
2616FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2617 collectLocals();
2618
2619 if (Locals.empty()) return;
2620
2621 // Collect all the blocks in the function.
2622 for (llvm::Function::iterator
2623 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2624 BlocksBeforeTry.insert(&*I);
2625
2626 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2627
2628 // Create a read hazard for the allocas. This inhibits dead-store
2629 // optimizations and forces the values to memory. This hazard is
2630 // inserted before any 'throwing' calls in the protected scope to
2631 // reflect the possibility that the variables might be read from the
2632 // catch block if the call throws.
2633 {
2634 std::string Constraint;
2635 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2636 if (I) Constraint += ',';
2637 Constraint += "*m";
2638 }
2639
2640 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2641 }
2642
2643 // Create a write hazard for the allocas. This inhibits folding
2644 // loads across the hazard. This hazard is inserted at the
2645 // beginning of the catch path to reflect the possibility that the
2646 // variables might have been written within the protected scope.
2647 {
2648 std::string Constraint;
2649 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2650 if (I) Constraint += ',';
2651 Constraint += "=*m";
2652 }
2653
2654 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2655 }
2656}
2657
2658/// Emit a write hazard at the current location.
2659void FragileHazards::emitWriteHazard() {
2660 if (Locals.empty()) return;
2661
2662 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2663 ->setDoesNotThrow();
2664}
2665
John McCall87bb5822010-07-31 23:20:56 +00002666void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2667 assert(!Locals.empty());
2668 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2669 ->setDoesNotThrow();
2670}
2671
2672/// Emit read hazards in all the protected blocks, i.e. all the blocks
2673/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002674void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002675 if (Locals.empty()) return;
2676
2677 CGBuilderTy Builder(CGF.getLLVMContext());
2678
2679 // Iterate through all blocks, skipping those prior to the try.
2680 for (llvm::Function::iterator
2681 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2682 llvm::BasicBlock &BB = *FI;
2683 if (BlocksBeforeTry.count(&BB)) continue;
2684
2685 // Walk through all the calls in the block.
2686 for (llvm::BasicBlock::iterator
2687 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2688 llvm::Instruction &I = *BI;
2689
2690 // Ignore instructions that aren't non-intrinsic calls.
2691 // These are the only calls that can possibly call longjmp.
2692 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2693 if (isa<llvm::IntrinsicInst>(I))
2694 continue;
2695
2696 // Ignore call sites marked nounwind. This may be questionable,
2697 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2698 llvm::CallSite CS(&I);
2699 if (CS.doesNotThrow()) continue;
2700
John McCall0b251722010-08-04 05:59:32 +00002701 // Insert a read hazard before the call. This will ensure that
2702 // any writes to the locals are performed before making the
2703 // call. If the call throws, then this is sufficient to
2704 // guarantee correctness as long as it doesn't also write to any
2705 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002706 Builder.SetInsertPoint(&BB, BI);
2707 emitReadHazard(Builder);
2708 }
2709 }
2710}
2711
2712static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2713 if (V) S.insert(V);
2714}
2715
2716void FragileHazards::collectLocals() {
2717 // Compute a set of allocas to ignore.
2718 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2719 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2720 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2721 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2722
2723 // Collect all the allocas currently in the function. This is
2724 // probably way too aggressive.
2725 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2726 for (llvm::BasicBlock::iterator
2727 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2728 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2729 Locals.push_back(&*I);
2730}
2731
2732llvm::FunctionType *FragileHazards::GetAsmFnType() {
2733 std::vector<const llvm::Type *> Tys(Locals.size());
2734 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2735 Tys[I] = Locals[I]->getType();
2736 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002737}
2738
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002739/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002740
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002741 Objective-C setjmp-longjmp (sjlj) Exception Handling
2742 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002743
John McCallf1549f62010-07-06 01:34:17 +00002744 A catch buffer is a setjmp buffer plus:
2745 - a pointer to the exception that was caught
2746 - a pointer to the previous exception data buffer
2747 - two pointers of reserved storage
2748 Therefore catch buffers form a stack, with a pointer to the top
2749 of the stack kept in thread-local storage.
2750
2751 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2752 objc_exception_try_exit pops the given catch buffer, which is
2753 required to be the top of the EH stack.
2754 objc_exception_throw pops the top of the EH stack, writes the
2755 thrown exception into the appropriate field, and longjmps
2756 to the setjmp buffer. It crashes the process (with a printf
2757 and an abort()) if there are no catch buffers on the stack.
2758 objc_exception_extract just reads the exception pointer out of the
2759 catch buffer.
2760
2761 There's no reason an implementation couldn't use a light-weight
2762 setjmp here --- something like __builtin_setjmp, but API-compatible
2763 with the heavyweight setjmp. This will be more important if we ever
2764 want to implement correct ObjC/C++ exception interactions for the
2765 fragile ABI.
2766
2767 Note that for this use of setjmp/longjmp to be correct, we may need
2768 to mark some local variables volatile: if a non-volatile local
2769 variable is modified between the setjmp and the longjmp, it has
2770 indeterminate value. For the purposes of LLVM IR, it may be
2771 sufficient to make loads and stores within the @try (to variables
2772 declared outside the @try) volatile. This is necessary for
2773 optimized correctness, but is not currently being done; this is
2774 being tracked as rdar://problem/8160285
2775
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002776 The basic framework for a @try-catch-finally is as follows:
2777 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002778 objc_exception_data d;
2779 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002780 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002781
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002782 objc_exception_try_enter(&d);
2783 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002784 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002785 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002786 // exception path
2787 id _caught = objc_exception_extract(&d);
2788
2789 // enter new try scope for handlers
2790 if (!setjmp(d.jmp_buf)) {
2791 ... match exception and execute catch blocks ...
2792
2793 // fell off end, rethrow.
2794 _rethrow = _caught;
2795 ... jump-through-finally to finally_rethrow ...
2796 } else {
2797 // exception in catch block
2798 _rethrow = objc_exception_extract(&d);
2799 _call_try_exit = false;
2800 ... jump-through-finally to finally_rethrow ...
2801 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002802 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002803 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002804
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002805 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002806 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002807 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002808
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002809 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002810 ... dispatch to finally destination ...
2811
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002812 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002813 objc_exception_throw(_rethrow);
2814
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002815 finally_end:
2816 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002817
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002818 This framework differs slightly from the one gcc uses, in that gcc
2819 uses _rethrow to determine if objc_exception_try_exit should be called
2820 and if the object should be rethrown. This breaks in the face of
2821 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002822
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002823 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002825 - If there are no catch blocks, then we avoid emitting the second
2826 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002827
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002828 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2829 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002830
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831 - FIXME: If there is no @finally block we can do a few more
2832 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002833
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002834 Rethrows and Jumps-Through-Finally
2835 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836
John McCallf1549f62010-07-06 01:34:17 +00002837 '@throw;' is supported by pushing the currently-caught exception
2838 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002839
John McCallf1549f62010-07-06 01:34:17 +00002840 Branches through the @finally block are handled with an ordinary
2841 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2842 exceptions are not compatible with C++ exceptions, and this is
2843 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002844
John McCallf1549f62010-07-06 01:34:17 +00002845 @synchronized(expr) { stmt; } is emitted as if it were:
2846 id synch_value = expr;
2847 objc_sync_enter(synch_value);
2848 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002849*/
2850
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002851void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2852 const Stmt &S) {
2853 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002854
2855 // A destination for the fall-through edges of the catch handlers to
2856 // jump to.
2857 CodeGenFunction::JumpDest FinallyEnd =
2858 CGF.getJumpDestInCurrentScope("finally.end");
2859
2860 // A destination for the rethrow edge of the catch handlers to jump
2861 // to.
2862 CodeGenFunction::JumpDest FinallyRethrow =
2863 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864
Daniel Dunbar1c566672009-02-24 01:43:46 +00002865 // For @synchronized, call objc_sync_enter(sync.expr). The
2866 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002867 // @synchronized. We can't avoid a temp here because we need the
2868 // value to be preserved. If the backend ever does liveness
2869 // correctly after setjmp, this will be unnecessary.
2870 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002871 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002872 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002873 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2874 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002875 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2876 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002877
2878 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2879 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002880 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002881
John McCall0b251722010-08-04 05:59:32 +00002882 // Allocate memory for the setjmp buffer. This needs to be kept
2883 // live throughout the try and catch blocks.
2884 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2885 "exceptiondata.ptr");
2886
John McCall87bb5822010-07-31 23:20:56 +00002887 // Create the fragile hazards. Note that this will not capture any
2888 // of the allocas required for exception processing, but will
2889 // capture the current basic block (which extends all the way to the
2890 // setjmp call) as "before the @try".
2891 FragileHazards Hazards(CGF);
2892
John McCallf1549f62010-07-06 01:34:17 +00002893 // Create a flag indicating whether the cleanup needs to call
2894 // objc_exception_try_exit. This is true except when
2895 // - no catches match and we're branching through the cleanup
2896 // just to rethrow the exception, or
2897 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002898 // The setjmp-safety rule here is that we should always store to this
2899 // variable in a place that dominates the branch through the cleanup
2900 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002901 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002902 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903
John McCall9e2213d2010-10-04 23:42:51 +00002904 // A slot containing the exception to rethrow. Only needed when we
2905 // have both a @catch and a @finally.
2906 llvm::Value *PropagatingExnVar = 0;
2907
John McCallf1549f62010-07-06 01:34:17 +00002908 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002909 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002910 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002911 CallTryExitVar,
2912 ExceptionData,
2913 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002914
2915 // Enter a try block:
2916 // - Call objc_exception_try_enter to push ExceptionData on top of
2917 // the EH stack.
2918 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2919 ->setDoesNotThrow();
2920
2921 // - Call setjmp on the exception data buffer.
2922 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2923 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2924 llvm::Value *SetJmpBuffer =
2925 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2926 llvm::CallInst *SetJmpResult =
2927 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2928 SetJmpResult->setDoesNotThrow();
2929
2930 // If setjmp returned 0, enter the protected block; otherwise,
2931 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002932 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2933 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002934 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002935 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2936 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002937
John McCallf1549f62010-07-06 01:34:17 +00002938 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002939 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002940 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002941 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002942 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002943
2944 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002945
John McCallf1549f62010-07-06 01:34:17 +00002946 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002947 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002948
John McCall87bb5822010-07-31 23:20:56 +00002949 // Don't optimize loads of the in-scope locals across this point.
2950 Hazards.emitWriteHazard();
2951
John McCallf1549f62010-07-06 01:34:17 +00002952 // For a @synchronized (or a @try with no catches), just branch
2953 // through the cleanup to the rethrow block.
2954 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2955 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002956 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002957 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002958
2959 // Otherwise, we have to match against the caught exceptions.
2960 } else {
John McCall0b251722010-08-04 05:59:32 +00002961 // Retrieve the exception object. We may emit multiple blocks but
2962 // nothing can cross this so the value is already in SSA form.
2963 llvm::CallInst *Caught =
2964 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2965 ExceptionData, "caught");
2966 Caught->setDoesNotThrow();
2967
John McCallf1549f62010-07-06 01:34:17 +00002968 // Push the exception to rethrow onto the EH value stack for the
2969 // benefit of any @throws in the handlers.
2970 CGF.ObjCEHValueStack.push_back(Caught);
2971
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002972 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002973
John McCall0b251722010-08-04 05:59:32 +00002974 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00002975
John McCall0b251722010-08-04 05:59:32 +00002976 llvm::BasicBlock *CatchBlock = 0;
2977 llvm::BasicBlock *CatchHandler = 0;
2978 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00002979 // Save the currently-propagating exception before
2980 // objc_exception_try_enter clears the exception slot.
2981 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2982 "propagating_exception");
2983 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2984
John McCall0b251722010-08-04 05:59:32 +00002985 // Enter a new exception try block (in case a @catch block
2986 // throws an exception).
2987 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2988 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002989
John McCall0b251722010-08-04 05:59:32 +00002990 llvm::CallInst *SetJmpResult =
2991 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2992 "setjmp.result");
2993 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002994
John McCall0b251722010-08-04 05:59:32 +00002995 llvm::Value *Threw =
2996 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2997
2998 CatchBlock = CGF.createBasicBlock("catch");
2999 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3000 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3001
3002 CGF.EmitBlock(CatchBlock);
3003 }
3004
3005 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003006
Daniel Dunbar55e40722008-09-27 07:03:52 +00003007 // Handle catch list. As a special case we check if everything is
3008 // matched and avoid generating code for falling off the end if
3009 // so.
3010 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003011 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3012 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003013
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003014 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003015 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003016
Anders Carlsson80f25672008-09-09 17:59:25 +00003017 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003018 if (!CatchParam) {
3019 AllMatched = true;
3020 } else {
John McCall183700f2009-09-21 23:43:11 +00003021 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003022
John McCallf1549f62010-07-06 01:34:17 +00003023 // catch(id e) always matches under this ABI, since only
3024 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003025 // FIXME: For the time being we also match id<X>; this should
3026 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003027 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003028 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003029 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003030
John McCallf1549f62010-07-06 01:34:17 +00003031 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003032 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003033 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3034
Anders Carlssondde0a942008-09-11 09:15:33 +00003035 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003036 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003037 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003038
3039 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003040 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003041 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003042
Anders Carlssondde0a942008-09-11 09:15:33 +00003043 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003044
3045 // The scope of the catch variable ends right here.
3046 CatchVarCleanups.ForceCleanup();
3047
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003048 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003049 break;
3050 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003051
Steve Naroff14108da2009-07-10 23:34:53 +00003052 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003053 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003054
3055 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003056 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3057 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003058
3059 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003060 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003061
John McCallf1549f62010-07-06 01:34:17 +00003062 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003063 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3064 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003065 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003066
John McCallf1549f62010-07-06 01:34:17 +00003067 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3068 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
3070 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003071 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003072
Anders Carlsson80f25672008-09-09 17:59:25 +00003073 // Emit the @catch block.
3074 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003075
3076 // Collect any cleanups for the catch variable. The scope lasts until
3077 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003078 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003079
John McCallb6bbcc92010-10-15 04:57:14 +00003080 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003081 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003082
John McCallf1549f62010-07-06 01:34:17 +00003083 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003084 llvm::Value *Tmp =
3085 CGF.Builder.CreateBitCast(Caught,
3086 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003087 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003088 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003089
Anders Carlssondde0a942008-09-11 09:15:33 +00003090 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003091
3092 // We're done with the catch variable.
3093 CatchVarCleanups.ForceCleanup();
3094
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003095 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096
Anders Carlsson80f25672008-09-09 17:59:25 +00003097 CGF.EmitBlock(NextCatchBlock);
3098 }
3099
John McCallf1549f62010-07-06 01:34:17 +00003100 CGF.ObjCEHValueStack.pop_back();
3101
John McCall0b251722010-08-04 05:59:32 +00003102 // If nothing wanted anything to do with the caught exception,
3103 // kill the extract call.
3104 if (Caught->use_empty())
3105 Caught->eraseFromParent();
3106
3107 if (!AllMatched)
3108 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3109
3110 if (HasFinally) {
3111 // Emit the exception handler for the @catch blocks.
3112 CGF.EmitBlock(CatchHandler);
3113
3114 // In theory we might now need a write hazard, but actually it's
3115 // unnecessary because there's no local-accessing code between
3116 // the try's write hazard and here.
3117 //Hazards.emitWriteHazard();
3118
John McCall9e2213d2010-10-04 23:42:51 +00003119 // Extract the new exception and save it to the
3120 // propagating-exception slot.
3121 assert(PropagatingExnVar);
3122 llvm::CallInst *NewCaught =
3123 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3124 ExceptionData, "caught");
3125 NewCaught->setDoesNotThrow();
3126 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3127
John McCall0b251722010-08-04 05:59:32 +00003128 // Don't pop the catch handler; the throw already did.
3129 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003130 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003131 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003132 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003133
John McCall87bb5822010-07-31 23:20:56 +00003134 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003135 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003136
John McCallf1549f62010-07-06 01:34:17 +00003137 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003138 CGF.Builder.restoreIP(TryFallthroughIP);
3139 if (CGF.HaveInsertPoint())
3140 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003141 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003142 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003143
John McCallf1549f62010-07-06 01:34:17 +00003144 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003145 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003146 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003147 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003148 // If we have a propagating-exception variable, check it.
3149 llvm::Value *PropagatingExn;
3150 if (PropagatingExnVar) {
3151 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003152
John McCall9e2213d2010-10-04 23:42:51 +00003153 // Otherwise, just look in the buffer for the exception to throw.
3154 } else {
3155 llvm::CallInst *Caught =
3156 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3157 ExceptionData);
3158 Caught->setDoesNotThrow();
3159 PropagatingExn = Caught;
3160 }
3161
3162 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003163 ->setDoesNotThrow();
3164 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003165 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003166
John McCall87bb5822010-07-31 23:20:56 +00003167 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003168}
3169
3170void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003171 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003172 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003173
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003174 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3175 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003177 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3178 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003179 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003180 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003181 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003182 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003183
John McCallf1549f62010-07-06 01:34:17 +00003184 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3185 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003186 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003187
3188 // Clear the insertion point to indicate we are in unreachable code.
3189 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003190}
3191
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003192/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003193/// object: objc_read_weak (id *src)
3194///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003195llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003196 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003197 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003198 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3199 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3200 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003201 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003202 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003203 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003204 return read_weak;
3205}
3206
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003207/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3208/// objc_assign_weak (id src, id *dst)
3209///
3210void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003211 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003212 const llvm::Type * SrcTy = src->getType();
3213 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003214 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003215 assert(Size <= 8 && "does not support size > 8");
3216 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003217 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003218 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3219 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003220 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3221 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003222 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003223 src, dst, "weakassign");
3224 return;
3225}
3226
Fariborz Jahanian58626502008-11-19 00:59:10 +00003227/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3228/// objc_assign_global (id src, id *dst)
3229///
3230void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003231 llvm::Value *src, llvm::Value *dst,
3232 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003233 const llvm::Type * SrcTy = src->getType();
3234 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003235 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003236 assert(Size <= 8 && "does not support size > 8");
3237 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003238 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003239 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3240 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003241 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3242 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003243 if (!threadlocal)
3244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3245 src, dst, "globalassign");
3246 else
3247 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3248 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003249 return;
3250}
3251
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003252/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003253/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003254///
3255void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003256 llvm::Value *src, llvm::Value *dst,
3257 llvm::Value *ivarOffset) {
3258 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003259 const llvm::Type * SrcTy = src->getType();
3260 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003261 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003262 assert(Size <= 8 && "does not support size > 8");
3263 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003264 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003265 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3266 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003267 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3268 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003269 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3270 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003271 return;
3272}
3273
Fariborz Jahanian58626502008-11-19 00:59:10 +00003274/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3275/// objc_assign_strongCast (id src, id *dst)
3276///
3277void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003278 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003279 const llvm::Type * SrcTy = src->getType();
3280 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003281 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003282 assert(Size <= 8 && "does not support size > 8");
3283 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003285 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3286 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003287 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3288 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003289 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003290 src, dst, "weakassign");
3291 return;
3292}
3293
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003294void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003295 llvm::Value *DestPtr,
3296 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003297 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003298 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3299 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003300 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003301 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003302 return;
3303}
3304
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003305/// EmitObjCValueForIvar - Code Gen for ivar reference.
3306///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003307LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3308 QualType ObjectTy,
3309 llvm::Value *BaseValue,
3310 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003311 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003312 const ObjCInterfaceDecl *ID =
3313 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003314 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3315 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003316}
3317
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003318llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003319 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003320 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003321 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003322 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003323 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3324 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003325}
3326
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003327/* *** Private Interface *** */
3328
3329/// EmitImageInfo - Emit the image info marker used to encode some module
3330/// level information.
3331///
3332/// See: <rdr://4810609&4810587&4810587>
3333/// struct IMAGE_INFO {
3334/// unsigned version;
3335/// unsigned flags;
3336/// };
3337enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003338 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003339 eImageInfo_GarbageCollected = (1 << 1),
3340 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003341 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3342
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003343 // A flag indicating that the module has no instances of a @synthesize of a
3344 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003345 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003346};
3347
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003348void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003349 unsigned version = 0; // Version is unused?
3350 unsigned flags = 0;
3351
3352 // FIXME: Fix and continue?
3353 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3354 flags |= eImageInfo_GarbageCollected;
3355 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3356 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003357
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003358 // We never allow @synthesize of a superclass property.
3359 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003360
Chris Lattner77b89b82010-06-27 07:15:29 +00003361 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3362
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003363 // Emitted as int[2];
3364 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003365 llvm::ConstantInt::get(Int32Ty, version),
3366 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003367 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003368 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003369
3370 const char *Section;
3371 if (ObjCABI == 1)
3372 Section = "__OBJC, __image_info,regular";
3373 else
3374 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003375 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003376 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003377 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003378 Section,
3379 0,
3380 true);
3381 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003382}
3383
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003384
3385// struct objc_module {
3386// unsigned long version;
3387// unsigned long size;
3388// const char *name;
3389// Symtab symtab;
3390// };
3391
3392// FIXME: Get from somewhere
3393static const int ModuleVersion = 7;
3394
3395void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003396 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003397
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003398 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003399 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3400 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003401 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003402 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003403 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003404 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003405 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003406 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003407 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003408}
3409
3410llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003411 unsigned NumClasses = DefinedClasses.size();
3412 unsigned NumCategories = DefinedCategories.size();
3413
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003414 // Return null if no symbols were defined.
3415 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003416 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003417
3418 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003419 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003420 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003421 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3422 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003423
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003424 // The runtime expects exactly the list of defined classes followed
3425 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003426 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003427 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003428 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003429 ObjCTypes.Int8PtrTy);
3430 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003431 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003432 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003433 ObjCTypes.Int8PtrTy);
3434
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003435 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003436 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003437 NumClasses + NumCategories),
3438 Symbols);
3439
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003440 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003441
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003442 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003443 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3444 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003445 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003446 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003447}
3448
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003449llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003450 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003451 LazySymbols.insert(ID->getIdentifier());
3452
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003453 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003454
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003455 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003456 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003457 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003458 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003459 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003460 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3461 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003462 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003463 }
3464
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003465 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003466}
3467
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003468llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3469 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003470 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003471
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003472 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003473 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003474 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003475 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003476 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003477 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3478 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003479 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003480 }
3481
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003482 if (lvalue)
3483 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003484 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003485}
3486
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003487llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003488 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003489
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003490 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003491 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003492 llvm::ConstantArray::get(VMContext,
3493 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003494 ((ObjCABI == 2) ?
3495 "__TEXT,__objc_classname,cstring_literals" :
3496 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003497 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003498
Owen Andersona1cf15f2009-07-14 23:10:40 +00003499 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003500}
3501
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003502llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3503 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3504 I = MethodDefinitions.find(MD);
3505 if (I != MethodDefinitions.end())
3506 return I->second;
3507
3508 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3509 // MD isn't emitted yet because it comes from PCH.
3510 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3511 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3512 return MethodDefinitions[MD];
3513 }
3514
3515 return NULL;
3516}
3517
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003518/// GetIvarLayoutName - Returns a unique constant for the given
3519/// ivar layout bitmap.
3520llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003521 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003522 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003523}
3524
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003525void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003526 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003527 bool ForStrongLayout,
3528 bool &HasUnion) {
3529 const RecordDecl *RD = RT->getDecl();
3530 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003531 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003532 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003533 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003534 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003535
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003536 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3537 ForStrongLayout, HasUnion);
3538}
3539
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003540void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003541 const llvm::StructLayout *Layout,
3542 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003543 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003544 unsigned int BytePos, bool ForStrongLayout,
3545 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003546 bool IsUnion = (RD && RD->isUnion());
3547 uint64_t MaxUnionIvarSize = 0;
3548 uint64_t MaxSkippedUnionIvarSize = 0;
3549 FieldDecl *MaxField = 0;
3550 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003551 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003552 uint64_t MaxFieldOffset = 0;
3553 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003554 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003555
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003556 if (RecFields.empty())
3557 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003558 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3559 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3560
Chris Lattnerf1690852009-03-31 08:48:01 +00003561 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003562 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003563 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003564 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003565 // Note that 'i' here is actually the field index inside RD of Field,
3566 // although this dependency is hidden.
3567 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckfb67ccd2011-04-14 00:43:09 +00003568 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003569 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003570 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003571
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003572 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003573 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003574 LastFieldBitfieldOrUnnamed = Field;
3575 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003576 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003577 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003578
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003579 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003580 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003581 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003582 if (FQT->isUnionType())
3583 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003584
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003586 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003587 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003588 continue;
3589 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003590
Chris Lattnerf1690852009-03-31 08:48:01 +00003591 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003592 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003593 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003594 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003595 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003596 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003597 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3598 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003599 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003600 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003601 FQT = CArray->getElementType();
3602 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003603
3604 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003605 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003606 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003607 int OldIndex = IvarsInfo.size() - 1;
3608 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003609
Ted Kremenek6217b802009-07-29 21:53:49 +00003610 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003611 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003612 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003613
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003614 // Replicate layout information for each array element. Note that
3615 // one element is already done.
3616 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003617 for (int FirstIndex = IvarsInfo.size() - 1,
3618 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003619 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003620 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3621 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3622 IvarsInfo[i].ivar_size));
3623 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3624 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3625 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003626 }
3627 continue;
3628 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003629 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003630 // At this point, we are done with Record/Union and array there of.
3631 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003632 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003633
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003634 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003635 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3636 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003637 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003638 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003639 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 MaxUnionIvarSize = UnionIvarSize;
3641 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003642 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003643 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003644 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003645 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003646 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003647 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003648 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003649 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3650 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003651 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003652 // FIXME: Why the asymmetry? We divide by word size in bits on other
3653 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003654 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003655 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003656 MaxSkippedUnionIvarSize = UnionIvarSize;
3657 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003658 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003659 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003660 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003661 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003662 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003663 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003664 }
3665 }
3666 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003667
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003668 if (LastFieldBitfieldOrUnnamed) {
3669 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3670 // Last field was a bitfield. Must update skip info.
3671 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3672 uint64_t BitFieldSize =
3673 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3674 GC_IVAR skivar;
3675 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3676 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3677 + ((BitFieldSize % ByteSizeInBits) != 0);
3678 SkipIvars.push_back(skivar);
3679 } else {
3680 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3681 // Last field was unnamed. Must update skip info.
3682 unsigned FieldSize
3683 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3684 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3685 FieldSize / ByteSizeInBits));
3686 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003687 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003688
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003689 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003690 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003691 MaxUnionIvarSize));
3692 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003693 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003694 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003695}
3696
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003697/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3698/// the computations and returning the layout bitmap (for ivar or blocks) in
3699/// the given argument BitMap string container. Routine reads
3700/// two containers, IvarsInfo and SkipIvars which are assumed to be
3701/// filled already by the caller.
3702llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003703 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003704 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003705
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003706 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003707 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003708 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003709 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003710 if (IvarsInfo[0].ivar_bytepos == 0) {
3711 WordsToSkip = 0;
3712 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003713 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003714 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3715 WordsToScan = IvarsInfo[0].ivar_size;
3716 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003717 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003718 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003719 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003720 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003721 // consecutive 'scanned' object pointers.
3722 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003723 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003724 // Skip over 'gc'able object pointer which lay over each other.
3725 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3726 continue;
3727 // Must skip over 1 or more words. We save current skip/scan values
3728 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003729 SKIP_SCAN SkScan;
3730 SkScan.skip = WordsToSkip;
3731 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003732 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003733
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003734 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003735 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3736 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003737 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003738 WordsToSkip = 0;
3739 WordsToScan = IvarsInfo[i].ivar_size;
3740 }
3741 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003742 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003743 SKIP_SCAN SkScan;
3744 SkScan.skip = WordsToSkip;
3745 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003746 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003747 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003748
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003749 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003750 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003751 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003752 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003753 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003754 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003755 IvarsInfo[LastIndex].ivar_bytepos +
3756 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003757 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003758 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003759 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003760 SKIP_SCAN SkScan;
3761 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3762 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003763 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003764 }
3765 }
3766 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3767 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003768 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003769 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003770 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3771 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3772 // 0xM0 followed by 0x0N detected.
3773 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3774 for (int j = i+1; j < SkipScan; j++)
3775 SkipScanIvars[j] = SkipScanIvars[j+1];
3776 --SkipScan;
3777 }
3778 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003779
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003780 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003781 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003782 unsigned char byte;
3783 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3784 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3785 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3786 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003787
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003788 // first skip big.
3789 for (unsigned int ix = 0; ix < skip_big; ix++)
3790 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003791
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003792 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003793 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003794 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003795 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003796 byte |= 0xf;
3797 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003798 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003799 byte |= scan_small;
3800 scan_small = 0;
3801 }
3802 BitMap += byte;
3803 }
3804 // next scan big
3805 for (unsigned int ix = 0; ix < scan_big; ix++)
3806 BitMap += (unsigned char)(0x0f);
3807 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003808 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003809 byte = scan_small;
3810 BitMap += byte;
3811 }
3812 }
3813 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003814 unsigned char zero = 0;
3815 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003816
3817 llvm::GlobalVariable * Entry =
3818 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3819 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003820 ((ObjCABI == 2) ?
3821 "__TEXT,__objc_classname,cstring_literals" :
3822 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003823 1, true);
3824 return getConstantGEP(VMContext, Entry, 0, 0);
3825}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003826
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003827/// BuildIvarLayout - Builds ivar layout bitmap for the class
3828/// implementation for the __strong or __weak case.
3829/// The layout map displays which words in ivar list must be skipped
3830/// and which must be scanned by GC (see below). String is built of bytes.
3831/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3832/// of words to skip and right nibble is count of words to scan. So, each
3833/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3834/// represented by a 0x00 byte which also ends the string.
3835/// 1. when ForStrongLayout is true, following ivars are scanned:
3836/// - id, Class
3837/// - object *
3838/// - __strong anything
3839///
3840/// 2. When ForStrongLayout is false, following ivars are scanned:
3841/// - __weak anything
3842///
3843llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3844 const ObjCImplementationDecl *OMD,
3845 bool ForStrongLayout) {
3846 bool hasUnion = false;
3847
3848 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3849 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3850 return llvm::Constant::getNullValue(PtrTy);
3851
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003852 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003853 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003854 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003855
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003856 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003857 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3858 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3859
3860 if (RecFields.empty())
3861 return llvm::Constant::getNullValue(PtrTy);
3862
3863 SkipIvars.clear();
3864 IvarsInfo.clear();
3865
3866 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3867 if (IvarsInfo.empty())
3868 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003869 // Sort on byte position in case we encounterred a union nested in
3870 // the ivar list.
3871 if (hasUnion && !IvarsInfo.empty())
3872 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3873 if (hasUnion && !SkipIvars.empty())
3874 std::sort(SkipIvars.begin(), SkipIvars.end());
3875
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003876 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003877 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003878
3879 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003880 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003881 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003882 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003883 const unsigned char *s = (unsigned char*)BitMap.c_str();
3884 for (unsigned i = 0; i < BitMap.size(); i++)
3885 if (!(s[i] & 0xf0))
3886 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3887 else
3888 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3889 printf("\n");
3890 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003891 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003892}
3893
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003894llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003895 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3896
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003897 // FIXME: Avoid std::string copying.
3898 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003899 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003900 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003901 ((ObjCABI == 2) ?
3902 "__TEXT,__objc_methname,cstring_literals" :
3903 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003904 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003905
Owen Andersona1cf15f2009-07-14 23:10:40 +00003906 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003907}
3908
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003909// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003910llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003911 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3912}
3913
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003914llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003915 std::string TypeStr;
3916 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3917
3918 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003919
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003920 if (!Entry)
3921 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003922 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003923 ((ObjCABI == 2) ?
3924 "__TEXT,__objc_methtype,cstring_literals" :
3925 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003926 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003927
Owen Andersona1cf15f2009-07-14 23:10:40 +00003928 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003929}
3930
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003931llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003932 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003933 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3934 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003935
3936 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3937
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003938 if (!Entry)
3939 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003940 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003941 ((ObjCABI == 2) ?
3942 "__TEXT,__objc_methtype,cstring_literals" :
3943 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003944 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003945
Owen Andersona1cf15f2009-07-14 23:10:40 +00003946 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003947}
3948
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003949// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003950llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003951 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003952
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003953 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003954 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003955 llvm::ConstantArray::get(VMContext,
3956 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003957 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003958 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003959
Owen Andersona1cf15f2009-07-14 23:10:40 +00003960 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003961}
3962
3963// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003964// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003965llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003966CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3967 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003968 std::string TypeStr;
3969 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003970 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3971}
3972
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003973void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003974 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003975 llvm::SmallVectorImpl<char> &Name) {
3976 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003977 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003978 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3979 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003980 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003981 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003982 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003983 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003984}
3985
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003986void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003987 EmitModuleInfo();
3988
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003989 // Emit the dummy bodies for any protocols which were referenced but
3990 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003992 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3993 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003994 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003995
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003996 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003997 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003998 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003999 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004000 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004001 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004002 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004003 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004004 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004005 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004006 }
4007
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004008 // Add assembler directives to add lazy undefined symbol references
4009 // for classes which are referenced but not defined. This is
4010 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004011 //
4012 // FIXME: It would be nice if we had an LLVM construct for this.
4013 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4014 llvm::SmallString<256> Asm;
4015 Asm += CGM.getModule().getModuleInlineAsm();
4016 if (!Asm.empty() && Asm.back() != '\n')
4017 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004018
Daniel Dunbar33063492009-09-07 00:20:42 +00004019 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004020 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4021 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004022 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4023 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004024 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004025 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004026 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004027 }
4028
4029 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4030 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4031 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4032 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004033
Daniel Dunbar33063492009-09-07 00:20:42 +00004034 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004035 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004036}
4037
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004039 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004040 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004041 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004042 ObjCABI = 2;
4043}
4044
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004045/* *** */
4046
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004047ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004048 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004049 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4050 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004051
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004052 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004053 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004054 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004055 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004056 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004057
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004058 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004059 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004060 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004061
Mike Stumpf5408fe2009-05-16 07:57:57 +00004062 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4063 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004064 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004065 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004066
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004067 // I'm not sure I like this. The implicit coordination is a bit
4068 // gross. We should solve this in a reasonable fashion because this
4069 // is a pretty common task (match some runtime data structure with
4070 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004071
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004072 // FIXME: This is leaked.
4073 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004074
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004075 // struct _objc_super {
4076 // id self;
4077 // Class cls;
4078 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004079 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004080 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004081 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004082 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004083 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004084 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004085 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004086 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004087 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004088
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004089 SuperCTy = Ctx.getTagDeclType(RD);
4090 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004091
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004092 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004093 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4094
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004095 // struct _prop_t {
4096 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004097 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004098 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004099 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004100 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004101 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004102
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004103 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004104 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004105 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004106 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004107 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004108 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004109 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004110 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004111 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004112 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004113 PropertyListTy);
4114 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004115 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004116
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004117 // struct _objc_method {
4118 // SEL _cmd;
4119 // char *method_type;
4120 // char *_imp;
4121 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004122 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004123 Int8PtrTy,
4124 Int8PtrTy,
4125 NULL);
4126 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004128 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004129 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004130 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004131 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004132}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004133
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004134ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004135 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004136 // struct _objc_method_description {
4137 // SEL name;
4138 // char *types;
4139 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004140 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004141 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004142 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004143 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004145 MethodDescriptionTy);
4146
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004147 // struct _objc_method_description_list {
4148 // int count;
4149 // struct _objc_method_description[1];
4150 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004151 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004152 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004153 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004154 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004155 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004156 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004157
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004158 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004159 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004160 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004161
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004162 // Protocol description structures
4163
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004164 // struct _objc_protocol_extension {
4165 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4166 // struct _objc_method_description_list *optional_instance_methods;
4167 // struct _objc_method_description_list *optional_class_methods;
4168 // struct _objc_property_list *instance_properties;
4169 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004170 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004171 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004172 MethodDescriptionListPtrTy,
4173 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004174 PropertyListPtrTy,
4175 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004176 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004177 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004178
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004179 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004180 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004181
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004182 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004183
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004184 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4185 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004186
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004188 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004189 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004190 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004191 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004192 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004193 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4194
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004195 // struct _objc_protocol {
4196 // struct _objc_protocol_extension *isa;
4197 // char *protocol_name;
4198 // struct _objc_protocol **_objc_protocol_list;
4199 // struct _objc_method_description_list *instance_methods;
4200 // struct _objc_method_description_list *class_methods;
4201 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004202 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004203 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004204 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004205 MethodDescriptionListPtrTy,
4206 MethodDescriptionListPtrTy,
4207 NULL);
4208 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4209
4210 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004211 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004212 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004213 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004214 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004215
4216 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004217 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004218 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004219
4220 // Class description structures
4221
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004222 // struct _objc_ivar {
4223 // char *ivar_name;
4224 // char *ivar_type;
4225 // int ivar_offset;
4226 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004227 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004228 Int8PtrTy,
4229 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004230 NULL);
4231 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4232
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004233 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004234 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004235 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004236 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004237
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004238 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004239 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004240 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004241 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004242
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004243 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004244 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004245 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004246 Int8PtrTy,
4247 PropertyListPtrTy,
4248 NULL);
4249 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004250 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004251
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004252 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004253
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004254 // struct _objc_class {
4255 // Class isa;
4256 // Class super_class;
4257 // char *name;
4258 // long version;
4259 // long info;
4260 // long instance_size;
4261 // struct _objc_ivar_list *ivars;
4262 // struct _objc_method_list *methods;
4263 // struct _objc_cache *cache;
4264 // struct _objc_protocol_list *protocols;
4265 // char *ivar_layout;
4266 // struct _objc_class_ext *ext;
4267 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004268 T = llvm::StructType::get(VMContext,
4269 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004270 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004271 Int8PtrTy,
4272 LongTy,
4273 LongTy,
4274 LongTy,
4275 IvarListPtrTy,
4276 MethodListPtrTy,
4277 CachePtrTy,
4278 ProtocolListPtrTy,
4279 Int8PtrTy,
4280 ClassExtensionPtrTy,
4281 NULL);
4282 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004283
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004284 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4285 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004286 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004287
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004288 // struct _objc_category {
4289 // char *category_name;
4290 // char *class_name;
4291 // struct _objc_method_list *instance_method;
4292 // struct _objc_method_list *class_method;
4293 // uint32_t size; // sizeof(struct _objc_category)
4294 // struct _objc_property_list *instance_properties;// category's @property
4295 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004296 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004297 Int8PtrTy,
4298 MethodListPtrTy,
4299 MethodListPtrTy,
4300 ProtocolListPtrTy,
4301 IntTy,
4302 PropertyListPtrTy,
4303 NULL);
4304 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4305
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004306 // Global metadata structures
4307
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004308 // struct _objc_symtab {
4309 // long sel_ref_cnt;
4310 // SEL *refs;
4311 // short cls_def_cnt;
4312 // short cat_def_cnt;
4313 // char *defs[cls_def_cnt + cat_def_cnt];
4314 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004315 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316 SelectorPtrTy,
4317 ShortTy,
4318 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004319 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004320 NULL);
4321 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004322 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004324 // struct _objc_module {
4325 // long version;
4326 // long size; // sizeof(struct _objc_module)
4327 // char *name;
4328 // struct _objc_symtab* symtab;
4329 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004331 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004332 LongTy,
4333 Int8PtrTy,
4334 SymtabPtrTy,
4335 NULL);
4336 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004337
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004338
Mike Stumpf5408fe2009-05-16 07:57:57 +00004339 // FIXME: This is the size of the setjmp buffer and should be target
4340 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004341 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004342
Anders Carlsson124526b2008-09-09 10:10:21 +00004343 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004344 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004345 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004346
4347 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004348 llvm::StructType::get(VMContext,
4349 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4350 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004351 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004352 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004353 ExceptionDataTy);
4354
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004355}
4356
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004358 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004359 // struct _method_list_t {
4360 // uint32_t entsize; // sizeof(struct _objc_method)
4361 // uint32_t method_count;
4362 // struct _objc_method method_list[method_count];
4363 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004364 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004365 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004366 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004367 NULL);
4368 CGM.getModule().addTypeName("struct.__method_list_t",
4369 MethodListnfABITy);
4370 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004371 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004372
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004373 // struct _protocol_t {
4374 // id isa; // NULL
4375 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004376 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004377 // const struct method_list_t * const instance_methods;
4378 // const struct method_list_t * const class_methods;
4379 // const struct method_list_t *optionalInstanceMethods;
4380 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004381 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004382 // const uint32_t size; // sizeof(struct _protocol_t)
4383 // const uint32_t flags; // = 0
4384 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004386 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004387 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004388
Owen Anderson47a434f2009-08-05 23:18:46 +00004389 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004390 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004391 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004392 ProtocolListTyHolder),
4393 MethodListnfABIPtrTy,
4394 MethodListnfABIPtrTy,
4395 MethodListnfABIPtrTy,
4396 MethodListnfABIPtrTy,
4397 PropertyListPtrTy,
4398 IntTy,
4399 IntTy,
4400 NULL);
4401 CGM.getModule().addTypeName("struct._protocol_t",
4402 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004403
4404 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004405 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004406
Fariborz Jahanianda320092009-01-29 19:24:30 +00004407 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004408 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004409 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004410 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004411 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004412 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004413 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004414 NULL);
4415 CGM.getModule().addTypeName("struct._objc_protocol_list",
4416 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004417 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004418 ProtocolListnfABITy);
4419
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004420 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004421 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004422
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004423 // struct _ivar_t {
4424 // unsigned long int *offset; // pointer to ivar offset location
4425 // char *name;
4426 // char *type;
4427 // uint32_t alignment;
4428 // uint32_t size;
4429 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004430 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004431 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004432 Int8PtrTy,
4433 Int8PtrTy,
4434 IntTy,
4435 IntTy,
4436 NULL);
4437 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004439 // struct _ivar_list_t {
4440 // uint32 entsize; // sizeof(struct _ivar_t)
4441 // uint32 count;
4442 // struct _iver_t list[count];
4443 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004444 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004445 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004446 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004447 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004448 NULL);
4449 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450
Owen Anderson96e0fc72009-07-29 22:16:19 +00004451 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004452
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004453 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004454 // uint32_t const flags;
4455 // uint32_t const instanceStart;
4456 // uint32_t const instanceSize;
4457 // uint32_t const reserved; // only when building for 64bit targets
4458 // const uint8_t * const ivarLayout;
4459 // const char *const name;
4460 // const struct _method_list_t * const baseMethods;
4461 // const struct _objc_protocol_list *const baseProtocols;
4462 // const struct _ivar_list_t *const ivars;
4463 // const uint8_t * const weakIvarLayout;
4464 // const struct _prop_list_t * const properties;
4465 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004466
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004467 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004468 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004469 IntTy,
4470 IntTy,
4471 Int8PtrTy,
4472 Int8PtrTy,
4473 MethodListnfABIPtrTy,
4474 ProtocolListnfABIPtrTy,
4475 IvarListnfABIPtrTy,
4476 Int8PtrTy,
4477 PropertyListPtrTy,
4478 NULL);
4479 CGM.getModule().addTypeName("struct._class_ro_t",
4480 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004481
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004482 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4483 std::vector<const llvm::Type*> Params;
4484 Params.push_back(ObjectPtrTy);
4485 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004486 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004487 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4488
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004489 // struct _class_t {
4490 // struct _class_t *isa;
4491 // struct _class_t * const superclass;
4492 // void *cache;
4493 // IMP *vtable;
4494 // struct class_ro_t *ro;
4495 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004497 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004498 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004499 llvm::StructType::get(VMContext,
4500 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004501 llvm::PointerType::getUnqual(ClassTyHolder),
4502 CachePtrTy,
4503 llvm::PointerType::getUnqual(ImpnfABITy),
4504 llvm::PointerType::getUnqual(ClassRonfABITy),
4505 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004506 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4507
4508 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004509 ClassnfABITy);
4510
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004511 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004512 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004513
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004514 // struct _category_t {
4515 // const char * const name;
4516 // struct _class_t *const cls;
4517 // const struct _method_list_t * const instance_methods;
4518 // const struct _method_list_t * const class_methods;
4519 // const struct _protocol_list_t * const protocols;
4520 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004521 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004522 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004523 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004524 MethodListnfABIPtrTy,
4525 MethodListnfABIPtrTy,
4526 ProtocolListnfABIPtrTy,
4527 PropertyListPtrTy,
4528 NULL);
4529 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004530
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004531 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004532 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4533 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004534
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004535 // MessageRefTy - LLVM for:
4536 // struct _message_ref_t {
4537 // IMP messenger;
4538 // SEL name;
4539 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004540
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004541 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004542 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004543 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004544 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004545 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004546 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004547 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004548 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004549 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004550 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004552 MessageRefCTy = Ctx.getTagDeclType(RD);
4553 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4554 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004555
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004556 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004557 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004558
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004559 // SuperMessageRefTy - LLVM for:
4560 // struct _super_message_ref_t {
4561 // SUPER_IMP messenger;
4562 // SEL name;
4563 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004564 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004565 SelectorPtrTy,
4566 NULL);
4567 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004568
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004569 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004570 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4571
Daniel Dunbare588b992009-03-01 04:46:24 +00004572
4573 // struct objc_typeinfo {
4574 // const void** vtable; // objc_ehtype_vtable + 2
4575 // const char* name; // c++ typeinfo string
4576 // Class cls;
4577 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004578 EHTypeTy = llvm::StructType::get(VMContext,
4579 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004580 Int8PtrTy,
4581 ClassnfABIPtrTy,
4582 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004583 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004584 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004585}
4586
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004587llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004588 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004589
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004590 return NULL;
4591}
4592
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004593void CGObjCNonFragileABIMac::AddModuleClassList(const
4594 std::vector<llvm::GlobalValue*>
4595 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004596 const char *SymbolName,
4597 const char *SectionName) {
4598 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Daniel Dunbar463b8762009-05-15 21:48:48 +00004600 if (!NumClasses)
4601 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602
Daniel Dunbar463b8762009-05-15 21:48:48 +00004603 std::vector<llvm::Constant*> Symbols(NumClasses);
4604 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004605 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004606 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004607 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004608 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004609 NumClasses),
4610 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004611
Daniel Dunbar463b8762009-05-15 21:48:48 +00004612 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004613 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004614 llvm::GlobalValue::InternalLinkage,
4615 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004616 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004617 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004618 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004619 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004620}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004622void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4623 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004624
Daniel Dunbar463b8762009-05-15 21:48:48 +00004625 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004626 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004627 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004628 "\01L_OBJC_LABEL_CLASS_$",
4629 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004630
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004631 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4632 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4633 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4634 continue;
4635 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004636 }
4637
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004638 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4639 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4640 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4641 continue;
4642 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4643 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004644
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004645 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004646 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4647 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004648
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004649 // Build list of all implemented category addresses in array
4650 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004652 "\01L_OBJC_LABEL_CATEGORY_$",
4653 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004654 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004655 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4656 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004657
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004658 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004659}
4660
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004661/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004662/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004664/// message dispatch call for all the rest.
4665///
4666bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004667 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4668 default:
4669 assert(0 && "Invalid dispatch method!");
4670 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004671 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004672 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004673 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004674 case CodeGenOptions::Mixed:
4675 break;
4676 }
4677
4678 // If so, see whether this selector is in the white-list of things which must
4679 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004680 if (NonLegacyDispatchMethods.empty()) {
4681 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4682 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4683 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4684 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4685 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4686 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4687 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4688 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4689 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4690 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004691
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004692 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4693 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4694 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4695 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4696 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4697 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4698 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4699 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004700 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004701 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004702 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4703 &CGM.getContext().Idents.get("objects"),
4704 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004705 };
4706 NonLegacyDispatchMethods.insert(
4707 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004708 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004709
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004710 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004711}
4712
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004713// Metadata flags
4714enum MetaDataDlags {
4715 CLS = 0x0,
4716 CLS_META = 0x1,
4717 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004718 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004719 CLS_EXCEPTION = 0x20
4720};
4721/// BuildClassRoTInitializer - generate meta-data for:
4722/// struct _class_ro_t {
4723/// uint32_t const flags;
4724/// uint32_t const instanceStart;
4725/// uint32_t const instanceSize;
4726/// uint32_t const reserved; // only when building for 64bit targets
4727/// const uint8_t * const ivarLayout;
4728/// const char *const name;
4729/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004730/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004731/// const struct _ivar_list_t *const ivars;
4732/// const uint8_t * const weakIvarLayout;
4733/// const struct _prop_list_t * const properties;
4734/// }
4735///
4736llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737 unsigned flags,
4738 unsigned InstanceStart,
4739 unsigned InstanceSize,
4740 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004741 std::string ClassName = ID->getNameAsString();
4742 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004743 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4744 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4745 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004746 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004747 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4748 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004749 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004750 // const struct _method_list_t * const baseMethods;
4751 std::vector<llvm::Constant*> Methods;
4752 std::string MethodListName("\01l_OBJC_$_");
4753 if (flags & CLS_META) {
4754 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004755 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004756 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004757 // Class methods should always be defined.
4758 Methods.push_back(GetMethodConstant(*i));
4759 }
4760 } else {
4761 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004762 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004763 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004764 // Instance methods should always be defined.
4765 Methods.push_back(GetMethodConstant(*i));
4766 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004767 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004768 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004769 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004771 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4772 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004773
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004774 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4775 if (llvm::Constant *C = GetMethodConstant(MD))
4776 Methods.push_back(C);
4777 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4778 if (llvm::Constant *C = GetMethodConstant(MD))
4779 Methods.push_back(C);
4780 }
4781 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004782 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004783 Values[ 5] = EmitMethodList(MethodListName,
4784 "__DATA, __objc_const", Methods);
4785
Fariborz Jahanianda320092009-01-29 19:24:30 +00004786 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4787 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004788 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004789 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004790 OID->all_referenced_protocol_begin(),
4791 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004792
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004793 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004794 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004795 else
4796 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004797 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4798 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004799 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004800 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004801 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004802 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4803 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004804 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004805 Values);
4806 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004807 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4808 llvm::GlobalValue::InternalLinkage,
4809 Init,
4810 (flags & CLS_META) ?
4811 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4812 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004813 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004814 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004815 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004816 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004817
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004818}
4819
4820/// BuildClassMetaData - This routine defines that to-level meta-data
4821/// for the given ClassName for:
4822/// struct _class_t {
4823/// struct _class_t *isa;
4824/// struct _class_t * const superclass;
4825/// void *cache;
4826/// IMP *vtable;
4827/// struct class_ro_t *ro;
4828/// }
4829///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004830llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004831 std::string &ClassName,
4832 llvm::Constant *IsAGV,
4833 llvm::Constant *SuperClassGV,
4834 llvm::Constant *ClassRoGV,
4835 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004836 std::vector<llvm::Constant*> Values(5);
4837 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004838 Values[1] = SuperClassGV;
4839 if (!Values[1])
4840 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004841 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4842 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4843 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004845 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004846 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4847 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004848 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004849 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004850 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004851 if (HiddenVisibility)
4852 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004853 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004854}
4855
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004856bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004857CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004858 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004859}
4860
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004861void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004862 uint32_t &InstanceStart,
4863 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004864 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004865 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004866
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004867 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004868 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004869
4870 // If there are no fields, the start is the same as the end.
4871 if (!RL.getFieldCount())
4872 InstanceStart = InstanceSize;
4873 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004874 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004875}
4876
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004877void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4878 std::string ClassName = ID->getNameAsString();
4879 if (!ObjCEmptyCacheVar) {
4880 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004881 CGM.getModule(),
4882 ObjCTypes.CacheTy,
4883 false,
4884 llvm::GlobalValue::ExternalLinkage,
4885 0,
4886 "_objc_empty_cache");
4887
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004888 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004889 CGM.getModule(),
4890 ObjCTypes.ImpnfABITy,
4891 false,
4892 llvm::GlobalValue::ExternalLinkage,
4893 0,
4894 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004895 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004896 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004897 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004898 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004899 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004900 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004901 uint32_t InstanceSize = InstanceStart;
4902 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004903 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4904 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004905
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004906 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004907
4908 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004909 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004910 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004911 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004912 if (ID->getNumIvarInitializers())
4913 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004914 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004915 // class is root
4916 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004917 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004918 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004919 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004920 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004921 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4922 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4923 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004924 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004925 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004926 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004927 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004928 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004929 ObjCMetaClassName +
4930 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004931 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004932 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004933 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004934 }
4935 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4936 InstanceStart,
4937 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004938 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004939 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004940 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4941 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004942 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004943
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004944 // Metadata for the class
4945 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004946 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004947 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004948 if (ID->getNumIvarInitializers())
4949 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004950
Douglas Gregor68584ed2009-06-18 16:11:24 +00004951 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004952 flags |= CLS_EXCEPTION;
4953
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004954 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004955 flags |= CLS_ROOT;
4956 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004957 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004958 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004959 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004960 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004961 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004962 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004963 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004964 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004965 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004966 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004967 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004969 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004970
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004971 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004972 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004973 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4974 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004975 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004976
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004977 // Determine if this class is also "non-lazy".
4978 if (ImplementationIsNonLazy(ID))
4979 DefinedNonLazyClasses.push_back(ClassMD);
4980
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004981 // Force the definition of the EHType if necessary.
4982 if (flags & CLS_EXCEPTION)
4983 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00004984 // Make sure method definition entries are all clear for next implementation.
4985 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004986}
4987
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004988/// GenerateProtocolRef - This routine is called to generate code for
4989/// a protocol reference expression; as in:
4990/// @code
4991/// @protocol(Proto1);
4992/// @endcode
4993/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4994/// which will hold address of the protocol meta-data.
4995///
4996llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004997 const ObjCProtocolDecl *PD) {
4998
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004999 // This routine is called for @protocol only. So, we must build definition
5000 // of protocol's meta-data (not a reference to it!)
5001 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005002 llvm::Constant *Init =
5003 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5004 ObjCTypes.ExternalProtocolPtrTy);
5005
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005006 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005007 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005009 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5010 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005011 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005012 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 CGM.getModule(),
5014 Init->getType(), false,
5015 llvm::GlobalValue::WeakAnyLinkage,
5016 Init,
5017 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005018 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5019 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005020 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005021 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005022}
5023
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005024/// GenerateCategory - Build metadata for a category implementation.
5025/// struct _category_t {
5026/// const char * const name;
5027/// struct _class_t *const cls;
5028/// const struct _method_list_t * const instance_methods;
5029/// const struct _method_list_t * const class_methods;
5030/// const struct _protocol_list_t * const protocols;
5031/// const struct _prop_list_t * const properties;
5032/// }
5033///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005034void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005035 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005036 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005037 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5038 "_$_" + OCD->getNameAsString());
5039 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005040 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005041
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005042 std::vector<llvm::Constant*> Values(6);
5043 Values[0] = GetClassName(OCD->getIdentifier());
5044 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005045 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005046 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005047 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5048
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005049 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005050 std::vector<llvm::Constant*> Methods;
5051 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005052 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005053 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054
5055 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005056 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005057 // Instance methods should always be defined.
5058 Methods.push_back(GetMethodConstant(*i));
5059 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005060
5061 Values[2] = EmitMethodList(MethodListName,
5062 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005063 Methods);
5064
5065 MethodListName = Prefix;
5066 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5067 OCD->getNameAsString();
5068 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005069 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005070 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005071 // Class methods should always be defined.
5072 Methods.push_back(GetMethodConstant(*i));
5073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005074
5075 Values[3] = EmitMethodList(MethodListName,
5076 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005077 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005078 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005079 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005080 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005081 llvm::SmallString<256> ExtName;
5082 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5083 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005084 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005085 + Interface->getName() + "_$_"
5086 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005087 Category->protocol_begin(),
5088 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005089 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5090 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005091 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005092 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5093 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005094 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005095
5096 llvm::Constant *Init =
5097 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005098 Values);
5099 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005100 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005101 false,
5102 llvm::GlobalValue::InternalLinkage,
5103 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005104 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005105 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005106 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005107 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005108 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005109 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005110
5111 // Determine if this category is also "non-lazy".
5112 if (ImplementationIsNonLazy(OCD))
5113 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005114 // method definition entries must be clear for next implementation.
5115 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005116}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005117
5118/// GetMethodConstant - Return a struct objc_method constant for the
5119/// given method if it has been defined. The result is null if the
5120/// method has not been defined. The return value has type MethodPtrTy.
5121llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005122 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005123 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005124 if (!Fn)
5125 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005126
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005127 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005128 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005129 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005130 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005131 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005132 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005133 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005134}
5135
5136/// EmitMethodList - Build meta-data for method declarations
5137/// struct _method_list_t {
5138/// uint32_t entsize; // sizeof(struct _objc_method)
5139/// uint32_t method_count;
5140/// struct _objc_method method_list[method_count];
5141/// }
5142///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005143llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5144 const char *Section,
5145 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005146 // Return null for empty list.
5147 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005148 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005149
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005150 std::vector<llvm::Constant*> Values(3);
5151 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005152 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005153 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005154 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005155 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005156 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005157 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005158 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005159 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005160
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005161 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005162 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005163 llvm::GlobalValue::InternalLinkage,
5164 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005165 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005166 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005167 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005168 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005169 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005170 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005171 ObjCTypes.MethodListnfABIPtrTy);
5172}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005173
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005174/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5175/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005176llvm::GlobalVariable *
5177CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5178 const ObjCIvarDecl *Ivar) {
5179 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005180 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005181 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005182 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005183 CGM.getModule().getGlobalVariable(Name);
5184 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005185 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005186 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005187 false,
5188 llvm::GlobalValue::ExternalLinkage,
5189 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005190 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005191 return IvarOffsetGV;
5192}
5193
Daniel Dunbare83be122010-04-02 21:14:02 +00005194llvm::Constant *
5195CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5196 const ObjCIvarDecl *Ivar,
5197 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005198 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005199 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005200 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005201 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005202 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005203
Mike Stumpf5408fe2009-05-16 07:57:57 +00005204 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5205 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005206 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5207 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005208 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005209 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005210 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005211 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005212 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005213 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005214}
5215
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005216/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005217/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005218/// IvarListnfABIPtrTy.
5219/// struct _ivar_t {
5220/// unsigned long int *offset; // pointer to ivar offset location
5221/// char *name;
5222/// char *type;
5223/// uint32_t alignment;
5224/// uint32_t size;
5225/// }
5226/// struct _ivar_list_t {
5227/// uint32 entsize; // sizeof(struct _ivar_t)
5228/// uint32 count;
5229/// struct _iver_t list[count];
5230/// }
5231///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005232
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005233llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005234 const ObjCImplementationDecl *ID) {
5235
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005236 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005237
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005238 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5239 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005240
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005241 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
Daniel Dunbar91636d62009-04-20 00:33:43 +00005243 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005244 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005245 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005246
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005247 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5248 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005249 // Ignore unnamed bit-fields.
5250 if (!IVD->getDeclName())
5251 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005253 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005254 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5255 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005256 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005257 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005258 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005259 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005260 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005261 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005262 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005263 // NOTE. Size of a bitfield does not match gcc's, because of the
5264 // way bitfields are treated special in each. But I am told that
5265 // 'size' for bitfield ivars is ignored by the runtime so it does
5266 // not matter. If it matters, there is enough info to get the
5267 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005268 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005269 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005270 }
5271 // Return null for empty list.
5272 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005273 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005274 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005275 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005276 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5277 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005278 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005279 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005280 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005281 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005282 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5283 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005284 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005285 llvm::GlobalValue::InternalLinkage,
5286 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005287 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005288 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005289 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005290 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291
Chris Lattnerad64e022009-07-17 23:57:13 +00005292 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005293 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005294}
5295
5296llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005298 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
Fariborz Jahanianda320092009-01-29 19:24:30 +00005300 if (!Entry) {
5301 // We use the initializer as a marker of whether this is a forward
5302 // reference or not. At module finalization we add the empty
5303 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304 Entry =
5305 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5306 llvm::GlobalValue::ExternalLinkage,
5307 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005308 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005309 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311
Fariborz Jahanianda320092009-01-29 19:24:30 +00005312 return Entry;
5313}
5314
5315/// GetOrEmitProtocol - Generate the protocol meta-data:
5316/// @code
5317/// struct _protocol_t {
5318/// id isa; // NULL
5319/// const char * const protocol_name;
5320/// const struct _protocol_list_t * protocol_list; // super protocols
5321/// const struct method_list_t * const instance_methods;
5322/// const struct method_list_t * const class_methods;
5323/// const struct method_list_t *optionalInstanceMethods;
5324/// const struct method_list_t *optionalClassMethods;
5325/// const struct _prop_list_t * properties;
5326/// const uint32_t size; // sizeof(struct _protocol_t)
5327/// const uint32_t flags; // = 0
5328/// }
5329/// @endcode
5330///
5331
5332llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005333 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005334 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005335
Fariborz Jahanianda320092009-01-29 19:24:30 +00005336 // Early exit if a defining object has already been generated.
5337 if (Entry && Entry->hasInitializer())
5338 return Entry;
5339
Fariborz Jahanianda320092009-01-29 19:24:30 +00005340 // Construct method lists.
5341 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5342 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005344 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005345 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005346 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005347 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5348 OptInstanceMethods.push_back(C);
5349 } else {
5350 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005351 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005352 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005353
5354 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005355 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005356 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005357 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005358 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5359 OptClassMethods.push_back(C);
5360 } else {
5361 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005362 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005363 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005364
Fariborz Jahanianda320092009-01-29 19:24:30 +00005365 std::vector<llvm::Constant*> Values(10);
5366 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005367 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005368 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5370 PD->protocol_begin(),
5371 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005373 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005374 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005375 "__DATA, __objc_const",
5376 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005377 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005378 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 "__DATA, __objc_const",
5380 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005381 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005382 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005383 "__DATA, __objc_const",
5384 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005385 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005386 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005387 "__DATA, __objc_const",
5388 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005389 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005390 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005391 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005392 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005393 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005394 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005395 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005396 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397
Fariborz Jahanianda320092009-01-29 19:24:30 +00005398 if (Entry) {
5399 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005400 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005401 Entry->setInitializer(Init);
5402 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005404 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5405 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5406 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005407 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005408 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005409 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005411 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005412 CGM.AddUsedGlobal(Entry);
5413
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005414 // Use this protocol meta-data to build protocol list table in section
5415 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005416 llvm::GlobalVariable *PTGV =
5417 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5418 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5419 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005420 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005421 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005422 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005423 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005424 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005425 return Entry;
5426}
5427
5428/// EmitProtocolList - Generate protocol list meta-data:
5429/// @code
5430/// struct _protocol_list_t {
5431/// long protocol_count; // Note, this is 32/64 bit
5432/// struct _protocol_t[protocol_count];
5433/// }
5434/// @endcode
5435///
5436llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005437CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5438 ObjCProtocolDecl::protocol_iterator begin,
5439 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005440 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005441
Fariborz Jahanianda320092009-01-29 19:24:30 +00005442 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005444 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005445
Daniel Dunbar948e2582009-02-15 07:36:20 +00005446 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005447 llvm::SmallString<256> TmpName;
5448 Name.toVector(TmpName);
5449 llvm::GlobalVariable *GV =
5450 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005451 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005452 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005453
Daniel Dunbar948e2582009-02-15 07:36:20 +00005454 for (; begin != end; ++begin)
5455 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5456
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005458 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 ObjCTypes.ProtocolnfABIPtrTy));
5460
Fariborz Jahanianda320092009-01-29 19:24:30 +00005461 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005462 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005463 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005464 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005465 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005466 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467 ProtocolRefs.size()),
5468 ProtocolRefs);
5469
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005470 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005471 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005472 llvm::GlobalValue::InternalLinkage,
5473 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005474 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005475 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005476 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005477 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005478 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005479 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005480 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005481}
5482
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005483/// GetMethodDescriptionConstant - This routine build following meta-data:
5484/// struct _objc_method {
5485/// SEL _cmd;
5486/// char *method_type;
5487/// char *_imp;
5488/// }
5489
5490llvm::Constant *
5491CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5492 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005493 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005494 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5495 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005496 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005497 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005498 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005499 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005500}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005501
5502/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5503/// This code gen. amounts to generating code for:
5504/// @code
5505/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5506/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005507///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005508LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005509 CodeGen::CodeGenFunction &CGF,
5510 QualType ObjectTy,
5511 llvm::Value *BaseValue,
5512 const ObjCIvarDecl *Ivar,
5513 unsigned CVRQualifiers) {
5514 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005515 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5516 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005517}
5518
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005519llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005520 CodeGen::CodeGenFunction &CGF,
5521 const ObjCInterfaceDecl *Interface,
5522 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005523 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005524}
5525
Fariborz Jahanian46551122009-02-04 00:22:57 +00005526CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005527 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005528 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005529 QualType ResultType,
5530 Selector Sel,
5531 llvm::Value *Receiver,
5532 QualType Arg0Ty,
5533 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005534 const CallArgList &CallArgs,
5535 const ObjCMethodDecl *Method) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005536 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5537 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005538 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005539 llvm::Value *Arg0 = Receiver;
5540 if (!IsSuper)
5541 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005542
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005543 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005544 // FIXME. This is too much work to get the ABI-specific result type needed to
5545 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005546 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005547 = Types.getFunctionInfo(ResultType, CallArgList(),
5548 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005549 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005550 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005551 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00005552 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner9b7d6702010-08-18 16:09:06 +00005553 if (IsSuper) {
5554 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5555 Name += "objc_msgSendSuper2_stret_fixup";
5556 } else {
5557 Fn = ObjCTypes.getMessageSendStretFixupFn();
5558 Name += "objc_msgSend_stret_fixup";
5559 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005560 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5561 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5562 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005563 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005564 if (IsSuper) {
5565 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5566 Name += "objc_msgSendSuper2_fixup";
5567 } else {
5568 Fn = ObjCTypes.getMessageSendFixupFn();
5569 Name += "objc_msgSend_fixup";
5570 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005571 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005572 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005573 Name += '_';
5574 std::string SelName(Sel.getAsString());
5575 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005576 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005577 if (SelName[i] == ':')
5578 SelName[i] = '_';
5579 Name += SelName;
5580 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5581 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005582 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005583 std::vector<llvm::Constant*> Values(2);
5584 Values[0] = Fn;
5585 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005586 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005587 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005588 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005589 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005590 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005591 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005592 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005593 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005594 }
5595 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005596
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005597 CallArgList ActualArgs;
Eli Friedman04c9a492011-05-02 17:57:46 +00005598 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
5599 ActualArgs.add(RValue::get(Arg1), ObjCTypes.MessageRefCPtrTy);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005600 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005601 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005602 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005603 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5604 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005605 const llvm::FunctionType *FTy =
5606 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005607 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005608 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005609 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005610}
5611
5612/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005613CodeGen::RValue
5614CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005615 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005616 QualType ResultType,
5617 Selector Sel,
5618 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005619 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005620 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005621 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005622 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005623 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5624 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005625 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005626 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005627 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005628 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005629 false, CallArgs, Method);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005630}
5631
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005632llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005633CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005634 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5635
Daniel Dunbardfff2302009-03-02 05:18:14 +00005636 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005637 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005638 false, llvm::GlobalValue::ExternalLinkage,
5639 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005640 }
5641
5642 return GV;
5643}
5644
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005645llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5646 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005647 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005649 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005650 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005651 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652 Entry =
5653 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005654 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005655 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005656 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005657 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005658 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005660 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005661 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005662 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005664 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005665}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005666
Daniel Dunbar11394522009-04-18 08:51:00 +00005667llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005668CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005669 const ObjCInterfaceDecl *ID) {
5670 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005671
Daniel Dunbar11394522009-04-18 08:51:00 +00005672 if (!Entry) {
5673 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5674 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675 Entry =
5676 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005677 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005678 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005679 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005680 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005681 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005682 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005683 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005684 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005685 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005686
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005687 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005688}
5689
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005690/// EmitMetaClassRef - Return a Value * of the address of _class_t
5691/// meta-data
5692///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005693llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5694 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005695 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5696 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005697 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005698
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005699 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005700 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005702 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005703 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005704 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005705 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005706 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005707 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005708 ObjCTypes.ClassnfABIPtrTy));
5709
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005710 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005711 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005713 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005714}
5715
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005716/// GetClass - Return a reference to the class for the given interface
5717/// decl.
5718llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5719 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005720 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005721 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5722 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5723 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5724 }
5725
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005726 return EmitClassRef(Builder, ID);
5727}
5728
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005729/// Generates a message send where the super is the receiver. This is
5730/// a message send to self with special delivery semantics indicating
5731/// which class's method should be called.
5732CodeGen::RValue
5733CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005734 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005735 QualType ResultType,
5736 Selector Sel,
5737 const ObjCInterfaceDecl *Class,
5738 bool isCategoryImpl,
5739 llvm::Value *Receiver,
5740 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005741 const CodeGen::CallArgList &CallArgs,
5742 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005743 // ...
5744 // Create and init a super structure; this is a (receiver, class)
5745 // pair we will pass to objc_msgSendSuper.
5746 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005747 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005748
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005749 llvm::Value *ReceiverAsObject =
5750 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5751 CGF.Builder.CreateStore(ReceiverAsObject,
5752 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005753
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005755 llvm::Value *Target;
5756 if (IsClassMessage) {
5757 if (isCategoryImpl) {
5758 // Message sent to "super' in a class method defined in
5759 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005760 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005761 Target = CGF.Builder.CreateStructGEP(Target, 0);
5762 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005763 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005764 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005765 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005766 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005767
Mike Stumpf5408fe2009-05-16 07:57:57 +00005768 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5769 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005770 const llvm::Type *ClassTy =
5771 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5772 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5773 CGF.Builder.CreateStore(Target,
5774 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005775
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005776 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005777 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5778 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005779 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005780 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005781 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005782 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005783 true, CallArgs, Method);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005784}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005785
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005786llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005787 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005788 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005789
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005790 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005791 llvm::Constant *Casted =
5792 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5793 ObjCTypes.SelectorPtrTy);
5794 Entry =
5795 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5796 llvm::GlobalValue::InternalLinkage,
5797 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005798 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005799 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005800 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005801
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005802 if (lval)
5803 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005804 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005805}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005806/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005807/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005808///
5809void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005810 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005811 llvm::Value *dst,
5812 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005813 const llvm::Type * SrcTy = src->getType();
5814 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005815 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005816 assert(Size <= 8 && "does not support size > 8");
5817 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5818 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005819 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5820 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005821 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5822 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005823 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5824 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005825 return;
5826}
5827
5828/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5829/// objc_assign_strongCast (id src, id *dst)
5830///
5831void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005833 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005834 const llvm::Type * SrcTy = src->getType();
5835 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005836 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005837 assert(Size <= 8 && "does not support size > 8");
5838 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005839 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005840 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5841 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005842 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5843 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005844 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005845 src, dst, "weakassign");
5846 return;
5847}
5848
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005849void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850 CodeGen::CodeGenFunction &CGF,
5851 llvm::Value *DestPtr,
5852 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005853 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005854 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5855 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005856 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005857 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005858 return;
5859}
5860
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005861/// EmitObjCWeakRead - Code gen for loading value of a __weak
5862/// object: objc_read_weak (id *src)
5863///
5864llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005865 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005866 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005867 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005868 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5869 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005870 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005871 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005872 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873 return read_weak;
5874}
5875
5876/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5877/// objc_assign_weak (id src, id *dst)
5878///
5879void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005880 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005881 const llvm::Type * SrcTy = src->getType();
5882 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005883 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005884 assert(Size <= 8 && "does not support size > 8");
5885 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5886 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005887 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5888 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005889 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5890 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005891 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005892 src, dst, "weakassign");
5893 return;
5894}
5895
5896/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5897/// objc_assign_global (id src, id *dst)
5898///
5899void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005900 llvm::Value *src, llvm::Value *dst,
5901 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005902 const llvm::Type * SrcTy = src->getType();
5903 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005904 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005905 assert(Size <= 8 && "does not support size > 8");
5906 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5907 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005908 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5909 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005910 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5911 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005912 if (!threadlocal)
5913 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5914 src, dst, "globalassign");
5915 else
5916 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5917 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005918 return;
5919}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005920
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921void
John McCallf1549f62010-07-06 01:34:17 +00005922CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5923 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005924 EmitAtSynchronizedStmt(CGF, S,
5925 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5926 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005927}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005928
John McCall5a180392010-07-24 00:37:23 +00005929llvm::Constant *
5930CGObjCNonFragileABIMac::GetEHType(QualType T) {
5931 // There's a particular fixed type info for 'id'.
5932 if (T->isObjCIdType() ||
5933 T->isObjCQualifiedIdType()) {
5934 llvm::Constant *IDEHType =
5935 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5936 if (!IDEHType)
5937 IDEHType =
5938 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5939 false,
5940 llvm::GlobalValue::ExternalLinkage,
5941 0, "OBJC_EHTYPE_id");
5942 return IDEHType;
5943 }
5944
5945 // All other types should be Objective-C interface pointer types.
5946 const ObjCObjectPointerType *PT =
5947 T->getAs<ObjCObjectPointerType>();
5948 assert(PT && "Invalid @catch type.");
5949 const ObjCInterfaceType *IT = PT->getInterfaceType();
5950 assert(IT && "Invalid @catch type.");
5951 return GetInterfaceEHType(IT->getDecl(), false);
5952}
5953
John McCallf1549f62010-07-06 01:34:17 +00005954void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5955 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005956 EmitTryCatchStmt(CGF, S,
5957 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5958 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5959 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005960}
5961
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005962/// EmitThrowStmt - Generate code for a throw statement.
5963void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5964 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005965 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00005966 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00005967 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
5968 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00005969 llvm::Value *Args[] = { Exception };
5970 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
5971 Args, Args+1)
5972 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005973 } else {
John McCall7ec404c2010-10-16 08:21:07 +00005974 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
5975 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005976 }
5977
John McCall7ec404c2010-10-16 08:21:07 +00005978 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005979 CGF.Builder.ClearInsertionPoint();
5980}
Daniel Dunbare588b992009-03-01 04:46:24 +00005981
John McCall5a180392010-07-24 00:37:23 +00005982llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005983CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005984 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005985 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005986
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005987 // If we don't need a definition, return the entry if found or check
5988 // if we use an external reference.
5989 if (!ForDefinition) {
5990 if (Entry)
5991 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005992
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005993 // If this type (or a super class) has the __objc_exception__
5994 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005995 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005996 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005997 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005998 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005999 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006000 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006001 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006002 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006004 // Otherwise we need to either make a new entry or fill in the
6005 // initializer.
6006 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006007 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006008 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006009 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006010 CGM.getModule().getGlobalVariable(VTableName);
6011 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006012 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6013 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006014 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006015 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006016
Chris Lattner77b89b82010-06-27 07:15:29 +00006017 llvm::Value *VTableIdx =
6018 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006019
6020 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006021 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006022 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006023 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006024 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006025 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006026
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006027 if (Entry) {
6028 Entry->setInitializer(Init);
6029 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006030 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006031 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006032 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006033 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006034 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006035 }
6036
John McCall1fb0caa2010-10-22 21:05:15 +00006037 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006038 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006039 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6040 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006041
6042 if (ForDefinition) {
6043 Entry->setSection("__DATA,__objc_const");
6044 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6045 } else {
6046 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6047 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006048
6049 return Entry;
6050}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006051
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006052/* *** */
6053
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006054CodeGen::CGObjCRuntime *
6055CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006056 if (CGM.getLangOptions().ObjCNonFragileABI)
6057 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006058 return new CGObjCMac(CGM);
6059}