blob: 51f5977c848e7041cde01d382b9f28b13fb2b85c [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//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
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");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001525 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001526 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001527 CGF.getContext().getObjCSelType()));
1528 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001529
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001530 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001531 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001532 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001533 const llvm::FunctionType *FTy =
1534 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001535
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001536 if (Method)
1537 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1538 CGM.getContext().getCanonicalType(ResultType) &&
1539 "Result type mismatch!");
1540
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001541 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001542 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00001543 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001544 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001545 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001546 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1547 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1548 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001549 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001550 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001551 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001552 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001553 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001554 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001555}
1556
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001557static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1558 if (FQT.isObjCGCStrong())
1559 return Qualifiers::Strong;
1560
1561 if (FQT.isObjCGCWeak())
1562 return Qualifiers::Weak;
1563
1564 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1565 return Qualifiers::Strong;
1566
1567 if (const PointerType *PT = FQT->getAs<PointerType>())
1568 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1569
1570 return Qualifiers::GCNone;
1571}
1572
John McCall6b5a61b2011-02-07 10:33:21 +00001573llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1574 const CGBlockInfo &blockInfo) {
1575 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001576 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001577
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001578 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall6b5a61b2011-02-07 10:33:21 +00001579 return nullPtr;
1580
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001581 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001582 SkipIvars.clear();
1583 IvarsInfo.clear();
1584 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1585 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1586
Fariborz Jahanian81979822010-09-09 00:21:45 +00001587 // __isa is the first field in block descriptor and must assume by runtime's
1588 // convention that it is GC'able.
1589 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001590
1591 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1592
1593 // Calculate the basic layout of the block structure.
1594 const llvm::StructLayout *layout =
1595 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1596
1597 // Ignore the optional 'this' capture: C++ objects are not assumed
1598 // to be GC'ed.
1599
1600 // Walk the captured variables.
1601 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1602 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1603 const VarDecl *variable = ci->getVariable();
1604 QualType type = variable->getType();
1605
1606 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1607
1608 // Ignore constant captures.
1609 if (capture.isConstant()) continue;
1610
1611 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1612
1613 // __block variables are passed by their descriptor address.
1614 if (ci->isByRef()) {
1615 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001616 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001617 }
1618
1619 assert(!type->isArrayType() && "array variable should not be caught");
1620 if (const RecordType *record = type->getAs<RecordType>()) {
1621 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001622 continue;
1623 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001624
John McCall6b5a61b2011-02-07 10:33:21 +00001625 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1626 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1627
1628 if (GCAttr == Qualifiers::Strong)
1629 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1630 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001631 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001632 SkipIvars.push_back(GC_IVAR(fieldOffset,
1633 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001634 }
1635
1636 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001637 return nullPtr;
1638
1639 // Sort on byte position; captures might not be allocated in order,
1640 // and unions can do funny things.
1641 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1642 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001643
1644 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001645 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001646 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1647 printf("\n block variable layout for block: ");
1648 const unsigned char *s = (unsigned char*)BitMap.c_str();
1649 for (unsigned i = 0; i < BitMap.size(); i++)
1650 if (!(s[i] & 0xf0))
1651 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1652 else
1653 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1654 printf("\n");
1655 }
1656
1657 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001658}
1659
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001660llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001661 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001662 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001663 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001664 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1665
Owen Anderson3c4972d2009-07-29 18:54:39 +00001666 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001668}
1669
Fariborz Jahanianda320092009-01-29 19:24:30 +00001670void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001671 // FIXME: We shouldn't need this, the protocol decl should contain enough
1672 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001673 DefinedProtocols.insert(PD->getIdentifier());
1674
1675 // If we have generated a forward reference to this protocol, emit
1676 // it now. Otherwise do nothing, the protocol objects are lazily
1677 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001678 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001679 GetOrEmitProtocol(PD);
1680}
1681
Fariborz Jahanianda320092009-01-29 19:24:30 +00001682llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001683 if (DefinedProtocols.count(PD->getIdentifier()))
1684 return GetOrEmitProtocol(PD);
1685 return GetOrEmitProtocolRef(PD);
1686}
1687
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001688/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001689// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1690struct _objc_protocol {
1691struct _objc_protocol_extension *isa;
1692char *protocol_name;
1693struct _objc_protocol_list *protocol_list;
1694struct _objc__method_prototype_list *instance_methods;
1695struct _objc__method_prototype_list *class_methods
1696};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001697
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001698See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001699*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001700llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1701 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1702
1703 // Early exit if a defining object has already been generated.
1704 if (Entry && Entry->hasInitializer())
1705 return Entry;
1706
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001707 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001708 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001709 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1710
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001711 // Construct method lists.
1712 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1713 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001714 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001715 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001716 ObjCMethodDecl *MD = *i;
1717 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1718 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1719 OptInstanceMethods.push_back(C);
1720 } else {
1721 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001722 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001723 }
1724
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001725 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001726 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001727 ObjCMethodDecl *MD = *i;
1728 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1729 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1730 OptClassMethods.push_back(C);
1731 } else {
1732 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001733 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001734 }
1735
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001737 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001738 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001740 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001741 PD->protocol_begin(),
1742 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001743 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001744 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001745 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1746 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001747 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001748 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001749 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1750 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001751 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001752 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001753
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001754 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001755 // Already created, fix the linkage and update the initializer.
1756 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001757 Entry->setInitializer(Init);
1758 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001759 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001760 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001761 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001762 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001763 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001764 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001765 // FIXME: Is this necessary? Why only for protocol?
1766 Entry->setAlignment(4);
1767 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001768 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001769
1770 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771}
1772
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001773llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001774 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1775
1776 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001777 // We use the initializer as a marker of whether this is a forward
1778 // reference or not. At module finalization we add the empty
1779 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001780 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001781 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001782 llvm::GlobalValue::ExternalLinkage,
1783 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001784 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001785 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001786 // FIXME: Is this necessary? Why only for protocol?
1787 Entry->setAlignment(4);
1788 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001789
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001790 return Entry;
1791}
1792
1793/*
1794 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001795 uint32_t size;
1796 struct objc_method_description_list *optional_instance_methods;
1797 struct objc_method_description_list *optional_class_methods;
1798 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001799 };
1800*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001801llvm::Constant *
1802CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1803 const ConstantVector &OptInstanceMethods,
1804 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001805 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001806 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001807 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001808 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001809 Values[1] =
1810 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001811 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001812 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1813 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001815 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001816 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1817 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001818 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001819 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001820
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001821 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001822 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001823 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001824 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001825
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001826 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001827 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001828
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001829 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001830 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001831 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001832 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001833}
1834
1835/*
1836 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001837 struct objc_protocol_list *next;
1838 long count;
1839 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001840 };
1841*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001842llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001843CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001844 ObjCProtocolDecl::protocol_iterator begin,
1845 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001846 std::vector<llvm::Constant*> ProtocolRefs;
1847
Daniel Dunbardbc933702008-08-21 21:57:41 +00001848 for (; begin != end; ++begin)
1849 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001850
1851 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001852 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001853 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001854
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001855 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001856 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001857
1858 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001859 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001860 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001861 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001862 ProtocolRefs.size() - 1);
1863 Values[2] =
1864 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1865 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001866 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001867
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001868 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001869 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001870 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001871 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001872 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873}
1874
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001875void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1876 std::vector<llvm::Constant*> &Properties,
1877 const Decl *Container,
1878 const ObjCProtocolDecl *PROTO,
1879 const ObjCCommonTypesHelper &ObjCTypes) {
1880 std::vector<llvm::Constant*> Prop(2);
1881 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1882 E = PROTO->protocol_end(); P != E; ++P)
1883 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1884 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1885 E = PROTO->prop_end(); I != E; ++I) {
1886 const ObjCPropertyDecl *PD = *I;
1887 if (!PropertySet.insert(PD->getIdentifier()))
1888 continue;
1889 Prop[0] = GetPropertyName(PD->getIdentifier());
1890 Prop[1] = GetPropertyTypeString(PD, Container);
1891 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1892 }
1893}
1894
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001895/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001896 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001897 const char * const name;
1898 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001899 };
1900
1901 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001902 uint32_t entsize; // sizeof (struct _objc_property)
1903 uint32_t prop_count;
1904 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001905 };
1906*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001907llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001908 const Decl *Container,
1909 const ObjCContainerDecl *OCD,
1910 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001911 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001912 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001913 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1914 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001915 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001916 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001917 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001918 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001919 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001920 Prop));
1921 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001922 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001923 for (ObjCInterfaceDecl::all_protocol_iterator
1924 P = OID->all_referenced_protocol_begin(),
1925 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001926 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1927 ObjCTypes);
1928 }
1929 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1930 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1931 E = CD->protocol_end(); P != E; ++P)
1932 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1933 ObjCTypes);
1934 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001935
1936 // Return null for empty list.
1937 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001938 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001939
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001940 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001941 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001942 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001943 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1944 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001945 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001946 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001947 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001948 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001949
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950 llvm::GlobalVariable *GV =
1951 CreateMetadataVar(Name, Init,
1952 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001953 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001954 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001955 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001956 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001957}
1958
1959/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001960 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001961 int count;
1962 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001963 };
1964*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001965llvm::Constant *
1966CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1967 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001968 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001969 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1970 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001971 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001972 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001973 Desc);
1974}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001975
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001976llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001977 const char *Section,
1978 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001979 // Return null for empty list.
1980 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001981 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001982
1983 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001984 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001985 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001986 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001987 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001988 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001989
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001990 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001991 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001992 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001993}
1994
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001995/*
1996 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001997 char *category_name;
1998 char *class_name;
1999 struct _objc_method_list *instance_methods;
2000 struct _objc_method_list *class_methods;
2001 struct _objc_protocol_list *protocols;
2002 uint32_t size; // <rdar://4585769>
2003 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002004 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002005*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002006void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002007 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002008
Mike Stumpf5408fe2009-05-16 07:57:57 +00002009 // FIXME: This is poor design, the OCD should have a pointer to the category
2010 // decl. Additionally, note that Category can be null for the @implementation
2011 // w/o an @interface case. Sema should just create one for us as it does for
2012 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002013 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002014 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002015 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002016
2017 llvm::SmallString<256> ExtName;
2018 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2019 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002020
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002021 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002022 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002023 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002024 // Instance methods should always be defined.
2025 InstanceMethods.push_back(GetMethodConstant(*i));
2026 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002027 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002028 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002029 // Class methods should always be defined.
2030 ClassMethods.push_back(GetMethodConstant(*i));
2031 }
2032
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002033 std::vector<llvm::Constant*> Values(7);
2034 Values[0] = GetClassName(OCD->getIdentifier());
2035 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002036 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002037 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002038 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002039 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002040 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002041 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002042 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002043 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002044 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002045 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002046 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002047 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002048 Category->protocol_begin(),
2049 Category->protocol_end());
2050 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002051 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002052 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002053 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002054
2055 // If there is no category @interface then there can be no properties.
2056 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002057 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002058 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002059 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002060 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002061 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002062
Owen Anderson08e25242009-07-27 22:29:56 +00002063 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002064 Values);
2065
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002066 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002067 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002068 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002069 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002070 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002071 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002072}
2073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002074// FIXME: Get from somewhere?
2075enum ClassFlags {
2076 eClassFlags_Factory = 0x00001,
2077 eClassFlags_Meta = 0x00002,
2078 // <rdr://5142207>
2079 eClassFlags_HasCXXStructors = 0x02000,
2080 eClassFlags_Hidden = 0x20000,
2081 eClassFlags_ABI2_Hidden = 0x00010,
2082 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2083};
2084
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002085/*
2086 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002087 Class isa;
2088 Class super_class;
2089 const char *name;
2090 long version;
2091 long info;
2092 long instance_size;
2093 struct _objc_ivar_list *ivars;
2094 struct _objc_method_list *methods;
2095 struct _objc_cache *cache;
2096 struct _objc_protocol_list *protocols;
2097 // Objective-C 1.0 extensions (<rdr://4585769>)
2098 const char *ivar_layout;
2099 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002100 };
2101
2102 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002105 DefinedSymbols.insert(ID->getIdentifier());
2106
Chris Lattner8ec03f52008-11-24 03:54:41 +00002107 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002108 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002110 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002112 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002113 Interface->all_referenced_protocol_begin(),
2114 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002115 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002116 if (ID->getNumIvarInitializers())
2117 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002118 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002119 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002120
2121 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002122 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002123 Flags |= eClassFlags_Hidden;
2124
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002126 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002127 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002128 // Instance methods should always be defined.
2129 InstanceMethods.push_back(GetMethodConstant(*i));
2130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002131 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002132 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002133 // Class methods should always be defined.
2134 ClassMethods.push_back(GetMethodConstant(*i));
2135 }
2136
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002137 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002138 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002139 ObjCPropertyImplDecl *PID = *i;
2140
2141 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2142 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2143
2144 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2145 if (llvm::Constant *C = GetMethodConstant(MD))
2146 InstanceMethods.push_back(C);
2147 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2148 if (llvm::Constant *C = GetMethodConstant(MD))
2149 InstanceMethods.push_back(C);
2150 }
2151 }
2152
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002154 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002156 // Record a reference to the super class.
2157 LazySymbols.insert(Super->getIdentifier());
2158
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002159 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002160 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161 ObjCTypes.ClassPtrTy);
2162 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002163 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164 }
2165 Values[ 2] = GetClassName(ID->getIdentifier());
2166 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002167 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2168 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2169 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002170 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002171 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002172 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002173 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002174 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002175 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002176 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002178 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002180 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002182 std::string Name("\01L_OBJC_CLASS_");
2183 Name += ClassName;
2184 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2185 // Check for a forward reference.
2186 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2187 if (GV) {
2188 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2189 "Forward metaclass reference has incorrect type.");
2190 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2191 GV->setInitializer(Init);
2192 GV->setSection(Section);
2193 GV->setAlignment(4);
2194 CGM.AddUsedGlobal(GV);
2195 }
2196 else
2197 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002198 DefinedClasses.push_back(GV);
2199}
2200
2201llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2202 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002203 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002205 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206
John McCall1fb0caa2010-10-22 21:05:15 +00002207 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002209
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210 std::vector<llvm::Constant*> Values(12);
2211 // The isa for the metaclass is the root of the hierarchy.
2212 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2213 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2214 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002215 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002216 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002218 // The super class for the metaclass is emitted as the name of the
2219 // super class. The runtime fixes this up to point to the
2220 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002222 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002223 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 ObjCTypes.ClassPtrTy);
2225 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002226 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 }
2228 Values[ 2] = GetClassName(ID->getIdentifier());
2229 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002230 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2231 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2232 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002233 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002234 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002235 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002236 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002237 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002239 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002240 Values[ 9] = Protocols;
2241 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002242 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002244 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002245 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246 Values);
2247
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002248 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002249 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002250
2251 // Check for a forward reference.
2252 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2253 if (GV) {
2254 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2255 "Forward metaclass reference has incorrect type.");
2256 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2257 GV->setInitializer(Init);
2258 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002259 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002260 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002261 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002262 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002264 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002265 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266
2267 return GV;
2268}
2269
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002270llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002271 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002272
Mike Stumpf5408fe2009-05-16 07:57:57 +00002273 // FIXME: Should we look these up somewhere other than the module. Its a bit
2274 // silly since we only generate these while processing an implementation, so
2275 // exactly one pointer would work if know when we entered/exitted an
2276 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002277
2278 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002279 // Previously, metaclass with internal linkage may have been defined.
2280 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002281 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2282 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002283 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2284 "Forward metaclass reference has incorrect type.");
2285 return GV;
2286 } else {
2287 // Generate as an external reference to keep a consistent
2288 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002289 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002290 llvm::GlobalValue::ExternalLinkage,
2291 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002292 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002293 }
2294}
2295
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002296llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2297 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2298
2299 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2300 true)) {
2301 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2302 "Forward class metadata reference has incorrect type.");
2303 return GV;
2304 } else {
2305 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2306 llvm::GlobalValue::ExternalLinkage,
2307 0,
2308 Name);
2309 }
2310}
2311
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312/*
2313 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002314 uint32_t size;
2315 const char *weak_ivar_layout;
2316 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 };
2318*/
2319llvm::Constant *
2320CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002321 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002322 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323
2324 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002325 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002326 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002327 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002328 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329
2330 // Return null if no extension bits are used.
2331 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002332 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002334 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002335 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002336 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002338 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339}
2340
2341/*
2342 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002343 char *ivar_name;
2344 char *ivar_type;
2345 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 };
2347
2348 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002349 int ivar_count;
2350 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002352*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002353llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002354 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 std::vector<llvm::Constant*> Ivars, Ivar(3);
2356
2357 // When emitting the root class GCC emits ivar entries for the
2358 // actual class structure. It is not clear if we need to follow this
2359 // behavior; for now lets try and get away with not doing it. If so,
2360 // the cleanest solution would be to make up an ObjCInterfaceDecl
2361 // for the class.
2362 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002363 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364
2365 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002367
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002368 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002369 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002371 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2372 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002373 // Ignore unnamed bit-fields.
2374 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002375 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002376 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2377 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002378 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002379 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002380 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381 }
2382
2383 // Return null for empty list.
2384 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002385 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002386
2387 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002388 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002389 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002390 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002391 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002392 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002394 llvm::GlobalVariable *GV;
2395 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002396 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002398 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002399 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002400 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002401 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002402 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002403 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002404}
2405
2406/*
2407 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002408 SEL method_name;
2409 char *method_types;
2410 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002413 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002414 struct objc_method_list *obsolete;
2415 int count;
2416 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417 };
2418*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002419
2420/// GetMethodConstant - Return a struct objc_method constant for the
2421/// given method if it has been defined. The result is null if the
2422/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002423llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002424 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002425 if (!Fn)
2426 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002427
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002428 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002429 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002430 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002431 ObjCTypes.SelectorPtrTy);
2432 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002433 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002434 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002435}
2436
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002437llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002438 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002439 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 // Return null for empty list.
2441 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002442 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002443
2444 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002445 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002446 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002447 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002449 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002450 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002452 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002453 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002455}
2456
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002457llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002458 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002459 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002460 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002461
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002462 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002463 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002464 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002465 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002466 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002467 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002468 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002469 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002470 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002471
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002472 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002473}
2474
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002475llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002476CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002477 llvm::Constant *Init,
2478 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002479 unsigned Align,
2480 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002481 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002483 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002484 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002485 if (Section)
2486 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002487 if (Align)
2488 GV->setAlignment(Align);
2489 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002490 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002491 return GV;
2492}
2493
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002494llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002495 // Abuse this interface function as a place to finalize.
2496 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002497 return NULL;
2498}
2499
Chris Lattner74391b42009-03-22 21:03:39 +00002500llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002501 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002502}
2503
Chris Lattner74391b42009-03-22 21:03:39 +00002504llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002505 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002506}
2507
David Chisnall8fac25d2010-12-26 22:13:16 +00002508llvm::Constant *CGObjCMac::GetGetStructFunction() {
2509 return ObjCTypes.getCopyStructFn();
2510}
2511llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002512 return ObjCTypes.getCopyStructFn();
2513}
2514
Chris Lattner74391b42009-03-22 21:03:39 +00002515llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002516 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002517}
2518
John McCallf1549f62010-07-06 01:34:17 +00002519void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2520 return EmitTryOrSynchronizedStmt(CGF, S);
2521}
2522
2523void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2524 const ObjCAtSynchronizedStmt &S) {
2525 return EmitTryOrSynchronizedStmt(CGF, S);
2526}
2527
John McCallcc505292010-07-21 06:59:36 +00002528namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002529 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002530 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002531 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002532 llvm::Value *CallTryExitVar;
2533 llvm::Value *ExceptionData;
2534 ObjCTypesHelper &ObjCTypes;
2535 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002536 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002537 llvm::Value *CallTryExitVar,
2538 llvm::Value *ExceptionData,
2539 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002540 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002541 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2542
2543 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2544 // Check whether we need to call objc_exception_try_exit.
2545 // In optimized code, this branch will always be folded.
2546 llvm::BasicBlock *FinallyCallExit =
2547 CGF.createBasicBlock("finally.call_exit");
2548 llvm::BasicBlock *FinallyNoCallExit =
2549 CGF.createBasicBlock("finally.no_call_exit");
2550 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2551 FinallyCallExit, FinallyNoCallExit);
2552
2553 CGF.EmitBlock(FinallyCallExit);
2554 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2555 ->setDoesNotThrow();
2556
2557 CGF.EmitBlock(FinallyNoCallExit);
2558
2559 if (isa<ObjCAtTryStmt>(S)) {
2560 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002561 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2562 // Save the current cleanup destination in case there's
2563 // control flow inside the finally statement.
2564 llvm::Value *CurCleanupDest =
2565 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2566
John McCallcc505292010-07-21 06:59:36 +00002567 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2568
John McCalld96a8e72010-08-11 00:16:14 +00002569 if (CGF.HaveInsertPoint()) {
2570 CGF.Builder.CreateStore(CurCleanupDest,
2571 CGF.getNormalCleanupDestSlot());
2572 } else {
2573 // Currently, the end of the cleanup must always exist.
2574 CGF.EnsureInsertPoint();
2575 }
2576 }
John McCallcc505292010-07-21 06:59:36 +00002577 } else {
2578 // Emit objc_sync_exit(expr); as finally's sole statement for
2579 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002580 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002581 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2582 ->setDoesNotThrow();
2583 }
2584 }
2585 };
John McCall87bb5822010-07-31 23:20:56 +00002586
2587 class FragileHazards {
2588 CodeGenFunction &CGF;
2589 llvm::SmallVector<llvm::Value*, 20> Locals;
2590 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2591
2592 llvm::InlineAsm *ReadHazard;
2593 llvm::InlineAsm *WriteHazard;
2594
2595 llvm::FunctionType *GetAsmFnType();
2596
2597 void collectLocals();
2598 void emitReadHazard(CGBuilderTy &Builder);
2599
2600 public:
2601 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002602
John McCall87bb5822010-07-31 23:20:56 +00002603 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002604 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002605 };
2606}
2607
2608/// Create the fragile-ABI read and write hazards based on the current
2609/// state of the function, which is presumed to be immediately prior
2610/// to a @try block. These hazards are used to maintain correct
2611/// semantics in the face of optimization and the fragile ABI's
2612/// cavalier use of setjmp/longjmp.
2613FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2614 collectLocals();
2615
2616 if (Locals.empty()) return;
2617
2618 // Collect all the blocks in the function.
2619 for (llvm::Function::iterator
2620 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2621 BlocksBeforeTry.insert(&*I);
2622
2623 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2624
2625 // Create a read hazard for the allocas. This inhibits dead-store
2626 // optimizations and forces the values to memory. This hazard is
2627 // inserted before any 'throwing' calls in the protected scope to
2628 // reflect the possibility that the variables might be read from the
2629 // catch block if the call throws.
2630 {
2631 std::string Constraint;
2632 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2633 if (I) Constraint += ',';
2634 Constraint += "*m";
2635 }
2636
2637 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2638 }
2639
2640 // Create a write hazard for the allocas. This inhibits folding
2641 // loads across the hazard. This hazard is inserted at the
2642 // beginning of the catch path to reflect the possibility that the
2643 // variables might have been written within the protected scope.
2644 {
2645 std::string Constraint;
2646 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2647 if (I) Constraint += ',';
2648 Constraint += "=*m";
2649 }
2650
2651 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2652 }
2653}
2654
2655/// Emit a write hazard at the current location.
2656void FragileHazards::emitWriteHazard() {
2657 if (Locals.empty()) return;
2658
2659 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2660 ->setDoesNotThrow();
2661}
2662
John McCall87bb5822010-07-31 23:20:56 +00002663void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2664 assert(!Locals.empty());
2665 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2666 ->setDoesNotThrow();
2667}
2668
2669/// Emit read hazards in all the protected blocks, i.e. all the blocks
2670/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002671void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002672 if (Locals.empty()) return;
2673
2674 CGBuilderTy Builder(CGF.getLLVMContext());
2675
2676 // Iterate through all blocks, skipping those prior to the try.
2677 for (llvm::Function::iterator
2678 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2679 llvm::BasicBlock &BB = *FI;
2680 if (BlocksBeforeTry.count(&BB)) continue;
2681
2682 // Walk through all the calls in the block.
2683 for (llvm::BasicBlock::iterator
2684 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2685 llvm::Instruction &I = *BI;
2686
2687 // Ignore instructions that aren't non-intrinsic calls.
2688 // These are the only calls that can possibly call longjmp.
2689 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2690 if (isa<llvm::IntrinsicInst>(I))
2691 continue;
2692
2693 // Ignore call sites marked nounwind. This may be questionable,
2694 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2695 llvm::CallSite CS(&I);
2696 if (CS.doesNotThrow()) continue;
2697
John McCall0b251722010-08-04 05:59:32 +00002698 // Insert a read hazard before the call. This will ensure that
2699 // any writes to the locals are performed before making the
2700 // call. If the call throws, then this is sufficient to
2701 // guarantee correctness as long as it doesn't also write to any
2702 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002703 Builder.SetInsertPoint(&BB, BI);
2704 emitReadHazard(Builder);
2705 }
2706 }
2707}
2708
2709static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2710 if (V) S.insert(V);
2711}
2712
2713void FragileHazards::collectLocals() {
2714 // Compute a set of allocas to ignore.
2715 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2716 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2717 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2718 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2719
2720 // Collect all the allocas currently in the function. This is
2721 // probably way too aggressive.
2722 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2723 for (llvm::BasicBlock::iterator
2724 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2725 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2726 Locals.push_back(&*I);
2727}
2728
2729llvm::FunctionType *FragileHazards::GetAsmFnType() {
2730 std::vector<const llvm::Type *> Tys(Locals.size());
2731 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2732 Tys[I] = Locals[I]->getType();
2733 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002734}
2735
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002736/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002737
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002738 Objective-C setjmp-longjmp (sjlj) Exception Handling
2739 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002740
John McCallf1549f62010-07-06 01:34:17 +00002741 A catch buffer is a setjmp buffer plus:
2742 - a pointer to the exception that was caught
2743 - a pointer to the previous exception data buffer
2744 - two pointers of reserved storage
2745 Therefore catch buffers form a stack, with a pointer to the top
2746 of the stack kept in thread-local storage.
2747
2748 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2749 objc_exception_try_exit pops the given catch buffer, which is
2750 required to be the top of the EH stack.
2751 objc_exception_throw pops the top of the EH stack, writes the
2752 thrown exception into the appropriate field, and longjmps
2753 to the setjmp buffer. It crashes the process (with a printf
2754 and an abort()) if there are no catch buffers on the stack.
2755 objc_exception_extract just reads the exception pointer out of the
2756 catch buffer.
2757
2758 There's no reason an implementation couldn't use a light-weight
2759 setjmp here --- something like __builtin_setjmp, but API-compatible
2760 with the heavyweight setjmp. This will be more important if we ever
2761 want to implement correct ObjC/C++ exception interactions for the
2762 fragile ABI.
2763
2764 Note that for this use of setjmp/longjmp to be correct, we may need
2765 to mark some local variables volatile: if a non-volatile local
2766 variable is modified between the setjmp and the longjmp, it has
2767 indeterminate value. For the purposes of LLVM IR, it may be
2768 sufficient to make loads and stores within the @try (to variables
2769 declared outside the @try) volatile. This is necessary for
2770 optimized correctness, but is not currently being done; this is
2771 being tracked as rdar://problem/8160285
2772
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002773 The basic framework for a @try-catch-finally is as follows:
2774 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002775 objc_exception_data d;
2776 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002777 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002778
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002779 objc_exception_try_enter(&d);
2780 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002781 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002782 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002783 // exception path
2784 id _caught = objc_exception_extract(&d);
2785
2786 // enter new try scope for handlers
2787 if (!setjmp(d.jmp_buf)) {
2788 ... match exception and execute catch blocks ...
2789
2790 // fell off end, rethrow.
2791 _rethrow = _caught;
2792 ... jump-through-finally to finally_rethrow ...
2793 } else {
2794 // exception in catch block
2795 _rethrow = objc_exception_extract(&d);
2796 _call_try_exit = false;
2797 ... jump-through-finally to finally_rethrow ...
2798 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002799 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002800 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002801
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002802 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002803 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002804 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002805
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002806 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002807 ... dispatch to finally destination ...
2808
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002809 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002810 objc_exception_throw(_rethrow);
2811
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002812 finally_end:
2813 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002814
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002815 This framework differs slightly from the one gcc uses, in that gcc
2816 uses _rethrow to determine if objc_exception_try_exit should be called
2817 and if the object should be rethrown. This breaks in the face of
2818 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002819
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002820 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002821
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002822 - If there are no catch blocks, then we avoid emitting the second
2823 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002825 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2826 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002827
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002828 - FIXME: If there is no @finally block we can do a few more
2829 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002830
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831 Rethrows and Jumps-Through-Finally
2832 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002833
John McCallf1549f62010-07-06 01:34:17 +00002834 '@throw;' is supported by pushing the currently-caught exception
2835 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836
John McCallf1549f62010-07-06 01:34:17 +00002837 Branches through the @finally block are handled with an ordinary
2838 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2839 exceptions are not compatible with C++ exceptions, and this is
2840 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002841
John McCallf1549f62010-07-06 01:34:17 +00002842 @synchronized(expr) { stmt; } is emitted as if it were:
2843 id synch_value = expr;
2844 objc_sync_enter(synch_value);
2845 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002846*/
2847
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002848void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2849 const Stmt &S) {
2850 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002851
2852 // A destination for the fall-through edges of the catch handlers to
2853 // jump to.
2854 CodeGenFunction::JumpDest FinallyEnd =
2855 CGF.getJumpDestInCurrentScope("finally.end");
2856
2857 // A destination for the rethrow edge of the catch handlers to jump
2858 // to.
2859 CodeGenFunction::JumpDest FinallyRethrow =
2860 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002861
Daniel Dunbar1c566672009-02-24 01:43:46 +00002862 // For @synchronized, call objc_sync_enter(sync.expr). The
2863 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002864 // @synchronized. We can't avoid a temp here because we need the
2865 // value to be preserved. If the backend ever does liveness
2866 // correctly after setjmp, this will be unnecessary.
2867 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002868 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002869 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002870 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2871 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002872 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2873 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002874
2875 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2876 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002877 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002878
John McCall0b251722010-08-04 05:59:32 +00002879 // Allocate memory for the setjmp buffer. This needs to be kept
2880 // live throughout the try and catch blocks.
2881 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2882 "exceptiondata.ptr");
2883
John McCall87bb5822010-07-31 23:20:56 +00002884 // Create the fragile hazards. Note that this will not capture any
2885 // of the allocas required for exception processing, but will
2886 // capture the current basic block (which extends all the way to the
2887 // setjmp call) as "before the @try".
2888 FragileHazards Hazards(CGF);
2889
John McCallf1549f62010-07-06 01:34:17 +00002890 // Create a flag indicating whether the cleanup needs to call
2891 // objc_exception_try_exit. This is true except when
2892 // - no catches match and we're branching through the cleanup
2893 // just to rethrow the exception, or
2894 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002895 // The setjmp-safety rule here is that we should always store to this
2896 // variable in a place that dominates the branch through the cleanup
2897 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002898 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002899 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900
John McCall9e2213d2010-10-04 23:42:51 +00002901 // A slot containing the exception to rethrow. Only needed when we
2902 // have both a @catch and a @finally.
2903 llvm::Value *PropagatingExnVar = 0;
2904
John McCallf1549f62010-07-06 01:34:17 +00002905 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002906 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002907 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002908 CallTryExitVar,
2909 ExceptionData,
2910 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002911
2912 // Enter a try block:
2913 // - Call objc_exception_try_enter to push ExceptionData on top of
2914 // the EH stack.
2915 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2916 ->setDoesNotThrow();
2917
2918 // - Call setjmp on the exception data buffer.
2919 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2920 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2921 llvm::Value *SetJmpBuffer =
2922 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2923 llvm::CallInst *SetJmpResult =
2924 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2925 SetJmpResult->setDoesNotThrow();
2926
2927 // If setjmp returned 0, enter the protected block; otherwise,
2928 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002929 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2930 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002931 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002932 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2933 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002934
John McCallf1549f62010-07-06 01:34:17 +00002935 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002936 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002937 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002938 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002939 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002940
2941 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002942
John McCallf1549f62010-07-06 01:34:17 +00002943 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002944 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002945
John McCall87bb5822010-07-31 23:20:56 +00002946 // Don't optimize loads of the in-scope locals across this point.
2947 Hazards.emitWriteHazard();
2948
John McCallf1549f62010-07-06 01:34:17 +00002949 // For a @synchronized (or a @try with no catches), just branch
2950 // through the cleanup to the rethrow block.
2951 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2952 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002953 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002954 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002955
2956 // Otherwise, we have to match against the caught exceptions.
2957 } else {
John McCall0b251722010-08-04 05:59:32 +00002958 // Retrieve the exception object. We may emit multiple blocks but
2959 // nothing can cross this so the value is already in SSA form.
2960 llvm::CallInst *Caught =
2961 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2962 ExceptionData, "caught");
2963 Caught->setDoesNotThrow();
2964
John McCallf1549f62010-07-06 01:34:17 +00002965 // Push the exception to rethrow onto the EH value stack for the
2966 // benefit of any @throws in the handlers.
2967 CGF.ObjCEHValueStack.push_back(Caught);
2968
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002969 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002970
John McCall0b251722010-08-04 05:59:32 +00002971 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00002972
John McCall0b251722010-08-04 05:59:32 +00002973 llvm::BasicBlock *CatchBlock = 0;
2974 llvm::BasicBlock *CatchHandler = 0;
2975 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00002976 // Save the currently-propagating exception before
2977 // objc_exception_try_enter clears the exception slot.
2978 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2979 "propagating_exception");
2980 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2981
John McCall0b251722010-08-04 05:59:32 +00002982 // Enter a new exception try block (in case a @catch block
2983 // throws an exception).
2984 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2985 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002986
John McCall0b251722010-08-04 05:59:32 +00002987 llvm::CallInst *SetJmpResult =
2988 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2989 "setjmp.result");
2990 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002991
John McCall0b251722010-08-04 05:59:32 +00002992 llvm::Value *Threw =
2993 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2994
2995 CatchBlock = CGF.createBasicBlock("catch");
2996 CatchHandler = CGF.createBasicBlock("catch_for_catch");
2997 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
2998
2999 CGF.EmitBlock(CatchBlock);
3000 }
3001
3002 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003003
Daniel Dunbar55e40722008-09-27 07:03:52 +00003004 // Handle catch list. As a special case we check if everything is
3005 // matched and avoid generating code for falling off the end if
3006 // so.
3007 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003008 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3009 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003010
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003011 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003012 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003013
Anders Carlsson80f25672008-09-09 17:59:25 +00003014 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003015 if (!CatchParam) {
3016 AllMatched = true;
3017 } else {
John McCall183700f2009-09-21 23:43:11 +00003018 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003019
John McCallf1549f62010-07-06 01:34:17 +00003020 // catch(id e) always matches under this ABI, since only
3021 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003022 // FIXME: For the time being we also match id<X>; this should
3023 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003024 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003025 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003026 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027
John McCallf1549f62010-07-06 01:34:17 +00003028 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003029 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003030 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3031
Anders Carlssondde0a942008-09-11 09:15:33 +00003032 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003033 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003034 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003035
3036 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003037 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003038 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003039
Anders Carlssondde0a942008-09-11 09:15:33 +00003040 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003041
3042 // The scope of the catch variable ends right here.
3043 CatchVarCleanups.ForceCleanup();
3044
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003045 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003046 break;
3047 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048
Steve Naroff14108da2009-07-10 23:34:53 +00003049 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003050 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003051
3052 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003053 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3054 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003055
3056 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003057 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003058
John McCallf1549f62010-07-06 01:34:17 +00003059 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003060 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3061 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003062 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003063
John McCallf1549f62010-07-06 01:34:17 +00003064 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3065 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003066
3067 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003068 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
Anders Carlsson80f25672008-09-09 17:59:25 +00003070 // Emit the @catch block.
3071 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003072
3073 // Collect any cleanups for the catch variable. The scope lasts until
3074 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003075 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003076
John McCallb6bbcc92010-10-15 04:57:14 +00003077 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003078 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003079
John McCallf1549f62010-07-06 01:34:17 +00003080 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003081 llvm::Value *Tmp =
3082 CGF.Builder.CreateBitCast(Caught,
3083 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003084 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003085 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086
Anders Carlssondde0a942008-09-11 09:15:33 +00003087 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003088
3089 // We're done with the catch variable.
3090 CatchVarCleanups.ForceCleanup();
3091
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003092 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093
Anders Carlsson80f25672008-09-09 17:59:25 +00003094 CGF.EmitBlock(NextCatchBlock);
3095 }
3096
John McCallf1549f62010-07-06 01:34:17 +00003097 CGF.ObjCEHValueStack.pop_back();
3098
John McCall0b251722010-08-04 05:59:32 +00003099 // If nothing wanted anything to do with the caught exception,
3100 // kill the extract call.
3101 if (Caught->use_empty())
3102 Caught->eraseFromParent();
3103
3104 if (!AllMatched)
3105 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3106
3107 if (HasFinally) {
3108 // Emit the exception handler for the @catch blocks.
3109 CGF.EmitBlock(CatchHandler);
3110
3111 // In theory we might now need a write hazard, but actually it's
3112 // unnecessary because there's no local-accessing code between
3113 // the try's write hazard and here.
3114 //Hazards.emitWriteHazard();
3115
John McCall9e2213d2010-10-04 23:42:51 +00003116 // Extract the new exception and save it to the
3117 // propagating-exception slot.
3118 assert(PropagatingExnVar);
3119 llvm::CallInst *NewCaught =
3120 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3121 ExceptionData, "caught");
3122 NewCaught->setDoesNotThrow();
3123 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3124
John McCall0b251722010-08-04 05:59:32 +00003125 // Don't pop the catch handler; the throw already did.
3126 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003127 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003128 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003129 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003130
John McCall87bb5822010-07-31 23:20:56 +00003131 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003132 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003133
John McCallf1549f62010-07-06 01:34:17 +00003134 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003135 CGF.Builder.restoreIP(TryFallthroughIP);
3136 if (CGF.HaveInsertPoint())
3137 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003138 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003139 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003140
John McCallf1549f62010-07-06 01:34:17 +00003141 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003142 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003143 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003144 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003145 // If we have a propagating-exception variable, check it.
3146 llvm::Value *PropagatingExn;
3147 if (PropagatingExnVar) {
3148 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003149
John McCall9e2213d2010-10-04 23:42:51 +00003150 // Otherwise, just look in the buffer for the exception to throw.
3151 } else {
3152 llvm::CallInst *Caught =
3153 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3154 ExceptionData);
3155 Caught->setDoesNotThrow();
3156 PropagatingExn = Caught;
3157 }
3158
3159 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003160 ->setDoesNotThrow();
3161 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003162 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003163
John McCall87bb5822010-07-31 23:20:56 +00003164 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003165}
3166
3167void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003168 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003169 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003170
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003171 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3172 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003173 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003174 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3175 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003177 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003178 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003179 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180
John McCallf1549f62010-07-06 01:34:17 +00003181 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3182 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003183 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003184
3185 // Clear the insertion point to indicate we are in unreachable code.
3186 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003187}
3188
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003189/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003190/// object: objc_read_weak (id *src)
3191///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003192llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003193 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003194 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003195 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3196 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3197 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003198 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003199 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003200 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003201 return read_weak;
3202}
3203
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003204/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3205/// objc_assign_weak (id src, id *dst)
3206///
3207void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003208 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003209 const llvm::Type * SrcTy = src->getType();
3210 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003211 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003212 assert(Size <= 8 && "does not support size > 8");
3213 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003214 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003215 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3216 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003217 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3218 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003219 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003220 src, dst, "weakassign");
3221 return;
3222}
3223
Fariborz Jahanian58626502008-11-19 00:59:10 +00003224/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3225/// objc_assign_global (id src, id *dst)
3226///
3227void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003228 llvm::Value *src, llvm::Value *dst,
3229 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003230 const llvm::Type * SrcTy = src->getType();
3231 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003232 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003233 assert(Size <= 8 && "does not support size > 8");
3234 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003235 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003236 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3237 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003238 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3239 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003240 if (!threadlocal)
3241 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3242 src, dst, "globalassign");
3243 else
3244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3245 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003246 return;
3247}
3248
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003249/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003250/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003251///
3252void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003253 llvm::Value *src, llvm::Value *dst,
3254 llvm::Value *ivarOffset) {
3255 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003256 const llvm::Type * SrcTy = src->getType();
3257 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003258 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003259 assert(Size <= 8 && "does not support size > 8");
3260 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003261 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003262 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3263 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003264 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3265 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003266 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3267 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003268 return;
3269}
3270
Fariborz Jahanian58626502008-11-19 00:59:10 +00003271/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3272/// objc_assign_strongCast (id src, id *dst)
3273///
3274void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003275 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003276 const llvm::Type * SrcTy = src->getType();
3277 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003278 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003279 assert(Size <= 8 && "does not support size > 8");
3280 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003282 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3283 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003284 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3285 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003286 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003287 src, dst, "weakassign");
3288 return;
3289}
3290
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003291void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003292 llvm::Value *DestPtr,
3293 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003294 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003295 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3296 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003297 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003298 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003299 return;
3300}
3301
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003302/// EmitObjCValueForIvar - Code Gen for ivar reference.
3303///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003304LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3305 QualType ObjectTy,
3306 llvm::Value *BaseValue,
3307 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003308 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003309 const ObjCInterfaceDecl *ID =
3310 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003311 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3312 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003313}
3314
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003315llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003316 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003317 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003318 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003319 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3321 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003322}
3323
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003324/* *** Private Interface *** */
3325
3326/// EmitImageInfo - Emit the image info marker used to encode some module
3327/// level information.
3328///
3329/// See: <rdr://4810609&4810587&4810587>
3330/// struct IMAGE_INFO {
3331/// unsigned version;
3332/// unsigned flags;
3333/// };
3334enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003335 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336 eImageInfo_GarbageCollected = (1 << 1),
3337 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003338 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3339
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003340 // A flag indicating that the module has no instances of a @synthesize of a
3341 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003342 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003343};
3344
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003345void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003346 unsigned version = 0; // Version is unused?
3347 unsigned flags = 0;
3348
3349 // FIXME: Fix and continue?
3350 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3351 flags |= eImageInfo_GarbageCollected;
3352 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3353 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003355 // We never allow @synthesize of a superclass property.
3356 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003357
Chris Lattner77b89b82010-06-27 07:15:29 +00003358 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3359
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003360 // Emitted as int[2];
3361 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003362 llvm::ConstantInt::get(Int32Ty, version),
3363 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003364 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003365 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003366
3367 const char *Section;
3368 if (ObjCABI == 1)
3369 Section = "__OBJC, __image_info,regular";
3370 else
3371 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003372 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003373 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003374 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003375 Section,
3376 0,
3377 true);
3378 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003379}
3380
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003381
3382// struct objc_module {
3383// unsigned long version;
3384// unsigned long size;
3385// const char *name;
3386// Symtab symtab;
3387// };
3388
3389// FIXME: Get from somewhere
3390static const int ModuleVersion = 7;
3391
3392void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003393 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003395 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003396 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3397 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003398 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003399 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003400 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003401 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003402 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003403 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003404 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003405}
3406
3407llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003408 unsigned NumClasses = DefinedClasses.size();
3409 unsigned NumCategories = DefinedCategories.size();
3410
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003411 // Return null if no symbols were defined.
3412 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003413 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003414
3415 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003416 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003417 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003418 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3419 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003420
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003421 // The runtime expects exactly the list of defined classes followed
3422 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003423 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003424 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003425 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003426 ObjCTypes.Int8PtrTy);
3427 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003428 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003429 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003430 ObjCTypes.Int8PtrTy);
3431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003432 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003433 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003434 NumClasses + NumCategories),
3435 Symbols);
3436
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003437 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003438
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003439 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003440 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3441 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003442 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444}
3445
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003446llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003447 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003448 LazySymbols.insert(ID->getIdentifier());
3449
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003450 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003451
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003452 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003454 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003455 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003456 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003457 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3458 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003459 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460 }
3461
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003462 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003463}
3464
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003465llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3466 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003467 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003469 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003470 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003471 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003472 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003473 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003474 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3475 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003476 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003477 }
3478
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003479 if (lvalue)
3480 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003481 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003482}
3483
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003484llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003485 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003486
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003487 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003488 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003489 llvm::ConstantArray::get(VMContext,
3490 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003491 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003492 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003493
Owen Andersona1cf15f2009-07-14 23:10:40 +00003494 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003495}
3496
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003497llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3498 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3499 I = MethodDefinitions.find(MD);
3500 if (I != MethodDefinitions.end())
3501 return I->second;
3502
3503 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3504 // MD isn't emitted yet because it comes from PCH.
3505 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3506 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3507 return MethodDefinitions[MD];
3508 }
3509
3510 return NULL;
3511}
3512
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003513/// GetIvarLayoutName - Returns a unique constant for the given
3514/// ivar layout bitmap.
3515llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003516 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003517 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003518}
3519
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003520void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003521 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003522 bool ForStrongLayout,
3523 bool &HasUnion) {
3524 const RecordDecl *RD = RT->getDecl();
3525 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003526 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003527 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003528 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003529 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003530
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003531 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3532 ForStrongLayout, HasUnion);
3533}
3534
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003535void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536 const llvm::StructLayout *Layout,
3537 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003538 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003539 unsigned int BytePos, bool ForStrongLayout,
3540 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003541 bool IsUnion = (RD && RD->isUnion());
3542 uint64_t MaxUnionIvarSize = 0;
3543 uint64_t MaxSkippedUnionIvarSize = 0;
3544 FieldDecl *MaxField = 0;
3545 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003546 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003547 uint64_t MaxFieldOffset = 0;
3548 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003549 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003550
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003551 if (RecFields.empty())
3552 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003553 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3554 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3555
Chris Lattnerf1690852009-03-31 08:48:01 +00003556 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003557 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003558 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003559 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003560 // Note that 'i' here is actually the field index inside RD of Field,
3561 // although this dependency is hidden.
3562 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3563 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003564 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003565 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003566
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003567 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003568 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003569 LastFieldBitfieldOrUnnamed = Field;
3570 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003571 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003572 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003573
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003574 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003575 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003576 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003577 if (FQT->isUnionType())
3578 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003579
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003581 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003582 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003583 continue;
3584 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003585
Chris Lattnerf1690852009-03-31 08:48:01 +00003586 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003588 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003589 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003590 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003591 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003592 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3593 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003594 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003595 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003596 FQT = CArray->getElementType();
3597 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003598
3599 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003600 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003601 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003602 int OldIndex = IvarsInfo.size() - 1;
3603 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003604
Ted Kremenek6217b802009-07-29 21:53:49 +00003605 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003606 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003607 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003608
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003609 // Replicate layout information for each array element. Note that
3610 // one element is already done.
3611 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003612 for (int FirstIndex = IvarsInfo.size() - 1,
3613 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003614 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003615 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3616 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3617 IvarsInfo[i].ivar_size));
3618 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3619 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3620 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003621 }
3622 continue;
3623 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003624 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003625 // At this point, we are done with Record/Union and array there of.
3626 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003627 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003628
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003629 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003630 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3631 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003632 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003633 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003634 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003635 MaxUnionIvarSize = UnionIvarSize;
3636 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003637 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003638 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003639 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003640 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003641 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003642 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003643 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003644 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3645 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003646 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003647 // FIXME: Why the asymmetry? We divide by word size in bits on other
3648 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003649 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003650 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003651 MaxSkippedUnionIvarSize = UnionIvarSize;
3652 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003653 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003654 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003655 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003656 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003657 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003658 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003659 }
3660 }
3661 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003662
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003663 if (LastFieldBitfieldOrUnnamed) {
3664 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3665 // Last field was a bitfield. Must update skip info.
3666 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3667 uint64_t BitFieldSize =
3668 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3669 GC_IVAR skivar;
3670 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3671 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3672 + ((BitFieldSize % ByteSizeInBits) != 0);
3673 SkipIvars.push_back(skivar);
3674 } else {
3675 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3676 // Last field was unnamed. Must update skip info.
3677 unsigned FieldSize
3678 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3679 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3680 FieldSize / ByteSizeInBits));
3681 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003682 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003683
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003684 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003686 MaxUnionIvarSize));
3687 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003688 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003689 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003690}
3691
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003692/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3693/// the computations and returning the layout bitmap (for ivar or blocks) in
3694/// the given argument BitMap string container. Routine reads
3695/// two containers, IvarsInfo and SkipIvars which are assumed to be
3696/// filled already by the caller.
3697llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003698 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003699 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003700
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003701 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003702 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003703 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003704 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003705 if (IvarsInfo[0].ivar_bytepos == 0) {
3706 WordsToSkip = 0;
3707 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003708 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003709 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3710 WordsToScan = IvarsInfo[0].ivar_size;
3711 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003712 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003713 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003714 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003715 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003716 // consecutive 'scanned' object pointers.
3717 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003718 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003719 // Skip over 'gc'able object pointer which lay over each other.
3720 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3721 continue;
3722 // Must skip over 1 or more words. We save current skip/scan values
3723 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003724 SKIP_SCAN SkScan;
3725 SkScan.skip = WordsToSkip;
3726 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003727 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003728
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003729 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003730 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3731 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003732 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003733 WordsToSkip = 0;
3734 WordsToScan = IvarsInfo[i].ivar_size;
3735 }
3736 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003737 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003738 SKIP_SCAN SkScan;
3739 SkScan.skip = WordsToSkip;
3740 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003741 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003742 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003743
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003744 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003745 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003746 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003747 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003748 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003749 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003750 IvarsInfo[LastIndex].ivar_bytepos +
3751 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003752 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003753 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003754 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003755 SKIP_SCAN SkScan;
3756 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3757 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003758 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003759 }
3760 }
3761 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3762 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003763 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003764 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003765 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3766 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3767 // 0xM0 followed by 0x0N detected.
3768 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3769 for (int j = i+1; j < SkipScan; j++)
3770 SkipScanIvars[j] = SkipScanIvars[j+1];
3771 --SkipScan;
3772 }
3773 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003774
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003775 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003776 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003777 unsigned char byte;
3778 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3779 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3780 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3781 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003782
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003783 // first skip big.
3784 for (unsigned int ix = 0; ix < skip_big; ix++)
3785 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003786
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003787 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003788 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003790 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003791 byte |= 0xf;
3792 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003793 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003794 byte |= scan_small;
3795 scan_small = 0;
3796 }
3797 BitMap += byte;
3798 }
3799 // next scan big
3800 for (unsigned int ix = 0; ix < scan_big; ix++)
3801 BitMap += (unsigned char)(0x0f);
3802 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003803 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003804 byte = scan_small;
3805 BitMap += byte;
3806 }
3807 }
3808 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003809 unsigned char zero = 0;
3810 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003811
3812 llvm::GlobalVariable * Entry =
3813 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3814 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3815 "__TEXT,__cstring,cstring_literals",
3816 1, true);
3817 return getConstantGEP(VMContext, Entry, 0, 0);
3818}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003819
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003820/// BuildIvarLayout - Builds ivar layout bitmap for the class
3821/// implementation for the __strong or __weak case.
3822/// The layout map displays which words in ivar list must be skipped
3823/// and which must be scanned by GC (see below). String is built of bytes.
3824/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3825/// of words to skip and right nibble is count of words to scan. So, each
3826/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3827/// represented by a 0x00 byte which also ends the string.
3828/// 1. when ForStrongLayout is true, following ivars are scanned:
3829/// - id, Class
3830/// - object *
3831/// - __strong anything
3832///
3833/// 2. When ForStrongLayout is false, following ivars are scanned:
3834/// - __weak anything
3835///
3836llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3837 const ObjCImplementationDecl *OMD,
3838 bool ForStrongLayout) {
3839 bool hasUnion = false;
3840
3841 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3842 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3843 return llvm::Constant::getNullValue(PtrTy);
3844
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003845 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003846 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003847 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003848
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003849 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003850 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3851 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3852
3853 if (RecFields.empty())
3854 return llvm::Constant::getNullValue(PtrTy);
3855
3856 SkipIvars.clear();
3857 IvarsInfo.clear();
3858
3859 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3860 if (IvarsInfo.empty())
3861 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003862 // Sort on byte position in case we encounterred a union nested in
3863 // the ivar list.
3864 if (hasUnion && !IvarsInfo.empty())
3865 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3866 if (hasUnion && !SkipIvars.empty())
3867 std::sort(SkipIvars.begin(), SkipIvars.end());
3868
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003869 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003870 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003871
3872 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003873 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003874 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003875 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003876 const unsigned char *s = (unsigned char*)BitMap.c_str();
3877 for (unsigned i = 0; i < BitMap.size(); i++)
3878 if (!(s[i] & 0xf0))
3879 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3880 else
3881 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3882 printf("\n");
3883 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003884 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003885}
3886
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003887llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003888 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3889
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003890 // FIXME: Avoid std::string copying.
3891 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003892 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003893 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003894 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003895 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003896
Owen Andersona1cf15f2009-07-14 23:10:40 +00003897 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003898}
3899
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003900// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003901llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003902 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3903}
3904
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003905llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003906 std::string TypeStr;
3907 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3908
3909 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003910
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003911 if (!Entry)
3912 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003913 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003914 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003915 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003916
Owen Andersona1cf15f2009-07-14 23:10:40 +00003917 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003918}
3919
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003920llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003921 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003922 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3923 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003924
3925 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3926
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003927 if (!Entry)
3928 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003929 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003930 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003931 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003932
Owen Andersona1cf15f2009-07-14 23:10:40 +00003933 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003934}
3935
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003936// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003937llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003938 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003939
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003940 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003941 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003942 llvm::ConstantArray::get(VMContext,
3943 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003944 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003945 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003946
Owen Andersona1cf15f2009-07-14 23:10:40 +00003947 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003948}
3949
3950// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003951// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003952llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003953CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3954 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003955 std::string TypeStr;
3956 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003957 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3958}
3959
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003960void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003961 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003962 llvm::SmallVectorImpl<char> &Name) {
3963 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003964 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003965 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3966 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003967 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003968 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003969 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003970 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003971}
3972
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003973void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003974 EmitModuleInfo();
3975
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003976 // Emit the dummy bodies for any protocols which were referenced but
3977 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003978 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003979 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3980 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003981 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003982
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003983 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003984 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003985 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003986 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003987 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003988 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003989 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003990 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003991 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003992 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003993 }
3994
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003995 // Add assembler directives to add lazy undefined symbol references
3996 // for classes which are referenced but not defined. This is
3997 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00003998 //
3999 // FIXME: It would be nice if we had an LLVM construct for this.
4000 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4001 llvm::SmallString<256> Asm;
4002 Asm += CGM.getModule().getModuleInlineAsm();
4003 if (!Asm.empty() && Asm.back() != '\n')
4004 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004005
Daniel Dunbar33063492009-09-07 00:20:42 +00004006 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004007 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4008 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004009 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4010 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004011 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004012 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004013 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004014 }
4015
4016 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4017 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4018 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4019 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004020
Daniel Dunbar33063492009-09-07 00:20:42 +00004021 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004022 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004023}
4024
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004025CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004026 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004027 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004028 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004029 ObjCABI = 2;
4030}
4031
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004032/* *** */
4033
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004034ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004035 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004036 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4037 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004039 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004040 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004041 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004042 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004043 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004044
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004045 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004046 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004047 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004048
Mike Stumpf5408fe2009-05-16 07:57:57 +00004049 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4050 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004051 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004052 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004053
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004054 // I'm not sure I like this. The implicit coordination is a bit
4055 // gross. We should solve this in a reasonable fashion because this
4056 // is a pretty common task (match some runtime data structure with
4057 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004058
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004059 // FIXME: This is leaked.
4060 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004061
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004062 // struct _objc_super {
4063 // id self;
4064 // Class cls;
4065 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004066 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004067 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004068 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004069 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004070 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004071 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004072 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004073 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004074 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004075
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004076 SuperCTy = Ctx.getTagDeclType(RD);
4077 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004078
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004079 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004080 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4081
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004082 // struct _prop_t {
4083 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004084 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004085 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004086 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004087 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004088 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004089
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004090 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004091 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004092 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004093 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004094 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004095 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004096 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004097 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004098 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004099 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004100 PropertyListTy);
4101 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004102 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004103
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004104 // struct _objc_method {
4105 // SEL _cmd;
4106 // char *method_type;
4107 // char *_imp;
4108 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004109 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004110 Int8PtrTy,
4111 Int8PtrTy,
4112 NULL);
4113 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004114
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004115 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004116 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004117 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004118 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004119}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004120
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004122 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004123 // struct _objc_method_description {
4124 // SEL name;
4125 // char *types;
4126 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004128 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004129 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004130 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004132 MethodDescriptionTy);
4133
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004134 // struct _objc_method_description_list {
4135 // int count;
4136 // struct _objc_method_description[1];
4137 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004138 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004139 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004140 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004141 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004142 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004143 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004145 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004146 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004147 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004148
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004149 // Protocol description structures
4150
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004151 // struct _objc_protocol_extension {
4152 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4153 // struct _objc_method_description_list *optional_instance_methods;
4154 // struct _objc_method_description_list *optional_class_methods;
4155 // struct _objc_property_list *instance_properties;
4156 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004157 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004158 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004159 MethodDescriptionListPtrTy,
4160 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004161 PropertyListPtrTy,
4162 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004164 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004165
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004166 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004167 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004168
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004169 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004170
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004171 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4172 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004173
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004174 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004175 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004176 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004177 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004178 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004179 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004180 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4181
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004182 // struct _objc_protocol {
4183 // struct _objc_protocol_extension *isa;
4184 // char *protocol_name;
4185 // struct _objc_protocol **_objc_protocol_list;
4186 // struct _objc_method_description_list *instance_methods;
4187 // struct _objc_method_description_list *class_methods;
4188 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004189 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004190 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004191 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004192 MethodDescriptionListPtrTy,
4193 MethodDescriptionListPtrTy,
4194 NULL);
4195 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4196
4197 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004199 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004200 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004202
4203 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004204 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004205 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004206
4207 // Class description structures
4208
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004209 // struct _objc_ivar {
4210 // char *ivar_name;
4211 // char *ivar_type;
4212 // int ivar_offset;
4213 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004214 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004215 Int8PtrTy,
4216 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004217 NULL);
4218 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4219
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004220 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004221 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004222 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004223 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004224
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004225 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004226 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004227 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004228 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004229
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004230 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004231 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004232 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004233 Int8PtrTy,
4234 PropertyListPtrTy,
4235 NULL);
4236 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004237 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004238
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004239 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004240
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004241 // struct _objc_class {
4242 // Class isa;
4243 // Class super_class;
4244 // char *name;
4245 // long version;
4246 // long info;
4247 // long instance_size;
4248 // struct _objc_ivar_list *ivars;
4249 // struct _objc_method_list *methods;
4250 // struct _objc_cache *cache;
4251 // struct _objc_protocol_list *protocols;
4252 // char *ivar_layout;
4253 // struct _objc_class_ext *ext;
4254 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004255 T = llvm::StructType::get(VMContext,
4256 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004257 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004258 Int8PtrTy,
4259 LongTy,
4260 LongTy,
4261 LongTy,
4262 IvarListPtrTy,
4263 MethodListPtrTy,
4264 CachePtrTy,
4265 ProtocolListPtrTy,
4266 Int8PtrTy,
4267 ClassExtensionPtrTy,
4268 NULL);
4269 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004270
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004271 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4272 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004273 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004274
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004275 // struct _objc_category {
4276 // char *category_name;
4277 // char *class_name;
4278 // struct _objc_method_list *instance_method;
4279 // struct _objc_method_list *class_method;
4280 // uint32_t size; // sizeof(struct _objc_category)
4281 // struct _objc_property_list *instance_properties;// category's @property
4282 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004283 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004284 Int8PtrTy,
4285 MethodListPtrTy,
4286 MethodListPtrTy,
4287 ProtocolListPtrTy,
4288 IntTy,
4289 PropertyListPtrTy,
4290 NULL);
4291 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4292
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004293 // Global metadata structures
4294
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004295 // struct _objc_symtab {
4296 // long sel_ref_cnt;
4297 // SEL *refs;
4298 // short cls_def_cnt;
4299 // short cat_def_cnt;
4300 // char *defs[cls_def_cnt + cat_def_cnt];
4301 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004302 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004303 SelectorPtrTy,
4304 ShortTy,
4305 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004306 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004307 NULL);
4308 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004309 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004310
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004311 // struct _objc_module {
4312 // long version;
4313 // long size; // sizeof(struct _objc_module)
4314 // char *name;
4315 // struct _objc_symtab* symtab;
4316 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004317 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004318 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004319 LongTy,
4320 Int8PtrTy,
4321 SymtabPtrTy,
4322 NULL);
4323 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004324
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004325
Mike Stumpf5408fe2009-05-16 07:57:57 +00004326 // FIXME: This is the size of the setjmp buffer and should be target
4327 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004328 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004329
Anders Carlsson124526b2008-09-09 10:10:21 +00004330 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004331 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004332 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004333
4334 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004335 llvm::StructType::get(VMContext,
4336 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4337 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004338 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004339 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004340 ExceptionDataTy);
4341
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004342}
4343
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004344ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004345 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004346 // struct _method_list_t {
4347 // uint32_t entsize; // sizeof(struct _objc_method)
4348 // uint32_t method_count;
4349 // struct _objc_method method_list[method_count];
4350 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004351 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004352 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004353 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004354 NULL);
4355 CGM.getModule().addTypeName("struct.__method_list_t",
4356 MethodListnfABITy);
4357 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004358 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004359
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004360 // struct _protocol_t {
4361 // id isa; // NULL
4362 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004363 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004364 // const struct method_list_t * const instance_methods;
4365 // const struct method_list_t * const class_methods;
4366 // const struct method_list_t *optionalInstanceMethods;
4367 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004368 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004369 // const uint32_t size; // sizeof(struct _protocol_t)
4370 // const uint32_t flags; // = 0
4371 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004372
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004373 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004374 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004375
Owen Anderson47a434f2009-08-05 23:18:46 +00004376 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004377 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004378 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004379 ProtocolListTyHolder),
4380 MethodListnfABIPtrTy,
4381 MethodListnfABIPtrTy,
4382 MethodListnfABIPtrTy,
4383 MethodListnfABIPtrTy,
4384 PropertyListPtrTy,
4385 IntTy,
4386 IntTy,
4387 NULL);
4388 CGM.getModule().addTypeName("struct._protocol_t",
4389 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004390
4391 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004392 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004393
Fariborz Jahanianda320092009-01-29 19:24:30 +00004394 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004395 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004396 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004397 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004398 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004399 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004400 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004401 NULL);
4402 CGM.getModule().addTypeName("struct._objc_protocol_list",
4403 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004404 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004405 ProtocolListnfABITy);
4406
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004407 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004408 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004410 // struct _ivar_t {
4411 // unsigned long int *offset; // pointer to ivar offset location
4412 // char *name;
4413 // char *type;
4414 // uint32_t alignment;
4415 // uint32_t size;
4416 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004417 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004418 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004419 Int8PtrTy,
4420 Int8PtrTy,
4421 IntTy,
4422 IntTy,
4423 NULL);
4424 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004425
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004426 // struct _ivar_list_t {
4427 // uint32 entsize; // sizeof(struct _ivar_t)
4428 // uint32 count;
4429 // struct _iver_t list[count];
4430 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004431 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004432 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004433 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004434 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004435 NULL);
4436 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004437
Owen Anderson96e0fc72009-07-29 22:16:19 +00004438 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004439
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004440 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004441 // uint32_t const flags;
4442 // uint32_t const instanceStart;
4443 // uint32_t const instanceSize;
4444 // uint32_t const reserved; // only when building for 64bit targets
4445 // const uint8_t * const ivarLayout;
4446 // const char *const name;
4447 // const struct _method_list_t * const baseMethods;
4448 // const struct _objc_protocol_list *const baseProtocols;
4449 // const struct _ivar_list_t *const ivars;
4450 // const uint8_t * const weakIvarLayout;
4451 // const struct _prop_list_t * const properties;
4452 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004453
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004454 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004455 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004456 IntTy,
4457 IntTy,
4458 Int8PtrTy,
4459 Int8PtrTy,
4460 MethodListnfABIPtrTy,
4461 ProtocolListnfABIPtrTy,
4462 IvarListnfABIPtrTy,
4463 Int8PtrTy,
4464 PropertyListPtrTy,
4465 NULL);
4466 CGM.getModule().addTypeName("struct._class_ro_t",
4467 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004469 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4470 std::vector<const llvm::Type*> Params;
4471 Params.push_back(ObjectPtrTy);
4472 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004473 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4475
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004476 // struct _class_t {
4477 // struct _class_t *isa;
4478 // struct _class_t * const superclass;
4479 // void *cache;
4480 // IMP *vtable;
4481 // struct class_ro_t *ro;
4482 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004483
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004484 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004485 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004486 llvm::StructType::get(VMContext,
4487 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004488 llvm::PointerType::getUnqual(ClassTyHolder),
4489 CachePtrTy,
4490 llvm::PointerType::getUnqual(ImpnfABITy),
4491 llvm::PointerType::getUnqual(ClassRonfABITy),
4492 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004493 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4494
4495 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496 ClassnfABITy);
4497
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004498 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004499 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004500
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004501 // struct _category_t {
4502 // const char * const name;
4503 // struct _class_t *const cls;
4504 // const struct _method_list_t * const instance_methods;
4505 // const struct _method_list_t * const class_methods;
4506 // const struct _protocol_list_t * const protocols;
4507 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004508 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004509 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004510 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004511 MethodListnfABIPtrTy,
4512 MethodListnfABIPtrTy,
4513 ProtocolListnfABIPtrTy,
4514 PropertyListPtrTy,
4515 NULL);
4516 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004517
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004518 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004519 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4520 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004521
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004522 // MessageRefTy - LLVM for:
4523 // struct _message_ref_t {
4524 // IMP messenger;
4525 // SEL name;
4526 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004527
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004528 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004529 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004530 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004531 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004532 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004533 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004534 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004535 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004536 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004537 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004538
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004539 MessageRefCTy = Ctx.getTagDeclType(RD);
4540 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4541 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004542
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004543 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004544 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004545
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004546 // SuperMessageRefTy - LLVM for:
4547 // struct _super_message_ref_t {
4548 // SUPER_IMP messenger;
4549 // SEL name;
4550 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004551 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004552 SelectorPtrTy,
4553 NULL);
4554 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004555
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004556 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4558
Daniel Dunbare588b992009-03-01 04:46:24 +00004559
4560 // struct objc_typeinfo {
4561 // const void** vtable; // objc_ehtype_vtable + 2
4562 // const char* name; // c++ typeinfo string
4563 // Class cls;
4564 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004565 EHTypeTy = llvm::StructType::get(VMContext,
4566 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004567 Int8PtrTy,
4568 ClassnfABIPtrTy,
4569 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004570 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004571 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004572}
4573
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004574llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004575 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004576
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004577 return NULL;
4578}
4579
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004580void CGObjCNonFragileABIMac::AddModuleClassList(const
4581 std::vector<llvm::GlobalValue*>
4582 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004583 const char *SymbolName,
4584 const char *SectionName) {
4585 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Daniel Dunbar463b8762009-05-15 21:48:48 +00004587 if (!NumClasses)
4588 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004589
Daniel Dunbar463b8762009-05-15 21:48:48 +00004590 std::vector<llvm::Constant*> Symbols(NumClasses);
4591 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004592 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004593 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004595 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004596 NumClasses),
4597 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004598
Daniel Dunbar463b8762009-05-15 21:48:48 +00004599 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004601 llvm::GlobalValue::InternalLinkage,
4602 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004603 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004604 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004605 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004606 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004607}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004609void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4610 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004611
Daniel Dunbar463b8762009-05-15 21:48:48 +00004612 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004613 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004614 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004615 "\01L_OBJC_LABEL_CLASS_$",
4616 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004617
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004618 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4619 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4620 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4621 continue;
4622 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004623 }
4624
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004625 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4626 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4627 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4628 continue;
4629 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4630 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004631
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004632 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004633 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4634 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004635
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004636 // Build list of all implemented category addresses in array
4637 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004639 "\01L_OBJC_LABEL_CATEGORY_$",
4640 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004641 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004642 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4643 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004645 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004646}
4647
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004648/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004649/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004650/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004651/// message dispatch call for all the rest.
4652///
4653bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004654 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4655 default:
4656 assert(0 && "Invalid dispatch method!");
4657 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004658 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004659 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004660 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004661 case CodeGenOptions::Mixed:
4662 break;
4663 }
4664
4665 // If so, see whether this selector is in the white-list of things which must
4666 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004667 if (NonLegacyDispatchMethods.empty()) {
4668 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4669 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4670 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4671 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4672 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4673 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4674 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4675 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4676 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4677 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004678
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004679 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4680 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4681 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4682 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4683 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4684 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4685 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4686 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004687 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004688 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004689 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4690 &CGM.getContext().Idents.get("objects"),
4691 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004692 };
4693 NonLegacyDispatchMethods.insert(
4694 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004695 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004696
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004697 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004698}
4699
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004700// Metadata flags
4701enum MetaDataDlags {
4702 CLS = 0x0,
4703 CLS_META = 0x1,
4704 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004705 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004706 CLS_EXCEPTION = 0x20
4707};
4708/// BuildClassRoTInitializer - generate meta-data for:
4709/// struct _class_ro_t {
4710/// uint32_t const flags;
4711/// uint32_t const instanceStart;
4712/// uint32_t const instanceSize;
4713/// uint32_t const reserved; // only when building for 64bit targets
4714/// const uint8_t * const ivarLayout;
4715/// const char *const name;
4716/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004717/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004718/// const struct _ivar_list_t *const ivars;
4719/// const uint8_t * const weakIvarLayout;
4720/// const struct _prop_list_t * const properties;
4721/// }
4722///
4723llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004724 unsigned flags,
4725 unsigned InstanceStart,
4726 unsigned InstanceSize,
4727 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004728 std::string ClassName = ID->getNameAsString();
4729 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004730 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4731 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4732 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004733 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004734 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4735 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004736 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004737 // const struct _method_list_t * const baseMethods;
4738 std::vector<llvm::Constant*> Methods;
4739 std::string MethodListName("\01l_OBJC_$_");
4740 if (flags & CLS_META) {
4741 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004742 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004743 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004744 // Class methods should always be defined.
4745 Methods.push_back(GetMethodConstant(*i));
4746 }
4747 } else {
4748 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004749 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004750 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004751 // Instance methods should always be defined.
4752 Methods.push_back(GetMethodConstant(*i));
4753 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004754 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004755 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004756 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004757
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004758 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4759 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004761 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4762 if (llvm::Constant *C = GetMethodConstant(MD))
4763 Methods.push_back(C);
4764 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4765 if (llvm::Constant *C = GetMethodConstant(MD))
4766 Methods.push_back(C);
4767 }
4768 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004769 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770 Values[ 5] = EmitMethodList(MethodListName,
4771 "__DATA, __objc_const", Methods);
4772
Fariborz Jahanianda320092009-01-29 19:24:30 +00004773 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4774 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004775 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004776 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004777 OID->all_referenced_protocol_begin(),
4778 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004779
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004780 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004781 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004782 else
4783 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004784 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4785 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004786 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004787 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004788 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004789 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4790 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004791 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004792 Values);
4793 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4795 llvm::GlobalValue::InternalLinkage,
4796 Init,
4797 (flags & CLS_META) ?
4798 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4799 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004800 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004801 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004802 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004803 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004804
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004805}
4806
4807/// BuildClassMetaData - This routine defines that to-level meta-data
4808/// for the given ClassName for:
4809/// struct _class_t {
4810/// struct _class_t *isa;
4811/// struct _class_t * const superclass;
4812/// void *cache;
4813/// IMP *vtable;
4814/// struct class_ro_t *ro;
4815/// }
4816///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004817llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004818 std::string &ClassName,
4819 llvm::Constant *IsAGV,
4820 llvm::Constant *SuperClassGV,
4821 llvm::Constant *ClassRoGV,
4822 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004823 std::vector<llvm::Constant*> Values(5);
4824 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004825 Values[1] = SuperClassGV;
4826 if (!Values[1])
4827 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004828 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4829 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4830 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004831 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004832 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004833 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4834 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004835 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004836 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004837 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004838 if (HiddenVisibility)
4839 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004840 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004841}
4842
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004843bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004844CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004845 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004846}
4847
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004848void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004849 uint32_t &InstanceStart,
4850 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004852 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004854 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004855 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004856
4857 // If there are no fields, the start is the same as the end.
4858 if (!RL.getFieldCount())
4859 InstanceStart = InstanceSize;
4860 else
4861 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004862}
4863
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004864void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4865 std::string ClassName = ID->getNameAsString();
4866 if (!ObjCEmptyCacheVar) {
4867 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868 CGM.getModule(),
4869 ObjCTypes.CacheTy,
4870 false,
4871 llvm::GlobalValue::ExternalLinkage,
4872 0,
4873 "_objc_empty_cache");
4874
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004875 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004876 CGM.getModule(),
4877 ObjCTypes.ImpnfABITy,
4878 false,
4879 llvm::GlobalValue::ExternalLinkage,
4880 0,
4881 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004882 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004883 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004884 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004885 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004886 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004887 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004888 uint32_t InstanceSize = InstanceStart;
4889 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004890 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4891 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004892
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004893 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004894
4895 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004896 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004897 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004898 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004899 if (ID->getNumIvarInitializers())
4900 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004901 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004902 // class is root
4903 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004904 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004905 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004906 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004907 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004908 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4909 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4910 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004911 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004912 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004913 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004914 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004915 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004916 ObjCMetaClassName +
4917 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004918 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004919 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004920 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004921 }
4922 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4923 InstanceStart,
4924 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004925 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004926 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004927 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4928 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004929 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004930
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004931 // Metadata for the class
4932 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004933 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004934 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004935 if (ID->getNumIvarInitializers())
4936 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004937
Douglas Gregor68584ed2009-06-18 16:11:24 +00004938 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004939 flags |= CLS_EXCEPTION;
4940
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004941 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004942 flags |= CLS_ROOT;
4943 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004944 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004945 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004946 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004947 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004948 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004949 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004950 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004951 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004952 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004953 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004954 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004955 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004956 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004957
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004958 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004959 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004960 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4961 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004962 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004963
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004964 // Determine if this class is also "non-lazy".
4965 if (ImplementationIsNonLazy(ID))
4966 DefinedNonLazyClasses.push_back(ClassMD);
4967
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004968 // Force the definition of the EHType if necessary.
4969 if (flags & CLS_EXCEPTION)
4970 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004971}
4972
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004973/// GenerateProtocolRef - This routine is called to generate code for
4974/// a protocol reference expression; as in:
4975/// @code
4976/// @protocol(Proto1);
4977/// @endcode
4978/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4979/// which will hold address of the protocol meta-data.
4980///
4981llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004982 const ObjCProtocolDecl *PD) {
4983
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004984 // This routine is called for @protocol only. So, we must build definition
4985 // of protocol's meta-data (not a reference to it!)
4986 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004987 llvm::Constant *Init =
4988 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4989 ObjCTypes.ExternalProtocolPtrTy);
4990
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004991 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00004992 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004994 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4995 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004996 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004997 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004998 CGM.getModule(),
4999 Init->getType(), false,
5000 llvm::GlobalValue::WeakAnyLinkage,
5001 Init,
5002 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005003 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5004 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005005 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005006 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005007}
5008
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005009/// GenerateCategory - Build metadata for a category implementation.
5010/// struct _category_t {
5011/// const char * const name;
5012/// struct _class_t *const cls;
5013/// const struct _method_list_t * const instance_methods;
5014/// const struct _method_list_t * const class_methods;
5015/// const struct _protocol_list_t * const protocols;
5016/// const struct _prop_list_t * const properties;
5017/// }
5018///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005019void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005020 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005021 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005022 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5023 "_$_" + OCD->getNameAsString());
5024 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005025 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005026
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005027 std::vector<llvm::Constant*> Values(6);
5028 Values[0] = GetClassName(OCD->getIdentifier());
5029 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005030 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005031 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005032 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5033
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005034 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005035 std::vector<llvm::Constant*> Methods;
5036 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005037 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005038 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005039
5040 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005041 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005042 // Instance methods should always be defined.
5043 Methods.push_back(GetMethodConstant(*i));
5044 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005045
5046 Values[2] = EmitMethodList(MethodListName,
5047 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005048 Methods);
5049
5050 MethodListName = Prefix;
5051 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5052 OCD->getNameAsString();
5053 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005055 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005056 // Class methods should always be defined.
5057 Methods.push_back(GetMethodConstant(*i));
5058 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005059
5060 Values[3] = EmitMethodList(MethodListName,
5061 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005062 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005063 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005064 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005065 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005066 llvm::SmallString<256> ExtName;
5067 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5068 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005069 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005070 + Interface->getName() + "_$_"
5071 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005072 Category->protocol_begin(),
5073 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005074 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5075 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005076 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005077 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5078 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005079 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005080
5081 llvm::Constant *Init =
5082 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005083 Values);
5084 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005086 false,
5087 llvm::GlobalValue::InternalLinkage,
5088 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005089 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005090 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005091 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005092 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005093 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005094 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005095
5096 // Determine if this category is also "non-lazy".
5097 if (ImplementationIsNonLazy(OCD))
5098 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005099}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005100
5101/// GetMethodConstant - Return a struct objc_method constant for the
5102/// given method if it has been defined. The result is null if the
5103/// method has not been defined. The return value has type MethodPtrTy.
5104llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005105 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005106 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005107 if (!Fn)
5108 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005110 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005111 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005112 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005113 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005114 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005115 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005116 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005117}
5118
5119/// EmitMethodList - Build meta-data for method declarations
5120/// struct _method_list_t {
5121/// uint32_t entsize; // sizeof(struct _objc_method)
5122/// uint32_t method_count;
5123/// struct _objc_method method_list[method_count];
5124/// }
5125///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005126llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5127 const char *Section,
5128 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005129 // Return null for empty list.
5130 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005131 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005132
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005133 std::vector<llvm::Constant*> Values(3);
5134 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005135 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005136 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005137 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005138 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005139 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005140 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005141 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005142 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005143
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005144 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005145 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005146 llvm::GlobalValue::InternalLinkage,
5147 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005148 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005149 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005150 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005151 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005152 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005153 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005154 ObjCTypes.MethodListnfABIPtrTy);
5155}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005156
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005157/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5158/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005159llvm::GlobalVariable *
5160CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5161 const ObjCIvarDecl *Ivar) {
5162 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005163 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005164 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005165 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005166 CGM.getModule().getGlobalVariable(Name);
5167 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005168 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005169 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005170 false,
5171 llvm::GlobalValue::ExternalLinkage,
5172 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005173 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005174 return IvarOffsetGV;
5175}
5176
Daniel Dunbare83be122010-04-02 21:14:02 +00005177llvm::Constant *
5178CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5179 const ObjCIvarDecl *Ivar,
5180 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005181 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005182 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005183 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005184 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005185 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005186
Mike Stumpf5408fe2009-05-16 07:57:57 +00005187 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5188 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005189 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5190 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005191 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005192 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005193 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005194 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005195 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005196 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005197}
5198
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005199/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005200/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005201/// IvarListnfABIPtrTy.
5202/// struct _ivar_t {
5203/// unsigned long int *offset; // pointer to ivar offset location
5204/// char *name;
5205/// char *type;
5206/// uint32_t alignment;
5207/// uint32_t size;
5208/// }
5209/// struct _ivar_list_t {
5210/// uint32 entsize; // sizeof(struct _ivar_t)
5211/// uint32 count;
5212/// struct _iver_t list[count];
5213/// }
5214///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005215
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005216llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005217 const ObjCImplementationDecl *ID) {
5218
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005219 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005220
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005221 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5222 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005223
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005224 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005225
Daniel Dunbar91636d62009-04-20 00:33:43 +00005226 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005227 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005228 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005229
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005230 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5231 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005232 // Ignore unnamed bit-fields.
5233 if (!IVD->getDeclName())
5234 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005235 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005236 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005237 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5238 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005239 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005240 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005241 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005242 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005243 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005244 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005245 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005246 // NOTE. Size of a bitfield does not match gcc's, because of the
5247 // way bitfields are treated special in each. But I am told that
5248 // 'size' for bitfield ivars is ignored by the runtime so it does
5249 // not matter. If it matters, there is enough info to get the
5250 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005251 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005252 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005253 }
5254 // Return null for empty list.
5255 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005256 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005257 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005258 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005259 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5260 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005261 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005262 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005263 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005264 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005265 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5266 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005267 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005268 llvm::GlobalValue::InternalLinkage,
5269 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005270 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005271 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005272 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005273 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005274
Chris Lattnerad64e022009-07-17 23:57:13 +00005275 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005276 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005277}
5278
5279llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005281 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005282
Fariborz Jahanianda320092009-01-29 19:24:30 +00005283 if (!Entry) {
5284 // We use the initializer as a marker of whether this is a forward
5285 // reference or not. At module finalization we add the empty
5286 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005287 Entry =
5288 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5289 llvm::GlobalValue::ExternalLinkage,
5290 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005291 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005292 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005293 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005294
Fariborz Jahanianda320092009-01-29 19:24:30 +00005295 return Entry;
5296}
5297
5298/// GetOrEmitProtocol - Generate the protocol meta-data:
5299/// @code
5300/// struct _protocol_t {
5301/// id isa; // NULL
5302/// const char * const protocol_name;
5303/// const struct _protocol_list_t * protocol_list; // super protocols
5304/// const struct method_list_t * const instance_methods;
5305/// const struct method_list_t * const class_methods;
5306/// const struct method_list_t *optionalInstanceMethods;
5307/// const struct method_list_t *optionalClassMethods;
5308/// const struct _prop_list_t * properties;
5309/// const uint32_t size; // sizeof(struct _protocol_t)
5310/// const uint32_t flags; // = 0
5311/// }
5312/// @endcode
5313///
5314
5315llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005317 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318
Fariborz Jahanianda320092009-01-29 19:24:30 +00005319 // Early exit if a defining object has already been generated.
5320 if (Entry && Entry->hasInitializer())
5321 return Entry;
5322
Fariborz Jahanianda320092009-01-29 19:24:30 +00005323 // Construct method lists.
5324 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5325 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005326 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005327 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005328 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005329 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005330 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5331 OptInstanceMethods.push_back(C);
5332 } else {
5333 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005335 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005336
5337 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005338 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005339 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005340 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005341 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5342 OptClassMethods.push_back(C);
5343 } else {
5344 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005345 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005346 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347
Fariborz Jahanianda320092009-01-29 19:24:30 +00005348 std::vector<llvm::Constant*> Values(10);
5349 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005350 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005351 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005352 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5353 PD->protocol_begin(),
5354 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005355
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005356 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005357 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005358 "__DATA, __objc_const",
5359 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005361 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005362 "__DATA, __objc_const",
5363 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005364 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005365 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 "__DATA, __objc_const",
5367 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005368 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005369 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005370 "__DATA, __objc_const",
5371 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005372 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005373 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005375 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005376 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005377 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005378 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005379 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005380
Fariborz Jahanianda320092009-01-29 19:24:30 +00005381 if (Entry) {
5382 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005383 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005384 Entry->setInitializer(Init);
5385 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005387 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5388 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5389 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005390 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005391 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005392 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005393 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005394 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005395 CGM.AddUsedGlobal(Entry);
5396
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005397 // Use this protocol meta-data to build protocol list table in section
5398 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005399 llvm::GlobalVariable *PTGV =
5400 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5401 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5402 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005403 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005404 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005405 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005406 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005407 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005408 return Entry;
5409}
5410
5411/// EmitProtocolList - Generate protocol list meta-data:
5412/// @code
5413/// struct _protocol_list_t {
5414/// long protocol_count; // Note, this is 32/64 bit
5415/// struct _protocol_t[protocol_count];
5416/// }
5417/// @endcode
5418///
5419llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005420CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5421 ObjCProtocolDecl::protocol_iterator begin,
5422 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005423 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005424
Fariborz Jahanianda320092009-01-29 19:24:30 +00005425 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005426 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005427 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005428
Daniel Dunbar948e2582009-02-15 07:36:20 +00005429 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005430 llvm::SmallString<256> TmpName;
5431 Name.toVector(TmpName);
5432 llvm::GlobalVariable *GV =
5433 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005434 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005435 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005436
Daniel Dunbar948e2582009-02-15 07:36:20 +00005437 for (; begin != end; ++begin)
5438 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5439
Fariborz Jahanianda320092009-01-29 19:24:30 +00005440 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005441 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005442 ObjCTypes.ProtocolnfABIPtrTy));
5443
Fariborz Jahanianda320092009-01-29 19:24:30 +00005444 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005445 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005446 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005447 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005448 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005449 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005450 ProtocolRefs.size()),
5451 ProtocolRefs);
5452
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005453 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005454 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005455 llvm::GlobalValue::InternalLinkage,
5456 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005457 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005458 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005459 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005460 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005461 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005463 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005464}
5465
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005466/// GetMethodDescriptionConstant - This routine build following meta-data:
5467/// struct _objc_method {
5468/// SEL _cmd;
5469/// char *method_type;
5470/// char *_imp;
5471/// }
5472
5473llvm::Constant *
5474CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5475 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005476 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5478 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005479 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005480 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005481 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005482 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005483}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005484
5485/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5486/// This code gen. amounts to generating code for:
5487/// @code
5488/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5489/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005490///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005491LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005492 CodeGen::CodeGenFunction &CGF,
5493 QualType ObjectTy,
5494 llvm::Value *BaseValue,
5495 const ObjCIvarDecl *Ivar,
5496 unsigned CVRQualifiers) {
5497 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005498 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5499 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005500}
5501
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005502llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005503 CodeGen::CodeGenFunction &CGF,
5504 const ObjCInterfaceDecl *Interface,
5505 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005506 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005507}
5508
Fariborz Jahanian46551122009-02-04 00:22:57 +00005509CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005510 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005511 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005512 QualType ResultType,
5513 Selector Sel,
5514 llvm::Value *Receiver,
5515 QualType Arg0Ty,
5516 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005517 const CallArgList &CallArgs,
5518 const ObjCMethodDecl *Method) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005519 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5520 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005521 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005522 llvm::Value *Arg0 = Receiver;
5523 if (!IsSuper)
5524 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005526 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005527 // FIXME. This is too much work to get the ABI-specific result type needed to
5528 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005529 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005530 = Types.getFunctionInfo(ResultType, CallArgList(),
5531 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005532 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005533 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005534 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00005535 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner9b7d6702010-08-18 16:09:06 +00005536 if (IsSuper) {
5537 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5538 Name += "objc_msgSendSuper2_stret_fixup";
5539 } else {
5540 Fn = ObjCTypes.getMessageSendStretFixupFn();
5541 Name += "objc_msgSend_stret_fixup";
5542 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005543 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5544 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5545 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005546 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005547 if (IsSuper) {
5548 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5549 Name += "objc_msgSendSuper2_fixup";
5550 } else {
5551 Fn = ObjCTypes.getMessageSendFixupFn();
5552 Name += "objc_msgSend_fixup";
5553 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005554 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005555 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005556 Name += '_';
5557 std::string SelName(Sel.getAsString());
5558 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005559 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005560 if (SelName[i] == ':')
5561 SelName[i] = '_';
5562 Name += SelName;
5563 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5564 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005565 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005566 std::vector<llvm::Constant*> Values(2);
5567 Values[0] = Fn;
5568 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005569 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005570 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005571 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005572 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005573 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005574 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005575 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005576 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005577 }
5578 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005579
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005580 CallArgList ActualArgs;
5581 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005583 ObjCTypes.MessageRefCPtrTy));
5584 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005585 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005586 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005587 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5588 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005589 const llvm::FunctionType *FTy =
5590 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005591 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005592 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005593 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005594}
5595
5596/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005597CodeGen::RValue
5598CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005599 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005600 QualType ResultType,
5601 Selector Sel,
5602 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005603 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005604 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005605 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005606 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005607 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5608 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005610 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005611 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005612 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005613 false, CallArgs, Method);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005614}
5615
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005616llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005617CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005618 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5619
Daniel Dunbardfff2302009-03-02 05:18:14 +00005620 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005621 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005622 false, llvm::GlobalValue::ExternalLinkage,
5623 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005624 }
5625
5626 return GV;
5627}
5628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005629llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5630 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005631 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005632
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005633 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005634 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005635 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005636 Entry =
5637 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005638 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005640 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005641 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005642 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005643 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005644 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005645 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005646 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005647
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005648 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005649}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005650
Daniel Dunbar11394522009-04-18 08:51:00 +00005651llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005653 const ObjCInterfaceDecl *ID) {
5654 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005655
Daniel Dunbar11394522009-04-18 08:51:00 +00005656 if (!Entry) {
5657 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5658 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 Entry =
5660 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005661 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005662 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005663 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005664 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005665 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005666 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005667 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005668 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005669 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005670
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005671 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005672}
5673
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005674/// EmitMetaClassRef - Return a Value * of the address of _class_t
5675/// meta-data
5676///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005677llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5678 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005679 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5680 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005681 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005682
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005683 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005684 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005685 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005686 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005687 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005688 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005689 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005690 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005691 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692 ObjCTypes.ClassnfABIPtrTy));
5693
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005694 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005695 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005696
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005697 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005698}
5699
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005700/// GetClass - Return a reference to the class for the given interface
5701/// decl.
5702llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5703 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005704 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005705 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5706 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5707 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5708 }
5709
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005710 return EmitClassRef(Builder, ID);
5711}
5712
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005713/// Generates a message send where the super is the receiver. This is
5714/// a message send to self with special delivery semantics indicating
5715/// which class's method should be called.
5716CodeGen::RValue
5717CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005718 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719 QualType ResultType,
5720 Selector Sel,
5721 const ObjCInterfaceDecl *Class,
5722 bool isCategoryImpl,
5723 llvm::Value *Receiver,
5724 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005725 const CodeGen::CallArgList &CallArgs,
5726 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005727 // ...
5728 // Create and init a super structure; this is a (receiver, class)
5729 // pair we will pass to objc_msgSendSuper.
5730 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005731 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005733 llvm::Value *ReceiverAsObject =
5734 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5735 CGF.Builder.CreateStore(ReceiverAsObject,
5736 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005738 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005739 llvm::Value *Target;
5740 if (IsClassMessage) {
5741 if (isCategoryImpl) {
5742 // Message sent to "super' in a class method defined in
5743 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005744 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005745 Target = CGF.Builder.CreateStructGEP(Target, 0);
5746 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005747 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005748 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005749 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005750 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005751
Mike Stumpf5408fe2009-05-16 07:57:57 +00005752 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5753 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754 const llvm::Type *ClassTy =
5755 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5756 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5757 CGF.Builder.CreateStore(Target,
5758 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005760 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005761 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5762 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005764 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005765 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005766 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005767 true, CallArgs, Method);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005768}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005769
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005770llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005771 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005772 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005773
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005774 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005775 llvm::Constant *Casted =
5776 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5777 ObjCTypes.SelectorPtrTy);
5778 Entry =
5779 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5780 llvm::GlobalValue::InternalLinkage,
5781 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005782 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005783 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005784 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005785
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005786 if (lval)
5787 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005788 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005789}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005790/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005791/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005792///
5793void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005794 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005795 llvm::Value *dst,
5796 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005797 const llvm::Type * SrcTy = src->getType();
5798 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005799 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005800 assert(Size <= 8 && "does not support size > 8");
5801 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5802 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005803 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5804 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005805 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5806 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005807 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5808 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005809 return;
5810}
5811
5812/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5813/// objc_assign_strongCast (id src, id *dst)
5814///
5815void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005816 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005817 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005818 const llvm::Type * SrcTy = src->getType();
5819 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005820 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005821 assert(Size <= 8 && "does not support size > 8");
5822 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005823 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005824 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5825 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005826 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5827 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005828 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005829 src, dst, "weakassign");
5830 return;
5831}
5832
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005833void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834 CodeGen::CodeGenFunction &CGF,
5835 llvm::Value *DestPtr,
5836 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005837 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005838 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5839 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005840 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005841 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005842 return;
5843}
5844
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005845/// EmitObjCWeakRead - Code gen for loading value of a __weak
5846/// object: objc_read_weak (id *src)
5847///
5848llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005850 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005851 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005852 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5853 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005854 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005855 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005856 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005857 return read_weak;
5858}
5859
5860/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5861/// objc_assign_weak (id src, id *dst)
5862///
5863void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005864 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005865 const llvm::Type * SrcTy = src->getType();
5866 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005867 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005868 assert(Size <= 8 && "does not support size > 8");
5869 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5870 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005871 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5872 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5874 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005875 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005876 src, dst, "weakassign");
5877 return;
5878}
5879
5880/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5881/// objc_assign_global (id src, id *dst)
5882///
5883void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005884 llvm::Value *src, llvm::Value *dst,
5885 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005886 const llvm::Type * SrcTy = src->getType();
5887 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005888 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005889 assert(Size <= 8 && "does not support size > 8");
5890 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5891 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005892 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5893 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005894 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5895 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005896 if (!threadlocal)
5897 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5898 src, dst, "globalassign");
5899 else
5900 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5901 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005902 return;
5903}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005904
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905void
John McCallf1549f62010-07-06 01:34:17 +00005906CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5907 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005908 EmitAtSynchronizedStmt(CGF, S,
5909 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5910 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005911}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005912
John McCall5a180392010-07-24 00:37:23 +00005913llvm::Constant *
5914CGObjCNonFragileABIMac::GetEHType(QualType T) {
5915 // There's a particular fixed type info for 'id'.
5916 if (T->isObjCIdType() ||
5917 T->isObjCQualifiedIdType()) {
5918 llvm::Constant *IDEHType =
5919 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5920 if (!IDEHType)
5921 IDEHType =
5922 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5923 false,
5924 llvm::GlobalValue::ExternalLinkage,
5925 0, "OBJC_EHTYPE_id");
5926 return IDEHType;
5927 }
5928
5929 // All other types should be Objective-C interface pointer types.
5930 const ObjCObjectPointerType *PT =
5931 T->getAs<ObjCObjectPointerType>();
5932 assert(PT && "Invalid @catch type.");
5933 const ObjCInterfaceType *IT = PT->getInterfaceType();
5934 assert(IT && "Invalid @catch type.");
5935 return GetInterfaceEHType(IT->getDecl(), false);
5936}
5937
John McCallf1549f62010-07-06 01:34:17 +00005938void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5939 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005940 EmitTryCatchStmt(CGF, S,
5941 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5942 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5943 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005944}
5945
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005946/// EmitThrowStmt - Generate code for a throw statement.
5947void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5948 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005949 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00005950 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00005951 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
5952 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00005953 llvm::Value *Args[] = { Exception };
5954 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
5955 Args, Args+1)
5956 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005957 } else {
John McCall7ec404c2010-10-16 08:21:07 +00005958 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
5959 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005960 }
5961
John McCall7ec404c2010-10-16 08:21:07 +00005962 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005963 CGF.Builder.ClearInsertionPoint();
5964}
Daniel Dunbare588b992009-03-01 04:46:24 +00005965
John McCall5a180392010-07-24 00:37:23 +00005966llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005967CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005968 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005969 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005970
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005971 // If we don't need a definition, return the entry if found or check
5972 // if we use an external reference.
5973 if (!ForDefinition) {
5974 if (Entry)
5975 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005976
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005977 // If this type (or a super class) has the __objc_exception__
5978 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005979 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005980 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005981 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005982 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005983 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005984 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005985 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005986 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005987
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005988 // Otherwise we need to either make a new entry or fill in the
5989 // initializer.
5990 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005991 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005992 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005993 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00005994 CGM.getModule().getGlobalVariable(VTableName);
5995 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005996 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5997 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005998 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005999 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006000
Chris Lattner77b89b82010-06-27 07:15:29 +00006001 llvm::Value *VTableIdx =
6002 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006003
6004 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006005 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006006 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006007 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006008 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006009 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006010
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006011 if (Entry) {
6012 Entry->setInitializer(Init);
6013 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006014 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006015 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006016 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006017 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006018 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006019 }
6020
John McCall1fb0caa2010-10-22 21:05:15 +00006021 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006022 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006023 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6024 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006025
6026 if (ForDefinition) {
6027 Entry->setSection("__DATA,__objc_const");
6028 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6029 } else {
6030 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6031 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006032
6033 return Entry;
6034}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006035
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006036/* *** */
6037
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006038CodeGen::CGObjCRuntime *
6039CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006040 if (CGM.getLangOptions().ObjCNonFragileABI)
6041 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006042 return new CGObjCMac(CGM);
6043}