blob: 9466c77ed37e54b0409e89c319fabfebe0d643d8 [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());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002072}
2073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002074// FIXME: Get from somewhere?
2075enum ClassFlags {
2076 eClassFlags_Factory = 0x00001,
2077 eClassFlags_Meta = 0x00002,
2078 // <rdr://5142207>
2079 eClassFlags_HasCXXStructors = 0x02000,
2080 eClassFlags_Hidden = 0x20000,
2081 eClassFlags_ABI2_Hidden = 0x00010,
2082 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2083};
2084
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002085/*
2086 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002087 Class isa;
2088 Class super_class;
2089 const char *name;
2090 long version;
2091 long info;
2092 long instance_size;
2093 struct _objc_ivar_list *ivars;
2094 struct _objc_method_list *methods;
2095 struct _objc_cache *cache;
2096 struct _objc_protocol_list *protocols;
2097 // Objective-C 1.0 extensions (<rdr://4585769>)
2098 const char *ivar_layout;
2099 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002100 };
2101
2102 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002103*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002105 DefinedSymbols.insert(ID->getIdentifier());
2106
Chris Lattner8ec03f52008-11-24 03:54:41 +00002107 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002108 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002110 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002112 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002113 Interface->all_referenced_protocol_begin(),
2114 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002115 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002116 if (ID->getNumIvarInitializers())
2117 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002118 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002119 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002120
2121 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002122 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002123 Flags |= eClassFlags_Hidden;
2124
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002126 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002127 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002128 // Instance methods should always be defined.
2129 InstanceMethods.push_back(GetMethodConstant(*i));
2130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002131 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002132 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002133 // Class methods should always be defined.
2134 ClassMethods.push_back(GetMethodConstant(*i));
2135 }
2136
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002137 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002138 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002139 ObjCPropertyImplDecl *PID = *i;
2140
2141 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2142 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2143
2144 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2145 if (llvm::Constant *C = GetMethodConstant(MD))
2146 InstanceMethods.push_back(C);
2147 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2148 if (llvm::Constant *C = GetMethodConstant(MD))
2149 InstanceMethods.push_back(C);
2150 }
2151 }
2152
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002154 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002155 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002156 // Record a reference to the super class.
2157 LazySymbols.insert(Super->getIdentifier());
2158
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002159 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002160 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161 ObjCTypes.ClassPtrTy);
2162 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002163 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164 }
2165 Values[ 2] = GetClassName(ID->getIdentifier());
2166 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002167 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2168 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2169 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002170 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002171 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002172 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002173 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002174 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002175 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002176 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002178 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002180 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002182 std::string Name("\01L_OBJC_CLASS_");
2183 Name += ClassName;
2184 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2185 // Check for a forward reference.
2186 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2187 if (GV) {
2188 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2189 "Forward metaclass reference has incorrect type.");
2190 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2191 GV->setInitializer(Init);
2192 GV->setSection(Section);
2193 GV->setAlignment(4);
2194 CGM.AddUsedGlobal(GV);
2195 }
2196 else
2197 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002198 DefinedClasses.push_back(GV);
2199}
2200
2201llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2202 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002203 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002205 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206
John McCall1fb0caa2010-10-22 21:05:15 +00002207 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002209
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210 std::vector<llvm::Constant*> Values(12);
2211 // The isa for the metaclass is the root of the hierarchy.
2212 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2213 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2214 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002215 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002216 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002217 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002218 // The super class for the metaclass is emitted as the name of the
2219 // super class. The runtime fixes this up to point to the
2220 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002222 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002223 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 ObjCTypes.ClassPtrTy);
2225 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002226 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 }
2228 Values[ 2] = GetClassName(ID->getIdentifier());
2229 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002230 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2231 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2232 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002233 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002234 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002235 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002236 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002237 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002239 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002240 Values[ 9] = Protocols;
2241 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002242 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002244 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002245 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246 Values);
2247
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002248 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002249 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002250
2251 // Check for a forward reference.
2252 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2253 if (GV) {
2254 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2255 "Forward metaclass reference has incorrect type.");
2256 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2257 GV->setInitializer(Init);
2258 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002259 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002260 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002261 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002262 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002264 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002265 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266
2267 return GV;
2268}
2269
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002270llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002271 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002272
Mike Stumpf5408fe2009-05-16 07:57:57 +00002273 // FIXME: Should we look these up somewhere other than the module. Its a bit
2274 // silly since we only generate these while processing an implementation, so
2275 // exactly one pointer would work if know when we entered/exitted an
2276 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002277
2278 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002279 // Previously, metaclass with internal linkage may have been defined.
2280 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002281 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2282 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002283 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2284 "Forward metaclass reference has incorrect type.");
2285 return GV;
2286 } else {
2287 // Generate as an external reference to keep a consistent
2288 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002289 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002290 llvm::GlobalValue::ExternalLinkage,
2291 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002292 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002293 }
2294}
2295
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002296llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2297 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2298
2299 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2300 true)) {
2301 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2302 "Forward class metadata reference has incorrect type.");
2303 return GV;
2304 } else {
2305 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2306 llvm::GlobalValue::ExternalLinkage,
2307 0,
2308 Name);
2309 }
2310}
2311
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312/*
2313 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002314 uint32_t size;
2315 const char *weak_ivar_layout;
2316 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 };
2318*/
2319llvm::Constant *
2320CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002321 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002322 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323
2324 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002325 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002326 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002327 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002328 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329
2330 // Return null if no extension bits are used.
2331 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002332 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002334 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002335 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002336 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002338 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339}
2340
2341/*
2342 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002343 char *ivar_name;
2344 char *ivar_type;
2345 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 };
2347
2348 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002349 int ivar_count;
2350 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002352*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002353llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002354 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 std::vector<llvm::Constant*> Ivars, Ivar(3);
2356
2357 // When emitting the root class GCC emits ivar entries for the
2358 // actual class structure. It is not clear if we need to follow this
2359 // behavior; for now lets try and get away with not doing it. If so,
2360 // the cleanest solution would be to make up an ObjCInterfaceDecl
2361 // for the class.
2362 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002363 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364
2365 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002367
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002368 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002369 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002371 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2372 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002373 // Ignore unnamed bit-fields.
2374 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002375 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002376 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2377 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002378 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002379 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002380 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381 }
2382
2383 // Return null for empty list.
2384 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002385 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002386
2387 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002388 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002389 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002390 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002391 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002392 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002394 llvm::GlobalVariable *GV;
2395 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002396 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002398 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002399 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002400 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002401 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002402 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002403 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002404}
2405
2406/*
2407 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002408 SEL method_name;
2409 char *method_types;
2410 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002412
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002413 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002414 struct objc_method_list *obsolete;
2415 int count;
2416 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417 };
2418*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002419
2420/// GetMethodConstant - Return a struct objc_method constant for the
2421/// given method if it has been defined. The result is null if the
2422/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002423llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002424 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002425 if (!Fn)
2426 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002427
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002428 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002429 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002430 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002431 ObjCTypes.SelectorPtrTy);
2432 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002433 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002434 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002435}
2436
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002437llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002438 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002439 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440 // Return null for empty list.
2441 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002442 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002443
2444 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002445 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002446 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002447 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002449 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002450 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002452 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002453 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002455}
2456
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002457llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002458 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002459 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002460 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002461
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002462 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002463 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002464 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002465 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002466 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002467 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002468 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002469 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002470 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002471
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002472 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002473}
2474
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002475llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002476CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002477 llvm::Constant *Init,
2478 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002479 unsigned Align,
2480 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002481 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002483 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002484 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002485 if (Section)
2486 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002487 if (Align)
2488 GV->setAlignment(Align);
2489 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002490 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002491 return GV;
2492}
2493
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002494llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002495 // Abuse this interface function as a place to finalize.
2496 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002497 return NULL;
2498}
2499
Chris Lattner74391b42009-03-22 21:03:39 +00002500llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002501 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002502}
2503
Chris Lattner74391b42009-03-22 21:03:39 +00002504llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002505 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002506}
2507
David Chisnall8fac25d2010-12-26 22:13:16 +00002508llvm::Constant *CGObjCMac::GetGetStructFunction() {
2509 return ObjCTypes.getCopyStructFn();
2510}
2511llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002512 return ObjCTypes.getCopyStructFn();
2513}
2514
Chris Lattner74391b42009-03-22 21:03:39 +00002515llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002516 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002517}
2518
John McCallf1549f62010-07-06 01:34:17 +00002519void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2520 return EmitTryOrSynchronizedStmt(CGF, S);
2521}
2522
2523void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2524 const ObjCAtSynchronizedStmt &S) {
2525 return EmitTryOrSynchronizedStmt(CGF, S);
2526}
2527
John McCallcc505292010-07-21 06:59:36 +00002528namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002529 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002530 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002531 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002532 llvm::Value *CallTryExitVar;
2533 llvm::Value *ExceptionData;
2534 ObjCTypesHelper &ObjCTypes;
2535 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002536 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002537 llvm::Value *CallTryExitVar,
2538 llvm::Value *ExceptionData,
2539 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002540 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002541 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2542
2543 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2544 // Check whether we need to call objc_exception_try_exit.
2545 // In optimized code, this branch will always be folded.
2546 llvm::BasicBlock *FinallyCallExit =
2547 CGF.createBasicBlock("finally.call_exit");
2548 llvm::BasicBlock *FinallyNoCallExit =
2549 CGF.createBasicBlock("finally.no_call_exit");
2550 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2551 FinallyCallExit, FinallyNoCallExit);
2552
2553 CGF.EmitBlock(FinallyCallExit);
2554 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2555 ->setDoesNotThrow();
2556
2557 CGF.EmitBlock(FinallyNoCallExit);
2558
2559 if (isa<ObjCAtTryStmt>(S)) {
2560 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002561 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2562 // Save the current cleanup destination in case there's
2563 // control flow inside the finally statement.
2564 llvm::Value *CurCleanupDest =
2565 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2566
John McCallcc505292010-07-21 06:59:36 +00002567 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2568
John McCalld96a8e72010-08-11 00:16:14 +00002569 if (CGF.HaveInsertPoint()) {
2570 CGF.Builder.CreateStore(CurCleanupDest,
2571 CGF.getNormalCleanupDestSlot());
2572 } else {
2573 // Currently, the end of the cleanup must always exist.
2574 CGF.EnsureInsertPoint();
2575 }
2576 }
John McCallcc505292010-07-21 06:59:36 +00002577 } else {
2578 // Emit objc_sync_exit(expr); as finally's sole statement for
2579 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002580 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002581 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2582 ->setDoesNotThrow();
2583 }
2584 }
2585 };
John McCall87bb5822010-07-31 23:20:56 +00002586
2587 class FragileHazards {
2588 CodeGenFunction &CGF;
2589 llvm::SmallVector<llvm::Value*, 20> Locals;
2590 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2591
2592 llvm::InlineAsm *ReadHazard;
2593 llvm::InlineAsm *WriteHazard;
2594
2595 llvm::FunctionType *GetAsmFnType();
2596
2597 void collectLocals();
2598 void emitReadHazard(CGBuilderTy &Builder);
2599
2600 public:
2601 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002602
John McCall87bb5822010-07-31 23:20:56 +00002603 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002604 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002605 };
2606}
2607
2608/// Create the fragile-ABI read and write hazards based on the current
2609/// state of the function, which is presumed to be immediately prior
2610/// to a @try block. These hazards are used to maintain correct
2611/// semantics in the face of optimization and the fragile ABI's
2612/// cavalier use of setjmp/longjmp.
2613FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2614 collectLocals();
2615
2616 if (Locals.empty()) return;
2617
2618 // Collect all the blocks in the function.
2619 for (llvm::Function::iterator
2620 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2621 BlocksBeforeTry.insert(&*I);
2622
2623 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2624
2625 // Create a read hazard for the allocas. This inhibits dead-store
2626 // optimizations and forces the values to memory. This hazard is
2627 // inserted before any 'throwing' calls in the protected scope to
2628 // reflect the possibility that the variables might be read from the
2629 // catch block if the call throws.
2630 {
2631 std::string Constraint;
2632 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2633 if (I) Constraint += ',';
2634 Constraint += "*m";
2635 }
2636
2637 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2638 }
2639
2640 // Create a write hazard for the allocas. This inhibits folding
2641 // loads across the hazard. This hazard is inserted at the
2642 // beginning of the catch path to reflect the possibility that the
2643 // variables might have been written within the protected scope.
2644 {
2645 std::string Constraint;
2646 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2647 if (I) Constraint += ',';
2648 Constraint += "=*m";
2649 }
2650
2651 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2652 }
2653}
2654
2655/// Emit a write hazard at the current location.
2656void FragileHazards::emitWriteHazard() {
2657 if (Locals.empty()) return;
2658
2659 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2660 ->setDoesNotThrow();
2661}
2662
John McCall87bb5822010-07-31 23:20:56 +00002663void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2664 assert(!Locals.empty());
2665 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2666 ->setDoesNotThrow();
2667}
2668
2669/// Emit read hazards in all the protected blocks, i.e. all the blocks
2670/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002671void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002672 if (Locals.empty()) return;
2673
2674 CGBuilderTy Builder(CGF.getLLVMContext());
2675
2676 // Iterate through all blocks, skipping those prior to the try.
2677 for (llvm::Function::iterator
2678 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2679 llvm::BasicBlock &BB = *FI;
2680 if (BlocksBeforeTry.count(&BB)) continue;
2681
2682 // Walk through all the calls in the block.
2683 for (llvm::BasicBlock::iterator
2684 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2685 llvm::Instruction &I = *BI;
2686
2687 // Ignore instructions that aren't non-intrinsic calls.
2688 // These are the only calls that can possibly call longjmp.
2689 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2690 if (isa<llvm::IntrinsicInst>(I))
2691 continue;
2692
2693 // Ignore call sites marked nounwind. This may be questionable,
2694 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2695 llvm::CallSite CS(&I);
2696 if (CS.doesNotThrow()) continue;
2697
John McCall0b251722010-08-04 05:59:32 +00002698 // Insert a read hazard before the call. This will ensure that
2699 // any writes to the locals are performed before making the
2700 // call. If the call throws, then this is sufficient to
2701 // guarantee correctness as long as it doesn't also write to any
2702 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002703 Builder.SetInsertPoint(&BB, BI);
2704 emitReadHazard(Builder);
2705 }
2706 }
2707}
2708
2709static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2710 if (V) S.insert(V);
2711}
2712
2713void FragileHazards::collectLocals() {
2714 // Compute a set of allocas to ignore.
2715 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2716 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2717 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2718 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2719
2720 // Collect all the allocas currently in the function. This is
2721 // probably way too aggressive.
2722 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2723 for (llvm::BasicBlock::iterator
2724 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2725 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2726 Locals.push_back(&*I);
2727}
2728
2729llvm::FunctionType *FragileHazards::GetAsmFnType() {
2730 std::vector<const llvm::Type *> Tys(Locals.size());
2731 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2732 Tys[I] = Locals[I]->getType();
2733 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002734}
2735
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002736/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002737
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002738 Objective-C setjmp-longjmp (sjlj) Exception Handling
2739 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002740
John McCallf1549f62010-07-06 01:34:17 +00002741 A catch buffer is a setjmp buffer plus:
2742 - a pointer to the exception that was caught
2743 - a pointer to the previous exception data buffer
2744 - two pointers of reserved storage
2745 Therefore catch buffers form a stack, with a pointer to the top
2746 of the stack kept in thread-local storage.
2747
2748 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2749 objc_exception_try_exit pops the given catch buffer, which is
2750 required to be the top of the EH stack.
2751 objc_exception_throw pops the top of the EH stack, writes the
2752 thrown exception into the appropriate field, and longjmps
2753 to the setjmp buffer. It crashes the process (with a printf
2754 and an abort()) if there are no catch buffers on the stack.
2755 objc_exception_extract just reads the exception pointer out of the
2756 catch buffer.
2757
2758 There's no reason an implementation couldn't use a light-weight
2759 setjmp here --- something like __builtin_setjmp, but API-compatible
2760 with the heavyweight setjmp. This will be more important if we ever
2761 want to implement correct ObjC/C++ exception interactions for the
2762 fragile ABI.
2763
2764 Note that for this use of setjmp/longjmp to be correct, we may need
2765 to mark some local variables volatile: if a non-volatile local
2766 variable is modified between the setjmp and the longjmp, it has
2767 indeterminate value. For the purposes of LLVM IR, it may be
2768 sufficient to make loads and stores within the @try (to variables
2769 declared outside the @try) volatile. This is necessary for
2770 optimized correctness, but is not currently being done; this is
2771 being tracked as rdar://problem/8160285
2772
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002773 The basic framework for a @try-catch-finally is as follows:
2774 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002775 objc_exception_data d;
2776 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002777 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002778
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002779 objc_exception_try_enter(&d);
2780 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002781 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002782 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002783 // exception path
2784 id _caught = objc_exception_extract(&d);
2785
2786 // enter new try scope for handlers
2787 if (!setjmp(d.jmp_buf)) {
2788 ... match exception and execute catch blocks ...
2789
2790 // fell off end, rethrow.
2791 _rethrow = _caught;
2792 ... jump-through-finally to finally_rethrow ...
2793 } else {
2794 // exception in catch block
2795 _rethrow = objc_exception_extract(&d);
2796 _call_try_exit = false;
2797 ... jump-through-finally to finally_rethrow ...
2798 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002799 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002800 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002801
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002802 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002803 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002804 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002805
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002806 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002807 ... dispatch to finally destination ...
2808
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002809 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002810 objc_exception_throw(_rethrow);
2811
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002812 finally_end:
2813 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002814
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002815 This framework differs slightly from the one gcc uses, in that gcc
2816 uses _rethrow to determine if objc_exception_try_exit should be called
2817 and if the object should be rethrown. This breaks in the face of
2818 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002819
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002820 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002821
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002822 - If there are no catch blocks, then we avoid emitting the second
2823 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002825 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2826 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002827
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002828 - FIXME: If there is no @finally block we can do a few more
2829 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002830
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831 Rethrows and Jumps-Through-Finally
2832 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002833
John McCallf1549f62010-07-06 01:34:17 +00002834 '@throw;' is supported by pushing the currently-caught exception
2835 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002836
John McCallf1549f62010-07-06 01:34:17 +00002837 Branches through the @finally block are handled with an ordinary
2838 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2839 exceptions are not compatible with C++ exceptions, and this is
2840 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002841
John McCallf1549f62010-07-06 01:34:17 +00002842 @synchronized(expr) { stmt; } is emitted as if it were:
2843 id synch_value = expr;
2844 objc_sync_enter(synch_value);
2845 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002846*/
2847
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002848void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2849 const Stmt &S) {
2850 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002851
2852 // A destination for the fall-through edges of the catch handlers to
2853 // jump to.
2854 CodeGenFunction::JumpDest FinallyEnd =
2855 CGF.getJumpDestInCurrentScope("finally.end");
2856
2857 // A destination for the rethrow edge of the catch handlers to jump
2858 // to.
2859 CodeGenFunction::JumpDest FinallyRethrow =
2860 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002861
Daniel Dunbar1c566672009-02-24 01:43:46 +00002862 // For @synchronized, call objc_sync_enter(sync.expr). The
2863 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002864 // @synchronized. We can't avoid a temp here because we need the
2865 // value to be preserved. If the backend ever does liveness
2866 // correctly after setjmp, this will be unnecessary.
2867 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002868 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002869 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002870 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2871 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002872 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2873 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002874
2875 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2876 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002877 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002878
John McCall0b251722010-08-04 05:59:32 +00002879 // Allocate memory for the setjmp buffer. This needs to be kept
2880 // live throughout the try and catch blocks.
2881 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2882 "exceptiondata.ptr");
2883
John McCall87bb5822010-07-31 23:20:56 +00002884 // Create the fragile hazards. Note that this will not capture any
2885 // of the allocas required for exception processing, but will
2886 // capture the current basic block (which extends all the way to the
2887 // setjmp call) as "before the @try".
2888 FragileHazards Hazards(CGF);
2889
John McCallf1549f62010-07-06 01:34:17 +00002890 // Create a flag indicating whether the cleanup needs to call
2891 // objc_exception_try_exit. This is true except when
2892 // - no catches match and we're branching through the cleanup
2893 // just to rethrow the exception, or
2894 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002895 // The setjmp-safety rule here is that we should always store to this
2896 // variable in a place that dominates the branch through the cleanup
2897 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002898 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002899 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900
John McCall9e2213d2010-10-04 23:42:51 +00002901 // A slot containing the exception to rethrow. Only needed when we
2902 // have both a @catch and a @finally.
2903 llvm::Value *PropagatingExnVar = 0;
2904
John McCallf1549f62010-07-06 01:34:17 +00002905 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002906 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002907 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002908 CallTryExitVar,
2909 ExceptionData,
2910 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002911
2912 // Enter a try block:
2913 // - Call objc_exception_try_enter to push ExceptionData on top of
2914 // the EH stack.
2915 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2916 ->setDoesNotThrow();
2917
2918 // - Call setjmp on the exception data buffer.
2919 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2920 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2921 llvm::Value *SetJmpBuffer =
2922 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2923 llvm::CallInst *SetJmpResult =
2924 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2925 SetJmpResult->setDoesNotThrow();
2926
2927 // If setjmp returned 0, enter the protected block; otherwise,
2928 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002929 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2930 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002931 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00002932 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2933 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002934
John McCallf1549f62010-07-06 01:34:17 +00002935 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002936 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00002937 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002938 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002939 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00002940
2941 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002942
John McCallf1549f62010-07-06 01:34:17 +00002943 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002944 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002945
John McCall87bb5822010-07-31 23:20:56 +00002946 // Don't optimize loads of the in-scope locals across this point.
2947 Hazards.emitWriteHazard();
2948
John McCallf1549f62010-07-06 01:34:17 +00002949 // For a @synchronized (or a @try with no catches), just branch
2950 // through the cleanup to the rethrow block.
2951 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2952 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00002953 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002954 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002955
2956 // Otherwise, we have to match against the caught exceptions.
2957 } else {
John McCall0b251722010-08-04 05:59:32 +00002958 // Retrieve the exception object. We may emit multiple blocks but
2959 // nothing can cross this so the value is already in SSA form.
2960 llvm::CallInst *Caught =
2961 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2962 ExceptionData, "caught");
2963 Caught->setDoesNotThrow();
2964
John McCallf1549f62010-07-06 01:34:17 +00002965 // Push the exception to rethrow onto the EH value stack for the
2966 // benefit of any @throws in the handlers.
2967 CGF.ObjCEHValueStack.push_back(Caught);
2968
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002969 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002970
John McCall0b251722010-08-04 05:59:32 +00002971 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00002972
John McCall0b251722010-08-04 05:59:32 +00002973 llvm::BasicBlock *CatchBlock = 0;
2974 llvm::BasicBlock *CatchHandler = 0;
2975 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00002976 // Save the currently-propagating exception before
2977 // objc_exception_try_enter clears the exception slot.
2978 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2979 "propagating_exception");
2980 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2981
John McCall0b251722010-08-04 05:59:32 +00002982 // Enter a new exception try block (in case a @catch block
2983 // throws an exception).
2984 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2985 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002986
John McCall0b251722010-08-04 05:59:32 +00002987 llvm::CallInst *SetJmpResult =
2988 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2989 "setjmp.result");
2990 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002991
John McCall0b251722010-08-04 05:59:32 +00002992 llvm::Value *Threw =
2993 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2994
2995 CatchBlock = CGF.createBasicBlock("catch");
2996 CatchHandler = CGF.createBasicBlock("catch_for_catch");
2997 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
2998
2999 CGF.EmitBlock(CatchBlock);
3000 }
3001
3002 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003003
Daniel Dunbar55e40722008-09-27 07:03:52 +00003004 // Handle catch list. As a special case we check if everything is
3005 // matched and avoid generating code for falling off the end if
3006 // so.
3007 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003008 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3009 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003010
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003011 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003012 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003013
Anders Carlsson80f25672008-09-09 17:59:25 +00003014 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003015 if (!CatchParam) {
3016 AllMatched = true;
3017 } else {
John McCall183700f2009-09-21 23:43:11 +00003018 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003019
John McCallf1549f62010-07-06 01:34:17 +00003020 // catch(id e) always matches under this ABI, since only
3021 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003022 // FIXME: For the time being we also match id<X>; this should
3023 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003024 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003025 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003026 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027
John McCallf1549f62010-07-06 01:34:17 +00003028 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003029 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003030 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3031
Anders Carlssondde0a942008-09-11 09:15:33 +00003032 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003033 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003034 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003035
3036 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003037 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003038 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003039
Anders Carlssondde0a942008-09-11 09:15:33 +00003040 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003041
3042 // The scope of the catch variable ends right here.
3043 CatchVarCleanups.ForceCleanup();
3044
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003045 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003046 break;
3047 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048
Steve Naroff14108da2009-07-10 23:34:53 +00003049 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003050 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003051
3052 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003053 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3054 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003055
3056 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003057 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003058
John McCallf1549f62010-07-06 01:34:17 +00003059 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003060 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3061 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003062 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003063
John McCallf1549f62010-07-06 01:34:17 +00003064 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3065 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003066
3067 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003068 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069
Anders Carlsson80f25672008-09-09 17:59:25 +00003070 // Emit the @catch block.
3071 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003072
3073 // Collect any cleanups for the catch variable. The scope lasts until
3074 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003075 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003076
John McCallb6bbcc92010-10-15 04:57:14 +00003077 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003078 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003079
John McCallf1549f62010-07-06 01:34:17 +00003080 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003081 llvm::Value *Tmp =
3082 CGF.Builder.CreateBitCast(Caught,
3083 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003084 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003085 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086
Anders Carlssondde0a942008-09-11 09:15:33 +00003087 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003088
3089 // We're done with the catch variable.
3090 CatchVarCleanups.ForceCleanup();
3091
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003092 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093
Anders Carlsson80f25672008-09-09 17:59:25 +00003094 CGF.EmitBlock(NextCatchBlock);
3095 }
3096
John McCallf1549f62010-07-06 01:34:17 +00003097 CGF.ObjCEHValueStack.pop_back();
3098
John McCall0b251722010-08-04 05:59:32 +00003099 // If nothing wanted anything to do with the caught exception,
3100 // kill the extract call.
3101 if (Caught->use_empty())
3102 Caught->eraseFromParent();
3103
3104 if (!AllMatched)
3105 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3106
3107 if (HasFinally) {
3108 // Emit the exception handler for the @catch blocks.
3109 CGF.EmitBlock(CatchHandler);
3110
3111 // In theory we might now need a write hazard, but actually it's
3112 // unnecessary because there's no local-accessing code between
3113 // the try's write hazard and here.
3114 //Hazards.emitWriteHazard();
3115
John McCall9e2213d2010-10-04 23:42:51 +00003116 // Extract the new exception and save it to the
3117 // propagating-exception slot.
3118 assert(PropagatingExnVar);
3119 llvm::CallInst *NewCaught =
3120 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3121 ExceptionData, "caught");
3122 NewCaught->setDoesNotThrow();
3123 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3124
John McCall0b251722010-08-04 05:59:32 +00003125 // Don't pop the catch handler; the throw already did.
3126 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003127 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003128 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003129 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003130
John McCall87bb5822010-07-31 23:20:56 +00003131 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003132 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003133
John McCallf1549f62010-07-06 01:34:17 +00003134 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003135 CGF.Builder.restoreIP(TryFallthroughIP);
3136 if (CGF.HaveInsertPoint())
3137 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003138 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003139 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003140
John McCallf1549f62010-07-06 01:34:17 +00003141 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003142 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003143 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003144 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003145 // If we have a propagating-exception variable, check it.
3146 llvm::Value *PropagatingExn;
3147 if (PropagatingExnVar) {
3148 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003149
John McCall9e2213d2010-10-04 23:42:51 +00003150 // Otherwise, just look in the buffer for the exception to throw.
3151 } else {
3152 llvm::CallInst *Caught =
3153 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3154 ExceptionData);
3155 Caught->setDoesNotThrow();
3156 PropagatingExn = Caught;
3157 }
3158
3159 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003160 ->setDoesNotThrow();
3161 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003162 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003163
John McCall87bb5822010-07-31 23:20:56 +00003164 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003165}
3166
3167void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003168 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003169 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003170
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003171 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3172 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003173 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003174 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3175 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003177 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003178 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003179 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180
John McCallf1549f62010-07-06 01:34:17 +00003181 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3182 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003183 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003184
3185 // Clear the insertion point to indicate we are in unreachable code.
3186 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003187}
3188
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003189/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003190/// object: objc_read_weak (id *src)
3191///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003192llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003193 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003194 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003195 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3196 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3197 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003198 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003199 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003200 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003201 return read_weak;
3202}
3203
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003204/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3205/// objc_assign_weak (id src, id *dst)
3206///
3207void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003208 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003209 const llvm::Type * SrcTy = src->getType();
3210 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003211 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003212 assert(Size <= 8 && "does not support size > 8");
3213 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003214 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003215 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3216 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003217 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3218 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003219 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003220 src, dst, "weakassign");
3221 return;
3222}
3223
Fariborz Jahanian58626502008-11-19 00:59:10 +00003224/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3225/// objc_assign_global (id src, id *dst)
3226///
3227void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003228 llvm::Value *src, llvm::Value *dst,
3229 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003230 const llvm::Type * SrcTy = src->getType();
3231 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003232 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003233 assert(Size <= 8 && "does not support size > 8");
3234 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003235 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003236 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3237 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003238 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3239 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003240 if (!threadlocal)
3241 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3242 src, dst, "globalassign");
3243 else
3244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3245 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003246 return;
3247}
3248
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003249/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003250/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003251///
3252void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003253 llvm::Value *src, llvm::Value *dst,
3254 llvm::Value *ivarOffset) {
3255 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003256 const llvm::Type * SrcTy = src->getType();
3257 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003258 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003259 assert(Size <= 8 && "does not support size > 8");
3260 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003261 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003262 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3263 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003264 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3265 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003266 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3267 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003268 return;
3269}
3270
Fariborz Jahanian58626502008-11-19 00:59:10 +00003271/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3272/// objc_assign_strongCast (id src, id *dst)
3273///
3274void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003275 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003276 const llvm::Type * SrcTy = src->getType();
3277 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003278 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003279 assert(Size <= 8 && "does not support size > 8");
3280 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003282 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3283 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003284 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3285 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003286 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003287 src, dst, "weakassign");
3288 return;
3289}
3290
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003291void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003292 llvm::Value *DestPtr,
3293 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003294 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003295 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3296 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003297 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003298 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003299 return;
3300}
3301
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003302/// EmitObjCValueForIvar - Code Gen for ivar reference.
3303///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003304LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3305 QualType ObjectTy,
3306 llvm::Value *BaseValue,
3307 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003308 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003309 const ObjCInterfaceDecl *ID =
3310 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003311 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3312 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003313}
3314
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003315llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003316 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003317 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003318 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003319 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3321 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003322}
3323
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003324/* *** Private Interface *** */
3325
3326/// EmitImageInfo - Emit the image info marker used to encode some module
3327/// level information.
3328///
3329/// See: <rdr://4810609&4810587&4810587>
3330/// struct IMAGE_INFO {
3331/// unsigned version;
3332/// unsigned flags;
3333/// };
3334enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003335 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336 eImageInfo_GarbageCollected = (1 << 1),
3337 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003338 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3339
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003340 // A flag indicating that the module has no instances of a @synthesize of a
3341 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003342 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003343};
3344
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003345void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003346 unsigned version = 0; // Version is unused?
3347 unsigned flags = 0;
3348
3349 // FIXME: Fix and continue?
3350 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3351 flags |= eImageInfo_GarbageCollected;
3352 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3353 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003355 // We never allow @synthesize of a superclass property.
3356 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003357
Chris Lattner77b89b82010-06-27 07:15:29 +00003358 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3359
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003360 // Emitted as int[2];
3361 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003362 llvm::ConstantInt::get(Int32Ty, version),
3363 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003364 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003365 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003366
3367 const char *Section;
3368 if (ObjCABI == 1)
3369 Section = "__OBJC, __image_info,regular";
3370 else
3371 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003372 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003373 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003374 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003375 Section,
3376 0,
3377 true);
3378 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003379}
3380
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003381
3382// struct objc_module {
3383// unsigned long version;
3384// unsigned long size;
3385// const char *name;
3386// Symtab symtab;
3387// };
3388
3389// FIXME: Get from somewhere
3390static const int ModuleVersion = 7;
3391
3392void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003393 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003395 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003396 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3397 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003398 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003399 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003400 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003401 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003402 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003403 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003404 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003405}
3406
3407llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003408 unsigned NumClasses = DefinedClasses.size();
3409 unsigned NumCategories = DefinedCategories.size();
3410
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003411 // Return null if no symbols were defined.
3412 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003413 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003414
3415 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003416 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003417 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003418 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3419 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003420
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003421 // The runtime expects exactly the list of defined classes followed
3422 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003423 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003424 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003425 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003426 ObjCTypes.Int8PtrTy);
3427 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003428 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003429 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003430 ObjCTypes.Int8PtrTy);
3431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003432 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003433 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003434 NumClasses + NumCategories),
3435 Symbols);
3436
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003437 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003438
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003439 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003440 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3441 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003442 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003444}
3445
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003446llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003447 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003448 LazySymbols.insert(ID->getIdentifier());
3449
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003450 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003451
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003452 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003454 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003455 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003456 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003457 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3458 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003459 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003460 }
3461
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003462 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003463}
3464
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003465llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3466 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003467 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003468
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003469 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003470 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003471 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003472 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003473 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003474 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3475 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003476 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003477 }
3478
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003479 if (lvalue)
3480 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003481 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003482}
3483
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003484llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003485 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003486
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003487 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003488 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003489 llvm::ConstantArray::get(VMContext,
3490 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003491 ((ObjCABI == 2) ?
3492 "__TEXT,__objc_classname,cstring_literals" :
3493 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003494 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003495
Owen Andersona1cf15f2009-07-14 23:10:40 +00003496 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003497}
3498
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003499llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3500 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3501 I = MethodDefinitions.find(MD);
3502 if (I != MethodDefinitions.end())
3503 return I->second;
3504
3505 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3506 // MD isn't emitted yet because it comes from PCH.
3507 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3508 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3509 return MethodDefinitions[MD];
3510 }
3511
3512 return NULL;
3513}
3514
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003515/// GetIvarLayoutName - Returns a unique constant for the given
3516/// ivar layout bitmap.
3517llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003518 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003519 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003520}
3521
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003522void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003523 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003524 bool ForStrongLayout,
3525 bool &HasUnion) {
3526 const RecordDecl *RD = RT->getDecl();
3527 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003528 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003529 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003530 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003531 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003532
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003533 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3534 ForStrongLayout, HasUnion);
3535}
3536
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003537void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003538 const llvm::StructLayout *Layout,
3539 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003540 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003541 unsigned int BytePos, bool ForStrongLayout,
3542 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003543 bool IsUnion = (RD && RD->isUnion());
3544 uint64_t MaxUnionIvarSize = 0;
3545 uint64_t MaxSkippedUnionIvarSize = 0;
3546 FieldDecl *MaxField = 0;
3547 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003548 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003549 uint64_t MaxFieldOffset = 0;
3550 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003551 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003553 if (RecFields.empty())
3554 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003555 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3556 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3557
Chris Lattnerf1690852009-03-31 08:48:01 +00003558 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003559 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003560 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003561 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003562 // Note that 'i' here is actually the field index inside RD of Field,
3563 // although this dependency is hidden.
3564 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckfb67ccd2011-04-14 00:43:09 +00003565 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003566 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003567 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003568
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003569 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003570 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003571 LastFieldBitfieldOrUnnamed = Field;
3572 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003573 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003574 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003575
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003576 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003577 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003578 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003579 if (FQT->isUnionType())
3580 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003581
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003582 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003583 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003584 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003585 continue;
3586 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587
Chris Lattnerf1690852009-03-31 08:48:01 +00003588 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003589 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003590 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003591 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003592 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003593 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003594 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3595 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003596 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003597 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003598 FQT = CArray->getElementType();
3599 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003600
3601 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003602 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003603 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003604 int OldIndex = IvarsInfo.size() - 1;
3605 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003606
Ted Kremenek6217b802009-07-29 21:53:49 +00003607 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003608 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003609 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003610
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003611 // Replicate layout information for each array element. Note that
3612 // one element is already done.
3613 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003614 for (int FirstIndex = IvarsInfo.size() - 1,
3615 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003616 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003617 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3618 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3619 IvarsInfo[i].ivar_size));
3620 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3621 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3622 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003623 }
3624 continue;
3625 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003626 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003627 // At this point, we are done with Record/Union and array there of.
3628 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003629 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003630
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003631 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003632 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3633 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003634 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003635 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003636 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003637 MaxUnionIvarSize = UnionIvarSize;
3638 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003639 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003640 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003641 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003642 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003643 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003644 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003645 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003646 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3647 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003648 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003649 // FIXME: Why the asymmetry? We divide by word size in bits on other
3650 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003651 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003652 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003653 MaxSkippedUnionIvarSize = UnionIvarSize;
3654 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003655 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003656 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003657 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003658 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003659 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003660 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003661 }
3662 }
3663 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003664
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003665 if (LastFieldBitfieldOrUnnamed) {
3666 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3667 // Last field was a bitfield. Must update skip info.
3668 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3669 uint64_t BitFieldSize =
3670 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3671 GC_IVAR skivar;
3672 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3673 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3674 + ((BitFieldSize % ByteSizeInBits) != 0);
3675 SkipIvars.push_back(skivar);
3676 } else {
3677 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3678 // Last field was unnamed. Must update skip info.
3679 unsigned FieldSize
3680 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3681 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3682 FieldSize / ByteSizeInBits));
3683 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003684 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003686 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003688 MaxUnionIvarSize));
3689 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003690 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003691 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003692}
3693
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003694/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3695/// the computations and returning the layout bitmap (for ivar or blocks) in
3696/// the given argument BitMap string container. Routine reads
3697/// two containers, IvarsInfo and SkipIvars which are assumed to be
3698/// filled already by the caller.
3699llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003700 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003701 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003702
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003703 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003704 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003705 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003706 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003707 if (IvarsInfo[0].ivar_bytepos == 0) {
3708 WordsToSkip = 0;
3709 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003710 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003711 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3712 WordsToScan = IvarsInfo[0].ivar_size;
3713 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003714 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003715 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003716 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003717 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003718 // consecutive 'scanned' object pointers.
3719 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003720 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003721 // Skip over 'gc'able object pointer which lay over each other.
3722 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3723 continue;
3724 // Must skip over 1 or more words. We save current skip/scan values
3725 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003726 SKIP_SCAN SkScan;
3727 SkScan.skip = WordsToSkip;
3728 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003729 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003730
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003731 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003732 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3733 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003734 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003735 WordsToSkip = 0;
3736 WordsToScan = IvarsInfo[i].ivar_size;
3737 }
3738 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003739 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003740 SKIP_SCAN SkScan;
3741 SkScan.skip = WordsToSkip;
3742 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003743 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003744 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003745
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003746 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003747 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003749 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003750 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003751 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003752 IvarsInfo[LastIndex].ivar_bytepos +
3753 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003754 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003755 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003756 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003757 SKIP_SCAN SkScan;
3758 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3759 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003760 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003761 }
3762 }
3763 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3764 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003765 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003766 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003767 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3768 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3769 // 0xM0 followed by 0x0N detected.
3770 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3771 for (int j = i+1; j < SkipScan; j++)
3772 SkipScanIvars[j] = SkipScanIvars[j+1];
3773 --SkipScan;
3774 }
3775 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003776
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003777 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003778 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003779 unsigned char byte;
3780 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3781 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3782 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3783 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003784
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003785 // first skip big.
3786 for (unsigned int ix = 0; ix < skip_big; ix++)
3787 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003788
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003789 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003790 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003791 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003792 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003793 byte |= 0xf;
3794 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003795 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003796 byte |= scan_small;
3797 scan_small = 0;
3798 }
3799 BitMap += byte;
3800 }
3801 // next scan big
3802 for (unsigned int ix = 0; ix < scan_big; ix++)
3803 BitMap += (unsigned char)(0x0f);
3804 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003805 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003806 byte = scan_small;
3807 BitMap += byte;
3808 }
3809 }
3810 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003811 unsigned char zero = 0;
3812 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003813
3814 llvm::GlobalVariable * Entry =
3815 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3816 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003817 ((ObjCABI == 2) ?
3818 "__TEXT,__objc_classname,cstring_literals" :
3819 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003820 1, true);
3821 return getConstantGEP(VMContext, Entry, 0, 0);
3822}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003823
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003824/// BuildIvarLayout - Builds ivar layout bitmap for the class
3825/// implementation for the __strong or __weak case.
3826/// The layout map displays which words in ivar list must be skipped
3827/// and which must be scanned by GC (see below). String is built of bytes.
3828/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3829/// of words to skip and right nibble is count of words to scan. So, each
3830/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3831/// represented by a 0x00 byte which also ends the string.
3832/// 1. when ForStrongLayout is true, following ivars are scanned:
3833/// - id, Class
3834/// - object *
3835/// - __strong anything
3836///
3837/// 2. When ForStrongLayout is false, following ivars are scanned:
3838/// - __weak anything
3839///
3840llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3841 const ObjCImplementationDecl *OMD,
3842 bool ForStrongLayout) {
3843 bool hasUnion = false;
3844
3845 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3846 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3847 return llvm::Constant::getNullValue(PtrTy);
3848
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003849 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003850 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003851 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003852
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003853 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003854 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3855 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3856
3857 if (RecFields.empty())
3858 return llvm::Constant::getNullValue(PtrTy);
3859
3860 SkipIvars.clear();
3861 IvarsInfo.clear();
3862
3863 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3864 if (IvarsInfo.empty())
3865 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003866 // Sort on byte position in case we encounterred a union nested in
3867 // the ivar list.
3868 if (hasUnion && !IvarsInfo.empty())
3869 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3870 if (hasUnion && !SkipIvars.empty())
3871 std::sort(SkipIvars.begin(), SkipIvars.end());
3872
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003873 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003874 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003875
3876 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003877 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003878 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003879 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003880 const unsigned char *s = (unsigned char*)BitMap.c_str();
3881 for (unsigned i = 0; i < BitMap.size(); i++)
3882 if (!(s[i] & 0xf0))
3883 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3884 else
3885 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3886 printf("\n");
3887 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003888 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003889}
3890
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003891llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003892 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3893
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003894 // FIXME: Avoid std::string copying.
3895 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003896 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003897 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003898 ((ObjCABI == 2) ?
3899 "__TEXT,__objc_methname,cstring_literals" :
3900 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003901 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003902
Owen Andersona1cf15f2009-07-14 23:10:40 +00003903 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003904}
3905
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003906// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003907llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003908 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3909}
3910
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003911llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003912 std::string TypeStr;
3913 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3914
3915 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003916
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003917 if (!Entry)
3918 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003919 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003920 ((ObjCABI == 2) ?
3921 "__TEXT,__objc_methtype,cstring_literals" :
3922 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003923 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003924
Owen Andersona1cf15f2009-07-14 23:10:40 +00003925 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003926}
3927
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003928llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003929 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003930 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3931 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003932
3933 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3934
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003935 if (!Entry)
3936 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003937 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003938 ((ObjCABI == 2) ?
3939 "__TEXT,__objc_methtype,cstring_literals" :
3940 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003941 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003942
Owen Andersona1cf15f2009-07-14 23:10:40 +00003943 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003944}
3945
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003946// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003947llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003948 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003949
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003950 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003951 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003952 llvm::ConstantArray::get(VMContext,
3953 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003954 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003955 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003956
Owen Andersona1cf15f2009-07-14 23:10:40 +00003957 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003958}
3959
3960// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003961// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003962llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003963CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3964 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003965 std::string TypeStr;
3966 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003967 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3968}
3969
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003970void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003971 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003972 llvm::SmallVectorImpl<char> &Name) {
3973 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003974 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003975 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3976 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003977 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003978 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003979 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003980 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003981}
3982
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003983void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003984 EmitModuleInfo();
3985
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003986 // Emit the dummy bodies for any protocols which were referenced but
3987 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003988 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003989 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3990 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003991 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003992
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003993 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003994 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003995 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003996 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003997 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003998 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003999 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004000 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004001 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004002 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004003 }
4004
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004005 // Add assembler directives to add lazy undefined symbol references
4006 // for classes which are referenced but not defined. This is
4007 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004008 //
4009 // FIXME: It would be nice if we had an LLVM construct for this.
4010 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4011 llvm::SmallString<256> Asm;
4012 Asm += CGM.getModule().getModuleInlineAsm();
4013 if (!Asm.empty() && Asm.back() != '\n')
4014 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004015
Daniel Dunbar33063492009-09-07 00:20:42 +00004016 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004017 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4018 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004019 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4020 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004021 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004022 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004023 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004024 }
4025
4026 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4027 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4028 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4029 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004030
Daniel Dunbar33063492009-09-07 00:20:42 +00004031 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004032 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004033}
4034
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004035CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004036 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004037 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004038 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004039 ObjCABI = 2;
4040}
4041
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004042/* *** */
4043
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004044ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004045 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004046 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4047 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004048
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004049 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004050 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004051 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004052 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004053 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004054
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004055 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004056 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004057 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004058
Mike Stumpf5408fe2009-05-16 07:57:57 +00004059 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4060 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004061 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004062 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004063
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004064 // I'm not sure I like this. The implicit coordination is a bit
4065 // gross. We should solve this in a reasonable fashion because this
4066 // is a pretty common task (match some runtime data structure with
4067 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004068
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004069 // FIXME: This is leaked.
4070 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004071
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004072 // struct _objc_super {
4073 // id self;
4074 // Class cls;
4075 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004076 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004077 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004078 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004079 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004080 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004081 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004082 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004083 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004084 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004085
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004086 SuperCTy = Ctx.getTagDeclType(RD);
4087 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004088
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004089 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004090 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4091
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004092 // struct _prop_t {
4093 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004094 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004095 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004096 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004097 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004098 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004099
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004100 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004101 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004102 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004103 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004104 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004105 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004106 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004107 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004108 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004109 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004110 PropertyListTy);
4111 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004112 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004113
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004114 // struct _objc_method {
4115 // SEL _cmd;
4116 // char *method_type;
4117 // char *_imp;
4118 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004119 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004120 Int8PtrTy,
4121 Int8PtrTy,
4122 NULL);
4123 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004125 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004126 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004127 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004128 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004129}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004130
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004132 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004133 // struct _objc_method_description {
4134 // SEL name;
4135 // char *types;
4136 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004137 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004138 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004139 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004140 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004142 MethodDescriptionTy);
4143
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004144 // struct _objc_method_description_list {
4145 // int count;
4146 // struct _objc_method_description[1];
4147 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004148 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004149 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004150 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004151 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004153 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004154
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004155 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004156 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004157 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004158
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004159 // Protocol description structures
4160
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004161 // struct _objc_protocol_extension {
4162 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4163 // struct _objc_method_description_list *optional_instance_methods;
4164 // struct _objc_method_description_list *optional_class_methods;
4165 // struct _objc_property_list *instance_properties;
4166 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004168 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004169 MethodDescriptionListPtrTy,
4170 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004171 PropertyListPtrTy,
4172 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004174 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004175
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004176 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004177 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004178
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004179 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004180
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004181 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4182 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004183
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004184 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004185 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004186 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004187 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004188 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004189 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004190 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4191
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004192 // struct _objc_protocol {
4193 // struct _objc_protocol_extension *isa;
4194 // char *protocol_name;
4195 // struct _objc_protocol **_objc_protocol_list;
4196 // struct _objc_method_description_list *instance_methods;
4197 // struct _objc_method_description_list *class_methods;
4198 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004199 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004200 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004202 MethodDescriptionListPtrTy,
4203 MethodDescriptionListPtrTy,
4204 NULL);
4205 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4206
4207 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004208 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004209 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004210 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004211 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004212
4213 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004214 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004215 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004216
4217 // Class description structures
4218
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004219 // struct _objc_ivar {
4220 // char *ivar_name;
4221 // char *ivar_type;
4222 // int ivar_offset;
4223 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004224 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225 Int8PtrTy,
4226 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004227 NULL);
4228 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4229
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004230 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004231 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004232 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004233 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004234
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004235 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004236 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004237 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004238 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004239
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004240 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004241 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004242 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004243 Int8PtrTy,
4244 PropertyListPtrTy,
4245 NULL);
4246 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004247 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004248
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004249 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004250
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004251 // struct _objc_class {
4252 // Class isa;
4253 // Class super_class;
4254 // char *name;
4255 // long version;
4256 // long info;
4257 // long instance_size;
4258 // struct _objc_ivar_list *ivars;
4259 // struct _objc_method_list *methods;
4260 // struct _objc_cache *cache;
4261 // struct _objc_protocol_list *protocols;
4262 // char *ivar_layout;
4263 // struct _objc_class_ext *ext;
4264 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004265 T = llvm::StructType::get(VMContext,
4266 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004267 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004268 Int8PtrTy,
4269 LongTy,
4270 LongTy,
4271 LongTy,
4272 IvarListPtrTy,
4273 MethodListPtrTy,
4274 CachePtrTy,
4275 ProtocolListPtrTy,
4276 Int8PtrTy,
4277 ClassExtensionPtrTy,
4278 NULL);
4279 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004280
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004281 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4282 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004283 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004284
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004285 // struct _objc_category {
4286 // char *category_name;
4287 // char *class_name;
4288 // struct _objc_method_list *instance_method;
4289 // struct _objc_method_list *class_method;
4290 // uint32_t size; // sizeof(struct _objc_category)
4291 // struct _objc_property_list *instance_properties;// category's @property
4292 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004293 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004294 Int8PtrTy,
4295 MethodListPtrTy,
4296 MethodListPtrTy,
4297 ProtocolListPtrTy,
4298 IntTy,
4299 PropertyListPtrTy,
4300 NULL);
4301 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4302
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004303 // Global metadata structures
4304
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004305 // struct _objc_symtab {
4306 // long sel_ref_cnt;
4307 // SEL *refs;
4308 // short cls_def_cnt;
4309 // short cat_def_cnt;
4310 // char *defs[cls_def_cnt + cat_def_cnt];
4311 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004312 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004313 SelectorPtrTy,
4314 ShortTy,
4315 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004316 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004317 NULL);
4318 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004319 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004320
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004321 // struct _objc_module {
4322 // long version;
4323 // long size; // sizeof(struct _objc_module)
4324 // char *name;
4325 // struct _objc_symtab* symtab;
4326 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004327 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004328 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004329 LongTy,
4330 Int8PtrTy,
4331 SymtabPtrTy,
4332 NULL);
4333 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004334
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004335
Mike Stumpf5408fe2009-05-16 07:57:57 +00004336 // FIXME: This is the size of the setjmp buffer and should be target
4337 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004338 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004339
Anders Carlsson124526b2008-09-09 10:10:21 +00004340 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004341 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004342 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004343
4344 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004345 llvm::StructType::get(VMContext,
4346 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4347 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004348 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004349 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004350 ExceptionDataTy);
4351
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004352}
4353
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004354ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004355 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004356 // struct _method_list_t {
4357 // uint32_t entsize; // sizeof(struct _objc_method)
4358 // uint32_t method_count;
4359 // struct _objc_method method_list[method_count];
4360 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004361 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004362 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004363 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004364 NULL);
4365 CGM.getModule().addTypeName("struct.__method_list_t",
4366 MethodListnfABITy);
4367 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004368 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004370 // struct _protocol_t {
4371 // id isa; // NULL
4372 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004373 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004374 // const struct method_list_t * const instance_methods;
4375 // const struct method_list_t * const class_methods;
4376 // const struct method_list_t *optionalInstanceMethods;
4377 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004378 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004379 // const uint32_t size; // sizeof(struct _protocol_t)
4380 // const uint32_t flags; // = 0
4381 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004383 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004384 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Owen Anderson47a434f2009-08-05 23:18:46 +00004386 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004387 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004388 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004389 ProtocolListTyHolder),
4390 MethodListnfABIPtrTy,
4391 MethodListnfABIPtrTy,
4392 MethodListnfABIPtrTy,
4393 MethodListnfABIPtrTy,
4394 PropertyListPtrTy,
4395 IntTy,
4396 IntTy,
4397 NULL);
4398 CGM.getModule().addTypeName("struct._protocol_t",
4399 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004400
4401 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004402 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403
Fariborz Jahanianda320092009-01-29 19:24:30 +00004404 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004405 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004406 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004407 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004408 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004409 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004410 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004411 NULL);
4412 CGM.getModule().addTypeName("struct._objc_protocol_list",
4413 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004414 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004415 ProtocolListnfABITy);
4416
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004417 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004418 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004419
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004420 // struct _ivar_t {
4421 // unsigned long int *offset; // pointer to ivar offset location
4422 // char *name;
4423 // char *type;
4424 // uint32_t alignment;
4425 // uint32_t size;
4426 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004427 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004428 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004429 Int8PtrTy,
4430 Int8PtrTy,
4431 IntTy,
4432 IntTy,
4433 NULL);
4434 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004435
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004436 // struct _ivar_list_t {
4437 // uint32 entsize; // sizeof(struct _ivar_t)
4438 // uint32 count;
4439 // struct _iver_t list[count];
4440 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004441 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004442 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004443 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004445 NULL);
4446 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004447
Owen Anderson96e0fc72009-07-29 22:16:19 +00004448 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004449
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004450 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004451 // uint32_t const flags;
4452 // uint32_t const instanceStart;
4453 // uint32_t const instanceSize;
4454 // uint32_t const reserved; // only when building for 64bit targets
4455 // const uint8_t * const ivarLayout;
4456 // const char *const name;
4457 // const struct _method_list_t * const baseMethods;
4458 // const struct _objc_protocol_list *const baseProtocols;
4459 // const struct _ivar_list_t *const ivars;
4460 // const uint8_t * const weakIvarLayout;
4461 // const struct _prop_list_t * const properties;
4462 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004463
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004464 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004465 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004466 IntTy,
4467 IntTy,
4468 Int8PtrTy,
4469 Int8PtrTy,
4470 MethodListnfABIPtrTy,
4471 ProtocolListnfABIPtrTy,
4472 IvarListnfABIPtrTy,
4473 Int8PtrTy,
4474 PropertyListPtrTy,
4475 NULL);
4476 CGM.getModule().addTypeName("struct._class_ro_t",
4477 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004478
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004479 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4480 std::vector<const llvm::Type*> Params;
4481 Params.push_back(ObjectPtrTy);
4482 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004483 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004484 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4485
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004486 // struct _class_t {
4487 // struct _class_t *isa;
4488 // struct _class_t * const superclass;
4489 // void *cache;
4490 // IMP *vtable;
4491 // struct class_ro_t *ro;
4492 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004493
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004494 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004495 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004496 llvm::StructType::get(VMContext,
4497 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498 llvm::PointerType::getUnqual(ClassTyHolder),
4499 CachePtrTy,
4500 llvm::PointerType::getUnqual(ImpnfABITy),
4501 llvm::PointerType::getUnqual(ClassRonfABITy),
4502 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004503 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4504
4505 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004506 ClassnfABITy);
4507
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004508 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004509 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004510
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004511 // struct _category_t {
4512 // const char * const name;
4513 // struct _class_t *const cls;
4514 // const struct _method_list_t * const instance_methods;
4515 // const struct _method_list_t * const class_methods;
4516 // const struct _protocol_list_t * const protocols;
4517 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004518 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004519 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004520 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004521 MethodListnfABIPtrTy,
4522 MethodListnfABIPtrTy,
4523 ProtocolListnfABIPtrTy,
4524 PropertyListPtrTy,
4525 NULL);
4526 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004527
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004528 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004529 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4530 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004532 // MessageRefTy - LLVM for:
4533 // struct _message_ref_t {
4534 // IMP messenger;
4535 // SEL name;
4536 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004537
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004538 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004539 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004540 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004541 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004542 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004543 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004544 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004545 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004546 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004547 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004548
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004549 MessageRefCTy = Ctx.getTagDeclType(RD);
4550 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4551 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004553 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004554 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004555
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004556 // SuperMessageRefTy - LLVM for:
4557 // struct _super_message_ref_t {
4558 // SUPER_IMP messenger;
4559 // SEL name;
4560 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004561 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004562 SelectorPtrTy,
4563 NULL);
4564 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004565
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004566 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004567 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4568
Daniel Dunbare588b992009-03-01 04:46:24 +00004569
4570 // struct objc_typeinfo {
4571 // const void** vtable; // objc_ehtype_vtable + 2
4572 // const char* name; // c++ typeinfo string
4573 // Class cls;
4574 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004575 EHTypeTy = llvm::StructType::get(VMContext,
4576 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004577 Int8PtrTy,
4578 ClassnfABIPtrTy,
4579 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004580 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004581 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004582}
4583
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004585 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004586
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004587 return NULL;
4588}
4589
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004590void CGObjCNonFragileABIMac::AddModuleClassList(const
4591 std::vector<llvm::GlobalValue*>
4592 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004593 const char *SymbolName,
4594 const char *SectionName) {
4595 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596
Daniel Dunbar463b8762009-05-15 21:48:48 +00004597 if (!NumClasses)
4598 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004599
Daniel Dunbar463b8762009-05-15 21:48:48 +00004600 std::vector<llvm::Constant*> Symbols(NumClasses);
4601 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004602 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004603 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004605 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004606 NumClasses),
4607 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Daniel Dunbar463b8762009-05-15 21:48:48 +00004609 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004610 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004611 llvm::GlobalValue::InternalLinkage,
4612 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004613 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004614 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004615 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004616 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004617}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004619void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4620 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621
Daniel Dunbar463b8762009-05-15 21:48:48 +00004622 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004623 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004624 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004625 "\01L_OBJC_LABEL_CLASS_$",
4626 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004627
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004628 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4629 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4630 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4631 continue;
4632 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004633 }
4634
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004635 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4636 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4637 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4638 continue;
4639 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4640 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004641
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004642 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004643 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4644 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004645
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004646 // Build list of all implemented category addresses in array
4647 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004648 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004649 "\01L_OBJC_LABEL_CATEGORY_$",
4650 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004652 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4653 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004654
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004655 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004656}
4657
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004658/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004659/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004660/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004661/// message dispatch call for all the rest.
4662///
4663bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004664 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4665 default:
4666 assert(0 && "Invalid dispatch method!");
4667 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004668 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004669 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004670 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004671 case CodeGenOptions::Mixed:
4672 break;
4673 }
4674
4675 // If so, see whether this selector is in the white-list of things which must
4676 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004677 if (NonLegacyDispatchMethods.empty()) {
4678 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4679 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4680 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4681 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4682 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4683 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4684 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4685 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4686 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4687 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004688
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004689 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4690 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4691 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4692 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4693 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4694 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4695 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4696 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004697 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004698 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004699 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4700 &CGM.getContext().Idents.get("objects"),
4701 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004702 };
4703 NonLegacyDispatchMethods.insert(
4704 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004705 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004706
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004707 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004708}
4709
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004710// Metadata flags
4711enum MetaDataDlags {
4712 CLS = 0x0,
4713 CLS_META = 0x1,
4714 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004715 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004716 CLS_EXCEPTION = 0x20
4717};
4718/// BuildClassRoTInitializer - generate meta-data for:
4719/// struct _class_ro_t {
4720/// uint32_t const flags;
4721/// uint32_t const instanceStart;
4722/// uint32_t const instanceSize;
4723/// uint32_t const reserved; // only when building for 64bit targets
4724/// const uint8_t * const ivarLayout;
4725/// const char *const name;
4726/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004727/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004728/// const struct _ivar_list_t *const ivars;
4729/// const uint8_t * const weakIvarLayout;
4730/// const struct _prop_list_t * const properties;
4731/// }
4732///
4733llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004734 unsigned flags,
4735 unsigned InstanceStart,
4736 unsigned InstanceSize,
4737 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004738 std::string ClassName = ID->getNameAsString();
4739 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004740 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4741 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4742 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004743 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004744 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4745 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004746 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004747 // const struct _method_list_t * const baseMethods;
4748 std::vector<llvm::Constant*> Methods;
4749 std::string MethodListName("\01l_OBJC_$_");
4750 if (flags & CLS_META) {
4751 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004752 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004753 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004754 // Class methods should always be defined.
4755 Methods.push_back(GetMethodConstant(*i));
4756 }
4757 } else {
4758 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004759 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004760 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004761 // Instance methods should always be defined.
4762 Methods.push_back(GetMethodConstant(*i));
4763 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004765 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004766 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004767
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004768 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4769 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004771 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4772 if (llvm::Constant *C = GetMethodConstant(MD))
4773 Methods.push_back(C);
4774 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4775 if (llvm::Constant *C = GetMethodConstant(MD))
4776 Methods.push_back(C);
4777 }
4778 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004779 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004780 Values[ 5] = EmitMethodList(MethodListName,
4781 "__DATA, __objc_const", Methods);
4782
Fariborz Jahanianda320092009-01-29 19:24:30 +00004783 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4784 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004786 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004787 OID->all_referenced_protocol_begin(),
4788 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004789
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004790 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004791 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004792 else
4793 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4795 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004796 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004797 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004798 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004799 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4800 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004801 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004802 Values);
4803 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004804 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4805 llvm::GlobalValue::InternalLinkage,
4806 Init,
4807 (flags & CLS_META) ?
4808 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4809 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004810 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004811 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004812 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004813 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004814
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004815}
4816
4817/// BuildClassMetaData - This routine defines that to-level meta-data
4818/// for the given ClassName for:
4819/// struct _class_t {
4820/// struct _class_t *isa;
4821/// struct _class_t * const superclass;
4822/// void *cache;
4823/// IMP *vtable;
4824/// struct class_ro_t *ro;
4825/// }
4826///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004827llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004828 std::string &ClassName,
4829 llvm::Constant *IsAGV,
4830 llvm::Constant *SuperClassGV,
4831 llvm::Constant *ClassRoGV,
4832 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004833 std::vector<llvm::Constant*> Values(5);
4834 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004835 Values[1] = SuperClassGV;
4836 if (!Values[1])
4837 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004838 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4839 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4840 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004841 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004842 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004843 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4844 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004845 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004846 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004847 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004848 if (HiddenVisibility)
4849 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004850 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004851}
4852
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004854CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004855 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004856}
4857
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004858void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004859 uint32_t &InstanceStart,
4860 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004861 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004862 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004863
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004864 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004865 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004866
4867 // If there are no fields, the start is the same as the end.
4868 if (!RL.getFieldCount())
4869 InstanceStart = InstanceSize;
4870 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004871 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004872}
4873
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004874void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4875 std::string ClassName = ID->getNameAsString();
4876 if (!ObjCEmptyCacheVar) {
4877 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878 CGM.getModule(),
4879 ObjCTypes.CacheTy,
4880 false,
4881 llvm::GlobalValue::ExternalLinkage,
4882 0,
4883 "_objc_empty_cache");
4884
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004885 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004886 CGM.getModule(),
4887 ObjCTypes.ImpnfABITy,
4888 false,
4889 llvm::GlobalValue::ExternalLinkage,
4890 0,
4891 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004892 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004893 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004894 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004895 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004896 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004897 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004898 uint32_t InstanceSize = InstanceStart;
4899 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004900 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4901 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004902
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004903 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004904
4905 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004906 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004907 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004908 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004909 if (ID->getNumIvarInitializers())
4910 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004911 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004912 // class is root
4913 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004914 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004915 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004916 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004917 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004918 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4919 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4920 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004921 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004922 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004923 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004924 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004925 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004926 ObjCMetaClassName +
4927 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004928 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004929 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004930 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004931 }
4932 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4933 InstanceStart,
4934 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004935 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004936 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004937 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4938 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004939 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004940
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004941 // Metadata for the class
4942 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004943 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004944 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004945 if (ID->getNumIvarInitializers())
4946 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004947
Douglas Gregor68584ed2009-06-18 16:11:24 +00004948 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004949 flags |= CLS_EXCEPTION;
4950
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004951 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004952 flags |= CLS_ROOT;
4953 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004954 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004955 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004956 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004957 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004958 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004959 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004960 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004961 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004962 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004963 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004964 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004965 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004966 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004967
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004968 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004969 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004970 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4971 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004972 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004973
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004974 // Determine if this class is also "non-lazy".
4975 if (ImplementationIsNonLazy(ID))
4976 DefinedNonLazyClasses.push_back(ClassMD);
4977
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004978 // Force the definition of the EHType if necessary.
4979 if (flags & CLS_EXCEPTION)
4980 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004981}
4982
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004983/// GenerateProtocolRef - This routine is called to generate code for
4984/// a protocol reference expression; as in:
4985/// @code
4986/// @protocol(Proto1);
4987/// @endcode
4988/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4989/// which will hold address of the protocol meta-data.
4990///
4991llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004992 const ObjCProtocolDecl *PD) {
4993
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004994 // This routine is called for @protocol only. So, we must build definition
4995 // of protocol's meta-data (not a reference to it!)
4996 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004997 llvm::Constant *Init =
4998 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4999 ObjCTypes.ExternalProtocolPtrTy);
5000
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005001 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005002 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005003
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005004 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5005 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005006 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005007 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 CGM.getModule(),
5009 Init->getType(), false,
5010 llvm::GlobalValue::WeakAnyLinkage,
5011 Init,
5012 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005013 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5014 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005015 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005016 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005017}
5018
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005019/// GenerateCategory - Build metadata for a category implementation.
5020/// struct _category_t {
5021/// const char * const name;
5022/// struct _class_t *const cls;
5023/// const struct _method_list_t * const instance_methods;
5024/// const struct _method_list_t * const class_methods;
5025/// const struct _protocol_list_t * const protocols;
5026/// const struct _prop_list_t * const properties;
5027/// }
5028///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005029void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005030 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005031 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005032 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5033 "_$_" + OCD->getNameAsString());
5034 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005035 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005037 std::vector<llvm::Constant*> Values(6);
5038 Values[0] = GetClassName(OCD->getIdentifier());
5039 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005040 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005041 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005042 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5043
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005044 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005045 std::vector<llvm::Constant*> Methods;
5046 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005047 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005048 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005049
5050 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005051 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005052 // Instance methods should always be defined.
5053 Methods.push_back(GetMethodConstant(*i));
5054 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055
5056 Values[2] = EmitMethodList(MethodListName,
5057 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005058 Methods);
5059
5060 MethodListName = Prefix;
5061 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5062 OCD->getNameAsString();
5063 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005064 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005065 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005066 // Class methods should always be defined.
5067 Methods.push_back(GetMethodConstant(*i));
5068 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005069
5070 Values[3] = EmitMethodList(MethodListName,
5071 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005072 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005073 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005074 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005075 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005076 llvm::SmallString<256> ExtName;
5077 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5078 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005079 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005080 + Interface->getName() + "_$_"
5081 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005082 Category->protocol_begin(),
5083 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005084 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5085 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005086 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005087 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5088 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005089 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005090
5091 llvm::Constant *Init =
5092 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005093 Values);
5094 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005095 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005096 false,
5097 llvm::GlobalValue::InternalLinkage,
5098 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005099 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005100 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005101 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005102 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005103 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005104 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005105
5106 // Determine if this category is also "non-lazy".
5107 if (ImplementationIsNonLazy(OCD))
5108 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005109}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005110
5111/// GetMethodConstant - Return a struct objc_method constant for the
5112/// given method if it has been defined. The result is null if the
5113/// method has not been defined. The return value has type MethodPtrTy.
5114llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005115 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005116 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005117 if (!Fn)
5118 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005119
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005120 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005121 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005122 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005123 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005124 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005125 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005126 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005127}
5128
5129/// EmitMethodList - Build meta-data for method declarations
5130/// struct _method_list_t {
5131/// uint32_t entsize; // sizeof(struct _objc_method)
5132/// uint32_t method_count;
5133/// struct _objc_method method_list[method_count];
5134/// }
5135///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005136llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5137 const char *Section,
5138 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005139 // Return null for empty list.
5140 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005141 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005142
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005143 std::vector<llvm::Constant*> Values(3);
5144 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005145 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005146 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005147 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005148 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005149 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005150 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005151 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005152 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005153
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005154 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005155 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005156 llvm::GlobalValue::InternalLinkage,
5157 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005158 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005159 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005160 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005161 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005162 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005163 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005164 ObjCTypes.MethodListnfABIPtrTy);
5165}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005166
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005167/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5168/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005169llvm::GlobalVariable *
5170CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5171 const ObjCIvarDecl *Ivar) {
5172 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005173 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005174 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005175 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005176 CGM.getModule().getGlobalVariable(Name);
5177 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005178 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005179 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005180 false,
5181 llvm::GlobalValue::ExternalLinkage,
5182 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005183 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005184 return IvarOffsetGV;
5185}
5186
Daniel Dunbare83be122010-04-02 21:14:02 +00005187llvm::Constant *
5188CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5189 const ObjCIvarDecl *Ivar,
5190 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005191 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005193 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005194 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005195 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005196
Mike Stumpf5408fe2009-05-16 07:57:57 +00005197 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5198 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005199 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5200 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005201 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005202 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005203 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005204 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005205 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005206 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005207}
5208
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005209/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005210/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005211/// IvarListnfABIPtrTy.
5212/// struct _ivar_t {
5213/// unsigned long int *offset; // pointer to ivar offset location
5214/// char *name;
5215/// char *type;
5216/// uint32_t alignment;
5217/// uint32_t size;
5218/// }
5219/// struct _ivar_list_t {
5220/// uint32 entsize; // sizeof(struct _ivar_t)
5221/// uint32 count;
5222/// struct _iver_t list[count];
5223/// }
5224///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005225
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005226llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005227 const ObjCImplementationDecl *ID) {
5228
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005229 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005231 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5232 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005233
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005234 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005235
Daniel Dunbar91636d62009-04-20 00:33:43 +00005236 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005237 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005238 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005239
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005240 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5241 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005242 // Ignore unnamed bit-fields.
5243 if (!IVD->getDeclName())
5244 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005245 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005246 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005247 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5248 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005249 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005250 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005251 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005252 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005254 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005255 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005256 // NOTE. Size of a bitfield does not match gcc's, because of the
5257 // way bitfields are treated special in each. But I am told that
5258 // 'size' for bitfield ivars is ignored by the runtime so it does
5259 // not matter. If it matters, there is enough info to get the
5260 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005261 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005262 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005263 }
5264 // Return null for empty list.
5265 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005266 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005267 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005268 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005269 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5270 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005271 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005272 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005273 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005274 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005275 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5276 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005277 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005278 llvm::GlobalValue::InternalLinkage,
5279 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005280 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005281 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005282 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005283 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005284
Chris Lattnerad64e022009-07-17 23:57:13 +00005285 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005286 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005287}
5288
5289llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005290 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005291 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005292
Fariborz Jahanianda320092009-01-29 19:24:30 +00005293 if (!Entry) {
5294 // We use the initializer as a marker of whether this is a forward
5295 // reference or not. At module finalization we add the empty
5296 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297 Entry =
5298 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5299 llvm::GlobalValue::ExternalLinkage,
5300 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005301 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005302 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005303 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304
Fariborz Jahanianda320092009-01-29 19:24:30 +00005305 return Entry;
5306}
5307
5308/// GetOrEmitProtocol - Generate the protocol meta-data:
5309/// @code
5310/// struct _protocol_t {
5311/// id isa; // NULL
5312/// const char * const protocol_name;
5313/// const struct _protocol_list_t * protocol_list; // super protocols
5314/// const struct method_list_t * const instance_methods;
5315/// const struct method_list_t * const class_methods;
5316/// const struct method_list_t *optionalInstanceMethods;
5317/// const struct method_list_t *optionalClassMethods;
5318/// const struct _prop_list_t * properties;
5319/// const uint32_t size; // sizeof(struct _protocol_t)
5320/// const uint32_t flags; // = 0
5321/// }
5322/// @endcode
5323///
5324
5325llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005326 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005327 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005328
Fariborz Jahanianda320092009-01-29 19:24:30 +00005329 // Early exit if a defining object has already been generated.
5330 if (Entry && Entry->hasInitializer())
5331 return Entry;
5332
Fariborz Jahanianda320092009-01-29 19:24:30 +00005333 // Construct method lists.
5334 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5335 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005336 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005337 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005338 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005339 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005340 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5341 OptInstanceMethods.push_back(C);
5342 } else {
5343 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005344 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005345 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005346
5347 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005348 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005349 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005350 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005351 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5352 OptClassMethods.push_back(C);
5353 } else {
5354 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005355 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005356 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357
Fariborz Jahanianda320092009-01-29 19:24:30 +00005358 std::vector<llvm::Constant*> Values(10);
5359 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005360 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005361 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005362 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5363 PD->protocol_begin(),
5364 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005366 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005367 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005368 "__DATA, __objc_const",
5369 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005370 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005371 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005372 "__DATA, __objc_const",
5373 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005374 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005375 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376 "__DATA, __objc_const",
5377 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005379 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005380 "__DATA, __objc_const",
5381 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005382 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005383 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005384 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005385 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005386 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005387 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005388 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005389 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005390
Fariborz Jahanianda320092009-01-29 19:24:30 +00005391 if (Entry) {
5392 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005393 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005394 Entry->setInitializer(Init);
5395 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005396 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005397 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5398 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5399 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005400 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005401 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005402 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005403 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005404 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005405 CGM.AddUsedGlobal(Entry);
5406
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005407 // Use this protocol meta-data to build protocol list table in section
5408 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005409 llvm::GlobalVariable *PTGV =
5410 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5411 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5412 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005413 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005414 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005415 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005416 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005417 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005418 return Entry;
5419}
5420
5421/// EmitProtocolList - Generate protocol list meta-data:
5422/// @code
5423/// struct _protocol_list_t {
5424/// long protocol_count; // Note, this is 32/64 bit
5425/// struct _protocol_t[protocol_count];
5426/// }
5427/// @endcode
5428///
5429llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005430CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5431 ObjCProtocolDecl::protocol_iterator begin,
5432 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005433 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005434
Fariborz Jahanianda320092009-01-29 19:24:30 +00005435 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005436 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005437 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005438
Daniel Dunbar948e2582009-02-15 07:36:20 +00005439 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005440 llvm::SmallString<256> TmpName;
5441 Name.toVector(TmpName);
5442 llvm::GlobalVariable *GV =
5443 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005444 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005445 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Daniel Dunbar948e2582009-02-15 07:36:20 +00005447 for (; begin != end; ++begin)
5448 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5449
Fariborz Jahanianda320092009-01-29 19:24:30 +00005450 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005451 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005452 ObjCTypes.ProtocolnfABIPtrTy));
5453
Fariborz Jahanianda320092009-01-29 19:24:30 +00005454 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005455 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005456 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005457 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005458 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005459 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005460 ProtocolRefs.size()),
5461 ProtocolRefs);
5462
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005463 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005464 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 llvm::GlobalValue::InternalLinkage,
5466 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005467 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005468 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005469 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005470 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005471 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005472 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005473 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005474}
5475
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005476/// GetMethodDescriptionConstant - This routine build following meta-data:
5477/// struct _objc_method {
5478/// SEL _cmd;
5479/// char *method_type;
5480/// char *_imp;
5481/// }
5482
5483llvm::Constant *
5484CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5485 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005486 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005487 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5488 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005489 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005490 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005491 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005492 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005493}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005494
5495/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5496/// This code gen. amounts to generating code for:
5497/// @code
5498/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5499/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005500///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005501LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005502 CodeGen::CodeGenFunction &CGF,
5503 QualType ObjectTy,
5504 llvm::Value *BaseValue,
5505 const ObjCIvarDecl *Ivar,
5506 unsigned CVRQualifiers) {
5507 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005508 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5509 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005510}
5511
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005512llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005513 CodeGen::CodeGenFunction &CGF,
5514 const ObjCInterfaceDecl *Interface,
5515 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005516 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005517}
5518
Fariborz Jahanian46551122009-02-04 00:22:57 +00005519CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005520 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005521 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005522 QualType ResultType,
5523 Selector Sel,
5524 llvm::Value *Receiver,
5525 QualType Arg0Ty,
5526 bool IsSuper,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005527 const CallArgList &CallArgs,
5528 const ObjCMethodDecl *Method) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005529 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5530 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005531 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005532 llvm::Value *Arg0 = Receiver;
5533 if (!IsSuper)
5534 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005536 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005537 // FIXME. This is too much work to get the ABI-specific result type needed to
5538 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005539 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005540 = Types.getFunctionInfo(ResultType, CallArgList(),
5541 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005542 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005543 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005544 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall448d2cd2011-02-26 09:48:59 +00005545 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner9b7d6702010-08-18 16:09:06 +00005546 if (IsSuper) {
5547 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5548 Name += "objc_msgSendSuper2_stret_fixup";
5549 } else {
5550 Fn = ObjCTypes.getMessageSendStretFixupFn();
5551 Name += "objc_msgSend_stret_fixup";
5552 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005553 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5554 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5555 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005556 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005557 if (IsSuper) {
5558 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5559 Name += "objc_msgSendSuper2_fixup";
5560 } else {
5561 Fn = ObjCTypes.getMessageSendFixupFn();
5562 Name += "objc_msgSend_fixup";
5563 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005564 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005565 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005566 Name += '_';
5567 std::string SelName(Sel.getAsString());
5568 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005569 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005570 if (SelName[i] == ':')
5571 SelName[i] = '_';
5572 Name += SelName;
5573 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5574 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005575 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005576 std::vector<llvm::Constant*> Values(2);
5577 Values[0] = Fn;
5578 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005579 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005580 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005581 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005582 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005583 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005584 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005585 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005586 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005587 }
5588 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005590 CallArgList ActualArgs;
5591 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005592 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005593 ObjCTypes.MessageRefCPtrTy));
5594 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005595 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005596 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005597 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5598 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005599 const llvm::FunctionType *FTy =
5600 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005601 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005602 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005603 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005604}
5605
5606/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005607CodeGen::RValue
5608CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005609 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005610 QualType ResultType,
5611 Selector Sel,
5612 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005613 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005614 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005615 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005616 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005617 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5618 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005619 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005620 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005621 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005622 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005623 false, CallArgs, Method);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005624}
5625
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005626llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005627CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005628 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5629
Daniel Dunbardfff2302009-03-02 05:18:14 +00005630 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005631 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005632 false, llvm::GlobalValue::ExternalLinkage,
5633 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005634 }
5635
5636 return GV;
5637}
5638
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005639llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5640 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005641 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005642
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005643 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005644 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005645 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005646 Entry =
5647 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005648 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005649 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005650 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005651 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005652 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005653 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005654 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005655 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005656 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005658 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005659}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005660
Daniel Dunbar11394522009-04-18 08:51:00 +00005661llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005662CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005663 const ObjCInterfaceDecl *ID) {
5664 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005665
Daniel Dunbar11394522009-04-18 08:51:00 +00005666 if (!Entry) {
5667 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5668 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005669 Entry =
5670 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005671 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005672 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005673 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005674 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005675 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005676 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005677 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005678 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005679 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005680
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005681 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005682}
5683
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005684/// EmitMetaClassRef - Return a Value * of the address of _class_t
5685/// meta-data
5686///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005687llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5688 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005689 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5690 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005691 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005693 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005694 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005695 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005696 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005697 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005698 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005699 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005700 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005701 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005702 ObjCTypes.ClassnfABIPtrTy));
5703
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005704 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005705 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005707 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005708}
5709
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005710/// GetClass - Return a reference to the class for the given interface
5711/// decl.
5712llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5713 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005714 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005715 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5716 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5717 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5718 }
5719
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005720 return EmitClassRef(Builder, ID);
5721}
5722
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005723/// Generates a message send where the super is the receiver. This is
5724/// a message send to self with special delivery semantics indicating
5725/// which class's method should be called.
5726CodeGen::RValue
5727CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005728 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005729 QualType ResultType,
5730 Selector Sel,
5731 const ObjCInterfaceDecl *Class,
5732 bool isCategoryImpl,
5733 llvm::Value *Receiver,
5734 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005735 const CodeGen::CallArgList &CallArgs,
5736 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005737 // ...
5738 // Create and init a super structure; this is a (receiver, class)
5739 // pair we will pass to objc_msgSendSuper.
5740 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005741 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005742
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005743 llvm::Value *ReceiverAsObject =
5744 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5745 CGF.Builder.CreateStore(ReceiverAsObject,
5746 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005747
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005748 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005749 llvm::Value *Target;
5750 if (IsClassMessage) {
5751 if (isCategoryImpl) {
5752 // Message sent to "super' in a class method defined in
5753 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005754 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005755 Target = CGF.Builder.CreateStructGEP(Target, 0);
5756 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005757 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005758 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005759 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005760 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005761
Mike Stumpf5408fe2009-05-16 07:57:57 +00005762 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5763 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005764 const llvm::Type *ClassTy =
5765 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5766 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5767 CGF.Builder.CreateStore(Target,
5768 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005769
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005770 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005771 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5772 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005773 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005774 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005775 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005776 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00005777 true, CallArgs, Method);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005778}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005779
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005780llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005781 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005782 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005783
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005784 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005785 llvm::Constant *Casted =
5786 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5787 ObjCTypes.SelectorPtrTy);
5788 Entry =
5789 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5790 llvm::GlobalValue::InternalLinkage,
5791 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005792 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005793 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005794 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005795
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005796 if (lval)
5797 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005798 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005799}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005800/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005801/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005802///
5803void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005804 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005805 llvm::Value *dst,
5806 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005807 const llvm::Type * SrcTy = src->getType();
5808 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005809 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005810 assert(Size <= 8 && "does not support size > 8");
5811 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5812 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005813 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5814 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005815 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5816 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005817 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5818 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005819 return;
5820}
5821
5822/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5823/// objc_assign_strongCast (id src, id *dst)
5824///
5825void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005827 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005828 const llvm::Type * SrcTy = src->getType();
5829 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005830 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005831 assert(Size <= 8 && "does not support size > 8");
5832 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005833 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005834 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5835 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005836 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5837 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005838 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005839 src, dst, "weakassign");
5840 return;
5841}
5842
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005843void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844 CodeGen::CodeGenFunction &CGF,
5845 llvm::Value *DestPtr,
5846 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005847 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005848 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5849 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005850 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005851 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005852 return;
5853}
5854
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005855/// EmitObjCWeakRead - Code gen for loading value of a __weak
5856/// object: objc_read_weak (id *src)
5857///
5858llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005859 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005860 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005861 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005862 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5863 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005864 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005865 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005866 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005867 return read_weak;
5868}
5869
5870/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5871/// objc_assign_weak (id src, id *dst)
5872///
5873void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005874 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005875 const llvm::Type * SrcTy = src->getType();
5876 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005877 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005878 assert(Size <= 8 && "does not support size > 8");
5879 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5880 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005881 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5882 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005883 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5884 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005885 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005886 src, dst, "weakassign");
5887 return;
5888}
5889
5890/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5891/// objc_assign_global (id src, id *dst)
5892///
5893void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005894 llvm::Value *src, llvm::Value *dst,
5895 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005896 const llvm::Type * SrcTy = src->getType();
5897 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005898 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005899 assert(Size <= 8 && "does not support size > 8");
5900 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5901 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005902 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5903 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005904 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5905 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005906 if (!threadlocal)
5907 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5908 src, dst, "globalassign");
5909 else
5910 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5911 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005912 return;
5913}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005914
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005915void
John McCallf1549f62010-07-06 01:34:17 +00005916CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5917 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005918 EmitAtSynchronizedStmt(CGF, S,
5919 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5920 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00005921}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005922
John McCall5a180392010-07-24 00:37:23 +00005923llvm::Constant *
5924CGObjCNonFragileABIMac::GetEHType(QualType T) {
5925 // There's a particular fixed type info for 'id'.
5926 if (T->isObjCIdType() ||
5927 T->isObjCQualifiedIdType()) {
5928 llvm::Constant *IDEHType =
5929 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5930 if (!IDEHType)
5931 IDEHType =
5932 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5933 false,
5934 llvm::GlobalValue::ExternalLinkage,
5935 0, "OBJC_EHTYPE_id");
5936 return IDEHType;
5937 }
5938
5939 // All other types should be Objective-C interface pointer types.
5940 const ObjCObjectPointerType *PT =
5941 T->getAs<ObjCObjectPointerType>();
5942 assert(PT && "Invalid @catch type.");
5943 const ObjCInterfaceType *IT = PT->getInterfaceType();
5944 assert(IT && "Invalid @catch type.");
5945 return GetInterfaceEHType(IT->getDecl(), false);
5946}
5947
John McCallf1549f62010-07-06 01:34:17 +00005948void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5949 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00005950 EmitTryCatchStmt(CGF, S,
5951 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5952 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5953 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005954}
5955
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005956/// EmitThrowStmt - Generate code for a throw statement.
5957void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5958 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005959 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00005960 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00005961 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
5962 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00005963 llvm::Value *Args[] = { Exception };
5964 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
5965 Args, Args+1)
5966 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005967 } else {
John McCall7ec404c2010-10-16 08:21:07 +00005968 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
5969 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005970 }
5971
John McCall7ec404c2010-10-16 08:21:07 +00005972 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005973 CGF.Builder.ClearInsertionPoint();
5974}
Daniel Dunbare588b992009-03-01 04:46:24 +00005975
John McCall5a180392010-07-24 00:37:23 +00005976llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005977CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005978 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005979 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005980
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005981 // If we don't need a definition, return the entry if found or check
5982 // if we use an external reference.
5983 if (!ForDefinition) {
5984 if (Entry)
5985 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005986
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005987 // If this type (or a super class) has the __objc_exception__
5988 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005989 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005990 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005991 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005992 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005993 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005994 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005995 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005996 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005997
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005998 // Otherwise we need to either make a new entry or fill in the
5999 // initializer.
6000 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006001 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006002 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006004 CGM.getModule().getGlobalVariable(VTableName);
6005 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6007 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006008 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006009 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006010
Chris Lattner77b89b82010-06-27 07:15:29 +00006011 llvm::Value *VTableIdx =
6012 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006013
6014 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006015 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006016 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006017 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006018 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006019 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006020
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006021 if (Entry) {
6022 Entry->setInitializer(Init);
6023 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006024 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006025 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006026 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006027 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006028 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006029 }
6030
John McCall1fb0caa2010-10-22 21:05:15 +00006031 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006032 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006033 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6034 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006035
6036 if (ForDefinition) {
6037 Entry->setSection("__DATA,__objc_const");
6038 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6039 } else {
6040 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6041 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006042
6043 return Entry;
6044}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006045
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006046/* *** */
6047
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006048CodeGen::CGObjCRuntime *
6049CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006050 if (CGM.getLangOptions().ObjCNonFragileABI)
6051 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006052 return new CGObjCMac(CGM);
6053}