blob: 3447daf5c2fd67b87ff5e1b50b9aee9725f5a40d [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-04-22 07:32:20 +000045
John McCall448d2cd2011-02-26 09:48:59 +000046static void EmitNullReturnInitialization(CodeGenFunction &CGF,
47 ReturnValueSlot &returnSlot,
48 QualType resultType) {
49 // Force the return slot to exist.
50 if (!returnSlot.getValue())
51 returnSlot = ReturnValueSlot(CGF.CreateMemTemp(resultType), false);
52 CGF.EmitNullInitialization(returnSlot.getValue(), resultType);
53}
54
Daniel Dunbar97776872009-04-22 07:32:20 +000055
56///
57
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000058namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000059
Daniel Dunbar6bff2512009-08-03 17:06:42 +000060typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +000061
Daniel Dunbar6bff2512009-08-03 17:06:42 +000062// FIXME: We should find a nicer way to make the labels for metadata, string
63// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000064
Fariborz Jahanianee0af742009-01-21 22:04:16 +000065class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000066protected:
67 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000068
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000069private:
70 llvm::Constant *getMessageSendFn() const {
71 // id objc_msgSend (id, SEL, ...)
72 std::vector<const llvm::Type*> Params;
73 Params.push_back(ObjectPtrTy);
74 Params.push_back(SelectorPtrTy);
75 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +000076 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +000077 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +000078 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000079 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000080
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000081 llvm::Constant *getMessageSendStretFn() const {
82 // id objc_msgSend_stret (id, SEL, ...)
83 std::vector<const llvm::Type*> Params;
84 Params.push_back(ObjectPtrTy);
85 Params.push_back(SelectorPtrTy);
86 return
Owen Anderson0032b272009-08-13 21:57:51 +000087 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +000088 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +000089 "objc_msgSend_stret");
90
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000092
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
94 // FIXME: This should be long double on x86_64?
95 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
96 std::vector<const llvm::Type*> Params;
97 Params.push_back(ObjectPtrTy);
98 Params.push_back(SelectorPtrTy);
99 return
Owen Anderson0032b272009-08-13 21:57:51 +0000100 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
101 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000102 Params,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000103 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000104 "objc_msgSend_fpret");
105
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000106 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000107
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000108 llvm::Constant *getMessageSendSuperFn() const {
109 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
110 const char *SuperName = "objc_msgSendSuper";
111 std::vector<const llvm::Type*> Params;
112 Params.push_back(SuperPtrTy);
113 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000114 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000115 Params, true),
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000116 SuperName);
117 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000118
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn2() const {
120 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
121 const char *SuperName = "objc_msgSendSuper2";
122 std::vector<const llvm::Type*> Params;
123 Params.push_back(SuperPtrTy);
124 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000126 Params, true),
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000127 SuperName);
128 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000129
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperStretFn() const {
131 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
132 // SEL op, ...)
133 std::vector<const llvm::Type*> Params;
134 Params.push_back(Int8PtrTy);
135 Params.push_back(SuperPtrTy);
136 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000137 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000138 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000139 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000140 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000142
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000143 llvm::Constant *getMessageSendSuperStretFn2() const {
144 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
145 // SEL op, ...)
146 std::vector<const llvm::Type*> Params;
147 Params.push_back(Int8PtrTy);
148 Params.push_back(SuperPtrTy);
149 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000150 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000151 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000152 Params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000153 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000154 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperFpretFn() const {
157 // There is no objc_msgSendSuper_fpret? How can that work?
158 return getMessageSendSuperFn();
159 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000161 llvm::Constant *getMessageSendSuperFpretFn2() const {
162 // There is no objc_msgSendSuper_fpret? How can that work?
163 return getMessageSendSuperFn2();
164 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000165
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000166protected:
167 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000168
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000169public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000170 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000171 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000173 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
174 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000176 /// PtrObjectPtrTy - LLVM type for id *
177 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000179 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000180 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000181 /// ProtocolPtrTy - LLVM type for external protocol handles
182 /// (typeof(Protocol))
183 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000184
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000185 // SuperCTy - clang type for struct objc_super.
186 QualType SuperCTy;
187 // SuperPtrCTy - clang type for struct objc_super *.
188 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000189
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000190 /// SuperTy - LLVM type for struct objc_super.
191 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000192 /// SuperPtrTy - LLVM type for struct objc_super *.
193 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000194
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000195 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
196 /// in GCC parlance).
197 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000198
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000199 /// PropertyListTy - LLVM type for struct objc_property_list
200 /// (_prop_list_t in GCC parlance).
201 const llvm::StructType *PropertyListTy;
202 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
203 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000204
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000205 // MethodTy - LLVM type for struct objc_method.
206 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000207
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000208 /// CacheTy - LLVM type for struct objc_cache.
209 const llvm::Type *CacheTy;
210 /// CachePtrTy - LLVM type for struct objc_cache *.
211 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Chris Lattner72db6c32009-04-22 02:44:54 +0000213 llvm::Constant *getGetPropertyFn() {
214 CodeGen::CodeGenTypes &Types = CGM.getTypes();
215 ASTContext &Ctx = CGM.getContext();
216 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000217 llvm::SmallVector<CanQualType,4> Params;
218 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
219 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000220 Params.push_back(IdType);
221 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000222 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000223 Params.push_back(Ctx.BoolTy);
224 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000225 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000226 FunctionType::ExtInfo()),
227 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000228 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000230
Chris Lattner72db6c32009-04-22 02:44:54 +0000231 llvm::Constant *getSetPropertyFn() {
232 CodeGen::CodeGenTypes &Types = CGM.getTypes();
233 ASTContext &Ctx = CGM.getContext();
234 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000235 llvm::SmallVector<CanQualType,6> Params;
236 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
237 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000238 Params.push_back(IdType);
239 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000240 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000241 Params.push_back(IdType);
242 Params.push_back(Ctx.BoolTy);
243 Params.push_back(Ctx.BoolTy);
244 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000245 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000246 FunctionType::ExtInfo()),
247 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000251
252 llvm::Constant *getCopyStructFn() {
253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
254 ASTContext &Ctx = CGM.getContext();
255 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
256 llvm::SmallVector<CanQualType,5> Params;
257 Params.push_back(Ctx.VoidPtrTy);
258 Params.push_back(Ctx.VoidPtrTy);
259 Params.push_back(Ctx.LongTy);
260 Params.push_back(Ctx.BoolTy);
261 Params.push_back(Ctx.BoolTy);
262 const llvm::FunctionType *FTy =
263 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
264 FunctionType::ExtInfo()),
265 false);
266 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
267 }
268
Chris Lattner72db6c32009-04-22 02:44:54 +0000269 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
271 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000272 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000273 llvm::SmallVector<CanQualType,1> Params;
274 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000275 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000276 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000277 FunctionType::ExtInfo()),
278 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
280 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000281
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000282 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000283 llvm::Constant *getGcReadWeakFn() {
284 // id objc_read_weak (id *)
285 std::vector<const llvm::Type*> Args;
286 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000287 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000288 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000289 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000290 }
291
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000292 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000293 llvm::Constant *getGcAssignWeakFn() {
294 // id objc_assign_weak (id, id *)
295 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
296 Args.push_back(ObjectPtrTy->getPointerTo());
297 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000298 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000299 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
300 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000301
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000302 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000303 llvm::Constant *getGcAssignGlobalFn() {
304 // id objc_assign_global(id, id *)
305 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
306 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000307 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000308 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000309 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000311
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000312 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
313 llvm::Constant *getGcAssignThreadLocalFn() {
314 // id objc_assign_threadlocal(id src, id * dest)
315 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
316 Args.push_back(ObjectPtrTy->getPointerTo());
317 llvm::FunctionType *FTy =
318 llvm::FunctionType::get(ObjectPtrTy, Args, false);
319 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
320 }
321
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000322 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000323 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000324 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000325 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
326 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000327 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000328 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000329 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
331 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000332
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000333 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
334 llvm::Constant *GcMemmoveCollectableFn() {
335 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
336 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
337 Args.push_back(Int8PtrTy);
338 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000339 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
341 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000342
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000343 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000344 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000345 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000346 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
347 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000348 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000349 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000350 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
351 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000352
353 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000354 llvm::Constant *getExceptionThrowFn() {
355 // void objc_exception_throw(id)
356 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
357 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000358 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
360 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000361
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000362 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
363 llvm::Constant *getExceptionRethrowFn() {
364 // void objc_exception_rethrow(void)
365 std::vector<const llvm::Type*> Args;
366 llvm::FunctionType *FTy =
John McCall7ec404c2010-10-16 08:21:07 +0000367 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
369 }
370
Daniel Dunbar1c566672009-02-24 01:43:46 +0000371 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000372 llvm::Constant *getSyncEnterFn() {
373 // void objc_sync_enter (id)
374 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
375 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000376 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000379
Daniel Dunbar1c566672009-02-24 01:43:46 +0000380 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000381 llvm::Constant *getSyncExitFn() {
382 // void objc_sync_exit (id)
383 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
384 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000385 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000386 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
387 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000388
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000389 llvm::Constant *getSendFn(bool IsSuper) const {
390 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
391 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000392
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000393 llvm::Constant *getSendFn2(bool IsSuper) const {
394 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
395 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000396
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000397 llvm::Constant *getSendStretFn(bool IsSuper) const {
398 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000400
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000401 llvm::Constant *getSendStretFn2(bool IsSuper) const {
402 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000404
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendFpretFn(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
407 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000408
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
411 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000412
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000413 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
414 ~ObjCCommonTypesHelper(){}
415};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000416
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000417/// ObjCTypesHelper - Helper class that encapsulates lazy
418/// construction of varies types used during ObjC generation.
419class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000420public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000421 /// SymtabTy - LLVM type for struct objc_symtab.
422 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000423 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
424 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000425 /// ModuleTy - LLVM type for struct objc_module.
426 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000427
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000428 /// ProtocolTy - LLVM type for struct objc_protocol.
429 const llvm::StructType *ProtocolTy;
430 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
431 const llvm::Type *ProtocolPtrTy;
432 /// ProtocolExtensionTy - LLVM type for struct
433 /// objc_protocol_extension.
434 const llvm::StructType *ProtocolExtensionTy;
435 /// ProtocolExtensionTy - LLVM type for struct
436 /// objc_protocol_extension *.
437 const llvm::Type *ProtocolExtensionPtrTy;
438 /// MethodDescriptionTy - LLVM type for struct
439 /// objc_method_description.
440 const llvm::StructType *MethodDescriptionTy;
441 /// MethodDescriptionListTy - LLVM type for struct
442 /// objc_method_description_list.
443 const llvm::StructType *MethodDescriptionListTy;
444 /// MethodDescriptionListPtrTy - LLVM type for struct
445 /// objc_method_description_list *.
446 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000447 /// ProtocolListTy - LLVM type for struct objc_property_list.
448 const llvm::Type *ProtocolListTy;
449 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
450 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000451 /// CategoryTy - LLVM type for struct objc_category.
452 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000453 /// ClassTy - LLVM type for struct objc_class.
454 const llvm::StructType *ClassTy;
455 /// ClassPtrTy - LLVM type for struct objc_class *.
456 const llvm::Type *ClassPtrTy;
457 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
458 const llvm::StructType *ClassExtensionTy;
459 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
460 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000461 // IvarTy - LLVM type for struct objc_ivar.
462 const llvm::StructType *IvarTy;
463 /// IvarListTy - LLVM type for struct objc_ivar_list.
464 const llvm::Type *IvarListTy;
465 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
466 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000467 /// MethodListTy - LLVM type for struct objc_method_list.
468 const llvm::Type *MethodListTy;
469 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
470 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000471
Anders Carlsson124526b2008-09-09 10:10:21 +0000472 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
473 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000474
Anders Carlsson124526b2008-09-09 10:10:21 +0000475 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000476 llvm::Constant *getExceptionTryEnterFn() {
477 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000478 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000479 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000480 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000481 Params, false),
482 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000483 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000484
485 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000486 llvm::Constant *getExceptionTryExitFn() {
487 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000488 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000489 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000490 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000491 Params, false),
492 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000493 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000494
495 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000496 llvm::Constant *getExceptionExtractFn() {
497 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000498 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
499 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000500 Params, false),
501 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000502
Chris Lattner34b02a12009-04-22 02:26:14 +0000503 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000504
Anders Carlsson124526b2008-09-09 10:10:21 +0000505 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000506 llvm::Constant *getExceptionMatchFn() {
507 std::vector<const llvm::Type*> Params;
508 Params.push_back(ClassPtrTy);
509 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000510 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000511 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000512 Params, false),
513 "objc_exception_match");
514
Chris Lattner34b02a12009-04-22 02:26:14 +0000515 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000516
Anders Carlsson124526b2008-09-09 10:10:21 +0000517 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000518 llvm::Constant *getSetJmpFn() {
519 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000520 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000521 return
Owen Anderson0032b272009-08-13 21:57:51 +0000522 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000523 Params, false),
524 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000525
Chris Lattner34b02a12009-04-22 02:26:14 +0000526 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000527
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000528public:
529 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000530 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000531};
532
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000533/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000534/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000535class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000536public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000537
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000538 // MethodListnfABITy - LLVM for struct _method_list_t
539 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000540
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000541 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
542 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000544 // ProtocolnfABITy = LLVM for struct _protocol_t
545 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000546
Daniel Dunbar948e2582009-02-15 07:36:20 +0000547 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
548 const llvm::Type *ProtocolnfABIPtrTy;
549
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000550 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
551 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000553 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
554 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000555
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000556 // ClassnfABITy - LLVM for struct _class_t
557 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000558
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000559 // ClassnfABIPtrTy - LLVM for struct _class_t*
560 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000561
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000562 // IvarnfABITy - LLVM for struct _ivar_t
563 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000564
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000565 // IvarListnfABITy - LLVM for struct _ivar_list_t
566 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000568 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
569 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000571 // ClassRonfABITy - LLVM for struct _class_ro_t
572 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000573
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000574 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
575 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000577 // CategorynfABITy - LLVM for struct _category_t
578 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000580 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000582 // MessageRefTy - LLVM for:
583 // struct _message_ref_t {
584 // IMP messenger;
585 // SEL name;
586 // };
587 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000588 // MessageRefCTy - clang type for struct _message_ref_t
589 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000591 // MessageRefPtrTy - LLVM for struct _message_ref_t*
592 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000593 // MessageRefCPtrTy - clang type for struct _message_ref_t*
594 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000595
Fariborz Jahanianef163782009-02-05 01:13:09 +0000596 // MessengerTy - Type of the messenger (shown as IMP above)
597 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000599 // SuperMessageRefTy - LLVM for:
600 // struct _super_message_ref_t {
601 // SUPER_IMP messenger;
602 // SEL name;
603 // };
604 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000606 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
607 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000608
Chris Lattner1c02f862009-04-22 02:53:24 +0000609 llvm::Constant *getMessageSendFixupFn() {
610 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
611 std::vector<const llvm::Type*> Params;
612 Params.push_back(ObjectPtrTy);
613 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000614 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000615 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000616 "objc_msgSend_fixup");
617 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Chris Lattner1c02f862009-04-22 02:53:24 +0000619 llvm::Constant *getMessageSendFpretFixupFn() {
620 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
621 std::vector<const llvm::Type*> Params;
622 Params.push_back(ObjectPtrTy);
623 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000624 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000625 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000626 "objc_msgSend_fpret_fixup");
627 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Chris Lattner1c02f862009-04-22 02:53:24 +0000629 llvm::Constant *getMessageSendStretFixupFn() {
630 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
631 std::vector<const llvm::Type*> Params;
632 Params.push_back(ObjectPtrTy);
633 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000635 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000636 "objc_msgSend_stret_fixup");
637 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Chris Lattner1c02f862009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000641 // struct _super_message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(SuperPtrTy);
644 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000646 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000647 "objc_msgSendSuper2_fixup");
648 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Chris Lattner1c02f862009-04-22 02:53:24 +0000650 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000652 // struct _super_message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(SuperPtrTy);
655 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000656 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanianbf1f8262011-02-28 21:19:34 +0000657 Params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000658 "objc_msgSendSuper2_stret_fixup");
659 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Chris Lattner8a569112009-04-22 02:15:23 +0000661 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000662 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000663 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000664 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Chris Lattner8a569112009-04-22 02:15:23 +0000666 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Chris Lattner8a569112009-04-22 02:15:23 +0000668 llvm::Constant *getObjCBeginCatchFn() {
669 std::vector<const llvm::Type*> Params;
670 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000671 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000672 Params, false),
673 "objc_begin_catch");
674 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000675
676 const llvm::StructType *EHTypeTy;
677 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000678
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000679 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
680 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000681};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000682
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000683class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000684public:
685 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000686 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000687 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000688 unsigned ivar_bytepos;
689 unsigned ivar_size;
690 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000692
693 // Allow sorting based on byte pos.
694 bool operator<(const GC_IVAR &b) const {
695 return ivar_bytepos < b.ivar_bytepos;
696 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000697 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000698
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000699 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000700 public:
701 unsigned skip;
702 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000703 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000704 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000705 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000707protected:
708 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000709 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000710 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000711 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000713 // gc ivar layout bitmap calculation helper caches.
714 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
715 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000716
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000717 /// LazySymbols - Symbols to generate a lazy reference for. See
718 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000719 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000720
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000721 /// DefinedSymbols - External symbols which are defined by this
722 /// module. The symbols in this list and LazySymbols are used to add
723 /// special linker symbols which ensure that Objective-C modules are
724 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000725 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000726
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000727 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000728 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000729
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000730 /// MethodVarNames - uniqued method variable names.
731 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000733 /// DefinedCategoryNames - list of category names in form Class_Category.
734 llvm::SetVector<std::string> DefinedCategoryNames;
735
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000736 /// MethodVarTypes - uniqued method type signatures. We have to use
737 /// a StringMap here because have no other unique reference.
738 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000739
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000740 /// MethodDefinitions - map of methods which have been defined in
741 /// this translation unit.
742 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000743
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000744 /// PropertyNames - uniqued method variable names.
745 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000746
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000747 /// ClassReferences - uniqued class references.
748 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000750 /// SelectorReferences - uniqued selector references.
751 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000753 /// Protocols - Protocols for which an objc_protocol structure has
754 /// been emitted. Forward declarations are handled by creating an
755 /// empty structure whose initializer is filled in when/if defined.
756 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000758 /// DefinedProtocols - Protocols which have actually been
759 /// defined. We should not need this, see FIXME in GenerateProtocol.
760 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000762 /// DefinedClasses - List of defined classes.
763 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000764
765 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
766 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000767
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000768 /// DefinedCategories - List of defined categories.
769 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000771 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
772 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000773
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000774 /// GetNameForMethod - Return a name for the given method.
775 /// \param[out] NameOut - The return value.
776 void GetNameForMethod(const ObjCMethodDecl *OMD,
777 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000778 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000780 /// GetMethodVarName - Return a unique constant for the given
781 /// selector's name. The return value has type char *.
782 llvm::Constant *GetMethodVarName(Selector Sel);
783 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000784
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000785 /// GetMethodVarType - Return a unique constant for the given
786 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000788 // FIXME: This is a horrible name.
789 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000790 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000792 /// GetPropertyName - Return a unique constant for the given
793 /// name. The return value has type char *.
794 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000796 // FIXME: This can be dropped once string functions are unified.
797 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
798 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000800 /// GetClassName - Return a unique constant for the given selector's
801 /// name. The return value has type char *.
802 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000803
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000804 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
805
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000806 /// BuildIvarLayout - Builds ivar layout bitmap for the class
807 /// implementation for the __strong or __weak case.
808 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000809 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
810 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000811
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000812 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000813
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000814 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815 unsigned int BytePos, bool ForStrongLayout,
816 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000817 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000818 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000819 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000820 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000821 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000822 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000823
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000824 /// GetIvarLayoutName - Returns a unique constant for the given
825 /// ivar layout bitmap.
826 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
827 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000829 /// EmitPropertyList - Emit the given property list. The return
830 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000831 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000833 const ObjCContainerDecl *OCD,
834 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000835
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000836 /// PushProtocolProperties - Push protocol's property on the input stack.
837 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
838 std::vector<llvm::Constant*> &Properties,
839 const Decl *Container,
840 const ObjCProtocolDecl *PROTO,
841 const ObjCCommonTypesHelper &ObjCTypes);
842
Fariborz Jahanianda320092009-01-29 19:24:30 +0000843 /// GetProtocolRef - Return a reference to the internal protocol
844 /// description, creating an empty one if it has not been
845 /// defined. The return value has type ProtocolPtrTy.
846 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000847
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000848 /// CreateMetadataVar - Create a global variable with internal
849 /// linkage for use by the Objective-C runtime.
850 ///
851 /// This is a convenience wrapper which not only creates the
852 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000853 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000854 ///
855 /// \param Name - The variable name.
856 /// \param Init - The variable initializer; this is also used to
857 /// define the type of the variable.
858 /// \param Section - The section the variable should go into, or 0.
859 /// \param Align - The alignment for the variable, or 0.
860 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000861 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000862 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000863 llvm::Constant *Init,
864 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000865 unsigned Align,
866 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000867
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000868 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000869 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000870 QualType ResultType,
871 llvm::Value *Sel,
872 llvm::Value *Arg0,
873 QualType Arg0Ty,
874 bool IsSuper,
875 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000876 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000877 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000878
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000879 /// EmitImageInfo - Emit the image info marker used to encode some module
880 /// level information.
881 void EmitImageInfo();
882
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000883public:
Owen Anderson69243822009-07-13 04:10:07 +0000884 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000885 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
David Chisnall0d13f6f2010-01-23 02:40:42 +0000887 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000888
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000889 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
890 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000891
Fariborz Jahanianda320092009-01-29 19:24:30 +0000892 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Fariborz Jahanianda320092009-01-29 19:24:30 +0000894 /// GetOrEmitProtocol - Get the protocol object for the given
895 /// declaration, emitting it if necessary. The return value has type
896 /// ProtocolPtrTy.
897 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanianda320092009-01-29 19:24:30 +0000899 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
900 /// object for the given declaration, emitting it if needed. These
901 /// forward references will be filled in with empty bodies if no
902 /// definition is seen. The return value has type ProtocolPtrTy.
903 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000904 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
905 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000906
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000907};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000909class CGObjCMac : public CGObjCCommonMac {
910private:
911 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000912
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000913 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000915 void EmitModuleInfo();
916
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000917 /// EmitModuleSymols - Emit module symbols, the list of defined
918 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000919 llvm::Constant *EmitModuleSymbols();
920
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000921 /// FinishModule - Write out global data structures at the end of
922 /// processing a translation unit.
923 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000924
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000925 /// EmitClassExtension - Generate the class extension structure used
926 /// to store the weak ivar layout and properties. The return value
927 /// has type ClassExtensionPtrTy.
928 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
929
930 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
931 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000932 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000933 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000934
935 /// EmitSuperClassRef - Emits reference to class's main metadata class.
936 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000937
938 /// EmitIvarList - Emit the ivar list for the given
939 /// implementation. If ForClass is true the list of class ivars
940 /// (i.e. metaclass ivars) is emitted, otherwise the list of
941 /// interface ivars will be emitted. The return value has type
942 /// IvarListPtrTy.
943 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000944 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000945
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000946 /// EmitMetaClass - Emit a forward reference to the class structure
947 /// for the metaclass of the given interface. The return value has
948 /// type ClassPtrTy.
949 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
950
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000951 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000952 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000953 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
954 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000955 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000956
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000957 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000958
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000960
961 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000962 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000963 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000964 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000965 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000966
967 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000968 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969 /// - TypeName: The name for the type containing the methods.
970 /// - IsProtocol: True iff these methods are for a protocol.
971 /// - ClassMethds: True iff these are class methods.
972 /// - Required: When true, only "required" methods are
973 /// listed. Similarly, when false only "optional" methods are
974 /// listed. For classes this should always be true.
975 /// - begin, end: The method list to output.
976 ///
977 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000978 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000979 const char *Section,
980 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000981
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000982 /// GetOrEmitProtocol - Get the protocol object for the given
983 /// declaration, emitting it if necessary. The return value has type
984 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000985 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000986
987 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
988 /// object for the given declaration, emitting it if needed. These
989 /// forward references will be filled in with empty bodies if no
990 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000991 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000992
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993 /// EmitProtocolExtension - Generate the protocol extension
994 /// structure used to store optional instance and class methods, and
995 /// protocol properties. The return value has type
996 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000997 llvm::Constant *
998 EmitProtocolExtension(const ObjCProtocolDecl *PD,
999 const ConstantVector &OptInstanceMethods,
1000 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001
1002 /// EmitProtocolList - Generate the list of referenced
1003 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001004 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001005 ObjCProtocolDecl::protocol_iterator begin,
1006 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001007
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001008 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1009 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001010 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1011 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001012
1013public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001014 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001015
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001016 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001017
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001018 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001019 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001020 QualType ResultType,
1021 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001022 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001023 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001024 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001025 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001026
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001028 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001029 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001030 QualType ResultType,
1031 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001032 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001033 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001034 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001035 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001036 const CallArgList &CallArgs,
1037 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001038
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001039 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001040 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001041
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001042 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1043 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001044
1045 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1046 /// untyped one.
1047 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1048 const ObjCMethodDecl *Method);
1049
John McCall5a180392010-07-24 00:37:23 +00001050 virtual llvm::Constant *GetEHType(QualType T);
1051
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001052 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001053
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001054 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001055
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001056 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001057 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001058
Chris Lattner74391b42009-03-22 21:03:39 +00001059 virtual llvm::Constant *GetPropertyGetFunction();
1060 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001061 virtual llvm::Constant *GetGetStructFunction();
1062 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001063 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001064
John McCallf1549f62010-07-06 01:34:17 +00001065 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1066 const ObjCAtTryStmt &S);
1067 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1068 const ObjCAtSynchronizedStmt &S);
1069 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001070 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1071 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001072 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001073 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001074 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001075 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001076 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001077 llvm::Value *src, llvm::Value *dest,
1078 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001079 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001080 llvm::Value *src, llvm::Value *dest,
1081 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001082 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1083 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001084 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1085 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001086 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001087
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001088 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1089 QualType ObjectTy,
1090 llvm::Value *BaseValue,
1091 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001092 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001093 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001094 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001095 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001096};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001097
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001098class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001099private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001100 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001101 llvm::GlobalVariable* ObjCEmptyCacheVar;
1102 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103
Daniel Dunbar11394522009-04-18 08:51:00 +00001104 /// SuperClassReferences - uniqued super class references.
1105 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001106
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001107 /// MetaClassReferences - uniqued meta class references.
1108 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001109
1110 /// EHTypeReferences - uniqued class ehtype references.
1111 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001112
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001113 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001114 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001115 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001117 /// DefinedMetaClasses - List of defined meta-classes.
1118 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1119
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001120 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001121 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001122 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001123
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001124 /// FinishNonFragileABIModule - Write out global data structures at the end of
1125 /// processing a translation unit.
1126 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001127
Daniel Dunbar463b8762009-05-15 21:48:48 +00001128 /// AddModuleClassList - Add the given list of class pointers to the
1129 /// module with the provided symbol and section names.
1130 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1131 const char *SymbolName,
1132 const char *SectionName);
1133
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001134 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1135 unsigned InstanceStart,
1136 unsigned InstanceSize,
1137 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001138 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001139 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001140 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001141 llvm::Constant *ClassRoGV,
1142 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001143
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001144 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001145
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001146 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001147
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001148 /// EmitMethodList - Emit the method list for the given
1149 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001150 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001151 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001152 const ConstantVector &Methods);
1153 /// EmitIvarList - Emit the ivar list for the given
1154 /// implementation. If ForClass is true the list of class ivars
1155 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1156 /// interface ivars will be emitted. The return value has type
1157 /// IvarListnfABIPtrTy.
1158 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001159
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001160 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001161 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001162 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001163
Fariborz Jahanianda320092009-01-29 19:24:30 +00001164 /// GetOrEmitProtocol - Get the protocol object for the given
1165 /// declaration, emitting it if necessary. The return value has type
1166 /// ProtocolPtrTy.
1167 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001168
Fariborz Jahanianda320092009-01-29 19:24:30 +00001169 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1170 /// object for the given declaration, emitting it if needed. These
1171 /// forward references will be filled in with empty bodies if no
1172 /// definition is seen. The return value has type ProtocolPtrTy.
1173 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174
Fariborz Jahanianda320092009-01-29 19:24:30 +00001175 /// EmitProtocolList - Generate the list of referenced
1176 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001177 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001178 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001179 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001180
Fariborz Jahanian46551122009-02-04 00:22:57 +00001181 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001182 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001183 QualType ResultType,
1184 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001185 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001186 QualType Arg0Ty,
1187 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001188 const CallArgList &CallArgs,
1189 const ObjCMethodDecl *Method);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001190
1191 /// GetClassGlobal - Return the global variable for the Objective-C
1192 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001193 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001194
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001195 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001196 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001198 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001199
Daniel Dunbar11394522009-04-18 08:51:00 +00001200 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1201 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1203 const ObjCInterfaceDecl *ID);
1204
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001205 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1206 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001207 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001208 const ObjCInterfaceDecl *ID);
1209
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001210 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1211 /// the given ivar.
1212 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001213 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214 const ObjCInterfaceDecl *ID,
1215 const ObjCIvarDecl *Ivar);
1216
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001217 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1218 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001219 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1220 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001221
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001222 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001223 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001224 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001225 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001226
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001227 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001228 return "OBJC_METACLASS_$_";
1229 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001230
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001231 const char *getClassSymbolPrefix() const {
1232 return "OBJC_CLASS_$_";
1233 }
1234
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001235 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001236 uint32_t &InstanceStart,
1237 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001239 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001240 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001241 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1242 return CGM.getContext().Selectors.getSelector(0, &II);
1243 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001245 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001246 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1247 return CGM.getContext().Selectors.getSelector(1, &II);
1248 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001249
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001250 /// ImplementationIsNonLazy - Check whether the given category or
1251 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001252 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001253
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001254public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001255 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001256 // FIXME. All stubs for now!
1257 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001258
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001259 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001260 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001261 QualType ResultType,
1262 Selector Sel,
1263 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001264 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001265 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001266 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001267
1268 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001269 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001270 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001271 QualType ResultType,
1272 Selector Sel,
1273 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001274 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001275 llvm::Value *Receiver,
1276 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001277 const CallArgList &CallArgs,
1278 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001280 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001281 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001282
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001283 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1284 bool lvalue = false)
1285 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001286
1287 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1288 /// untyped one.
1289 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1290 const ObjCMethodDecl *Method)
1291 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001293 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001295 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001296 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001297 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001298
John McCall5a180392010-07-24 00:37:23 +00001299 virtual llvm::Constant *GetEHType(QualType T);
1300
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001302 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001303 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001304 virtual llvm::Constant *GetPropertySetFunction() {
1305 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001306 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001307
David Chisnall8fac25d2010-12-26 22:13:16 +00001308 virtual llvm::Constant *GetSetStructFunction() {
1309 return ObjCTypes.getCopyStructFn();
1310 }
1311 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001312 return ObjCTypes.getCopyStructFn();
1313 }
1314
Chris Lattner74391b42009-03-22 21:03:39 +00001315 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001316 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001317 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001318
John McCallf1549f62010-07-06 01:34:17 +00001319 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1320 const ObjCAtTryStmt &S);
1321 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1322 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001323 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001324 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001325 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001326 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001327 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001328 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001329 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001330 llvm::Value *src, llvm::Value *dest,
1331 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001333 llvm::Value *src, llvm::Value *dest,
1334 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001335 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001336 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001337 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1338 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001339 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001340 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1341 QualType ObjectTy,
1342 llvm::Value *BaseValue,
1343 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001344 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001345 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001346 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001347 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001348};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001350} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001351
1352/* *** Helper Functions *** */
1353
1354/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001355static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001356 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001357 unsigned idx0,
1358 unsigned idx1) {
1359 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001360 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1361 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001362 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001363 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001364}
1365
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001366/// hasObjCExceptionAttribute - Return true if this class or any super
1367/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001368static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001369 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001370 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001371 return true;
1372 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001373 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001374 return false;
1375}
1376
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001377/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001379CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001380 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001381 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001382 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001383}
1384
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001385/// GetClass - Return a reference to the class for the given interface
1386/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001387llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001388 const ObjCInterfaceDecl *ID) {
1389 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001390}
1391
1392/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001393llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1394 bool lval) {
1395 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001396}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001397llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001398 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001399 return EmitSelector(Builder, Method->getSelector());
1400}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001401
John McCall5a180392010-07-24 00:37:23 +00001402llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1403 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1404 return 0;
1405}
1406
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001407/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408/*
1409 struct __builtin_CFString {
1410 const int *isa; // point to __CFConstantStringClassReference
1411 int flags;
1412 const char *str;
1413 long length;
1414 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001415*/
1416
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001417/// or Generate a constant NSString object.
1418/*
1419 struct __builtin_NSString {
1420 const int *isa; // point to __NSConstantStringClassReference
1421 const char *str;
1422 unsigned int length;
1423 };
1424*/
1425
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001427 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001428 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1429 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001430 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001431}
1432
1433/// Generates a message send where the super is the receiver. This is
1434/// a message send to self with special delivery semantics indicating
1435/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001436CodeGen::RValue
1437CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001438 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001439 QualType ResultType,
1440 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001441 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001442 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001443 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001444 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001445 const CodeGen::CallArgList &CallArgs,
1446 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001447 // Create and init a super structure; this is a (receiver, class)
1448 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001450 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001451 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001452 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001453 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001454 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001455
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001456 // If this is a class message the metaclass is passed as the target.
1457 llvm::Value *Target;
1458 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001459 if (isCategoryImpl) {
1460 // Message sent to 'super' in a class method defined in a category
1461 // implementation requires an odd treatment.
1462 // If we are in a class method, we must retrieve the
1463 // _metaclass_ for the current class, pointed at by
1464 // the class's "isa" pointer. The following assumes that
1465 // isa" is the first ivar in a class (which it must be).
1466 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1467 Target = CGF.Builder.CreateStructGEP(Target, 0);
1468 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001469 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001470 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1471 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1472 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1473 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001474 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001475 }
1476 else if (isCategoryImpl)
1477 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1478 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001479 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1480 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1481 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001482 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001483 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1484 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001485 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001486 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001487 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001488 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001489 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001490 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001491 EmitSelector(CGF.Builder, Sel),
1492 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001493 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001495
1496/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001497CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001498 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001499 QualType ResultType,
1500 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001501 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001502 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001503 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001504 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001505 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001506 EmitSelector(CGF.Builder, Sel),
1507 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001508 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001509}
1510
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001511CodeGen::RValue
1512CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001513 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001514 QualType ResultType,
1515 llvm::Value *Sel,
1516 llvm::Value *Arg0,
1517 QualType Arg0Ty,
1518 bool IsSuper,
1519 const CallArgList &CallArgs,
1520 const ObjCMethodDecl *Method,
1521 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001522 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001523 if (!IsSuper)
1524 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
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());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002072 // method definition entries must be clear for next implementation.
2073 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002074}
2075
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002076// FIXME: Get from somewhere?
2077enum ClassFlags {
2078 eClassFlags_Factory = 0x00001,
2079 eClassFlags_Meta = 0x00002,
2080 // <rdr://5142207>
2081 eClassFlags_HasCXXStructors = 0x02000,
2082 eClassFlags_Hidden = 0x20000,
2083 eClassFlags_ABI2_Hidden = 0x00010,
2084 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2085};
2086
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002087/*
2088 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002089 Class isa;
2090 Class super_class;
2091 const char *name;
2092 long version;
2093 long info;
2094 long instance_size;
2095 struct _objc_ivar_list *ivars;
2096 struct _objc_method_list *methods;
2097 struct _objc_cache *cache;
2098 struct _objc_protocol_list *protocols;
2099 // Objective-C 1.0 extensions (<rdr://4585769>)
2100 const char *ivar_layout;
2101 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002102 };
2103
2104 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002105*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002106void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002107 DefinedSymbols.insert(ID->getIdentifier());
2108
Chris Lattner8ec03f52008-11-24 03:54:41 +00002109 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002110 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002112 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002113 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002114 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002115 Interface->all_referenced_protocol_begin(),
2116 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002117 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002118 if (ID->getNumIvarInitializers())
2119 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002121 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002122
2123 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002124 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002125 Flags |= eClassFlags_Hidden;
2126
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002127 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002128 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002129 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002130 // Instance methods should always be defined.
2131 InstanceMethods.push_back(GetMethodConstant(*i));
2132 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002133 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002134 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002135 // Class methods should always be defined.
2136 ClassMethods.push_back(GetMethodConstant(*i));
2137 }
2138
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002139 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002140 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002141 ObjCPropertyImplDecl *PID = *i;
2142
2143 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2144 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2145
2146 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2147 if (llvm::Constant *C = GetMethodConstant(MD))
2148 InstanceMethods.push_back(C);
2149 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2150 if (llvm::Constant *C = GetMethodConstant(MD))
2151 InstanceMethods.push_back(C);
2152 }
2153 }
2154
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002156 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002157 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002158 // Record a reference to the super class.
2159 LazySymbols.insert(Super->getIdentifier());
2160
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002162 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002163 ObjCTypes.ClassPtrTy);
2164 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002165 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 }
2167 Values[ 2] = GetClassName(ID->getIdentifier());
2168 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002169 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2170 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2171 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002172 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002173 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002174 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002175 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002176 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002178 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002180 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002182 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002183 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002184 std::string Name("\01L_OBJC_CLASS_");
2185 Name += ClassName;
2186 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2187 // Check for a forward reference.
2188 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2189 if (GV) {
2190 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2191 "Forward metaclass reference has incorrect type.");
2192 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2193 GV->setInitializer(Init);
2194 GV->setSection(Section);
2195 GV->setAlignment(4);
2196 CGM.AddUsedGlobal(GV);
2197 }
2198 else
2199 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002200 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002201 // method definition entries must be clear for next implementation.
2202 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203}
2204
2205llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2206 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002207 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002209 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210
John McCall1fb0caa2010-10-22 21:05:15 +00002211 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002212 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002213
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 std::vector<llvm::Constant*> Values(12);
2215 // The isa for the metaclass is the root of the hierarchy.
2216 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2217 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2218 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002219 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002220 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002222 // The super class for the metaclass is emitted as the name of the
2223 // super class. The runtime fixes this up to point to the
2224 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002226 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002227 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002228 ObjCTypes.ClassPtrTy);
2229 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002230 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231 }
2232 Values[ 2] = GetClassName(ID->getIdentifier());
2233 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002234 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2235 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2236 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002237 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002238 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002239 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002240 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002241 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002242 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002243 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244 Values[ 9] = Protocols;
2245 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002246 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002248 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002249 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250 Values);
2251
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002252 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002253 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002254
2255 // Check for a forward reference.
2256 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2257 if (GV) {
2258 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2259 "Forward metaclass reference has incorrect type.");
2260 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2261 GV->setInitializer(Init);
2262 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002263 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002264 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002265 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002266 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002268 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002269 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270
2271 return GV;
2272}
2273
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002274llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002275 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002276
Mike Stumpf5408fe2009-05-16 07:57:57 +00002277 // FIXME: Should we look these up somewhere other than the module. Its a bit
2278 // silly since we only generate these while processing an implementation, so
2279 // exactly one pointer would work if know when we entered/exitted an
2280 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002281
2282 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002283 // Previously, metaclass with internal linkage may have been defined.
2284 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002285 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2286 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002287 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2288 "Forward metaclass reference has incorrect type.");
2289 return GV;
2290 } else {
2291 // Generate as an external reference to keep a consistent
2292 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002293 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002294 llvm::GlobalValue::ExternalLinkage,
2295 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002296 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002297 }
2298}
2299
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002300llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2301 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2302
2303 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2304 true)) {
2305 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2306 "Forward class metadata reference has incorrect type.");
2307 return GV;
2308 } else {
2309 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2310 llvm::GlobalValue::ExternalLinkage,
2311 0,
2312 Name);
2313 }
2314}
2315
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002316/*
2317 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002318 uint32_t size;
2319 const char *weak_ivar_layout;
2320 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002321 };
2322*/
2323llvm::Constant *
2324CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002325 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002326 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327
2328 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002329 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002330 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002331 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002332 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333
2334 // Return null if no extension bits are used.
2335 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002336 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002337
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002338 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002339 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002340 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002342 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343}
2344
2345/*
2346 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002347 char *ivar_name;
2348 char *ivar_type;
2349 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002350 };
2351
2352 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002353 int ivar_count;
2354 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002356*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002358 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002359 std::vector<llvm::Constant*> Ivars, Ivar(3);
2360
2361 // When emitting the root class GCC emits ivar entries for the
2362 // actual class structure. It is not clear if we need to follow this
2363 // behavior; for now lets try and get away with not doing it. If so,
2364 // the cleanest solution would be to make up an ObjCInterfaceDecl
2365 // for the class.
2366 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002367 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002368
2369 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002370 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002371
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002372 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002373 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002374
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002375 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2376 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002377 // Ignore unnamed bit-fields.
2378 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002379 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002380 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2381 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002383 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002384 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002385 }
2386
2387 // Return null for empty list.
2388 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002389 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002390
2391 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002392 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002393 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002394 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002395 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002396 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002397
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002398 llvm::GlobalVariable *GV;
2399 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002400 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002401 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002402 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002403 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002404 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002405 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002406 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002407 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408}
2409
2410/*
2411 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412 SEL method_name;
2413 char *method_types;
2414 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002415 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002416
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002418 struct objc_method_list *obsolete;
2419 int count;
2420 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 };
2422*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002423
2424/// GetMethodConstant - Return a struct objc_method constant for the
2425/// given method if it has been defined. The result is null if the
2426/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002427llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002428 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002429 if (!Fn)
2430 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002431
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002432 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002433 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002434 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002435 ObjCTypes.SelectorPtrTy);
2436 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002437 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002438 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002439}
2440
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002441llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002442 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002443 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 // Return null for empty list.
2445 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002446 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002447
2448 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002449 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002450 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002451 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002453 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002454 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002455
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002456 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002457 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002458 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002459}
2460
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002461llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002462 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002463 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002464 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002465
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002466 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002467 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002468 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002469 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002470 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002471 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002472 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002473 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002474 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002475
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002476 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002477}
2478
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002479llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002480CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002481 llvm::Constant *Init,
2482 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002483 unsigned Align,
2484 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002485 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002486 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002487 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002488 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002489 if (Section)
2490 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002491 if (Align)
2492 GV->setAlignment(Align);
2493 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002494 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002495 return GV;
2496}
2497
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002498llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002499 // Abuse this interface function as a place to finalize.
2500 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002501 return NULL;
2502}
2503
Chris Lattner74391b42009-03-22 21:03:39 +00002504llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002505 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002506}
2507
Chris Lattner74391b42009-03-22 21:03:39 +00002508llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002509 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002510}
2511
David Chisnall8fac25d2010-12-26 22:13:16 +00002512llvm::Constant *CGObjCMac::GetGetStructFunction() {
2513 return ObjCTypes.getCopyStructFn();
2514}
2515llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002516 return ObjCTypes.getCopyStructFn();
2517}
2518
Chris Lattner74391b42009-03-22 21:03:39 +00002519llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002520 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002521}
2522
John McCallf1549f62010-07-06 01:34:17 +00002523void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2524 return EmitTryOrSynchronizedStmt(CGF, S);
2525}
2526
2527void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2528 const ObjCAtSynchronizedStmt &S) {
2529 return EmitTryOrSynchronizedStmt(CGF, S);
2530}
2531
John McCallcc505292010-07-21 06:59:36 +00002532namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002533 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002534 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002535 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002536 llvm::Value *CallTryExitVar;
2537 llvm::Value *ExceptionData;
2538 ObjCTypesHelper &ObjCTypes;
2539 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002540 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002541 llvm::Value *CallTryExitVar,
2542 llvm::Value *ExceptionData,
2543 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002544 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002545 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2546
2547 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2548 // Check whether we need to call objc_exception_try_exit.
2549 // In optimized code, this branch will always be folded.
2550 llvm::BasicBlock *FinallyCallExit =
2551 CGF.createBasicBlock("finally.call_exit");
2552 llvm::BasicBlock *FinallyNoCallExit =
2553 CGF.createBasicBlock("finally.no_call_exit");
2554 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2555 FinallyCallExit, FinallyNoCallExit);
2556
2557 CGF.EmitBlock(FinallyCallExit);
2558 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2559 ->setDoesNotThrow();
2560
2561 CGF.EmitBlock(FinallyNoCallExit);
2562
2563 if (isa<ObjCAtTryStmt>(S)) {
2564 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002565 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2566 // Save the current cleanup destination in case there's
2567 // control flow inside the finally statement.
2568 llvm::Value *CurCleanupDest =
2569 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2570
John McCallcc505292010-07-21 06:59:36 +00002571 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2572
John McCalld96a8e72010-08-11 00:16:14 +00002573 if (CGF.HaveInsertPoint()) {
2574 CGF.Builder.CreateStore(CurCleanupDest,
2575 CGF.getNormalCleanupDestSlot());
2576 } else {
2577 // Currently, the end of the cleanup must always exist.
2578 CGF.EnsureInsertPoint();
2579 }
2580 }
John McCallcc505292010-07-21 06:59:36 +00002581 } else {
2582 // Emit objc_sync_exit(expr); as finally's sole statement for
2583 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002584 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002585 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2586 ->setDoesNotThrow();
2587 }
2588 }
2589 };
John McCall87bb5822010-07-31 23:20:56 +00002590
2591 class FragileHazards {
2592 CodeGenFunction &CGF;
2593 llvm::SmallVector<llvm::Value*, 20> Locals;
2594 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2595
2596 llvm::InlineAsm *ReadHazard;
2597 llvm::InlineAsm *WriteHazard;
2598
2599 llvm::FunctionType *GetAsmFnType();
2600
2601 void collectLocals();
2602 void emitReadHazard(CGBuilderTy &Builder);
2603
2604 public:
2605 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002606
John McCall87bb5822010-07-31 23:20:56 +00002607 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002608 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002609 };
2610}
2611
2612/// Create the fragile-ABI read and write hazards based on the current
2613/// state of the function, which is presumed to be immediately prior
2614/// to a @try block. These hazards are used to maintain correct
2615/// semantics in the face of optimization and the fragile ABI's
2616/// cavalier use of setjmp/longjmp.
2617FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2618 collectLocals();
2619
2620 if (Locals.empty()) return;
2621
2622 // Collect all the blocks in the function.
2623 for (llvm::Function::iterator
2624 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2625 BlocksBeforeTry.insert(&*I);
2626
2627 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2628
2629 // Create a read hazard for the allocas. This inhibits dead-store
2630 // optimizations and forces the values to memory. This hazard is
2631 // inserted before any 'throwing' calls in the protected scope to
2632 // reflect the possibility that the variables might be read from the
2633 // catch block if the call throws.
2634 {
2635 std::string Constraint;
2636 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2637 if (I) Constraint += ',';
2638 Constraint += "*m";
2639 }
2640
2641 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2642 }
2643
2644 // Create a write hazard for the allocas. This inhibits folding
2645 // loads across the hazard. This hazard is inserted at the
2646 // beginning of the catch path to reflect the possibility that the
2647 // variables might have been written within the protected scope.
2648 {
2649 std::string Constraint;
2650 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2651 if (I) Constraint += ',';
2652 Constraint += "=*m";
2653 }
2654
2655 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2656 }
2657}
2658
2659/// Emit a write hazard at the current location.
2660void FragileHazards::emitWriteHazard() {
2661 if (Locals.empty()) return;
2662
2663 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2664 ->setDoesNotThrow();
2665}
2666
John McCall87bb5822010-07-31 23:20:56 +00002667void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2668 assert(!Locals.empty());
2669 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2670 ->setDoesNotThrow();
2671}
2672
2673/// Emit read hazards in all the protected blocks, i.e. all the blocks
2674/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002675void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002676 if (Locals.empty()) return;
2677
2678 CGBuilderTy Builder(CGF.getLLVMContext());
2679
2680 // Iterate through all blocks, skipping those prior to the try.
2681 for (llvm::Function::iterator
2682 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2683 llvm::BasicBlock &BB = *FI;
2684 if (BlocksBeforeTry.count(&BB)) continue;
2685
2686 // Walk through all the calls in the block.
2687 for (llvm::BasicBlock::iterator
2688 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2689 llvm::Instruction &I = *BI;
2690
2691 // Ignore instructions that aren't non-intrinsic calls.
2692 // These are the only calls that can possibly call longjmp.
2693 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2694 if (isa<llvm::IntrinsicInst>(I))
2695 continue;
2696
2697 // Ignore call sites marked nounwind. This may be questionable,
2698 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2699 llvm::CallSite CS(&I);
2700 if (CS.doesNotThrow()) continue;
2701
John McCall0b251722010-08-04 05:59:32 +00002702 // Insert a read hazard before the call. This will ensure that
2703 // any writes to the locals are performed before making the
2704 // call. If the call throws, then this is sufficient to
2705 // guarantee correctness as long as it doesn't also write to any
2706 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002707 Builder.SetInsertPoint(&BB, BI);
2708 emitReadHazard(Builder);
2709 }
2710 }
2711}
2712
2713static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2714 if (V) S.insert(V);
2715}
2716
2717void FragileHazards::collectLocals() {
2718 // Compute a set of allocas to ignore.
2719 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2720 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2721 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2722 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2723
2724 // Collect all the allocas currently in the function. This is
2725 // probably way too aggressive.
2726 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2727 for (llvm::BasicBlock::iterator
2728 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2729 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2730 Locals.push_back(&*I);
2731}
2732
2733llvm::FunctionType *FragileHazards::GetAsmFnType() {
2734 std::vector<const llvm::Type *> Tys(Locals.size());
2735 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2736 Tys[I] = Locals[I]->getType();
2737 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002738}
2739
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002740/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002741
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002742 Objective-C setjmp-longjmp (sjlj) Exception Handling
2743 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002744
John McCallf1549f62010-07-06 01:34:17 +00002745 A catch buffer is a setjmp buffer plus:
2746 - a pointer to the exception that was caught
2747 - a pointer to the previous exception data buffer
2748 - two pointers of reserved storage
2749 Therefore catch buffers form a stack, with a pointer to the top
2750 of the stack kept in thread-local storage.
2751
2752 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2753 objc_exception_try_exit pops the given catch buffer, which is
2754 required to be the top of the EH stack.
2755 objc_exception_throw pops the top of the EH stack, writes the
2756 thrown exception into the appropriate field, and longjmps
2757 to the setjmp buffer. It crashes the process (with a printf
2758 and an abort()) if there are no catch buffers on the stack.
2759 objc_exception_extract just reads the exception pointer out of the
2760 catch buffer.
2761
2762 There's no reason an implementation couldn't use a light-weight
2763 setjmp here --- something like __builtin_setjmp, but API-compatible
2764 with the heavyweight setjmp. This will be more important if we ever
2765 want to implement correct ObjC/C++ exception interactions for the
2766 fragile ABI.
2767
2768 Note that for this use of setjmp/longjmp to be correct, we may need
2769 to mark some local variables volatile: if a non-volatile local
2770 variable is modified between the setjmp and the longjmp, it has
2771 indeterminate value. For the purposes of LLVM IR, it may be
2772 sufficient to make loads and stores within the @try (to variables
2773 declared outside the @try) volatile. This is necessary for
2774 optimized correctness, but is not currently being done; this is
2775 being tracked as rdar://problem/8160285
2776
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002777 The basic framework for a @try-catch-finally is as follows:
2778 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002779 objc_exception_data d;
2780 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002781 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002782
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002783 objc_exception_try_enter(&d);
2784 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002785 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002786 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002787 // exception path
2788 id _caught = objc_exception_extract(&d);
2789
2790 // enter new try scope for handlers
2791 if (!setjmp(d.jmp_buf)) {
2792 ... match exception and execute catch blocks ...
2793
2794 // fell off end, rethrow.
2795 _rethrow = _caught;
2796 ... jump-through-finally to finally_rethrow ...
2797 } else {
2798 // exception in catch block
2799 _rethrow = objc_exception_extract(&d);
2800 _call_try_exit = false;
2801 ... jump-through-finally to finally_rethrow ...
2802 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002803 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002804 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002805
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002806 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002807 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002808 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002809
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002810 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002811 ... dispatch to finally destination ...
2812
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002813 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002814 objc_exception_throw(_rethrow);
2815
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002816 finally_end:
2817 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002818
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002819 This framework differs slightly from the one gcc uses, in that gcc
2820 uses _rethrow to determine if objc_exception_try_exit should be called
2821 and if the object should be rethrown. This breaks in the face of
2822 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002823
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002824 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002825
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002826 - If there are no catch blocks, then we avoid emitting the second
2827 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002828
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002829 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2830 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002831
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002832 - FIXME: If there is no @finally block we can do a few more
2833 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002834
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002835 Rethrows and Jumps-Through-Finally
2836 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002837
John McCallf1549f62010-07-06 01:34:17 +00002838 '@throw;' is supported by pushing the currently-caught exception
2839 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002840
John McCallf1549f62010-07-06 01:34:17 +00002841 Branches through the @finally block are handled with an ordinary
2842 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2843 exceptions are not compatible with C++ exceptions, and this is
2844 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002845
John McCallf1549f62010-07-06 01:34:17 +00002846 @synchronized(expr) { stmt; } is emitted as if it were:
2847 id synch_value = expr;
2848 objc_sync_enter(synch_value);
2849 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002850*/
2851
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002852void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2853 const Stmt &S) {
2854 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002855
2856 // A destination for the fall-through edges of the catch handlers to
2857 // jump to.
2858 CodeGenFunction::JumpDest FinallyEnd =
2859 CGF.getJumpDestInCurrentScope("finally.end");
2860
2861 // A destination for the rethrow edge of the catch handlers to jump
2862 // to.
2863 CodeGenFunction::JumpDest FinallyRethrow =
2864 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002865
Daniel Dunbar1c566672009-02-24 01:43:46 +00002866 // For @synchronized, call objc_sync_enter(sync.expr). The
2867 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002868 // @synchronized. We can't avoid a temp here because we need the
2869 // value to be preserved. If the backend ever does liveness
2870 // correctly after setjmp, this will be unnecessary.
2871 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002872 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002873 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002874 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2875 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002876 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2877 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002878
2879 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2880 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002881 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002882
John McCall0b251722010-08-04 05:59:32 +00002883 // Allocate memory for the setjmp buffer. This needs to be kept
2884 // live throughout the try and catch blocks.
2885 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2886 "exceptiondata.ptr");
2887
John McCall87bb5822010-07-31 23:20:56 +00002888 // Create the fragile hazards. Note that this will not capture any
2889 // of the allocas required for exception processing, but will
2890 // capture the current basic block (which extends all the way to the
2891 // setjmp call) as "before the @try".
2892 FragileHazards Hazards(CGF);
2893
John McCallf1549f62010-07-06 01:34:17 +00002894 // Create a flag indicating whether the cleanup needs to call
2895 // objc_exception_try_exit. This is true except when
2896 // - no catches match and we're branching through the cleanup
2897 // just to rethrow the exception, or
2898 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002899 // The setjmp-safety rule here is that we should always store to this
2900 // variable in a place that dominates the branch through the cleanup
2901 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002902 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002903 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002904
John McCall9e2213d2010-10-04 23:42:51 +00002905 // A slot containing the exception to rethrow. Only needed when we
2906 // have both a @catch and a @finally.
2907 llvm::Value *PropagatingExnVar = 0;
2908
John McCallf1549f62010-07-06 01:34:17 +00002909 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002910 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002911 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002912 CallTryExitVar,
2913 ExceptionData,
2914 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002915
2916 // Enter a try block:
2917 // - Call objc_exception_try_enter to push ExceptionData on top of
2918 // the EH stack.
2919 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2920 ->setDoesNotThrow();
2921
2922 // - Call setjmp on the exception data buffer.
2923 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2924 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2925 llvm::Value *SetJmpBuffer =
2926 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2927 llvm::CallInst *SetJmpResult =
2928 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2929 SetJmpResult->setDoesNotThrow();
2930
2931 // If setjmp returned 0, enter the protected block; otherwise,
2932 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002933 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2934 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002935 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002936 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2937 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002938
John McCallf1549f62010-07-06 01:34:17 +00002939 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002940 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002941 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002942 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002943 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002944
2945 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002946
John McCallf1549f62010-07-06 01:34:17 +00002947 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002948 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002949
John McCall87bb5822010-07-31 23:20:56 +00002950 // Don't optimize loads of the in-scope locals across this point.
2951 Hazards.emitWriteHazard();
2952
John McCallf1549f62010-07-06 01:34:17 +00002953 // For a @synchronized (or a @try with no catches), just branch
2954 // through the cleanup to the rethrow block.
2955 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2956 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002957 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002958 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002959
2960 // Otherwise, we have to match against the caught exceptions.
2961 } else {
John McCall0b251722010-08-04 05:59:32 +00002962 // Retrieve the exception object. We may emit multiple blocks but
2963 // nothing can cross this so the value is already in SSA form.
2964 llvm::CallInst *Caught =
2965 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2966 ExceptionData, "caught");
2967 Caught->setDoesNotThrow();
2968
John McCallf1549f62010-07-06 01:34:17 +00002969 // Push the exception to rethrow onto the EH value stack for the
2970 // benefit of any @throws in the handlers.
2971 CGF.ObjCEHValueStack.push_back(Caught);
2972
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002973 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002974
John McCall0b251722010-08-04 05:59:32 +00002975 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00002976
John McCall0b251722010-08-04 05:59:32 +00002977 llvm::BasicBlock *CatchBlock = 0;
2978 llvm::BasicBlock *CatchHandler = 0;
2979 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00002980 // Save the currently-propagating exception before
2981 // objc_exception_try_enter clears the exception slot.
2982 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2983 "propagating_exception");
2984 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2985
John McCall0b251722010-08-04 05:59:32 +00002986 // Enter a new exception try block (in case a @catch block
2987 // throws an exception).
2988 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2989 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002990
John McCall0b251722010-08-04 05:59:32 +00002991 llvm::CallInst *SetJmpResult =
2992 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2993 "setjmp.result");
2994 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002995
John McCall0b251722010-08-04 05:59:32 +00002996 llvm::Value *Threw =
2997 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2998
2999 CatchBlock = CGF.createBasicBlock("catch");
3000 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3001 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3002
3003 CGF.EmitBlock(CatchBlock);
3004 }
3005
3006 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003007
Daniel Dunbar55e40722008-09-27 07:03:52 +00003008 // Handle catch list. As a special case we check if everything is
3009 // matched and avoid generating code for falling off the end if
3010 // so.
3011 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003012 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3013 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003014
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003015 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003016 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003017
Anders Carlsson80f25672008-09-09 17:59:25 +00003018 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003019 if (!CatchParam) {
3020 AllMatched = true;
3021 } else {
John McCall183700f2009-09-21 23:43:11 +00003022 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003023
John McCallf1549f62010-07-06 01:34:17 +00003024 // catch(id e) always matches under this ABI, since only
3025 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003026 // FIXME: For the time being we also match id<X>; this should
3027 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003028 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003029 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003030 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003031
John McCallf1549f62010-07-06 01:34:17 +00003032 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003033 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003034 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3035
Anders Carlssondde0a942008-09-11 09:15:33 +00003036 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003037 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003038 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003039
3040 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003041 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003042 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003043
Anders Carlssondde0a942008-09-11 09:15:33 +00003044 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003045
3046 // The scope of the catch variable ends right here.
3047 CatchVarCleanups.ForceCleanup();
3048
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003049 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003050 break;
3051 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003052
Steve Naroff14108da2009-07-10 23:34:53 +00003053 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003054 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003055
3056 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003057 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3058 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003059
3060 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003061 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003062
John McCallf1549f62010-07-06 01:34:17 +00003063 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003064 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3065 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003066 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003067
John McCallf1549f62010-07-06 01:34:17 +00003068 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3069 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003070
3071 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003072 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003073
Anders Carlsson80f25672008-09-09 17:59:25 +00003074 // Emit the @catch block.
3075 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003076
3077 // Collect any cleanups for the catch variable. The scope lasts until
3078 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003079 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003080
John McCallb6bbcc92010-10-15 04:57:14 +00003081 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003082 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003083
John McCallf1549f62010-07-06 01:34:17 +00003084 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085 llvm::Value *Tmp =
3086 CGF.Builder.CreateBitCast(Caught,
3087 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003088 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003089 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090
Anders Carlssondde0a942008-09-11 09:15:33 +00003091 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003092
3093 // We're done with the catch variable.
3094 CatchVarCleanups.ForceCleanup();
3095
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003096 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003097
Anders Carlsson80f25672008-09-09 17:59:25 +00003098 CGF.EmitBlock(NextCatchBlock);
3099 }
3100
John McCallf1549f62010-07-06 01:34:17 +00003101 CGF.ObjCEHValueStack.pop_back();
3102
John McCall0b251722010-08-04 05:59:32 +00003103 // If nothing wanted anything to do with the caught exception,
3104 // kill the extract call.
3105 if (Caught->use_empty())
3106 Caught->eraseFromParent();
3107
3108 if (!AllMatched)
3109 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3110
3111 if (HasFinally) {
3112 // Emit the exception handler for the @catch blocks.
3113 CGF.EmitBlock(CatchHandler);
3114
3115 // In theory we might now need a write hazard, but actually it's
3116 // unnecessary because there's no local-accessing code between
3117 // the try's write hazard and here.
3118 //Hazards.emitWriteHazard();
3119
John McCall9e2213d2010-10-04 23:42:51 +00003120 // Extract the new exception and save it to the
3121 // propagating-exception slot.
3122 assert(PropagatingExnVar);
3123 llvm::CallInst *NewCaught =
3124 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3125 ExceptionData, "caught");
3126 NewCaught->setDoesNotThrow();
3127 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3128
John McCall0b251722010-08-04 05:59:32 +00003129 // Don't pop the catch handler; the throw already did.
3130 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003131 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003132 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003133 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003134
John McCall87bb5822010-07-31 23:20:56 +00003135 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003136 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003137
John McCallf1549f62010-07-06 01:34:17 +00003138 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003139 CGF.Builder.restoreIP(TryFallthroughIP);
3140 if (CGF.HaveInsertPoint())
3141 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003142 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003143 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003144
John McCallf1549f62010-07-06 01:34:17 +00003145 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003146 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003147 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003148 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003149 // If we have a propagating-exception variable, check it.
3150 llvm::Value *PropagatingExn;
3151 if (PropagatingExnVar) {
3152 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003153
John McCall9e2213d2010-10-04 23:42:51 +00003154 // Otherwise, just look in the buffer for the exception to throw.
3155 } else {
3156 llvm::CallInst *Caught =
3157 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3158 ExceptionData);
3159 Caught->setDoesNotThrow();
3160 PropagatingExn = Caught;
3161 }
3162
3163 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003164 ->setDoesNotThrow();
3165 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003166 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003167
John McCall87bb5822010-07-31 23:20:56 +00003168 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003169}
3170
3171void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003172 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003173 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003174
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003175 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3176 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003177 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003178 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3179 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003181 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003182 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003183 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003184
John McCallf1549f62010-07-06 01:34:17 +00003185 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3186 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003187 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003188
3189 // Clear the insertion point to indicate we are in unreachable code.
3190 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003191}
3192
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003193/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003194/// object: objc_read_weak (id *src)
3195///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003196llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003197 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003198 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003199 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3200 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3201 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003202 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003203 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003204 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003205 return read_weak;
3206}
3207
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003208/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3209/// objc_assign_weak (id src, id *dst)
3210///
3211void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003212 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003213 const llvm::Type * SrcTy = src->getType();
3214 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003215 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003216 assert(Size <= 8 && "does not support size > 8");
3217 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003218 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003219 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3220 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003221 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3222 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003223 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003224 src, dst, "weakassign");
3225 return;
3226}
3227
Fariborz Jahanian58626502008-11-19 00:59:10 +00003228/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3229/// objc_assign_global (id src, id *dst)
3230///
3231void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003232 llvm::Value *src, llvm::Value *dst,
3233 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003234 const llvm::Type * SrcTy = src->getType();
3235 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003236 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003237 assert(Size <= 8 && "does not support size > 8");
3238 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003239 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003240 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3241 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003242 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3243 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003244 if (!threadlocal)
3245 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3246 src, dst, "globalassign");
3247 else
3248 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3249 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003250 return;
3251}
3252
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003253/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003254/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003255///
3256void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003257 llvm::Value *src, llvm::Value *dst,
3258 llvm::Value *ivarOffset) {
3259 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003260 const llvm::Type * SrcTy = src->getType();
3261 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003262 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003263 assert(Size <= 8 && "does not support size > 8");
3264 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003265 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003266 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3267 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003268 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3269 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003270 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3271 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003272 return;
3273}
3274
Fariborz Jahanian58626502008-11-19 00:59:10 +00003275/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3276/// objc_assign_strongCast (id src, id *dst)
3277///
3278void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003279 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003280 const llvm::Type * SrcTy = src->getType();
3281 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003282 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003283 assert(Size <= 8 && "does not support size > 8");
3284 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003285 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003286 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3287 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003288 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3289 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003290 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003291 src, dst, "weakassign");
3292 return;
3293}
3294
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003295void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003296 llvm::Value *DestPtr,
3297 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003298 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003299 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3300 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003301 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003302 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003303 return;
3304}
3305
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003306/// EmitObjCValueForIvar - Code Gen for ivar reference.
3307///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003308LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3309 QualType ObjectTy,
3310 llvm::Value *BaseValue,
3311 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003312 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003313 const ObjCInterfaceDecl *ID =
3314 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003315 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3316 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003317}
3318
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003319llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003320 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003321 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003322 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003323 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003324 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3325 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003326}
3327
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003328/* *** Private Interface *** */
3329
3330/// EmitImageInfo - Emit the image info marker used to encode some module
3331/// level information.
3332///
3333/// See: <rdr://4810609&4810587&4810587>
3334/// struct IMAGE_INFO {
3335/// unsigned version;
3336/// unsigned flags;
3337/// };
3338enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003339 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003340 eImageInfo_GarbageCollected = (1 << 1),
3341 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003342 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3343
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003344 // A flag indicating that the module has no instances of a @synthesize of a
3345 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003346 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003347};
3348
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003349void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003350 unsigned version = 0; // Version is unused?
3351 unsigned flags = 0;
3352
3353 // FIXME: Fix and continue?
3354 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3355 flags |= eImageInfo_GarbageCollected;
3356 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3357 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003358
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003359 // We never allow @synthesize of a superclass property.
3360 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003361
Chris Lattner77b89b82010-06-27 07:15:29 +00003362 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3363
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003364 // Emitted as int[2];
3365 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003366 llvm::ConstantInt::get(Int32Ty, version),
3367 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003368 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003369 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003370
3371 const char *Section;
3372 if (ObjCABI == 1)
3373 Section = "__OBJC, __image_info,regular";
3374 else
3375 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003376 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003377 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003378 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003379 Section,
3380 0,
3381 true);
3382 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003383}
3384
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003385
3386// struct objc_module {
3387// unsigned long version;
3388// unsigned long size;
3389// const char *name;
3390// Symtab symtab;
3391// };
3392
3393// FIXME: Get from somewhere
3394static const int ModuleVersion = 7;
3395
3396void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003397 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003398
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003399 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003400 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3401 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003402 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003403 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003404 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003405 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003406 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003407 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003408 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003409}
3410
3411llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003412 unsigned NumClasses = DefinedClasses.size();
3413 unsigned NumCategories = DefinedCategories.size();
3414
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003415 // Return null if no symbols were defined.
3416 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003417 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003418
3419 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003420 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003421 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003422 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3423 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003424
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003425 // The runtime expects exactly the list of defined classes followed
3426 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003427 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003428 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003429 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003430 ObjCTypes.Int8PtrTy);
3431 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003432 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003433 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003434 ObjCTypes.Int8PtrTy);
3435
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003436 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003437 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003438 NumClasses + NumCategories),
3439 Symbols);
3440
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003441 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003442
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003443 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003444 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3445 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003446 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003447 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003448}
3449
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003450llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003451 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003452 LazySymbols.insert(ID->getIdentifier());
3453
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003454 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003456 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003457 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003458 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003459 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003460 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003461 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3462 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003463 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003464 }
3465
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003466 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003467}
3468
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003469llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3470 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003471 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003472
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003473 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003474 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003475 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003476 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003477 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003478 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3479 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003480 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003481 }
3482
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003483 if (lvalue)
3484 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003485 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003486}
3487
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003488llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003489 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003490
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003491 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003492 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003493 llvm::ConstantArray::get(VMContext,
3494 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003495 ((ObjCABI == 2) ?
3496 "__TEXT,__objc_classname,cstring_literals" :
3497 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003498 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003499
Owen Andersona1cf15f2009-07-14 23:10:40 +00003500 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003501}
3502
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003503llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3504 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3505 I = MethodDefinitions.find(MD);
3506 if (I != MethodDefinitions.end())
3507 return I->second;
3508
3509 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3510 // MD isn't emitted yet because it comes from PCH.
3511 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3512 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3513 return MethodDefinitions[MD];
3514 }
3515
3516 return NULL;
3517}
3518
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003519/// GetIvarLayoutName - Returns a unique constant for the given
3520/// ivar layout bitmap.
3521llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003522 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003523 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003524}
3525
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003526void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003528 bool ForStrongLayout,
3529 bool &HasUnion) {
3530 const RecordDecl *RD = RT->getDecl();
3531 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003532 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003533 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003534 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003535 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003537 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3538 ForStrongLayout, HasUnion);
3539}
3540
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003541void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003542 const llvm::StructLayout *Layout,
3543 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003544 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003545 unsigned int BytePos, bool ForStrongLayout,
3546 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003547 bool IsUnion = (RD && RD->isUnion());
3548 uint64_t MaxUnionIvarSize = 0;
3549 uint64_t MaxSkippedUnionIvarSize = 0;
3550 FieldDecl *MaxField = 0;
3551 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003552 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003553 uint64_t MaxFieldOffset = 0;
3554 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003555 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003557 if (RecFields.empty())
3558 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003559 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3560 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3561
Chris Lattnerf1690852009-03-31 08:48:01 +00003562 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003563 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003564 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003565 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003566 // Note that 'i' here is actually the field index inside RD of Field,
3567 // although this dependency is hidden.
3568 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckfb67ccd2011-04-14 00:43:09 +00003569 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003570 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003571 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003572
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003573 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003574 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003575 LastFieldBitfieldOrUnnamed = Field;
3576 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003577 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003578 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003579
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003580 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003581 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003582 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003583 if (FQT->isUnionType())
3584 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003585
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003586 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003587 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003588 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003589 continue;
3590 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003591
Chris Lattnerf1690852009-03-31 08:48:01 +00003592 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003593 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 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003596 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003597 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003598 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3599 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003600 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003601 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003602 FQT = CArray->getElementType();
3603 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003604
3605 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003606 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003607 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003608 int OldIndex = IvarsInfo.size() - 1;
3609 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003610
Ted Kremenek6217b802009-07-29 21:53:49 +00003611 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003612 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003613 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003614
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003615 // Replicate layout information for each array element. Note that
3616 // one element is already done.
3617 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003618 for (int FirstIndex = IvarsInfo.size() - 1,
3619 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003620 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003621 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3622 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3623 IvarsInfo[i].ivar_size));
3624 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3625 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3626 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003627 }
3628 continue;
3629 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003630 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003631 // At this point, we are done with Record/Union and array there of.
3632 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003633 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003634
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003635 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003636 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3637 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003638 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003639 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003640 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003641 MaxUnionIvarSize = UnionIvarSize;
3642 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003643 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003644 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003645 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003646 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003647 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003648 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003649 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003650 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3651 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003652 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003653 // FIXME: Why the asymmetry? We divide by word size in bits on other
3654 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003655 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003656 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003657 MaxSkippedUnionIvarSize = UnionIvarSize;
3658 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003659 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003660 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003661 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003662 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003663 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003664 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003665 }
3666 }
3667 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003668
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003669 if (LastFieldBitfieldOrUnnamed) {
3670 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3671 // Last field was a bitfield. Must update skip info.
3672 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3673 uint64_t BitFieldSize =
3674 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3675 GC_IVAR skivar;
3676 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3677 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3678 + ((BitFieldSize % ByteSizeInBits) != 0);
3679 SkipIvars.push_back(skivar);
3680 } else {
3681 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3682 // Last field was unnamed. Must update skip info.
3683 unsigned FieldSize
3684 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3685 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3686 FieldSize / ByteSizeInBits));
3687 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003688 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003689
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003690 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003691 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003692 MaxUnionIvarSize));
3693 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003694 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003695 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003696}
3697
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003698/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3699/// the computations and returning the layout bitmap (for ivar or blocks) in
3700/// the given argument BitMap string container. Routine reads
3701/// two containers, IvarsInfo and SkipIvars which are assumed to be
3702/// filled already by the caller.
3703llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003704 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003705 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003706
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003707 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003708 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003709 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003710 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003711 if (IvarsInfo[0].ivar_bytepos == 0) {
3712 WordsToSkip = 0;
3713 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003714 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003715 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3716 WordsToScan = IvarsInfo[0].ivar_size;
3717 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003718 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003719 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003720 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003721 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003722 // consecutive 'scanned' object pointers.
3723 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003724 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003725 // Skip over 'gc'able object pointer which lay over each other.
3726 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3727 continue;
3728 // Must skip over 1 or more words. We save current skip/scan values
3729 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003730 SKIP_SCAN SkScan;
3731 SkScan.skip = WordsToSkip;
3732 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003733 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003734
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003735 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003736 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3737 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003738 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003739 WordsToSkip = 0;
3740 WordsToScan = IvarsInfo[i].ivar_size;
3741 }
3742 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003743 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003744 SKIP_SCAN SkScan;
3745 SkScan.skip = WordsToSkip;
3746 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003747 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003748 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003749
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003750 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003751 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003752 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003753 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003754 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003755 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003756 IvarsInfo[LastIndex].ivar_bytepos +
3757 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003758 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003759 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003760 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003761 SKIP_SCAN SkScan;
3762 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3763 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003764 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003765 }
3766 }
3767 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3768 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003769 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003770 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003771 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3772 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3773 // 0xM0 followed by 0x0N detected.
3774 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3775 for (int j = i+1; j < SkipScan; j++)
3776 SkipScanIvars[j] = SkipScanIvars[j+1];
3777 --SkipScan;
3778 }
3779 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003780
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003781 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003782 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003783 unsigned char byte;
3784 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3785 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3786 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3787 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003788
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 // first skip big.
3790 for (unsigned int ix = 0; ix < skip_big; ix++)
3791 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003792
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003793 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003794 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003795 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003796 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003797 byte |= 0xf;
3798 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003799 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003800 byte |= scan_small;
3801 scan_small = 0;
3802 }
3803 BitMap += byte;
3804 }
3805 // next scan big
3806 for (unsigned int ix = 0; ix < scan_big; ix++)
3807 BitMap += (unsigned char)(0x0f);
3808 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003809 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003810 byte = scan_small;
3811 BitMap += byte;
3812 }
3813 }
3814 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003815 unsigned char zero = 0;
3816 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003817
3818 llvm::GlobalVariable * Entry =
3819 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3820 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003821 ((ObjCABI == 2) ?
3822 "__TEXT,__objc_classname,cstring_literals" :
3823 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003824 1, true);
3825 return getConstantGEP(VMContext, Entry, 0, 0);
3826}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003827
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003828/// BuildIvarLayout - Builds ivar layout bitmap for the class
3829/// implementation for the __strong or __weak case.
3830/// The layout map displays which words in ivar list must be skipped
3831/// and which must be scanned by GC (see below). String is built of bytes.
3832/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3833/// of words to skip and right nibble is count of words to scan. So, each
3834/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3835/// represented by a 0x00 byte which also ends the string.
3836/// 1. when ForStrongLayout is true, following ivars are scanned:
3837/// - id, Class
3838/// - object *
3839/// - __strong anything
3840///
3841/// 2. When ForStrongLayout is false, following ivars are scanned:
3842/// - __weak anything
3843///
3844llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3845 const ObjCImplementationDecl *OMD,
3846 bool ForStrongLayout) {
3847 bool hasUnion = false;
3848
3849 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3850 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3851 return llvm::Constant::getNullValue(PtrTy);
3852
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003853 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003854 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003855 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003856
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003857 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003858 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3859 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3860
3861 if (RecFields.empty())
3862 return llvm::Constant::getNullValue(PtrTy);
3863
3864 SkipIvars.clear();
3865 IvarsInfo.clear();
3866
3867 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3868 if (IvarsInfo.empty())
3869 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003870 // Sort on byte position in case we encounterred a union nested in
3871 // the ivar list.
3872 if (hasUnion && !IvarsInfo.empty())
3873 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3874 if (hasUnion && !SkipIvars.empty())
3875 std::sort(SkipIvars.begin(), SkipIvars.end());
3876
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003877 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003878 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003879
3880 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003881 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003882 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003883 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003884 const unsigned char *s = (unsigned char*)BitMap.c_str();
3885 for (unsigned i = 0; i < BitMap.size(); i++)
3886 if (!(s[i] & 0xf0))
3887 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3888 else
3889 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3890 printf("\n");
3891 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003892 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003893}
3894
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003895llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003896 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3897
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003898 // FIXME: Avoid std::string copying.
3899 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003900 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003901 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003902 ((ObjCABI == 2) ?
3903 "__TEXT,__objc_methname,cstring_literals" :
3904 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003905 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003906
Owen Andersona1cf15f2009-07-14 23:10:40 +00003907 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003908}
3909
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003910// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003911llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003912 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3913}
3914
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003915llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003916 std::string TypeStr;
3917 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3918
3919 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003920
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003921 if (!Entry)
3922 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003923 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003924 ((ObjCABI == 2) ?
3925 "__TEXT,__objc_methtype,cstring_literals" :
3926 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003927 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003928
Owen Andersona1cf15f2009-07-14 23:10:40 +00003929 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003930}
3931
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003932llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003933 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003934 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3935 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003936
3937 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3938
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003939 if (!Entry)
3940 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003941 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003942 ((ObjCABI == 2) ?
3943 "__TEXT,__objc_methtype,cstring_literals" :
3944 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003945 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003946
Owen Andersona1cf15f2009-07-14 23:10:40 +00003947 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003948}
3949
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003950// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003951llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003952 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003953
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003954 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003956 llvm::ConstantArray::get(VMContext,
3957 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003958 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003959 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003960
Owen Andersona1cf15f2009-07-14 23:10:40 +00003961 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003962}
3963
3964// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003965// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003966llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003967CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3968 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003969 std::string TypeStr;
3970 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003971 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3972}
3973
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003974void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003975 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003976 llvm::SmallVectorImpl<char> &Name) {
3977 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003978 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003979 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3980 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003981 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003982 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003983 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003984 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003985}
3986
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003987void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003988 EmitModuleInfo();
3989
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003990 // Emit the dummy bodies for any protocols which were referenced but
3991 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003992 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003993 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3994 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003995 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003996
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003997 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003998 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003999 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004000 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004001 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004002 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004003 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004004 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004005 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004006 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004007 }
4008
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004009 // Add assembler directives to add lazy undefined symbol references
4010 // for classes which are referenced but not defined. This is
4011 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004012 //
4013 // FIXME: It would be nice if we had an LLVM construct for this.
4014 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4015 llvm::SmallString<256> Asm;
4016 Asm += CGM.getModule().getModuleInlineAsm();
4017 if (!Asm.empty() && Asm.back() != '\n')
4018 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004019
Daniel Dunbar33063492009-09-07 00:20:42 +00004020 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004021 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4022 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004023 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4024 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004025 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004026 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004027 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004028 }
4029
4030 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4031 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4032 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4033 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004034
Daniel Dunbar33063492009-09-07 00:20:42 +00004035 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004036 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004037}
4038
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004039CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004040 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004041 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004042 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004043 ObjCABI = 2;
4044}
4045
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004046/* *** */
4047
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004048ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004049 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004050 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4051 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004053 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004054 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004055 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004056 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004057 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004058
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004059 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004060 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004061 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004062
Mike Stumpf5408fe2009-05-16 07:57:57 +00004063 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4064 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004065 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004066 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004067
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004068 // I'm not sure I like this. The implicit coordination is a bit
4069 // gross. We should solve this in a reasonable fashion because this
4070 // is a pretty common task (match some runtime data structure with
4071 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004072
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004073 // FIXME: This is leaked.
4074 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004075
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004076 // struct _objc_super {
4077 // id self;
4078 // Class cls;
4079 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004080 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004081 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004082 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004083 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004084 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004085 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004086 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004087 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004088 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004089
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004090 SuperCTy = Ctx.getTagDeclType(RD);
4091 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004092
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004093 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004094 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4095
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004096 // struct _prop_t {
4097 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004098 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004099 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004100 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004101 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004102 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004103
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004104 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004105 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004106 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004107 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004108 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004109 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004110 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004111 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004112 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004113 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004114 PropertyListTy);
4115 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004116 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004117
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004118 // struct _objc_method {
4119 // SEL _cmd;
4120 // char *method_type;
4121 // char *_imp;
4122 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004123 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004124 Int8PtrTy,
4125 Int8PtrTy,
4126 NULL);
4127 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004128
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004129 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004130 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004131 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004132 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004133}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004134
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004135ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004136 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004137 // struct _objc_method_description {
4138 // SEL name;
4139 // char *types;
4140 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004142 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004143 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004144 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004145 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004146 MethodDescriptionTy);
4147
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004148 // struct _objc_method_description_list {
4149 // int count;
4150 // struct _objc_method_description[1];
4151 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004153 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004154 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004155 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004156 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004157 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004158
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004159 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004160 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004161 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004162
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004163 // Protocol description structures
4164
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004165 // struct _objc_protocol_extension {
4166 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4167 // struct _objc_method_description_list *optional_instance_methods;
4168 // struct _objc_method_description_list *optional_class_methods;
4169 // struct _objc_property_list *instance_properties;
4170 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004171 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004172 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004173 MethodDescriptionListPtrTy,
4174 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004175 PropertyListPtrTy,
4176 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004178 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004179
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004180 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004181 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004182
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004183 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004184
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004185 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4186 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004187
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004188 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004189 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004190 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004191 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004192 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004193 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004194 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4195
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004196 // struct _objc_protocol {
4197 // struct _objc_protocol_extension *isa;
4198 // char *protocol_name;
4199 // struct _objc_protocol **_objc_protocol_list;
4200 // struct _objc_method_description_list *instance_methods;
4201 // struct _objc_method_description_list *class_methods;
4202 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004203 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004204 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004205 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004206 MethodDescriptionListPtrTy,
4207 MethodDescriptionListPtrTy,
4208 NULL);
4209 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4210
4211 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004213 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004214 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004215 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004216
4217 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004218 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004219 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004220
4221 // Class description structures
4222
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004223 // struct _objc_ivar {
4224 // char *ivar_name;
4225 // char *ivar_type;
4226 // int ivar_offset;
4227 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004228 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 Int8PtrTy,
4230 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004231 NULL);
4232 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4233
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004234 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004235 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004236 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004237 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004238
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004239 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004240 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004241 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004242 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004243
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004244 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004245 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004246 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004247 Int8PtrTy,
4248 PropertyListPtrTy,
4249 NULL);
4250 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004251 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004252
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004253 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004254
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004255 // struct _objc_class {
4256 // Class isa;
4257 // Class super_class;
4258 // char *name;
4259 // long version;
4260 // long info;
4261 // long instance_size;
4262 // struct _objc_ivar_list *ivars;
4263 // struct _objc_method_list *methods;
4264 // struct _objc_cache *cache;
4265 // struct _objc_protocol_list *protocols;
4266 // char *ivar_layout;
4267 // struct _objc_class_ext *ext;
4268 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004269 T = llvm::StructType::get(VMContext,
4270 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004271 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004272 Int8PtrTy,
4273 LongTy,
4274 LongTy,
4275 LongTy,
4276 IvarListPtrTy,
4277 MethodListPtrTy,
4278 CachePtrTy,
4279 ProtocolListPtrTy,
4280 Int8PtrTy,
4281 ClassExtensionPtrTy,
4282 NULL);
4283 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004284
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004285 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4286 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004287 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004288
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004289 // struct _objc_category {
4290 // char *category_name;
4291 // char *class_name;
4292 // struct _objc_method_list *instance_method;
4293 // struct _objc_method_list *class_method;
4294 // uint32_t size; // sizeof(struct _objc_category)
4295 // struct _objc_property_list *instance_properties;// category's @property
4296 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004297 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004298 Int8PtrTy,
4299 MethodListPtrTy,
4300 MethodListPtrTy,
4301 ProtocolListPtrTy,
4302 IntTy,
4303 PropertyListPtrTy,
4304 NULL);
4305 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4306
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004307 // Global metadata structures
4308
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004309 // struct _objc_symtab {
4310 // long sel_ref_cnt;
4311 // SEL *refs;
4312 // short cls_def_cnt;
4313 // short cat_def_cnt;
4314 // char *defs[cls_def_cnt + cat_def_cnt];
4315 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004316 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004317 SelectorPtrTy,
4318 ShortTy,
4319 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004320 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004321 NULL);
4322 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004323 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004324
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004325 // struct _objc_module {
4326 // long version;
4327 // long size; // sizeof(struct _objc_module)
4328 // char *name;
4329 // struct _objc_symtab* symtab;
4330 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004331 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004332 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004333 LongTy,
4334 Int8PtrTy,
4335 SymtabPtrTy,
4336 NULL);
4337 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004338
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004339
Mike Stumpf5408fe2009-05-16 07:57:57 +00004340 // FIXME: This is the size of the setjmp buffer and should be target
4341 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004342 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004343
Anders Carlsson124526b2008-09-09 10:10:21 +00004344 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004345 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004346 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004347
4348 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004349 llvm::StructType::get(VMContext,
4350 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4351 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004352 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004354 ExceptionDataTy);
4355
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004356}
4357
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004358ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004359 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004360 // struct _method_list_t {
4361 // uint32_t entsize; // sizeof(struct _objc_method)
4362 // uint32_t method_count;
4363 // struct _objc_method method_list[method_count];
4364 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004365 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004366 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004367 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004368 NULL);
4369 CGM.getModule().addTypeName("struct.__method_list_t",
4370 MethodListnfABITy);
4371 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004372 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004374 // struct _protocol_t {
4375 // id isa; // NULL
4376 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004377 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004378 // const struct method_list_t * const instance_methods;
4379 // const struct method_list_t * const class_methods;
4380 // const struct method_list_t *optionalInstanceMethods;
4381 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004382 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004383 // const uint32_t size; // sizeof(struct _protocol_t)
4384 // const uint32_t flags; // = 0
4385 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004386
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004387 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004388 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004389
Owen Anderson47a434f2009-08-05 23:18:46 +00004390 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004391 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004392 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004393 ProtocolListTyHolder),
4394 MethodListnfABIPtrTy,
4395 MethodListnfABIPtrTy,
4396 MethodListnfABIPtrTy,
4397 MethodListnfABIPtrTy,
4398 PropertyListPtrTy,
4399 IntTy,
4400 IntTy,
4401 NULL);
4402 CGM.getModule().addTypeName("struct._protocol_t",
4403 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004404
4405 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004406 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004407
Fariborz Jahanianda320092009-01-29 19:24:30 +00004408 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004409 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004410 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004411 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004412 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004413 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004414 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004415 NULL);
4416 CGM.getModule().addTypeName("struct._objc_protocol_list",
4417 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004418 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004419 ProtocolListnfABITy);
4420
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004421 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004422 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004424 // struct _ivar_t {
4425 // unsigned long int *offset; // pointer to ivar offset location
4426 // char *name;
4427 // char *type;
4428 // uint32_t alignment;
4429 // uint32_t size;
4430 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004431 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004432 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004433 Int8PtrTy,
4434 Int8PtrTy,
4435 IntTy,
4436 IntTy,
4437 NULL);
4438 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004439
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004440 // struct _ivar_list_t {
4441 // uint32 entsize; // sizeof(struct _ivar_t)
4442 // uint32 count;
4443 // struct _iver_t list[count];
4444 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004445 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004446 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004447 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004448 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004449 NULL);
4450 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004451
Owen Anderson96e0fc72009-07-29 22:16:19 +00004452 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004453
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004454 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004455 // uint32_t const flags;
4456 // uint32_t const instanceStart;
4457 // uint32_t const instanceSize;
4458 // uint32_t const reserved; // only when building for 64bit targets
4459 // const uint8_t * const ivarLayout;
4460 // const char *const name;
4461 // const struct _method_list_t * const baseMethods;
4462 // const struct _objc_protocol_list *const baseProtocols;
4463 // const struct _ivar_list_t *const ivars;
4464 // const uint8_t * const weakIvarLayout;
4465 // const struct _prop_list_t * const properties;
4466 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004467
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004468 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004469 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004470 IntTy,
4471 IntTy,
4472 Int8PtrTy,
4473 Int8PtrTy,
4474 MethodListnfABIPtrTy,
4475 ProtocolListnfABIPtrTy,
4476 IvarListnfABIPtrTy,
4477 Int8PtrTy,
4478 PropertyListPtrTy,
4479 NULL);
4480 CGM.getModule().addTypeName("struct._class_ro_t",
4481 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004482
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004483 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4484 std::vector<const llvm::Type*> Params;
4485 Params.push_back(ObjectPtrTy);
4486 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004487 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004488 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4489
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004490 // struct _class_t {
4491 // struct _class_t *isa;
4492 // struct _class_t * const superclass;
4493 // void *cache;
4494 // IMP *vtable;
4495 // struct class_ro_t *ro;
4496 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004497
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004498 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004499 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004500 llvm::StructType::get(VMContext,
4501 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004502 llvm::PointerType::getUnqual(ClassTyHolder),
4503 CachePtrTy,
4504 llvm::PointerType::getUnqual(ImpnfABITy),
4505 llvm::PointerType::getUnqual(ClassRonfABITy),
4506 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004507 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4508
4509 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004510 ClassnfABITy);
4511
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004512 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004513 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004514
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004515 // struct _category_t {
4516 // const char * const name;
4517 // struct _class_t *const cls;
4518 // const struct _method_list_t * const instance_methods;
4519 // const struct _method_list_t * const class_methods;
4520 // const struct _protocol_list_t * const protocols;
4521 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004522 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004523 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004524 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004525 MethodListnfABIPtrTy,
4526 MethodListnfABIPtrTy,
4527 ProtocolListnfABIPtrTy,
4528 PropertyListPtrTy,
4529 NULL);
4530 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004532 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004533 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4534 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004535
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004536 // MessageRefTy - LLVM for:
4537 // struct _message_ref_t {
4538 // IMP messenger;
4539 // SEL name;
4540 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004541
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004542 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004543 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004544 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004545 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004546 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004547 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004548 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004549 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004550 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004551 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004553 MessageRefCTy = Ctx.getTagDeclType(RD);
4554 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4555 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004556
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004557 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004558 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004559
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004560 // SuperMessageRefTy - LLVM for:
4561 // struct _super_message_ref_t {
4562 // SUPER_IMP messenger;
4563 // SEL name;
4564 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004565 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004566 SelectorPtrTy,
4567 NULL);
4568 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004569
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004570 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004571 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4572
Daniel Dunbare588b992009-03-01 04:46:24 +00004573
4574 // struct objc_typeinfo {
4575 // const void** vtable; // objc_ehtype_vtable + 2
4576 // const char* name; // c++ typeinfo string
4577 // Class cls;
4578 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004579 EHTypeTy = llvm::StructType::get(VMContext,
4580 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004581 Int8PtrTy,
4582 ClassnfABIPtrTy,
4583 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004584 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004585 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004586}
4587
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004588llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004589 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004590
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004591 return NULL;
4592}
4593
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594void CGObjCNonFragileABIMac::AddModuleClassList(const
4595 std::vector<llvm::GlobalValue*>
4596 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004597 const char *SymbolName,
4598 const char *SectionName) {
4599 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004600
Daniel Dunbar463b8762009-05-15 21:48:48 +00004601 if (!NumClasses)
4602 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004603
Daniel Dunbar463b8762009-05-15 21:48:48 +00004604 std::vector<llvm::Constant*> Symbols(NumClasses);
4605 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004606 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004607 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004609 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004610 NumClasses),
4611 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004612
Daniel Dunbar463b8762009-05-15 21:48:48 +00004613 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004614 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004615 llvm::GlobalValue::InternalLinkage,
4616 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004617 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004618 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004619 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004620 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004621}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004622
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004623void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4624 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625
Daniel Dunbar463b8762009-05-15 21:48:48 +00004626 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004627 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004628 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004629 "\01L_OBJC_LABEL_CLASS_$",
4630 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004631
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004632 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4633 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4634 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4635 continue;
4636 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004637 }
4638
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004639 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4640 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4641 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4642 continue;
4643 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4644 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004645
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004646 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004647 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4648 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004649
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004650 // Build list of all implemented category addresses in array
4651 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004652 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004653 "\01L_OBJC_LABEL_CATEGORY_$",
4654 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004655 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004656 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4657 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004658
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004659 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004660}
4661
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004662/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004663/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004664/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004665/// message dispatch call for all the rest.
4666///
4667bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004668 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4669 default:
4670 assert(0 && "Invalid dispatch method!");
4671 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004672 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004673 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004674 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004675 case CodeGenOptions::Mixed:
4676 break;
4677 }
4678
4679 // If so, see whether this selector is in the white-list of things which must
4680 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004681 if (NonLegacyDispatchMethods.empty()) {
4682 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4683 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4684 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4685 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4686 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4687 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4688 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4689 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4690 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4691 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004693 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4694 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4695 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4696 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4697 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4698 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4699 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4700 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004702 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004703 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4704 &CGM.getContext().Idents.get("objects"),
4705 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004706 };
4707 NonLegacyDispatchMethods.insert(
4708 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004709 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004710
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004711 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004712}
4713
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004714// Metadata flags
4715enum MetaDataDlags {
4716 CLS = 0x0,
4717 CLS_META = 0x1,
4718 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004719 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004720 CLS_EXCEPTION = 0x20
4721};
4722/// BuildClassRoTInitializer - generate meta-data for:
4723/// struct _class_ro_t {
4724/// uint32_t const flags;
4725/// uint32_t const instanceStart;
4726/// uint32_t const instanceSize;
4727/// uint32_t const reserved; // only when building for 64bit targets
4728/// const uint8_t * const ivarLayout;
4729/// const char *const name;
4730/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004731/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004732/// const struct _ivar_list_t *const ivars;
4733/// const uint8_t * const weakIvarLayout;
4734/// const struct _prop_list_t * const properties;
4735/// }
4736///
4737llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004738 unsigned flags,
4739 unsigned InstanceStart,
4740 unsigned InstanceSize,
4741 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004742 std::string ClassName = ID->getNameAsString();
4743 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004744 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4745 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4746 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004747 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004748 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4749 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004750 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004751 // const struct _method_list_t * const baseMethods;
4752 std::vector<llvm::Constant*> Methods;
4753 std::string MethodListName("\01l_OBJC_$_");
4754 if (flags & CLS_META) {
4755 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004756 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004757 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004758 // Class methods should always be defined.
4759 Methods.push_back(GetMethodConstant(*i));
4760 }
4761 } else {
4762 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004763 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004764 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004765 // Instance methods should always be defined.
4766 Methods.push_back(GetMethodConstant(*i));
4767 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004768 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004769 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004770 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004771
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004772 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4773 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004774
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004775 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4776 if (llvm::Constant *C = GetMethodConstant(MD))
4777 Methods.push_back(C);
4778 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4779 if (llvm::Constant *C = GetMethodConstant(MD))
4780 Methods.push_back(C);
4781 }
4782 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004783 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004784 Values[ 5] = EmitMethodList(MethodListName,
4785 "__DATA, __objc_const", Methods);
4786
Fariborz Jahanianda320092009-01-29 19:24:30 +00004787 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4788 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004789 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004790 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004791 OID->all_referenced_protocol_begin(),
4792 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004793
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004794 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004795 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004796 else
4797 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004798 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4799 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004800 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004801 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004802 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004803 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4804 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004805 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004806 Values);
4807 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004808 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4809 llvm::GlobalValue::InternalLinkage,
4810 Init,
4811 (flags & CLS_META) ?
4812 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4813 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004814 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004815 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004816 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004817 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004818
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004819}
4820
4821/// BuildClassMetaData - This routine defines that to-level meta-data
4822/// for the given ClassName for:
4823/// struct _class_t {
4824/// struct _class_t *isa;
4825/// struct _class_t * const superclass;
4826/// void *cache;
4827/// IMP *vtable;
4828/// struct class_ro_t *ro;
4829/// }
4830///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004831llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 std::string &ClassName,
4833 llvm::Constant *IsAGV,
4834 llvm::Constant *SuperClassGV,
4835 llvm::Constant *ClassRoGV,
4836 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004837 std::vector<llvm::Constant*> Values(5);
4838 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004839 Values[1] = SuperClassGV;
4840 if (!Values[1])
4841 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004842 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4843 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4844 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004845 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004846 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004847 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4848 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004849 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004850 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004851 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004852 if (HiddenVisibility)
4853 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004854 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004855}
4856
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004857bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004858CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004859 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004860}
4861
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004862void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004863 uint32_t &InstanceStart,
4864 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004865 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004866 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004867
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004868 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004869 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004870
4871 // If there are no fields, the start is the same as the end.
4872 if (!RL.getFieldCount())
4873 InstanceStart = InstanceSize;
4874 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004875 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004876}
4877
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004878void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4879 std::string ClassName = ID->getNameAsString();
4880 if (!ObjCEmptyCacheVar) {
4881 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882 CGM.getModule(),
4883 ObjCTypes.CacheTy,
4884 false,
4885 llvm::GlobalValue::ExternalLinkage,
4886 0,
4887 "_objc_empty_cache");
4888
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004889 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004890 CGM.getModule(),
4891 ObjCTypes.ImpnfABITy,
4892 false,
4893 llvm::GlobalValue::ExternalLinkage,
4894 0,
4895 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004896 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004897 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004898 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004899 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004900 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004901 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004902 uint32_t InstanceSize = InstanceStart;
4903 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004904 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4905 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004906
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004907 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004908
4909 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004910 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004911 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004912 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004913 if (ID->getNumIvarInitializers())
4914 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004915 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004916 // class is root
4917 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004918 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004919 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004920 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004921 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004922 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4923 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4924 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004925 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004926 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004927 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004928 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004929 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004930 ObjCMetaClassName +
4931 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004932 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004933 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004934 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004935 }
4936 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4937 InstanceStart,
4938 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004939 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004940 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004941 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4942 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004943 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004944
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004945 // Metadata for the class
4946 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004947 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004948 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004949 if (ID->getNumIvarInitializers())
4950 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004951
Douglas Gregor68584ed2009-06-18 16:11:24 +00004952 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004953 flags |= CLS_EXCEPTION;
4954
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004955 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004956 flags |= CLS_ROOT;
4957 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004958 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004959 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004960 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004961 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004962 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004963 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004964 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004965 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004966 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004967 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004968 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004969 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004970 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004971
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004972 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004973 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004974 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4975 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004976 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004977
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004978 // Determine if this class is also "non-lazy".
4979 if (ImplementationIsNonLazy(ID))
4980 DefinedNonLazyClasses.push_back(ClassMD);
4981
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004982 // Force the definition of the EHType if necessary.
4983 if (flags & CLS_EXCEPTION)
4984 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00004985 // Make sure method definition entries are all clear for next implementation.
4986 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004987}
4988
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004989/// GenerateProtocolRef - This routine is called to generate code for
4990/// a protocol reference expression; as in:
4991/// @code
4992/// @protocol(Proto1);
4993/// @endcode
4994/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4995/// which will hold address of the protocol meta-data.
4996///
4997llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004998 const ObjCProtocolDecl *PD) {
4999
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005000 // This routine is called for @protocol only. So, we must build definition
5001 // of protocol's meta-data (not a reference to it!)
5002 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005003 llvm::Constant *Init =
5004 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5005 ObjCTypes.ExternalProtocolPtrTy);
5006
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005007 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005008 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005010 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5011 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005012 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005013 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014 CGM.getModule(),
5015 Init->getType(), false,
5016 llvm::GlobalValue::WeakAnyLinkage,
5017 Init,
5018 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005019 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5020 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005021 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005022 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005023}
5024
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005025/// GenerateCategory - Build metadata for a category implementation.
5026/// struct _category_t {
5027/// const char * const name;
5028/// struct _class_t *const cls;
5029/// const struct _method_list_t * const instance_methods;
5030/// const struct _method_list_t * const class_methods;
5031/// const struct _protocol_list_t * const protocols;
5032/// const struct _prop_list_t * const properties;
5033/// }
5034///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005035void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005036 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005037 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005038 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5039 "_$_" + OCD->getNameAsString());
5040 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005041 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005043 std::vector<llvm::Constant*> Values(6);
5044 Values[0] = GetClassName(OCD->getIdentifier());
5045 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005046 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005047 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005048 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5049
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005050 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005051 std::vector<llvm::Constant*> Methods;
5052 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005054 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055
5056 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005057 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005058 // Instance methods should always be defined.
5059 Methods.push_back(GetMethodConstant(*i));
5060 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005061
5062 Values[2] = EmitMethodList(MethodListName,
5063 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005064 Methods);
5065
5066 MethodListName = Prefix;
5067 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5068 OCD->getNameAsString();
5069 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005070 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005071 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005072 // Class methods should always be defined.
5073 Methods.push_back(GetMethodConstant(*i));
5074 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075
5076 Values[3] = EmitMethodList(MethodListName,
5077 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005078 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005079 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005080 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005081 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005082 llvm::SmallString<256> ExtName;
5083 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5084 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005085 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005086 + Interface->getName() + "_$_"
5087 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005088 Category->protocol_begin(),
5089 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005090 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5091 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005092 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005093 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5094 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005095 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005096
5097 llvm::Constant *Init =
5098 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005099 Values);
5100 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005101 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005102 false,
5103 llvm::GlobalValue::InternalLinkage,
5104 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005105 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005106 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005107 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005108 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005109 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005110 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005111
5112 // Determine if this category is also "non-lazy".
5113 if (ImplementationIsNonLazy(OCD))
5114 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005115 // method definition entries must be clear for next implementation.
5116 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005117}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005118
5119/// GetMethodConstant - Return a struct objc_method constant for the
5120/// given method if it has been defined. The result is null if the
5121/// method has not been defined. The return value has type MethodPtrTy.
5122llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005123 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005124 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005125 if (!Fn)
5126 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005127
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005128 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005129 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005130 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005131 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005132 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005133 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005134 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005135}
5136
5137/// EmitMethodList - Build meta-data for method declarations
5138/// struct _method_list_t {
5139/// uint32_t entsize; // sizeof(struct _objc_method)
5140/// uint32_t method_count;
5141/// struct _objc_method method_list[method_count];
5142/// }
5143///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005144llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5145 const char *Section,
5146 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005147 // Return null for empty list.
5148 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005149 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005151 std::vector<llvm::Constant*> Values(3);
5152 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005153 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005154 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005155 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005156 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005157 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005158 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005159 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005160 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005161
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005162 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005163 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005164 llvm::GlobalValue::InternalLinkage,
5165 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005166 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005167 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005168 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005169 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005170 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005171 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005172 ObjCTypes.MethodListnfABIPtrTy);
5173}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005174
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005175/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5176/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005177llvm::GlobalVariable *
5178CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5179 const ObjCIvarDecl *Ivar) {
5180 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005181 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005182 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005183 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005184 CGM.getModule().getGlobalVariable(Name);
5185 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005187 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005188 false,
5189 llvm::GlobalValue::ExternalLinkage,
5190 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005191 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005192 return IvarOffsetGV;
5193}
5194
Daniel Dunbare83be122010-04-02 21:14:02 +00005195llvm::Constant *
5196CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5197 const ObjCIvarDecl *Ivar,
5198 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005199 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005200 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005201 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005202 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005203 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005204
Mike Stumpf5408fe2009-05-16 07:57:57 +00005205 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5206 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005207 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5208 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005209 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005210 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005211 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005212 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005213 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005214 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005215}
5216
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005217/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005218/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005219/// IvarListnfABIPtrTy.
5220/// struct _ivar_t {
5221/// unsigned long int *offset; // pointer to ivar offset location
5222/// char *name;
5223/// char *type;
5224/// uint32_t alignment;
5225/// uint32_t size;
5226/// }
5227/// struct _ivar_list_t {
5228/// uint32 entsize; // sizeof(struct _ivar_t)
5229/// uint32 count;
5230/// struct _iver_t list[count];
5231/// }
5232///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005233
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005234llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005235 const ObjCImplementationDecl *ID) {
5236
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005237 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005239 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5240 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005241
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005242 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005243
Daniel Dunbar91636d62009-04-20 00:33:43 +00005244 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005245 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005246 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005248 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5249 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005250 // Ignore unnamed bit-fields.
5251 if (!IVD->getDeclName())
5252 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005254 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005255 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5256 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005257 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005258 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005259 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005260 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005261 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005262 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005263 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005264 // NOTE. Size of a bitfield does not match gcc's, because of the
5265 // way bitfields are treated special in each. But I am told that
5266 // 'size' for bitfield ivars is ignored by the runtime so it does
5267 // not matter. If it matters, there is enough info to get the
5268 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005269 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005270 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005271 }
5272 // Return null for empty list.
5273 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005274 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005275 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005276 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005277 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5278 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005279 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005280 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005281 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005282 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005283 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5284 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005285 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005286 llvm::GlobalValue::InternalLinkage,
5287 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005288 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005289 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005290 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005291 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005292
Chris Lattnerad64e022009-07-17 23:57:13 +00005293 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005294 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005295}
5296
5297llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005298 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005299 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005300
Fariborz Jahanianda320092009-01-29 19:24:30 +00005301 if (!Entry) {
5302 // We use the initializer as a marker of whether this is a forward
5303 // reference or not. At module finalization we add the empty
5304 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005305 Entry =
5306 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5307 llvm::GlobalValue::ExternalLinkage,
5308 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005309 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005310 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005311 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Fariborz Jahanianda320092009-01-29 19:24:30 +00005313 return Entry;
5314}
5315
5316/// GetOrEmitProtocol - Generate the protocol meta-data:
5317/// @code
5318/// struct _protocol_t {
5319/// id isa; // NULL
5320/// const char * const protocol_name;
5321/// const struct _protocol_list_t * protocol_list; // super protocols
5322/// const struct method_list_t * const instance_methods;
5323/// const struct method_list_t * const class_methods;
5324/// const struct method_list_t *optionalInstanceMethods;
5325/// const struct method_list_t *optionalClassMethods;
5326/// const struct _prop_list_t * properties;
5327/// const uint32_t size; // sizeof(struct _protocol_t)
5328/// const uint32_t flags; // = 0
5329/// }
5330/// @endcode
5331///
5332
5333llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005335 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005336
Fariborz Jahanianda320092009-01-29 19:24:30 +00005337 // Early exit if a defining object has already been generated.
5338 if (Entry && Entry->hasInitializer())
5339 return Entry;
5340
Fariborz Jahanianda320092009-01-29 19:24:30 +00005341 // Construct method lists.
5342 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5343 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005344 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005345 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005346 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005347 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005348 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5349 OptInstanceMethods.push_back(C);
5350 } else {
5351 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005352 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005353 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005354
5355 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005356 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005357 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005358 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005359 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5360 OptClassMethods.push_back(C);
5361 } else {
5362 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005363 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005364 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 std::vector<llvm::Constant*> Values(10);
5367 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005368 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005369 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005370 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5371 PD->protocol_begin(),
5372 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005373
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005374 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005375 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376 "__DATA, __objc_const",
5377 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005379 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 "__DATA, __objc_const",
5381 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005382 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005383 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005384 "__DATA, __objc_const",
5385 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005387 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005388 "__DATA, __objc_const",
5389 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005390 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005392 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005393 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005394 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005395 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005396 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005397 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005398
Fariborz Jahanianda320092009-01-29 19:24:30 +00005399 if (Entry) {
5400 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005401 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005402 Entry->setInitializer(Init);
5403 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005405 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5406 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5407 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005408 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005409 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005411 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005412 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005413 CGM.AddUsedGlobal(Entry);
5414
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005415 // Use this protocol meta-data to build protocol list table in section
5416 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005417 llvm::GlobalVariable *PTGV =
5418 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5419 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5420 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005421 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005422 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005423 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005424 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005425 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005426 return Entry;
5427}
5428
5429/// EmitProtocolList - Generate protocol list meta-data:
5430/// @code
5431/// struct _protocol_list_t {
5432/// long protocol_count; // Note, this is 32/64 bit
5433/// struct _protocol_t[protocol_count];
5434/// }
5435/// @endcode
5436///
5437llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005438CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5439 ObjCProtocolDecl::protocol_iterator begin,
5440 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005441 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005442
Fariborz Jahanianda320092009-01-29 19:24:30 +00005443 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005445 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Daniel Dunbar948e2582009-02-15 07:36:20 +00005447 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005448 llvm::SmallString<256> TmpName;
5449 Name.toVector(TmpName);
5450 llvm::GlobalVariable *GV =
5451 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005452 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005453 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005454
Daniel Dunbar948e2582009-02-15 07:36:20 +00005455 for (; begin != end; ++begin)
5456 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5457
Fariborz Jahanianda320092009-01-29 19:24:30 +00005458 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005459 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005460 ObjCTypes.ProtocolnfABIPtrTy));
5461
Fariborz Jahanianda320092009-01-29 19:24:30 +00005462 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005463 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005464 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005465 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005466 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005467 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005468 ProtocolRefs.size()),
5469 ProtocolRefs);
5470
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005471 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005472 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005473 llvm::GlobalValue::InternalLinkage,
5474 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005475 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005476 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005477 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005478 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005479 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005481 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005482}
5483
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005484/// GetMethodDescriptionConstant - This routine build following meta-data:
5485/// struct _objc_method {
5486/// SEL _cmd;
5487/// char *method_type;
5488/// char *_imp;
5489/// }
5490
5491llvm::Constant *
5492CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5493 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005494 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005495 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5496 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005497 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005498 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005499 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005500 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005501}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005502
5503/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5504/// This code gen. amounts to generating code for:
5505/// @code
5506/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5507/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005508///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005509LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005510 CodeGen::CodeGenFunction &CGF,
5511 QualType ObjectTy,
5512 llvm::Value *BaseValue,
5513 const ObjCIvarDecl *Ivar,
5514 unsigned CVRQualifiers) {
5515 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005516 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5517 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005518}
5519
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005520llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005521 CodeGen::CodeGenFunction &CGF,
5522 const ObjCInterfaceDecl *Interface,
5523 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005524 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005525}
5526
Fariborz Jahanian46551122009-02-04 00:22:57 +00005527CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005528 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005529 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005530 QualType ResultType,
5531 Selector Sel,
5532 llvm::Value *Receiver,
5533 QualType Arg0Ty,
5534 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005535 const CallArgList &CallArgs,
5536 const ObjCMethodDecl *Method) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005537 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5538 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005539 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005540 llvm::Value *Arg0 = Receiver;
5541 if (!IsSuper)
5542 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005543
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005544 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005545 // FIXME. This is too much work to get the ABI-specific result type needed to
5546 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005547 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005548 = Types.getFunctionInfo(ResultType, CallArgList(),
5549 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005550 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005551 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005552 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00005553 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner9b7d6702010-08-18 16:09:06 +00005554 if (IsSuper) {
5555 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5556 Name += "objc_msgSendSuper2_stret_fixup";
5557 } else {
5558 Fn = ObjCTypes.getMessageSendStretFixupFn();
5559 Name += "objc_msgSend_stret_fixup";
5560 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005561 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5562 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5563 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005564 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005565 if (IsSuper) {
5566 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5567 Name += "objc_msgSendSuper2_fixup";
5568 } else {
5569 Fn = ObjCTypes.getMessageSendFixupFn();
5570 Name += "objc_msgSend_fixup";
5571 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005572 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005573 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005574 Name += '_';
5575 std::string SelName(Sel.getAsString());
5576 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005577 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005578 if (SelName[i] == ':')
5579 SelName[i] = '_';
5580 Name += SelName;
5581 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5582 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005583 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005584 std::vector<llvm::Constant*> Values(2);
5585 Values[0] = Fn;
5586 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005587 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005588 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005589 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005590 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005591 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005592 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005593 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005594 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005595 }
5596 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005597
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005598 CallArgList ActualArgs;
5599 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005601 ObjCTypes.MessageRefCPtrTy));
5602 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005603 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005604 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005605 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5606 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005607 const llvm::FunctionType *FTy =
5608 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005609 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005610 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005611 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005612}
5613
5614/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005615CodeGen::RValue
5616CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005617 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005618 QualType ResultType,
5619 Selector Sel,
5620 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005621 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005622 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005623 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005624 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005625 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5626 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005627 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005628 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005629 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005630 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005631 false, CallArgs, Method);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005632}
5633
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005634llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005635CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005636 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5637
Daniel Dunbardfff2302009-03-02 05:18:14 +00005638 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005640 false, llvm::GlobalValue::ExternalLinkage,
5641 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005642 }
5643
5644 return GV;
5645}
5646
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005647llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5648 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005649 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005650
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005651 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005652 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005653 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005654 Entry =
5655 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005656 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005658 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005659 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005660 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005661 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005662 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005663 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005664 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005665
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005666 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005667}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005668
Daniel Dunbar11394522009-04-18 08:51:00 +00005669llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005670CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005671 const ObjCInterfaceDecl *ID) {
5672 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005673
Daniel Dunbar11394522009-04-18 08:51:00 +00005674 if (!Entry) {
5675 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5676 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005677 Entry =
5678 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005679 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005681 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005682 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005683 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005684 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005685 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005686 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005687 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005688
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005689 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005690}
5691
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005692/// EmitMetaClassRef - Return a Value * of the address of _class_t
5693/// meta-data
5694///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005695llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5696 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005697 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5698 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005699 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005700
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005701 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005702 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005704 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005705 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005707 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005708 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005709 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710 ObjCTypes.ClassnfABIPtrTy));
5711
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005712 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005713 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005714
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005715 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005716}
5717
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005718/// GetClass - Return a reference to the class for the given interface
5719/// decl.
5720llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5721 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005722 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005723 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5724 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5725 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5726 }
5727
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005728 return EmitClassRef(Builder, ID);
5729}
5730
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005731/// Generates a message send where the super is the receiver. This is
5732/// a message send to self with special delivery semantics indicating
5733/// which class's method should be called.
5734CodeGen::RValue
5735CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005736 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005737 QualType ResultType,
5738 Selector Sel,
5739 const ObjCInterfaceDecl *Class,
5740 bool isCategoryImpl,
5741 llvm::Value *Receiver,
5742 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005743 const CodeGen::CallArgList &CallArgs,
5744 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005745 // ...
5746 // Create and init a super structure; this is a (receiver, class)
5747 // pair we will pass to objc_msgSendSuper.
5748 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005749 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005750
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005751 llvm::Value *ReceiverAsObject =
5752 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5753 CGF.Builder.CreateStore(ReceiverAsObject,
5754 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005755
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005756 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005757 llvm::Value *Target;
5758 if (IsClassMessage) {
5759 if (isCategoryImpl) {
5760 // Message sent to "super' in a class method defined in
5761 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005762 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005763 Target = CGF.Builder.CreateStructGEP(Target, 0);
5764 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005765 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005766 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005767 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005768 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005769
Mike Stumpf5408fe2009-05-16 07:57:57 +00005770 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5771 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005772 const llvm::Type *ClassTy =
5773 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5774 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5775 CGF.Builder.CreateStore(Target,
5776 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005777
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005778 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005779 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5780 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005781 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005782 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005783 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005784 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005785 true, CallArgs, Method);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005786}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005787
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005789 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005790 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005791
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005792 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005793 llvm::Constant *Casted =
5794 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5795 ObjCTypes.SelectorPtrTy);
5796 Entry =
5797 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5798 llvm::GlobalValue::InternalLinkage,
5799 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005800 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005801 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005802 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005803
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005804 if (lval)
5805 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005806 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005807}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005808/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005809/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005810///
5811void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005812 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005813 llvm::Value *dst,
5814 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005815 const llvm::Type * SrcTy = src->getType();
5816 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005817 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005818 assert(Size <= 8 && "does not support size > 8");
5819 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5820 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005821 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5822 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005823 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5824 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005825 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5826 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005827 return;
5828}
5829
5830/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5831/// objc_assign_strongCast (id src, id *dst)
5832///
5833void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005834 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005835 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005836 const llvm::Type * SrcTy = src->getType();
5837 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005838 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005839 assert(Size <= 8 && "does not support size > 8");
5840 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005841 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005842 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5843 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005844 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5845 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005846 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005847 src, dst, "weakassign");
5848 return;
5849}
5850
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005851void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005852 CodeGen::CodeGenFunction &CGF,
5853 llvm::Value *DestPtr,
5854 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005855 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005856 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5857 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005858 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005859 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005860 return;
5861}
5862
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005863/// EmitObjCWeakRead - Code gen for loading value of a __weak
5864/// object: objc_read_weak (id *src)
5865///
5866llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005867 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005868 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005869 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005870 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5871 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005872 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005873 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005874 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005875 return read_weak;
5876}
5877
5878/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5879/// objc_assign_weak (id src, id *dst)
5880///
5881void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005882 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005883 const llvm::Type * SrcTy = src->getType();
5884 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005885 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005886 assert(Size <= 8 && "does not support size > 8");
5887 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5888 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005889 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5890 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005891 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5892 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005893 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005894 src, dst, "weakassign");
5895 return;
5896}
5897
5898/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5899/// objc_assign_global (id src, id *dst)
5900///
5901void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005902 llvm::Value *src, llvm::Value *dst,
5903 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005904 const llvm::Type * SrcTy = src->getType();
5905 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005906 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005907 assert(Size <= 8 && "does not support size > 8");
5908 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5909 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005910 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5911 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005912 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5913 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005914 if (!threadlocal)
5915 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5916 src, dst, "globalassign");
5917 else
5918 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5919 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005920 return;
5921}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005922
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005923void
John McCallf1549f62010-07-06 01:34:17 +00005924CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5925 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005926 EmitAtSynchronizedStmt(CGF, S,
5927 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5928 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005929}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005930
John McCall5a180392010-07-24 00:37:23 +00005931llvm::Constant *
5932CGObjCNonFragileABIMac::GetEHType(QualType T) {
5933 // There's a particular fixed type info for 'id'.
5934 if (T->isObjCIdType() ||
5935 T->isObjCQualifiedIdType()) {
5936 llvm::Constant *IDEHType =
5937 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5938 if (!IDEHType)
5939 IDEHType =
5940 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5941 false,
5942 llvm::GlobalValue::ExternalLinkage,
5943 0, "OBJC_EHTYPE_id");
5944 return IDEHType;
5945 }
5946
5947 // All other types should be Objective-C interface pointer types.
5948 const ObjCObjectPointerType *PT =
5949 T->getAs<ObjCObjectPointerType>();
5950 assert(PT && "Invalid @catch type.");
5951 const ObjCInterfaceType *IT = PT->getInterfaceType();
5952 assert(IT && "Invalid @catch type.");
5953 return GetInterfaceEHType(IT->getDecl(), false);
5954}
5955
John McCallf1549f62010-07-06 01:34:17 +00005956void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5957 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005958 EmitTryCatchStmt(CGF, S,
5959 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5960 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5961 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005962}
5963
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005964/// EmitThrowStmt - Generate code for a throw statement.
5965void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5966 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005967 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00005968 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00005969 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
5970 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00005971 llvm::Value *Args[] = { Exception };
5972 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
5973 Args, Args+1)
5974 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005975 } else {
John McCall7ec404c2010-10-16 08:21:07 +00005976 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
5977 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005978 }
5979
John McCall7ec404c2010-10-16 08:21:07 +00005980 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005981 CGF.Builder.ClearInsertionPoint();
5982}
Daniel Dunbare588b992009-03-01 04:46:24 +00005983
John McCall5a180392010-07-24 00:37:23 +00005984llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005985CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005986 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005987 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005988
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005989 // If we don't need a definition, return the entry if found or check
5990 // if we use an external reference.
5991 if (!ForDefinition) {
5992 if (Entry)
5993 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005994
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005995 // If this type (or a super class) has the __objc_exception__
5996 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005997 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005998 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005999 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006000 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006001 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006002 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006003 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006004 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006005
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006006 // Otherwise we need to either make a new entry or fill in the
6007 // initializer.
6008 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006009 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006010 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006011 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006012 CGM.getModule().getGlobalVariable(VTableName);
6013 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006014 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6015 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006016 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006017 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006018
Chris Lattner77b89b82010-06-27 07:15:29 +00006019 llvm::Value *VTableIdx =
6020 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006021
6022 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006023 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006024 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006025 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006026 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006027 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006028
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006029 if (Entry) {
6030 Entry->setInitializer(Init);
6031 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006032 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006033 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006034 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006035 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006036 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006037 }
6038
John McCall1fb0caa2010-10-22 21:05:15 +00006039 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006040 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006041 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6042 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006043
6044 if (ForDefinition) {
6045 Entry->setSection("__DATA,__objc_const");
6046 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6047 } else {
6048 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6049 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006050
6051 return Entry;
6052}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006053
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006054/* *** */
6055
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006056CodeGen::CGObjCRuntime *
6057CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006058 if (CGM.getLangOptions().ObjCNonFragileABI)
6059 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006060 return new CGObjCMac(CGM);
6061}