blob: c7de520c1da2b429d2ca84f151f3b82f801dd6f2 [file] [log] [blame]
Daniel Dunbar303e2c22008-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 Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbar303e2c22008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045
John McCallc5799442011-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 Dunbar9fd114d2009-04-22 07:32:20 +000055
56///
57
Daniel Dunbar303e2c22008-08-11 02:45:11 +000058namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000059
Daniel Dunbar59e476b2009-08-03 17:06:42 +000060typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000061
Daniel Dunbar59e476b2009-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 Dunbarb036db82008-08-13 03:21:16 +000064
Fariborz Jahanian279eda62009-01-21 22:04:16 +000065class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000066protected:
67 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000068
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +000076 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +000077 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +000078 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000079 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000080
Fariborz Jahanian5d5ed2d2009-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 Anderson41a75022009-08-13 21:57:51 +000087 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +000088 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +000089 "objc_msgSend_stret");
90
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000091 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000092
Fariborz Jahanian5d5ed2d2009-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 Anderson41a75022009-08-13 21:57:51 +0000100 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
101 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000102 Params,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000103 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000104 "objc_msgSend_fpret");
105
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000106 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000107
Fariborz Jahanian5d5ed2d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000114 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000115 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000116 SuperName);
117 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000118
Fariborz Jahanian5d5ed2d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000126 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000127 SuperName);
128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000129
Fariborz Jahanian5d5ed2d2009-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 Anderson170229f2009-07-14 23:10:40 +0000137 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000138 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000139 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000140 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000141 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000142
Fariborz Jahanian5d5ed2d2009-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 Anderson170229f2009-07-14 23:10:40 +0000150 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000151 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000152 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000153 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000154 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000155
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000160
Fariborz Jahanian5d5ed2d2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000165
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000166protected:
167 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000168
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000169public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000170 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000171 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Daniel Dunbar5d715592008-08-12 05:28:47 +0000173 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
174 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000176 /// PtrObjectPtrTy - LLVM type for id *
177 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000178
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000179 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000180 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000181 /// ProtocolPtrTy - LLVM type for external protocol handles
182 /// (typeof(Protocol))
183 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000184
Daniel Dunbarc722b852008-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 Dunbar59e476b2009-08-03 17:06:42 +0000189
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000190 /// SuperTy - LLVM type for struct objc_super.
191 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000192 /// SuperPtrTy - LLVM type for struct objc_super *.
193 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000194
Fariborz Jahanianb15a3d52009-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 Dunbar59e476b2009-08-03 17:06:42 +0000198
Fariborz Jahanianb15a3d52009-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 Dunbar59e476b2009-08-03 17:06:42 +0000204
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000205 // MethodTy - LLVM type for struct objc_method.
206 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000207
Fariborz Jahanian0232c052009-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 Dunbar59e476b2009-08-03 17:06:42 +0000212
Chris Lattnerce8754e2009-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 McCall2da83a32010-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 Lattnerce8754e2009-04-22 02:44:54 +0000220 Params.push_back(IdType);
221 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000222 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000223 Params.push_back(Ctx.BoolTy);
224 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000225 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000226 FunctionType::ExtInfo()),
227 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000228 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
229 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000230
Chris Lattnerce8754e2009-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 McCall2da83a32010-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 Lattnerce8754e2009-04-22 02:44:54 +0000238 Params.push_back(IdType);
239 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000240 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-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 McCallab26cfa2010-02-05 21:31:56 +0000245 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000246 FunctionType::ExtInfo()),
247 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
249 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000250
Fariborz Jahanian5a8c2032010-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 Lattnerce8754e2009-04-22 02:44:54 +0000269 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
271 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000272 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000273 llvm::SmallVector<CanQualType,1> Params;
274 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000275 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000276 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000277 FunctionType::ExtInfo()),
278 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
280 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000281
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000282 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-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 Dunbar59e476b2009-08-03 17:06:42 +0000287 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000288 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000289 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000290 }
291
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000292 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000298 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000299 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
300 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000301
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000302 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-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 Anderson170229f2009-07-14 23:10:40 +0000307 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000308 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000309 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
310 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000311
Fariborz Jahanian217af242010-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 Jahanianeee54df2009-01-22 00:37:21 +0000322 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000323 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000324 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000325 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
326 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian30aa4aa2011-05-05 22:00:38 +0000327 const llvm::Type *PtrDiffTy =
328 CGM.getTypes().ConvertType(
329 CGM.getContext().getPointerDiffType()->getCanonicalTypeUnqualified());
330
331 Args.push_back(PtrDiffTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000332 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000333 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000334 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
335 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000336
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000337 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
338 llvm::Constant *GcMemmoveCollectableFn() {
339 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
340 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
341 Args.push_back(Int8PtrTy);
342 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000343 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000344 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
345 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000346
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000347 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000348 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000349 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000350 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
351 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000352 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000353 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000354 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
355 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000356
357 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000358 llvm::Constant *getExceptionThrowFn() {
359 // void objc_exception_throw(id)
360 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
361 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000362 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000363 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000365
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000366 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
367 llvm::Constant *getExceptionRethrowFn() {
368 // void objc_exception_rethrow(void)
369 std::vector<const llvm::Type*> Args;
370 llvm::FunctionType *FTy =
John McCall17afe452010-10-16 08:21:07 +0000371 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
373 }
374
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000375 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000376 llvm::Constant *getSyncEnterFn() {
377 // void objc_sync_enter (id)
378 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
379 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000380 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000381 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
382 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000383
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000384 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000385 llvm::Constant *getSyncExitFn() {
386 // void objc_sync_exit (id)
387 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
388 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000389 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000390 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
391 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000392
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000393 llvm::Constant *getSendFn(bool IsSuper) const {
394 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
395 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000396
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000397 llvm::Constant *getSendFn2(bool IsSuper) const {
398 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000400
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000401 llvm::Constant *getSendStretFn(bool IsSuper) const {
402 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
403 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000404
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendStretFn2(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
407 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000408
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFpretFn(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
411 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000412
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000413 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
414 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
415 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000416
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000417 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
418 ~ObjCCommonTypesHelper(){}
419};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000420
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000421/// ObjCTypesHelper - Helper class that encapsulates lazy
422/// construction of varies types used during ObjC generation.
423class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000424public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000425 /// SymtabTy - LLVM type for struct objc_symtab.
426 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000427 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
428 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000429 /// ModuleTy - LLVM type for struct objc_module.
430 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000431
Daniel Dunbarb036db82008-08-13 03:21:16 +0000432 /// ProtocolTy - LLVM type for struct objc_protocol.
433 const llvm::StructType *ProtocolTy;
434 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
435 const llvm::Type *ProtocolPtrTy;
436 /// ProtocolExtensionTy - LLVM type for struct
437 /// objc_protocol_extension.
438 const llvm::StructType *ProtocolExtensionTy;
439 /// ProtocolExtensionTy - LLVM type for struct
440 /// objc_protocol_extension *.
441 const llvm::Type *ProtocolExtensionPtrTy;
442 /// MethodDescriptionTy - LLVM type for struct
443 /// objc_method_description.
444 const llvm::StructType *MethodDescriptionTy;
445 /// MethodDescriptionListTy - LLVM type for struct
446 /// objc_method_description_list.
447 const llvm::StructType *MethodDescriptionListTy;
448 /// MethodDescriptionListPtrTy - LLVM type for struct
449 /// objc_method_description_list *.
450 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000451 /// ProtocolListTy - LLVM type for struct objc_property_list.
452 const llvm::Type *ProtocolListTy;
453 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
454 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000455 /// CategoryTy - LLVM type for struct objc_category.
456 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000457 /// ClassTy - LLVM type for struct objc_class.
458 const llvm::StructType *ClassTy;
459 /// ClassPtrTy - LLVM type for struct objc_class *.
460 const llvm::Type *ClassPtrTy;
461 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
462 const llvm::StructType *ClassExtensionTy;
463 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
464 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000465 // IvarTy - LLVM type for struct objc_ivar.
466 const llvm::StructType *IvarTy;
467 /// IvarListTy - LLVM type for struct objc_ivar_list.
468 const llvm::Type *IvarListTy;
469 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
470 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000471 /// MethodListTy - LLVM type for struct objc_method_list.
472 const llvm::Type *MethodListTy;
473 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
474 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000475
Anders Carlsson9ff22482008-09-09 10:10:21 +0000476 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
477 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000478
Anders Carlsson9ff22482008-09-09 10:10:21 +0000479 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000480 llvm::Constant *getExceptionTryEnterFn() {
481 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000482 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000483 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000484 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485 Params, false),
486 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000487 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000488
489 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000490 llvm::Constant *getExceptionTryExitFn() {
491 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000492 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000493 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000494 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000495 Params, false),
496 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000497 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000498
499 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000500 llvm::Constant *getExceptionExtractFn() {
501 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000502 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
503 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000504 Params, false),
505 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000506
Chris Lattnerc6406db2009-04-22 02:26:14 +0000507 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000508
Anders Carlsson9ff22482008-09-09 10:10:21 +0000509 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000510 llvm::Constant *getExceptionMatchFn() {
511 std::vector<const llvm::Type*> Params;
512 Params.push_back(ClassPtrTy);
513 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000514 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000515 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000516 Params, false),
517 "objc_exception_match");
518
Chris Lattnerc6406db2009-04-22 02:26:14 +0000519 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000520
Anders Carlsson9ff22482008-09-09 10:10:21 +0000521 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000522 llvm::Constant *getSetJmpFn() {
523 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000524 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000525 return
Owen Anderson41a75022009-08-13 21:57:51 +0000526 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000527 Params, false),
528 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000529
Chris Lattnerc6406db2009-04-22 02:26:14 +0000530 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000532public:
533 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000534 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000535};
536
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000537/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000538/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000539class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000540public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000541
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000542 // MethodListnfABITy - LLVM for struct _method_list_t
543 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000544
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000545 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
546 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000547
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000548 // ProtocolnfABITy = LLVM for struct _protocol_t
549 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000550
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000551 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
552 const llvm::Type *ProtocolnfABIPtrTy;
553
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000554 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
555 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000556
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000557 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
558 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000559
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000560 // ClassnfABITy - LLVM for struct _class_t
561 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000562
Fariborz Jahanian71394042009-01-23 23:53:38 +0000563 // ClassnfABIPtrTy - LLVM for struct _class_t*
564 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000566 // IvarnfABITy - LLVM for struct _ivar_t
567 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000569 // IvarListnfABITy - LLVM for struct _ivar_list_t
570 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000571
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000572 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
573 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000574
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000575 // ClassRonfABITy - LLVM for struct _class_ro_t
576 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000577
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000578 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
579 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000580
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000581 // CategorynfABITy - LLVM for struct _category_t
582 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000583
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000584 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000586 // MessageRefTy - LLVM for:
587 // struct _message_ref_t {
588 // IMP messenger;
589 // SEL name;
590 // };
591 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000592 // MessageRefCTy - clang type for struct _message_ref_t
593 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000594
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000595 // MessageRefPtrTy - LLVM for struct _message_ref_t*
596 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000597 // MessageRefCPtrTy - clang type for struct _message_ref_t*
598 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000599
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000600 // MessengerTy - Type of the messenger (shown as IMP above)
601 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000603 // SuperMessageRefTy - LLVM for:
604 // struct _super_message_ref_t {
605 // SUPER_IMP messenger;
606 // SEL name;
607 // };
608 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000610 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
611 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000612
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000613 llvm::Constant *getMessageSendFixupFn() {
614 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
615 std::vector<const llvm::Type*> Params;
616 Params.push_back(ObjectPtrTy);
617 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000618 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000619 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000620 "objc_msgSend_fixup");
621 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000622
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000623 llvm::Constant *getMessageSendFpretFixupFn() {
624 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
625 std::vector<const llvm::Type*> Params;
626 Params.push_back(ObjectPtrTy);
627 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000628 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000629 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000630 "objc_msgSend_fpret_fixup");
631 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000632
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000633 llvm::Constant *getMessageSendStretFixupFn() {
634 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
635 std::vector<const llvm::Type*> Params;
636 Params.push_back(ObjectPtrTy);
637 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000638 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000639 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000640 "objc_msgSend_stret_fixup");
641 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000642
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000643 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000645 // struct _super_message_ref_t*, ...)
646 std::vector<const llvm::Type*> Params;
647 Params.push_back(SuperPtrTy);
648 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000649 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000650 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000651 "objc_msgSendSuper2_fixup");
652 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000653
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000654 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000656 // struct _super_message_ref_t*, ...)
657 std::vector<const llvm::Type*> Params;
658 Params.push_back(SuperPtrTy);
659 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000660 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000661 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000662 "objc_msgSendSuper2_stret_fixup");
663 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Chris Lattnera7c00b42009-04-22 02:15:23 +0000665 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000666 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000667 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000668 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000669
Chris Lattnera7c00b42009-04-22 02:15:23 +0000670 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000671
Chris Lattnera7c00b42009-04-22 02:15:23 +0000672 llvm::Constant *getObjCBeginCatchFn() {
673 std::vector<const llvm::Type*> Params;
674 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000676 Params, false),
677 "objc_begin_catch");
678 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000679
680 const llvm::StructType *EHTypeTy;
681 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000682
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000683 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
684 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000685};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000686
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000687class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000688public:
689 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000690 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000691 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000692 unsigned ivar_bytepos;
693 unsigned ivar_size;
694 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000696
697 // Allow sorting based on byte pos.
698 bool operator<(const GC_IVAR &b) const {
699 return ivar_bytepos < b.ivar_bytepos;
700 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000701 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000702
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000703 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000704 public:
705 unsigned skip;
706 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000707 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000708 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000709 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000710
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000711protected:
712 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000713 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000714 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000715 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000716
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000717 // gc ivar layout bitmap calculation helper caches.
718 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
719 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000720
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000721 /// LazySymbols - Symbols to generate a lazy reference for. See
722 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000723 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000724
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000725 /// DefinedSymbols - External symbols which are defined by this
726 /// module. The symbols in this list and LazySymbols are used to add
727 /// special linker symbols which ensure that Objective-C modules are
728 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000729 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000730
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000731 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000732 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000733
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000734 /// MethodVarNames - uniqued method variable names.
735 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000736
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000737 /// DefinedCategoryNames - list of category names in form Class_Category.
738 llvm::SetVector<std::string> DefinedCategoryNames;
739
Daniel Dunbarb036db82008-08-13 03:21:16 +0000740 /// MethodVarTypes - uniqued method type signatures. We have to use
741 /// a StringMap here because have no other unique reference.
742 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000743
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000744 /// MethodDefinitions - map of methods which have been defined in
745 /// this translation unit.
746 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000747
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000748 /// PropertyNames - uniqued method variable names.
749 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000751 /// ClassReferences - uniqued class references.
752 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000753
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000754 /// SelectorReferences - uniqued selector references.
755 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000756
Daniel Dunbarb036db82008-08-13 03:21:16 +0000757 /// Protocols - Protocols for which an objc_protocol structure has
758 /// been emitted. Forward declarations are handled by creating an
759 /// empty structure whose initializer is filled in when/if defined.
760 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Daniel Dunbarc475d422008-10-29 22:36:39 +0000762 /// DefinedProtocols - Protocols which have actually been
763 /// defined. We should not need this, see FIXME in GenerateProtocol.
764 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000765
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000766 /// DefinedClasses - List of defined classes.
767 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000768
769 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
770 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000771
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000772 /// DefinedCategories - List of defined categories.
773 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000774
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000775 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
776 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000777
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000778 /// GetNameForMethod - Return a name for the given method.
779 /// \param[out] NameOut - The return value.
780 void GetNameForMethod(const ObjCMethodDecl *OMD,
781 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000782 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000783
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000784 /// GetMethodVarName - Return a unique constant for the given
785 /// selector's name. The return value has type char *.
786 llvm::Constant *GetMethodVarName(Selector Sel);
787 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000788
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000789 /// GetMethodVarType - Return a unique constant for the given
790 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000792 // FIXME: This is a horrible name.
793 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000794 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000795
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000796 /// GetPropertyName - Return a unique constant for the given
797 /// name. The return value has type char *.
798 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000799
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000800 // FIXME: This can be dropped once string functions are unified.
801 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
802 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000803
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000804 /// GetClassName - Return a unique constant for the given selector's
805 /// name. The return value has type char *.
806 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000807
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000808 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
809
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000810 /// BuildIvarLayout - Builds ivar layout bitmap for the class
811 /// implementation for the __strong or __weak case.
812 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000813 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
814 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000815
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000816 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000817
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000818 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000819 unsigned int BytePos, bool ForStrongLayout,
820 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000821 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000822 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000823 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000824 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000825 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000826 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000827
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000828 /// GetIvarLayoutName - Returns a unique constant for the given
829 /// ivar layout bitmap.
830 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
831 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000833 /// EmitPropertyList - Emit the given property list. The return
834 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000835 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000836 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000837 const ObjCContainerDecl *OCD,
838 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000839
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000840 /// PushProtocolProperties - Push protocol's property on the input stack.
841 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
842 std::vector<llvm::Constant*> &Properties,
843 const Decl *Container,
844 const ObjCProtocolDecl *PROTO,
845 const ObjCCommonTypesHelper &ObjCTypes);
846
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000847 /// GetProtocolRef - Return a reference to the internal protocol
848 /// description, creating an empty one if it has not been
849 /// defined. The return value has type ProtocolPtrTy.
850 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000851
Daniel Dunbar30c65362009-03-09 20:09:19 +0000852 /// CreateMetadataVar - Create a global variable with internal
853 /// linkage for use by the Objective-C runtime.
854 ///
855 /// This is a convenience wrapper which not only creates the
856 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000857 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000858 ///
859 /// \param Name - The variable name.
860 /// \param Init - The variable initializer; this is also used to
861 /// define the type of the variable.
862 /// \param Section - The section the variable should go into, or 0.
863 /// \param Align - The alignment for the variable, or 0.
864 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000865 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000866 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000867 llvm::Constant *Init,
868 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000869 unsigned Align,
870 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000871
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000872 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000873 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000874 QualType ResultType,
875 llvm::Value *Sel,
876 llvm::Value *Arg0,
877 QualType Arg0Ty,
878 bool IsSuper,
879 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000880 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000881 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000882
Daniel Dunbar5e639272010-04-25 20:39:01 +0000883 /// EmitImageInfo - Emit the image info marker used to encode some module
884 /// level information.
885 void EmitImageInfo();
886
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000887public:
Owen Andersonae86c192009-07-13 04:10:07 +0000888 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000889 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
David Chisnall481e3a82010-01-23 02:40:42 +0000891 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000892
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000893 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
894 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000895
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000896 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000897
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000898 /// GetOrEmitProtocol - Get the protocol object for the given
899 /// declaration, emitting it if necessary. The return value has type
900 /// ProtocolPtrTy.
901 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000903 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
904 /// object for the given declaration, emitting it if needed. These
905 /// forward references will be filled in with empty bodies if no
906 /// definition is seen. The return value has type ProtocolPtrTy.
907 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000908 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
909 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000910
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000911};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000912
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000913class CGObjCMac : public CGObjCCommonMac {
914private:
915 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000916
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000917 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000918 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000919 void EmitModuleInfo();
920
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000921 /// EmitModuleSymols - Emit module symbols, the list of defined
922 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000923 llvm::Constant *EmitModuleSymbols();
924
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000925 /// FinishModule - Write out global data structures at the end of
926 /// processing a translation unit.
927 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000928
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000929 /// EmitClassExtension - Generate the class extension structure used
930 /// to store the weak ivar layout and properties. The return value
931 /// has type ClassExtensionPtrTy.
932 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
933
934 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
935 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000936 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000937 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000938
939 /// EmitSuperClassRef - Emits reference to class's main metadata class.
940 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000941
942 /// EmitIvarList - Emit the ivar list for the given
943 /// implementation. If ForClass is true the list of class ivars
944 /// (i.e. metaclass ivars) is emitted, otherwise the list of
945 /// interface ivars will be emitted. The return value has type
946 /// IvarListPtrTy.
947 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000948 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000949
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000950 /// EmitMetaClass - Emit a forward reference to the class structure
951 /// for the metaclass of the given interface. The return value has
952 /// type ClassPtrTy.
953 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
954
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000955 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000956 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000957 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
958 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000959 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000960
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000961 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000962
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000963 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000964
965 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000966 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000967 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000968 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000969 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000970
971 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000972 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000973 /// - TypeName: The name for the type containing the methods.
974 /// - IsProtocol: True iff these methods are for a protocol.
975 /// - ClassMethds: True iff these are class methods.
976 /// - Required: When true, only "required" methods are
977 /// listed. Similarly, when false only "optional" methods are
978 /// listed. For classes this should always be true.
979 /// - begin, end: The method list to output.
980 ///
981 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000982 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000983 const char *Section,
984 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000985
Daniel Dunbarc475d422008-10-29 22:36:39 +0000986 /// GetOrEmitProtocol - Get the protocol object for the given
987 /// declaration, emitting it if necessary. The return value has type
988 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000989 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000990
991 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
992 /// object for the given declaration, emitting it if needed. These
993 /// forward references will be filled in with empty bodies if no
994 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000995 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000996
Daniel Dunbarb036db82008-08-13 03:21:16 +0000997 /// EmitProtocolExtension - Generate the protocol extension
998 /// structure used to store optional instance and class methods, and
999 /// protocol properties. The return value has type
1000 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001001 llvm::Constant *
1002 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1003 const ConstantVector &OptInstanceMethods,
1004 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001005
1006 /// EmitProtocolList - Generate the list of referenced
1007 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001008 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001009 ObjCProtocolDecl::protocol_iterator begin,
1010 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001011
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001012 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1013 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001014 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1015 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001016
1017public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001018 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001019
Fariborz Jahanian71394042009-01-23 23:53:38 +00001020 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001021
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001022 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001023 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001024 QualType ResultType,
1025 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001026 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001027 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001028 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001029 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001030
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001031 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001032 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001033 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001034 QualType ResultType,
1035 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001036 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001037 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001038 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001039 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001040 const CallArgList &CallArgs,
1041 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001042
Daniel Dunbarcb463852008-11-01 01:53:16 +00001043 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001044 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001045
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001046 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1047 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001048
1049 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1050 /// untyped one.
1051 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1052 const ObjCMethodDecl *Method);
1053
John McCall2ca705e2010-07-24 00:37:23 +00001054 virtual llvm::Constant *GetEHType(QualType T);
1055
Daniel Dunbar92992502008-08-15 22:20:32 +00001056 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001057
Daniel Dunbar92992502008-08-15 22:20:32 +00001058 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001059
Daniel Dunbarcb463852008-11-01 01:53:16 +00001060 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001061 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001062
Chris Lattnerd4808922009-03-22 21:03:39 +00001063 virtual llvm::Constant *GetPropertyGetFunction();
1064 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001065 virtual llvm::Constant *GetGetStructFunction();
1066 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001067 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001068
John McCallbd309292010-07-06 01:34:17 +00001069 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1070 const ObjCAtTryStmt &S);
1071 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1072 const ObjCAtSynchronizedStmt &S);
1073 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001074 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1075 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001076 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001077 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001078 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001079 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001080 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001081 llvm::Value *src, llvm::Value *dest,
1082 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001083 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001084 llvm::Value *src, llvm::Value *dest,
1085 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001086 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1087 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001088 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1089 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001090 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001091
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001092 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1093 QualType ObjectTy,
1094 llvm::Value *BaseValue,
1095 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001096 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001097 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001098 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001099 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001100};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001101
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001102class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001103private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001104 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001105 llvm::GlobalVariable* ObjCEmptyCacheVar;
1106 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001107
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001108 /// SuperClassReferences - uniqued super class references.
1109 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001110
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001111 /// MetaClassReferences - uniqued meta class references.
1112 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001113
1114 /// EHTypeReferences - uniqued class ehtype references.
1115 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001116
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001117 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001118 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001119 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001120
Fariborz Jahanian67260552009-11-17 21:37:35 +00001121 /// DefinedMetaClasses - List of defined meta-classes.
1122 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1123
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001124 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001125 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001126 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001127
Fariborz Jahanian71394042009-01-23 23:53:38 +00001128 /// FinishNonFragileABIModule - Write out global data structures at the end of
1129 /// processing a translation unit.
1130 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001131
Daniel Dunbar19573e72009-05-15 21:48:48 +00001132 /// AddModuleClassList - Add the given list of class pointers to the
1133 /// module with the provided symbol and section names.
1134 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1135 const char *SymbolName,
1136 const char *SectionName);
1137
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1139 unsigned InstanceStart,
1140 unsigned InstanceSize,
1141 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001142 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001143 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001144 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001145 llvm::Constant *ClassRoGV,
1146 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001147
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001148 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001149
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001150 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001151
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001152 /// EmitMethodList - Emit the method list for the given
1153 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001154 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001155 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001156 const ConstantVector &Methods);
1157 /// EmitIvarList - Emit the ivar list for the given
1158 /// implementation. If ForClass is true the list of class ivars
1159 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1160 /// interface ivars will be emitted. The return value has type
1161 /// IvarListnfABIPtrTy.
1162 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001163
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001164 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001165 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001166 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001167
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001168 /// GetOrEmitProtocol - Get the protocol object for the given
1169 /// declaration, emitting it if necessary. The return value has type
1170 /// ProtocolPtrTy.
1171 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001172
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001173 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1174 /// object for the given declaration, emitting it if needed. These
1175 /// forward references will be filled in with empty bodies if no
1176 /// definition is seen. The return value has type ProtocolPtrTy.
1177 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001178
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001179 /// EmitProtocolList - Generate the list of referenced
1180 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001181 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001182 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001183 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001184
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001185 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001186 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001187 QualType ResultType,
1188 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001189 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001190 QualType Arg0Ty,
1191 bool IsSuper,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00001192 const CallArgList &CallArgs,
1193 const ObjCMethodDecl *Method);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001194
1195 /// GetClassGlobal - Return the global variable for the Objective-C
1196 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001197 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001198
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001199 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001200 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001201 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001202 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001203
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001204 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1205 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001206 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1207 const ObjCInterfaceDecl *ID);
1208
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001209 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1210 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001211 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001212 const ObjCInterfaceDecl *ID);
1213
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001214 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1215 /// the given ivar.
1216 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001217 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218 const ObjCInterfaceDecl *ID,
1219 const ObjCIvarDecl *Ivar);
1220
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001221 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1222 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001223 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1224 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001225
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001226 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001227 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001228 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001229 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001230
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001231 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001232 return "OBJC_METACLASS_$_";
1233 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234
Daniel Dunbar15894b72009-04-07 05:48:37 +00001235 const char *getClassSymbolPrefix() const {
1236 return "OBJC_CLASS_$_";
1237 }
1238
Daniel Dunbar961202372009-05-03 12:57:56 +00001239 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001240 uint32_t &InstanceStart,
1241 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001243 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001244 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001245 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1246 return CGM.getContext().Selectors.getSelector(0, &II);
1247 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001248
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001249 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001250 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1251 return CGM.getContext().Selectors.getSelector(1, &II);
1252 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001253
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001254 /// ImplementationIsNonLazy - Check whether the given category or
1255 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001256 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001257
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001258public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001259 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001260 // FIXME. All stubs for now!
1261 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001262
Fariborz Jahanian71394042009-01-23 23:53:38 +00001263 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001264 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001265 QualType ResultType,
1266 Selector Sel,
1267 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001268 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001269 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001270 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001271
1272 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001273 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001274 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001275 QualType ResultType,
1276 Selector Sel,
1277 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001278 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001279 llvm::Value *Receiver,
1280 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001281 const CallArgList &CallArgs,
1282 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001283
Fariborz Jahanian71394042009-01-23 23:53:38 +00001284 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001285 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001287 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1288 bool lvalue = false)
1289 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001290
1291 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1292 /// untyped one.
1293 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1294 const ObjCMethodDecl *Method)
1295 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001296
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001297 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001298
Fariborz Jahanian71394042009-01-23 23:53:38 +00001299 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001300 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001301 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302
John McCall2ca705e2010-07-24 00:37:23 +00001303 virtual llvm::Constant *GetEHType(QualType T);
1304
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001306 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001307 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308 virtual llvm::Constant *GetPropertySetFunction() {
1309 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001310 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001311
David Chisnall168b80f2010-12-26 22:13:16 +00001312 virtual llvm::Constant *GetSetStructFunction() {
1313 return ObjCTypes.getCopyStructFn();
1314 }
1315 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001316 return ObjCTypes.getCopyStructFn();
1317 }
1318
Chris Lattnerd4808922009-03-22 21:03:39 +00001319 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001320 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322
John McCallbd309292010-07-06 01:34:17 +00001323 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1324 const ObjCAtTryStmt &S);
1325 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1326 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001327 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001328 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001329 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001330 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001331 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001332 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001333 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001334 llvm::Value *src, llvm::Value *dest,
1335 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001336 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001337 llvm::Value *src, llvm::Value *dest,
1338 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001339 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001340 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001341 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1342 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001343 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001344 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1345 QualType ObjectTy,
1346 llvm::Value *BaseValue,
1347 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001348 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001349 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001350 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001351 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001352};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001353
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001354} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001355
1356/* *** Helper Functions *** */
1357
1358/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001359static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001360 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001361 unsigned idx0,
1362 unsigned idx1) {
1363 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001364 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1365 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001366 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001367 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001368}
1369
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001370/// hasObjCExceptionAttribute - Return true if this class or any super
1371/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001372static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001373 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001374 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001375 return true;
1376 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001377 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001378 return false;
1379}
1380
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001381/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001382
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001383CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001384 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001385 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001386 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001387}
1388
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001389/// GetClass - Return a reference to the class for the given interface
1390/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001391llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001392 const ObjCInterfaceDecl *ID) {
1393 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001394}
1395
1396/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001397llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1398 bool lval) {
1399 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001400}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001401llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001402 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001403 return EmitSelector(Builder, Method->getSelector());
1404}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001405
John McCall2ca705e2010-07-24 00:37:23 +00001406llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1407 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1408 return 0;
1409}
1410
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001411/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001412/*
1413 struct __builtin_CFString {
1414 const int *isa; // point to __CFConstantStringClassReference
1415 int flags;
1416 const char *str;
1417 long length;
1418 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001419*/
1420
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001421/// or Generate a constant NSString object.
1422/*
1423 struct __builtin_NSString {
1424 const int *isa; // point to __NSConstantStringClassReference
1425 const char *str;
1426 unsigned int length;
1427 };
1428*/
1429
Fariborz Jahanian71394042009-01-23 23:53:38 +00001430llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001431 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001432 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1433 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001434 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001435}
1436
1437/// Generates a message send where the super is the receiver. This is
1438/// a message send to self with special delivery semantics indicating
1439/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001440CodeGen::RValue
1441CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001442 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001443 QualType ResultType,
1444 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001445 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001446 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001447 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001448 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001449 const CodeGen::CallArgList &CallArgs,
1450 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001451 // Create and init a super structure; this is a (receiver, class)
1452 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001453 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001454 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001455 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001456 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001457 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001458 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001459
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001460 // If this is a class message the metaclass is passed as the target.
1461 llvm::Value *Target;
1462 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001463 if (isCategoryImpl) {
1464 // Message sent to 'super' in a class method defined in a category
1465 // implementation requires an odd treatment.
1466 // If we are in a class method, we must retrieve the
1467 // _metaclass_ for the current class, pointed at by
1468 // the class's "isa" pointer. The following assumes that
1469 // isa" is the first ivar in a class (which it must be).
1470 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1471 Target = CGF.Builder.CreateStructGEP(Target, 0);
1472 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001473 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001474 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1475 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1476 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1477 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001478 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001479 }
1480 else if (isCategoryImpl)
1481 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1482 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001483 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1484 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1485 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001486 }
Mike Stump18bb9282009-05-16 07:57:57 +00001487 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1488 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001489 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001490 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001491 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001492 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001493 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001494 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001495 EmitSelector(CGF.Builder, Sel),
1496 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001497 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001498}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001499
1500/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001501CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001502 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001503 QualType ResultType,
1504 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001505 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001506 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001507 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001508 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001509 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001510 EmitSelector(CGF.Builder, Sel),
1511 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001512 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001513}
1514
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001515CodeGen::RValue
1516CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001517 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001518 QualType ResultType,
1519 llvm::Value *Sel,
1520 llvm::Value *Arg0,
1521 QualType Arg0Ty,
1522 bool IsSuper,
1523 const CallArgList &CallArgs,
1524 const ObjCMethodDecl *Method,
1525 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001526 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001527 if (!IsSuper)
1528 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman43dca6a2011-05-02 17:57:46 +00001529 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1530 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
Daniel Dunbarc722b852008-08-30 03:02:31 +00001531 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001532
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001533 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001534 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001535 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001536 const llvm::FunctionType *FTy =
1537 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001538
Anders Carlsson280e61f12010-06-21 20:59:55 +00001539 if (Method)
1540 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1541 CGM.getContext().getCanonicalType(ResultType) &&
1542 "Result type mismatch!");
1543
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001544 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001545 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00001546 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001547 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001548 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001549 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1550 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1551 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001552 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001553 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001554 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001555 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001556 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001557 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001558}
1559
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001560static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1561 if (FQT.isObjCGCStrong())
1562 return Qualifiers::Strong;
1563
1564 if (FQT.isObjCGCWeak())
1565 return Qualifiers::Weak;
1566
1567 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1568 return Qualifiers::Strong;
1569
1570 if (const PointerType *PT = FQT->getAs<PointerType>())
1571 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1572
1573 return Qualifiers::GCNone;
1574}
1575
John McCall351762c2011-02-07 10:33:21 +00001576llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1577 const CGBlockInfo &blockInfo) {
1578 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001579 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001580
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001581 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001582 return nullPtr;
1583
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001584 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001585 SkipIvars.clear();
1586 IvarsInfo.clear();
1587 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1588 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1589
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001590 // __isa is the first field in block descriptor and must assume by runtime's
1591 // convention that it is GC'able.
1592 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001593
1594 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1595
1596 // Calculate the basic layout of the block structure.
1597 const llvm::StructLayout *layout =
1598 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1599
1600 // Ignore the optional 'this' capture: C++ objects are not assumed
1601 // to be GC'ed.
1602
1603 // Walk the captured variables.
1604 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1605 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1606 const VarDecl *variable = ci->getVariable();
1607 QualType type = variable->getType();
1608
1609 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1610
1611 // Ignore constant captures.
1612 if (capture.isConstant()) continue;
1613
1614 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1615
1616 // __block variables are passed by their descriptor address.
1617 if (ci->isByRef()) {
1618 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001619 continue;
John McCall351762c2011-02-07 10:33:21 +00001620 }
1621
1622 assert(!type->isArrayType() && "array variable should not be caught");
1623 if (const RecordType *record = type->getAs<RecordType>()) {
1624 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001625 continue;
1626 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001627
John McCall351762c2011-02-07 10:33:21 +00001628 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1629 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1630
1631 if (GCAttr == Qualifiers::Strong)
1632 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1633 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001634 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001635 SkipIvars.push_back(GC_IVAR(fieldOffset,
1636 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001637 }
1638
1639 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001640 return nullPtr;
1641
1642 // Sort on byte position; captures might not be allocated in order,
1643 // and unions can do funny things.
1644 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1645 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001646
1647 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001648 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001649 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1650 printf("\n block variable layout for block: ");
1651 const unsigned char *s = (unsigned char*)BitMap.c_str();
1652 for (unsigned i = 0; i < BitMap.size(); i++)
1653 if (!(s[i] & 0xf0))
1654 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1655 else
1656 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1657 printf("\n");
1658 }
1659
1660 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001661}
1662
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001663llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001664 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001665 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001666 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001667 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1668
Owen Andersonade90fd2009-07-29 18:54:39 +00001669 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001670 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001671}
1672
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001673void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001674 // FIXME: We shouldn't need this, the protocol decl should contain enough
1675 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001676 DefinedProtocols.insert(PD->getIdentifier());
1677
1678 // If we have generated a forward reference to this protocol, emit
1679 // it now. Otherwise do nothing, the protocol objects are lazily
1680 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001681 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001682 GetOrEmitProtocol(PD);
1683}
1684
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001685llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001686 if (DefinedProtocols.count(PD->getIdentifier()))
1687 return GetOrEmitProtocol(PD);
1688 return GetOrEmitProtocolRef(PD);
1689}
1690
Daniel Dunbarb036db82008-08-13 03:21:16 +00001691/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001692// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1693struct _objc_protocol {
1694struct _objc_protocol_extension *isa;
1695char *protocol_name;
1696struct _objc_protocol_list *protocol_list;
1697struct _objc__method_prototype_list *instance_methods;
1698struct _objc__method_prototype_list *class_methods
1699};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001700
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001701See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001702*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001703llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1704 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1705
1706 // Early exit if a defining object has already been generated.
1707 if (Entry && Entry->hasInitializer())
1708 return Entry;
1709
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001710 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001711 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001712 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1713
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001714 // Construct method lists.
1715 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1716 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001717 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001718 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001719 ObjCMethodDecl *MD = *i;
1720 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1721 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1722 OptInstanceMethods.push_back(C);
1723 } else {
1724 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001725 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001726 }
1727
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001728 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001729 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001730 ObjCMethodDecl *MD = *i;
1731 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1732 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1733 OptClassMethods.push_back(C);
1734 } else {
1735 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001736 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001737 }
1738
Daniel Dunbarb036db82008-08-13 03:21:16 +00001739 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001740 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001741 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001742 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001743 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001744 PD->protocol_begin(),
1745 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001746 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001747 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001748 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1749 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001750 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001751 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001752 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1753 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001754 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001755 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001756
Daniel Dunbarb036db82008-08-13 03:21:16 +00001757 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001758 // Already created, fix the linkage and update the initializer.
1759 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001760 Entry->setInitializer(Init);
1761 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001762 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001763 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001764 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001765 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001766 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001767 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001768 // FIXME: Is this necessary? Why only for protocol?
1769 Entry->setAlignment(4);
1770 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001771 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001772
1773 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001774}
1775
Daniel Dunbarc475d422008-10-29 22:36:39 +00001776llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001777 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1778
1779 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001780 // We use the initializer as a marker of whether this is a forward
1781 // reference or not. At module finalization we add the empty
1782 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001783 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001784 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001785 llvm::GlobalValue::ExternalLinkage,
1786 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001787 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001788 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001789 // FIXME: Is this necessary? Why only for protocol?
1790 Entry->setAlignment(4);
1791 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001792
Daniel Dunbarb036db82008-08-13 03:21:16 +00001793 return Entry;
1794}
1795
1796/*
1797 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001798 uint32_t size;
1799 struct objc_method_description_list *optional_instance_methods;
1800 struct objc_method_description_list *optional_class_methods;
1801 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001802 };
1803*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001804llvm::Constant *
1805CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1806 const ConstantVector &OptInstanceMethods,
1807 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001808 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001809 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001810 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001811 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812 Values[1] =
1813 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001814 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001815 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1816 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001817 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001818 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001819 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1820 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001821 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001822 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001823
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001824 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001825 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001826 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001827 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001829 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001830 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001831
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001832 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001833 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001834 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001835 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001836}
1837
1838/*
1839 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001840 struct objc_protocol_list *next;
1841 long count;
1842 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843 };
1844*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001845llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001846CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001847 ObjCProtocolDecl::protocol_iterator begin,
1848 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001849 std::vector<llvm::Constant*> ProtocolRefs;
1850
Daniel Dunbardec75f82008-08-21 21:57:41 +00001851 for (; begin != end; ++begin)
1852 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001853
1854 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001855 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001856 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001858 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001859 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001860
1861 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001862 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001863 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001864 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001865 ProtocolRefs.size() - 1);
1866 Values[2] =
1867 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1868 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001870
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001871 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001872 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001873 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001874 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001875 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001876}
1877
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001878void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1879 std::vector<llvm::Constant*> &Properties,
1880 const Decl *Container,
1881 const ObjCProtocolDecl *PROTO,
1882 const ObjCCommonTypesHelper &ObjCTypes) {
1883 std::vector<llvm::Constant*> Prop(2);
1884 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1885 E = PROTO->protocol_end(); P != E; ++P)
1886 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1887 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1888 E = PROTO->prop_end(); I != E; ++I) {
1889 const ObjCPropertyDecl *PD = *I;
1890 if (!PropertySet.insert(PD->getIdentifier()))
1891 continue;
1892 Prop[0] = GetPropertyName(PD->getIdentifier());
1893 Prop[1] = GetPropertyTypeString(PD, Container);
1894 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1895 }
1896}
1897
Daniel Dunbarb036db82008-08-13 03:21:16 +00001898/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001899 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001900 const char * const name;
1901 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001902 };
1903
1904 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001905 uint32_t entsize; // sizeof (struct _objc_property)
1906 uint32_t prop_count;
1907 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001908 };
1909*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001910llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001911 const Decl *Container,
1912 const ObjCContainerDecl *OCD,
1913 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001914 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001915 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001916 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1917 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001918 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001919 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001920 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001921 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001922 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001923 Prop));
1924 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001925 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001926 for (ObjCInterfaceDecl::all_protocol_iterator
1927 P = OID->all_referenced_protocol_begin(),
1928 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001929 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1930 ObjCTypes);
1931 }
1932 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1933 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1934 E = CD->protocol_end(); P != E; ++P)
1935 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1936 ObjCTypes);
1937 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001938
1939 // Return null for empty list.
1940 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001941 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001942
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001943 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001944 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001945 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001946 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1947 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001948 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001949 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001950 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001951 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001952
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001953 llvm::GlobalVariable *GV =
1954 CreateMetadataVar(Name, Init,
1955 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001956 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001957 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001958 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001959 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001960}
1961
1962/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001963 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001964 int count;
1965 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001966 };
1967*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001968llvm::Constant *
1969CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1970 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001971 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001972 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1973 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001974 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001975 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001976 Desc);
1977}
Daniel Dunbarb036db82008-08-13 03:21:16 +00001978
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001979llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001980 const char *Section,
1981 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001982 // Return null for empty list.
1983 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001984 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001985
1986 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001987 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001989 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001990 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001991 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001993 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001995 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001996}
1997
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001998/*
1999 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002000 char *category_name;
2001 char *class_name;
2002 struct _objc_method_list *instance_methods;
2003 struct _objc_method_list *class_methods;
2004 struct _objc_protocol_list *protocols;
2005 uint32_t size; // <rdar://4585769>
2006 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002007 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002008*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002009void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002010 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002011
Mike Stump18bb9282009-05-16 07:57:57 +00002012 // FIXME: This is poor design, the OCD should have a pointer to the category
2013 // decl. Additionally, note that Category can be null for the @implementation
2014 // w/o an @interface case. Sema should just create one for us as it does for
2015 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002016 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002017 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002018 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002019
2020 llvm::SmallString<256> ExtName;
2021 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2022 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002023
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002024 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002026 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002027 // Instance methods should always be defined.
2028 InstanceMethods.push_back(GetMethodConstant(*i));
2029 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002030 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002031 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002032 // Class methods should always be defined.
2033 ClassMethods.push_back(GetMethodConstant(*i));
2034 }
2035
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002036 std::vector<llvm::Constant*> Values(7);
2037 Values[0] = GetClassName(OCD->getIdentifier());
2038 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002039 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002040 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002041 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002042 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002043 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002044 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002045 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002046 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002047 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002048 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002049 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002050 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002051 Category->protocol_begin(),
2052 Category->protocol_end());
2053 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002054 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002055 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002056 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002057
2058 // If there is no category @interface then there can be no properties.
2059 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002060 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002061 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002062 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002063 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002064 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002065
Owen Anderson0e0189d2009-07-27 22:29:56 +00002066 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002067 Values);
2068
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002070 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002071 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002072 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002073 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002074 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002075 // method definition entries must be clear for next implementation.
2076 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002077}
2078
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002079// FIXME: Get from somewhere?
2080enum ClassFlags {
2081 eClassFlags_Factory = 0x00001,
2082 eClassFlags_Meta = 0x00002,
2083 // <rdr://5142207>
2084 eClassFlags_HasCXXStructors = 0x02000,
2085 eClassFlags_Hidden = 0x20000,
2086 eClassFlags_ABI2_Hidden = 0x00010,
2087 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2088};
2089
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002090/*
2091 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002092 Class isa;
2093 Class super_class;
2094 const char *name;
2095 long version;
2096 long info;
2097 long instance_size;
2098 struct _objc_ivar_list *ivars;
2099 struct _objc_method_list *methods;
2100 struct _objc_cache *cache;
2101 struct _objc_protocol_list *protocols;
2102 // Objective-C 1.0 extensions (<rdr://4585769>)
2103 const char *ivar_layout;
2104 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002105 };
2106
2107 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002108*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002109void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002110 DefinedSymbols.insert(ID->getIdentifier());
2111
Chris Lattner86d7d912008-11-24 03:54:41 +00002112 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002113 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002114 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002115 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002116 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002117 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002118 Interface->all_referenced_protocol_begin(),
2119 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002120 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002121 if (ID->getNumIvarInitializers())
2122 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002123 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002124 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002125
2126 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002127 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002128 Flags |= eClassFlags_Hidden;
2129
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002130 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002132 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002133 // Instance methods should always be defined.
2134 InstanceMethods.push_back(GetMethodConstant(*i));
2135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002136 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002137 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002138 // Class methods should always be defined.
2139 ClassMethods.push_back(GetMethodConstant(*i));
2140 }
2141
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002142 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002143 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002144 ObjCPropertyImplDecl *PID = *i;
2145
2146 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2147 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2148
2149 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2150 if (llvm::Constant *C = GetMethodConstant(MD))
2151 InstanceMethods.push_back(C);
2152 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2153 if (llvm::Constant *C = GetMethodConstant(MD))
2154 InstanceMethods.push_back(C);
2155 }
2156 }
2157
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002158 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002159 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002160 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002161 // Record a reference to the super class.
2162 LazySymbols.insert(Super->getIdentifier());
2163
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002164 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002165 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002166 ObjCTypes.ClassPtrTy);
2167 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002168 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002169 }
2170 Values[ 2] = GetClassName(ID->getIdentifier());
2171 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002172 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2173 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2174 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002175 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002176 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002177 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002178 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002179 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002180 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002181 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002182 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002183 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002184 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002185 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002186 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002187 std::string Name("\01L_OBJC_CLASS_");
2188 Name += ClassName;
2189 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2190 // Check for a forward reference.
2191 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2192 if (GV) {
2193 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2194 "Forward metaclass reference has incorrect type.");
2195 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2196 GV->setInitializer(Init);
2197 GV->setSection(Section);
2198 GV->setAlignment(4);
2199 CGM.AddUsedGlobal(GV);
2200 }
2201 else
2202 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002203 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002204 // method definition entries must be clear for next implementation.
2205 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002206}
2207
2208llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2209 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002210 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002211 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002212 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002213
John McCall457a04e2010-10-22 21:05:15 +00002214 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002215 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002216
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002217 std::vector<llvm::Constant*> Values(12);
2218 // The isa for the metaclass is the root of the hierarchy.
2219 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2220 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2221 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002222 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002223 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002224 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002225 // The super class for the metaclass is emitted as the name of the
2226 // super class. The runtime fixes this up to point to the
2227 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002228 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002229 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002230 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002231 ObjCTypes.ClassPtrTy);
2232 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002233 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002234 }
2235 Values[ 2] = GetClassName(ID->getIdentifier());
2236 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002237 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2238 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2239 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002240 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002241 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002242 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002243 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002244 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002245 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002246 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002247 Values[ 9] = Protocols;
2248 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002249 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002251 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002252 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 Values);
2254
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002255 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002256 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002257
2258 // Check for a forward reference.
2259 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2260 if (GV) {
2261 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2262 "Forward metaclass reference has incorrect type.");
2263 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2264 GV->setInitializer(Init);
2265 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002266 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002267 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002268 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002269 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002270 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002271 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002272 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002273
2274 return GV;
2275}
2276
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002277llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002278 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002279
Mike Stump18bb9282009-05-16 07:57:57 +00002280 // FIXME: Should we look these up somewhere other than the module. Its a bit
2281 // silly since we only generate these while processing an implementation, so
2282 // exactly one pointer would work if know when we entered/exitted an
2283 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002284
2285 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002286 // Previously, metaclass with internal linkage may have been defined.
2287 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002288 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2289 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002290 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2291 "Forward metaclass reference has incorrect type.");
2292 return GV;
2293 } else {
2294 // Generate as an external reference to keep a consistent
2295 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002296 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002297 llvm::GlobalValue::ExternalLinkage,
2298 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002299 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002300 }
2301}
2302
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002303llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2304 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2305
2306 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2307 true)) {
2308 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2309 "Forward class metadata reference has incorrect type.");
2310 return GV;
2311 } else {
2312 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2313 llvm::GlobalValue::ExternalLinkage,
2314 0,
2315 Name);
2316 }
2317}
2318
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002319/*
2320 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002321 uint32_t size;
2322 const char *weak_ivar_layout;
2323 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002324 };
2325*/
2326llvm::Constant *
2327CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002328 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002329 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002330
2331 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002332 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002333 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002334 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002335 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336
2337 // Return null if no extension bits are used.
2338 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002339 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002341 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002342 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002343 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002344 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002345 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002346}
2347
2348/*
2349 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002350 char *ivar_name;
2351 char *ivar_type;
2352 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002353 };
2354
2355 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002356 int ivar_count;
2357 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002358 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002359*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002360llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002361 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002362 std::vector<llvm::Constant*> Ivars, Ivar(3);
2363
2364 // When emitting the root class GCC emits ivar entries for the
2365 // actual class structure. It is not clear if we need to follow this
2366 // behavior; for now lets try and get away with not doing it. If so,
2367 // the cleanest solution would be to make up an ObjCInterfaceDecl
2368 // for the class.
2369 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002370 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002371
2372 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002373 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002374
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002375 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002376 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002377
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002378 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2379 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002380 // Ignore unnamed bit-fields.
2381 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002382 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002383 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2384 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002385 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002386 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002387 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002388 }
2389
2390 // Return null for empty list.
2391 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002392 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002393
2394 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002395 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002396 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002397 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002398 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002399 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002400
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002401 llvm::GlobalVariable *GV;
2402 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002403 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002404 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002405 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002406 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002407 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002408 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002409 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002410 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002411}
2412
2413/*
2414 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002415 SEL method_name;
2416 char *method_types;
2417 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002418 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002419
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002421 struct objc_method_list *obsolete;
2422 int count;
2423 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002424 };
2425*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002426
2427/// GetMethodConstant - Return a struct objc_method constant for the
2428/// given method if it has been defined. The result is null if the
2429/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002430llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002431 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002432 if (!Fn)
2433 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002434
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002435 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002436 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002437 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002438 ObjCTypes.SelectorPtrTy);
2439 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002440 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002441 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002442}
2443
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002444llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002445 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002446 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002447 // Return null for empty list.
2448 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002449 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002450
2451 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002452 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002453 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002454 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002456 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002457 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002458
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002459 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002460 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002462}
2463
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002464llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002465 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002466 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002467 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002468
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002469 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002470 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002471 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002472 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002473 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002474 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002475 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002476 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002477 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002478
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002479 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002480}
2481
Daniel Dunbar30c65362009-03-09 20:09:19 +00002482llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002483CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002484 llvm::Constant *Init,
2485 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002486 unsigned Align,
2487 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002488 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002489 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002490 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002491 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002492 if (Section)
2493 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002494 if (Align)
2495 GV->setAlignment(Align);
2496 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002497 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002498 return GV;
2499}
2500
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002501llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002502 // Abuse this interface function as a place to finalize.
2503 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002504 return NULL;
2505}
2506
Chris Lattnerd4808922009-03-22 21:03:39 +00002507llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002508 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002509}
2510
Chris Lattnerd4808922009-03-22 21:03:39 +00002511llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002512 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002513}
2514
David Chisnall168b80f2010-12-26 22:13:16 +00002515llvm::Constant *CGObjCMac::GetGetStructFunction() {
2516 return ObjCTypes.getCopyStructFn();
2517}
2518llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002519 return ObjCTypes.getCopyStructFn();
2520}
2521
Chris Lattnerd4808922009-03-22 21:03:39 +00002522llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002523 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002524}
2525
John McCallbd309292010-07-06 01:34:17 +00002526void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2527 return EmitTryOrSynchronizedStmt(CGF, S);
2528}
2529
2530void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2531 const ObjCAtSynchronizedStmt &S) {
2532 return EmitTryOrSynchronizedStmt(CGF, S);
2533}
2534
John McCall65bea082010-07-21 06:59:36 +00002535namespace {
John McCallcda666c2010-07-21 07:22:38 +00002536 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002537 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002538 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002539 llvm::Value *CallTryExitVar;
2540 llvm::Value *ExceptionData;
2541 ObjCTypesHelper &ObjCTypes;
2542 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002543 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002544 llvm::Value *CallTryExitVar,
2545 llvm::Value *ExceptionData,
2546 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002547 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002548 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2549
2550 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2551 // Check whether we need to call objc_exception_try_exit.
2552 // In optimized code, this branch will always be folded.
2553 llvm::BasicBlock *FinallyCallExit =
2554 CGF.createBasicBlock("finally.call_exit");
2555 llvm::BasicBlock *FinallyNoCallExit =
2556 CGF.createBasicBlock("finally.no_call_exit");
2557 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2558 FinallyCallExit, FinallyNoCallExit);
2559
2560 CGF.EmitBlock(FinallyCallExit);
2561 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2562 ->setDoesNotThrow();
2563
2564 CGF.EmitBlock(FinallyNoCallExit);
2565
2566 if (isa<ObjCAtTryStmt>(S)) {
2567 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002568 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2569 // Save the current cleanup destination in case there's
2570 // control flow inside the finally statement.
2571 llvm::Value *CurCleanupDest =
2572 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2573
John McCall65bea082010-07-21 06:59:36 +00002574 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2575
John McCallcebe0ca2010-08-11 00:16:14 +00002576 if (CGF.HaveInsertPoint()) {
2577 CGF.Builder.CreateStore(CurCleanupDest,
2578 CGF.getNormalCleanupDestSlot());
2579 } else {
2580 // Currently, the end of the cleanup must always exist.
2581 CGF.EnsureInsertPoint();
2582 }
2583 }
John McCall65bea082010-07-21 06:59:36 +00002584 } else {
2585 // Emit objc_sync_exit(expr); as finally's sole statement for
2586 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002587 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002588 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2589 ->setDoesNotThrow();
2590 }
2591 }
2592 };
John McCall42227ed2010-07-31 23:20:56 +00002593
2594 class FragileHazards {
2595 CodeGenFunction &CGF;
2596 llvm::SmallVector<llvm::Value*, 20> Locals;
2597 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2598
2599 llvm::InlineAsm *ReadHazard;
2600 llvm::InlineAsm *WriteHazard;
2601
2602 llvm::FunctionType *GetAsmFnType();
2603
2604 void collectLocals();
2605 void emitReadHazard(CGBuilderTy &Builder);
2606
2607 public:
2608 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002609
John McCall42227ed2010-07-31 23:20:56 +00002610 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002611 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002612 };
2613}
2614
2615/// Create the fragile-ABI read and write hazards based on the current
2616/// state of the function, which is presumed to be immediately prior
2617/// to a @try block. These hazards are used to maintain correct
2618/// semantics in the face of optimization and the fragile ABI's
2619/// cavalier use of setjmp/longjmp.
2620FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2621 collectLocals();
2622
2623 if (Locals.empty()) return;
2624
2625 // Collect all the blocks in the function.
2626 for (llvm::Function::iterator
2627 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2628 BlocksBeforeTry.insert(&*I);
2629
2630 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2631
2632 // Create a read hazard for the allocas. This inhibits dead-store
2633 // optimizations and forces the values to memory. This hazard is
2634 // inserted before any 'throwing' calls in the protected scope to
2635 // reflect the possibility that the variables might be read from the
2636 // catch block if the call throws.
2637 {
2638 std::string Constraint;
2639 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2640 if (I) Constraint += ',';
2641 Constraint += "*m";
2642 }
2643
2644 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2645 }
2646
2647 // Create a write hazard for the allocas. This inhibits folding
2648 // loads across the hazard. This hazard is inserted at the
2649 // beginning of the catch path to reflect the possibility that the
2650 // variables might have been written within the protected scope.
2651 {
2652 std::string Constraint;
2653 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2654 if (I) Constraint += ',';
2655 Constraint += "=*m";
2656 }
2657
2658 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2659 }
2660}
2661
2662/// Emit a write hazard at the current location.
2663void FragileHazards::emitWriteHazard() {
2664 if (Locals.empty()) return;
2665
2666 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2667 ->setDoesNotThrow();
2668}
2669
John McCall42227ed2010-07-31 23:20:56 +00002670void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2671 assert(!Locals.empty());
2672 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2673 ->setDoesNotThrow();
2674}
2675
2676/// Emit read hazards in all the protected blocks, i.e. all the blocks
2677/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002678void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002679 if (Locals.empty()) return;
2680
2681 CGBuilderTy Builder(CGF.getLLVMContext());
2682
2683 // Iterate through all blocks, skipping those prior to the try.
2684 for (llvm::Function::iterator
2685 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2686 llvm::BasicBlock &BB = *FI;
2687 if (BlocksBeforeTry.count(&BB)) continue;
2688
2689 // Walk through all the calls in the block.
2690 for (llvm::BasicBlock::iterator
2691 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2692 llvm::Instruction &I = *BI;
2693
2694 // Ignore instructions that aren't non-intrinsic calls.
2695 // These are the only calls that can possibly call longjmp.
2696 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2697 if (isa<llvm::IntrinsicInst>(I))
2698 continue;
2699
2700 // Ignore call sites marked nounwind. This may be questionable,
2701 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2702 llvm::CallSite CS(&I);
2703 if (CS.doesNotThrow()) continue;
2704
John McCall2dd7d442010-08-04 05:59:32 +00002705 // Insert a read hazard before the call. This will ensure that
2706 // any writes to the locals are performed before making the
2707 // call. If the call throws, then this is sufficient to
2708 // guarantee correctness as long as it doesn't also write to any
2709 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002710 Builder.SetInsertPoint(&BB, BI);
2711 emitReadHazard(Builder);
2712 }
2713 }
2714}
2715
2716static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2717 if (V) S.insert(V);
2718}
2719
2720void FragileHazards::collectLocals() {
2721 // Compute a set of allocas to ignore.
2722 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2723 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2724 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2725 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2726
2727 // Collect all the allocas currently in the function. This is
2728 // probably way too aggressive.
2729 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2730 for (llvm::BasicBlock::iterator
2731 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2732 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2733 Locals.push_back(&*I);
2734}
2735
2736llvm::FunctionType *FragileHazards::GetAsmFnType() {
2737 std::vector<const llvm::Type *> Tys(Locals.size());
2738 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2739 Tys[I] = Locals[I]->getType();
2740 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002741}
2742
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002743/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002744
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002745 Objective-C setjmp-longjmp (sjlj) Exception Handling
2746 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002747
John McCallbd309292010-07-06 01:34:17 +00002748 A catch buffer is a setjmp buffer plus:
2749 - a pointer to the exception that was caught
2750 - a pointer to the previous exception data buffer
2751 - two pointers of reserved storage
2752 Therefore catch buffers form a stack, with a pointer to the top
2753 of the stack kept in thread-local storage.
2754
2755 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2756 objc_exception_try_exit pops the given catch buffer, which is
2757 required to be the top of the EH stack.
2758 objc_exception_throw pops the top of the EH stack, writes the
2759 thrown exception into the appropriate field, and longjmps
2760 to the setjmp buffer. It crashes the process (with a printf
2761 and an abort()) if there are no catch buffers on the stack.
2762 objc_exception_extract just reads the exception pointer out of the
2763 catch buffer.
2764
2765 There's no reason an implementation couldn't use a light-weight
2766 setjmp here --- something like __builtin_setjmp, but API-compatible
2767 with the heavyweight setjmp. This will be more important if we ever
2768 want to implement correct ObjC/C++ exception interactions for the
2769 fragile ABI.
2770
2771 Note that for this use of setjmp/longjmp to be correct, we may need
2772 to mark some local variables volatile: if a non-volatile local
2773 variable is modified between the setjmp and the longjmp, it has
2774 indeterminate value. For the purposes of LLVM IR, it may be
2775 sufficient to make loads and stores within the @try (to variables
2776 declared outside the @try) volatile. This is necessary for
2777 optimized correctness, but is not currently being done; this is
2778 being tracked as rdar://problem/8160285
2779
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002780 The basic framework for a @try-catch-finally is as follows:
2781 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002782 objc_exception_data d;
2783 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002784 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002785
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002786 objc_exception_try_enter(&d);
2787 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002788 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002789 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002790 // exception path
2791 id _caught = objc_exception_extract(&d);
2792
2793 // enter new try scope for handlers
2794 if (!setjmp(d.jmp_buf)) {
2795 ... match exception and execute catch blocks ...
2796
2797 // fell off end, rethrow.
2798 _rethrow = _caught;
2799 ... jump-through-finally to finally_rethrow ...
2800 } else {
2801 // exception in catch block
2802 _rethrow = objc_exception_extract(&d);
2803 _call_try_exit = false;
2804 ... jump-through-finally to finally_rethrow ...
2805 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002806 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002807 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002808
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002809 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002810 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002811 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002812
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002813 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002814 ... dispatch to finally destination ...
2815
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002816 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002817 objc_exception_throw(_rethrow);
2818
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002819 finally_end:
2820 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002821
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002822 This framework differs slightly from the one gcc uses, in that gcc
2823 uses _rethrow to determine if objc_exception_try_exit should be called
2824 and if the object should be rethrown. This breaks in the face of
2825 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002826
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002827 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002829 - If there are no catch blocks, then we avoid emitting the second
2830 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002831
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002832 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2833 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002834
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002835 - FIXME: If there is no @finally block we can do a few more
2836 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002837
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002838 Rethrows and Jumps-Through-Finally
2839 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002840
John McCallbd309292010-07-06 01:34:17 +00002841 '@throw;' is supported by pushing the currently-caught exception
2842 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002843
John McCallbd309292010-07-06 01:34:17 +00002844 Branches through the @finally block are handled with an ordinary
2845 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2846 exceptions are not compatible with C++ exceptions, and this is
2847 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002848
John McCallbd309292010-07-06 01:34:17 +00002849 @synchronized(expr) { stmt; } is emitted as if it were:
2850 id synch_value = expr;
2851 objc_sync_enter(synch_value);
2852 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002853*/
2854
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002855void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2856 const Stmt &S) {
2857 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002858
2859 // A destination for the fall-through edges of the catch handlers to
2860 // jump to.
2861 CodeGenFunction::JumpDest FinallyEnd =
2862 CGF.getJumpDestInCurrentScope("finally.end");
2863
2864 // A destination for the rethrow edge of the catch handlers to jump
2865 // to.
2866 CodeGenFunction::JumpDest FinallyRethrow =
2867 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002868
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002869 // For @synchronized, call objc_sync_enter(sync.expr). The
2870 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002871 // @synchronized. We can't avoid a temp here because we need the
2872 // value to be preserved. If the backend ever does liveness
2873 // correctly after setjmp, this will be unnecessary.
2874 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002875 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002876 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002877 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2878 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002879 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2880 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002881
2882 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2883 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002884 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002885
John McCall2dd7d442010-08-04 05:59:32 +00002886 // Allocate memory for the setjmp buffer. This needs to be kept
2887 // live throughout the try and catch blocks.
2888 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2889 "exceptiondata.ptr");
2890
John McCall42227ed2010-07-31 23:20:56 +00002891 // Create the fragile hazards. Note that this will not capture any
2892 // of the allocas required for exception processing, but will
2893 // capture the current basic block (which extends all the way to the
2894 // setjmp call) as "before the @try".
2895 FragileHazards Hazards(CGF);
2896
John McCallbd309292010-07-06 01:34:17 +00002897 // Create a flag indicating whether the cleanup needs to call
2898 // objc_exception_try_exit. This is true except when
2899 // - no catches match and we're branching through the cleanup
2900 // just to rethrow the exception, or
2901 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002902 // The setjmp-safety rule here is that we should always store to this
2903 // variable in a place that dominates the branch through the cleanup
2904 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002905 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002906 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002907
John McCall9916e3f2010-10-04 23:42:51 +00002908 // A slot containing the exception to rethrow. Only needed when we
2909 // have both a @catch and a @finally.
2910 llvm::Value *PropagatingExnVar = 0;
2911
John McCallbd309292010-07-06 01:34:17 +00002912 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002913 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002914 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002915 CallTryExitVar,
2916 ExceptionData,
2917 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002918
2919 // Enter a try block:
2920 // - Call objc_exception_try_enter to push ExceptionData on top of
2921 // the EH stack.
2922 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2923 ->setDoesNotThrow();
2924
2925 // - Call setjmp on the exception data buffer.
2926 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2927 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2928 llvm::Value *SetJmpBuffer =
2929 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2930 llvm::CallInst *SetJmpResult =
2931 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2932 SetJmpResult->setDoesNotThrow();
2933
2934 // If setjmp returned 0, enter the protected block; otherwise,
2935 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002936 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2937 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002938 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002939 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2940 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002941
John McCallbd309292010-07-06 01:34:17 +00002942 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002943 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002944 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002945 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002946 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002947
2948 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002949
John McCallbd309292010-07-06 01:34:17 +00002950 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002951 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002952
John McCall42227ed2010-07-31 23:20:56 +00002953 // Don't optimize loads of the in-scope locals across this point.
2954 Hazards.emitWriteHazard();
2955
John McCallbd309292010-07-06 01:34:17 +00002956 // For a @synchronized (or a @try with no catches), just branch
2957 // through the cleanup to the rethrow block.
2958 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2959 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002960 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002961 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002962
2963 // Otherwise, we have to match against the caught exceptions.
2964 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002965 // Retrieve the exception object. We may emit multiple blocks but
2966 // nothing can cross this so the value is already in SSA form.
2967 llvm::CallInst *Caught =
2968 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2969 ExceptionData, "caught");
2970 Caught->setDoesNotThrow();
2971
John McCallbd309292010-07-06 01:34:17 +00002972 // Push the exception to rethrow onto the EH value stack for the
2973 // benefit of any @throws in the handlers.
2974 CGF.ObjCEHValueStack.push_back(Caught);
2975
Douglas Gregor96c79492010-04-23 22:50:49 +00002976 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002977
John McCall2dd7d442010-08-04 05:59:32 +00002978 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00002979
John McCall2dd7d442010-08-04 05:59:32 +00002980 llvm::BasicBlock *CatchBlock = 0;
2981 llvm::BasicBlock *CatchHandler = 0;
2982 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00002983 // Save the currently-propagating exception before
2984 // objc_exception_try_enter clears the exception slot.
2985 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2986 "propagating_exception");
2987 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2988
John McCall2dd7d442010-08-04 05:59:32 +00002989 // Enter a new exception try block (in case a @catch block
2990 // throws an exception).
2991 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2992 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002993
John McCall2dd7d442010-08-04 05:59:32 +00002994 llvm::CallInst *SetJmpResult =
2995 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2996 "setjmp.result");
2997 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002998
John McCall2dd7d442010-08-04 05:59:32 +00002999 llvm::Value *Threw =
3000 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3001
3002 CatchBlock = CGF.createBasicBlock("catch");
3003 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3004 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3005
3006 CGF.EmitBlock(CatchBlock);
3007 }
3008
3009 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003010
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003011 // Handle catch list. As a special case we check if everything is
3012 // matched and avoid generating code for falling off the end if
3013 // so.
3014 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003015 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3016 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003017
Douglas Gregor46a572b2010-04-26 16:46:50 +00003018 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003019 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003020
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003021 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003022 if (!CatchParam) {
3023 AllMatched = true;
3024 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003025 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003026
John McCallbd309292010-07-06 01:34:17 +00003027 // catch(id e) always matches under this ABI, since only
3028 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003029 // FIXME: For the time being we also match id<X>; this should
3030 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003031 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003032 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003033 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003034
John McCallbd309292010-07-06 01:34:17 +00003035 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003036 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003037 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3038
Anders Carlsson9396a892008-09-11 09:15:33 +00003039 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003040 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003041 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003042
3043 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003044 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003045 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003046
Anders Carlsson9396a892008-09-11 09:15:33 +00003047 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003048
3049 // The scope of the catch variable ends right here.
3050 CatchVarCleanups.ForceCleanup();
3051
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003052 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003053 break;
3054 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003055
Steve Naroff7cae42b2009-07-10 23:34:53 +00003056 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003057 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003058
3059 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003060 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3061 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003062
3063 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003064 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003065
John McCallbd309292010-07-06 01:34:17 +00003066 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003067 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3068 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003069 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003070
John McCallbd309292010-07-06 01:34:17 +00003071 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3072 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003073
3074 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003075 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003076
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003077 // Emit the @catch block.
3078 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003079
3080 // Collect any cleanups for the catch variable. The scope lasts until
3081 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003082 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003083
John McCall1c9c3fd2010-10-15 04:57:14 +00003084 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003085 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003086
John McCallbd309292010-07-06 01:34:17 +00003087 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003088 llvm::Value *Tmp =
3089 CGF.Builder.CreateBitCast(Caught,
3090 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003091 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003092 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003093
Anders Carlsson9396a892008-09-11 09:15:33 +00003094 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003095
3096 // We're done with the catch variable.
3097 CatchVarCleanups.ForceCleanup();
3098
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003099 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003101 CGF.EmitBlock(NextCatchBlock);
3102 }
3103
John McCallbd309292010-07-06 01:34:17 +00003104 CGF.ObjCEHValueStack.pop_back();
3105
John McCall2dd7d442010-08-04 05:59:32 +00003106 // If nothing wanted anything to do with the caught exception,
3107 // kill the extract call.
3108 if (Caught->use_empty())
3109 Caught->eraseFromParent();
3110
3111 if (!AllMatched)
3112 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3113
3114 if (HasFinally) {
3115 // Emit the exception handler for the @catch blocks.
3116 CGF.EmitBlock(CatchHandler);
3117
3118 // In theory we might now need a write hazard, but actually it's
3119 // unnecessary because there's no local-accessing code between
3120 // the try's write hazard and here.
3121 //Hazards.emitWriteHazard();
3122
John McCall9916e3f2010-10-04 23:42:51 +00003123 // Extract the new exception and save it to the
3124 // propagating-exception slot.
3125 assert(PropagatingExnVar);
3126 llvm::CallInst *NewCaught =
3127 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3128 ExceptionData, "caught");
3129 NewCaught->setDoesNotThrow();
3130 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3131
John McCall2dd7d442010-08-04 05:59:32 +00003132 // Don't pop the catch handler; the throw already did.
3133 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003134 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003135 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003136 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003137
John McCall42227ed2010-07-31 23:20:56 +00003138 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003139 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003140
John McCallbd309292010-07-06 01:34:17 +00003141 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003142 CGF.Builder.restoreIP(TryFallthroughIP);
3143 if (CGF.HaveInsertPoint())
3144 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003145 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003146 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003147
John McCallbd309292010-07-06 01:34:17 +00003148 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003149 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003150 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003151 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003152 // If we have a propagating-exception variable, check it.
3153 llvm::Value *PropagatingExn;
3154 if (PropagatingExnVar) {
3155 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003156
John McCall9916e3f2010-10-04 23:42:51 +00003157 // Otherwise, just look in the buffer for the exception to throw.
3158 } else {
3159 llvm::CallInst *Caught =
3160 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3161 ExceptionData);
3162 Caught->setDoesNotThrow();
3163 PropagatingExn = Caught;
3164 }
3165
3166 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003167 ->setDoesNotThrow();
3168 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003169 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003170
John McCall42227ed2010-07-31 23:20:56 +00003171 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003172}
3173
3174void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003175 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003176 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003177
Anders Carlssone005aa12008-09-09 16:16:55 +00003178 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3179 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003180 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003181 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3182 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003183 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003184 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003185 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003186 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003187
John McCallbd309292010-07-06 01:34:17 +00003188 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3189 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003190 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003191
3192 // Clear the insertion point to indicate we are in unreachable code.
3193 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003194}
3195
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003196/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003197/// object: objc_read_weak (id *src)
3198///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003199llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003200 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003201 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003202 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3203 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3204 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003205 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003206 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003207 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003208 return read_weak;
3209}
3210
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003211/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3212/// objc_assign_weak (id src, id *dst)
3213///
3214void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003215 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003216 const llvm::Type * SrcTy = src->getType();
3217 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003218 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003219 assert(Size <= 8 && "does not support size > 8");
3220 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003221 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003222 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3223 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003224 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3225 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003226 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003227 src, dst, "weakassign");
3228 return;
3229}
3230
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003231/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3232/// objc_assign_global (id src, id *dst)
3233///
3234void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003235 llvm::Value *src, llvm::Value *dst,
3236 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003237 const llvm::Type * SrcTy = src->getType();
3238 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003239 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003240 assert(Size <= 8 && "does not support size > 8");
3241 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003242 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003243 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3244 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003245 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3246 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003247 if (!threadlocal)
3248 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3249 src, dst, "globalassign");
3250 else
3251 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3252 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003253 return;
3254}
3255
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003256/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003257/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003258///
3259void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003260 llvm::Value *src, llvm::Value *dst,
3261 llvm::Value *ivarOffset) {
3262 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003263 const llvm::Type * SrcTy = src->getType();
3264 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003265 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003266 assert(Size <= 8 && "does not support size > 8");
3267 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003268 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003269 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3270 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003271 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3272 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003273 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3274 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003275 return;
3276}
3277
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003278/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3279/// objc_assign_strongCast (id src, id *dst)
3280///
3281void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003282 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003283 const llvm::Type * SrcTy = src->getType();
3284 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003285 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003286 assert(Size <= 8 && "does not support size > 8");
3287 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003288 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003289 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3290 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003291 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3292 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003293 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003294 src, dst, "weakassign");
3295 return;
3296}
3297
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003298void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003299 llvm::Value *DestPtr,
3300 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003301 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003302 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3303 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003304 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003305 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003306 return;
3307}
3308
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003309/// EmitObjCValueForIvar - Code Gen for ivar reference.
3310///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003311LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3312 QualType ObjectTy,
3313 llvm::Value *BaseValue,
3314 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003315 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003316 const ObjCInterfaceDecl *ID =
3317 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003318 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3319 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003320}
3321
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003322llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003323 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003324 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003325 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003326 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003327 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3328 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003329}
3330
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003331/* *** Private Interface *** */
3332
3333/// EmitImageInfo - Emit the image info marker used to encode some module
3334/// level information.
3335///
3336/// See: <rdr://4810609&4810587&4810587>
3337/// struct IMAGE_INFO {
3338/// unsigned version;
3339/// unsigned flags;
3340/// };
3341enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003342 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003343 eImageInfo_GarbageCollected = (1 << 1),
3344 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003345 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3346
Daniel Dunbar5e639272010-04-25 20:39:01 +00003347 // A flag indicating that the module has no instances of a @synthesize of a
3348 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003349 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003350};
3351
Daniel Dunbar5e639272010-04-25 20:39:01 +00003352void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003353 unsigned version = 0; // Version is unused?
3354 unsigned flags = 0;
3355
3356 // FIXME: Fix and continue?
3357 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3358 flags |= eImageInfo_GarbageCollected;
3359 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3360 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003361
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003362 // We never allow @synthesize of a superclass property.
3363 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003364
Chris Lattner5e016ae2010-06-27 07:15:29 +00003365 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3366
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003367 // Emitted as int[2];
3368 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003369 llvm::ConstantInt::get(Int32Ty, version),
3370 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003371 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003372 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003373
3374 const char *Section;
3375 if (ObjCABI == 1)
3376 Section = "__OBJC, __image_info,regular";
3377 else
3378 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003379 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003380 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003381 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003382 Section,
3383 0,
3384 true);
3385 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003386}
3387
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003388
3389// struct objc_module {
3390// unsigned long version;
3391// unsigned long size;
3392// const char *name;
3393// Symtab symtab;
3394// };
3395
3396// FIXME: Get from somewhere
3397static const int ModuleVersion = 7;
3398
3399void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003400 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003401
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003402 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003403 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3404 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003405 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003406 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003407 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003408 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003409 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003410 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003411 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003412}
3413
3414llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003415 unsigned NumClasses = DefinedClasses.size();
3416 unsigned NumCategories = DefinedCategories.size();
3417
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003418 // Return null if no symbols were defined.
3419 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003420 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003421
3422 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003423 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003424 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003425 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3426 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003427
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003428 // The runtime expects exactly the list of defined classes followed
3429 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003430 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003431 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003432 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003433 ObjCTypes.Int8PtrTy);
3434 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003435 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003436 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003437 ObjCTypes.Int8PtrTy);
3438
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003439 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003440 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003441 NumClasses + NumCategories),
3442 Symbols);
3443
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003444 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003445
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003446 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003447 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3448 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003449 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003450 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003451}
3452
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003453llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003454 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003455 LazySymbols.insert(ID->getIdentifier());
3456
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003457 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003458
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003459 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003460 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003461 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003462 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003463 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003464 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3465 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003466 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003467 }
3468
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003469 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003470}
3471
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003472llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3473 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003474 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003475
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003476 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003477 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003478 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003479 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003480 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003481 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3482 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003483 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003484 }
3485
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003486 if (lvalue)
3487 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003488 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003489}
3490
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003491llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003492 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003493
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003494 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003495 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003496 llvm::ConstantArray::get(VMContext,
3497 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003498 ((ObjCABI == 2) ?
3499 "__TEXT,__objc_classname,cstring_literals" :
3500 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003501 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003502
Owen Anderson170229f2009-07-14 23:10:40 +00003503 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003504}
3505
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003506llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3507 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3508 I = MethodDefinitions.find(MD);
3509 if (I != MethodDefinitions.end())
3510 return I->second;
3511
3512 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3513 // MD isn't emitted yet because it comes from PCH.
3514 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3515 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3516 return MethodDefinitions[MD];
3517 }
3518
3519 return NULL;
3520}
3521
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003522/// GetIvarLayoutName - Returns a unique constant for the given
3523/// ivar layout bitmap.
3524llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003525 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003526 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003527}
3528
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003529void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003530 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003531 bool ForStrongLayout,
3532 bool &HasUnion) {
3533 const RecordDecl *RD = RT->getDecl();
3534 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003535 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003536 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003537 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003538 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003539
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003540 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3541 ForStrongLayout, HasUnion);
3542}
3543
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003544void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003545 const llvm::StructLayout *Layout,
3546 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003547 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003548 unsigned int BytePos, bool ForStrongLayout,
3549 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003550 bool IsUnion = (RD && RD->isUnion());
3551 uint64_t MaxUnionIvarSize = 0;
3552 uint64_t MaxSkippedUnionIvarSize = 0;
3553 FieldDecl *MaxField = 0;
3554 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003555 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003556 uint64_t MaxFieldOffset = 0;
3557 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003558 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003560 if (RecFields.empty())
3561 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003562 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3563 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3564
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003565 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003566 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003567 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003568 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003569 // Note that 'i' here is actually the field index inside RD of Field,
3570 // although this dependency is hidden.
3571 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckc5ca8762011-04-14 00:43:09 +00003572 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003573 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003574 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003575
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003576 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003577 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003578 LastFieldBitfieldOrUnnamed = Field;
3579 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003580 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003581 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003582
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003583 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003584 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003585 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003586 if (FQT->isUnionType())
3587 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003588
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003589 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003590 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003591 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003592 continue;
3593 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003594
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003595 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003596 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003597 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003598 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003599 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003600 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003601 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3602 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003603 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003604 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003605 FQT = CArray->getElementType();
3606 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003607
3608 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003609 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003610 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003611 int OldIndex = IvarsInfo.size() - 1;
3612 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003613
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003614 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003615 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003616 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003617
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003618 // Replicate layout information for each array element. Note that
3619 // one element is already done.
3620 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003621 for (int FirstIndex = IvarsInfo.size() - 1,
3622 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003623 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003624 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3625 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3626 IvarsInfo[i].ivar_size));
3627 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3628 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3629 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003630 }
3631 continue;
3632 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003633 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003634 // At this point, we are done with Record/Union and array there of.
3635 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003636 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003637
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003638 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003639 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3640 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003641 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003642 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003643 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003644 MaxUnionIvarSize = UnionIvarSize;
3645 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003646 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003647 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003648 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003649 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003650 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003651 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003652 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003653 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3654 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003655 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003656 // FIXME: Why the asymmetry? We divide by word size in bits on other
3657 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003658 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003659 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003660 MaxSkippedUnionIvarSize = UnionIvarSize;
3661 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003662 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003663 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003664 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003665 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003666 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003667 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003668 }
3669 }
3670 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003671
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003672 if (LastFieldBitfieldOrUnnamed) {
3673 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3674 // Last field was a bitfield. Must update skip info.
3675 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3676 uint64_t BitFieldSize =
3677 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3678 GC_IVAR skivar;
3679 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3680 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3681 + ((BitFieldSize % ByteSizeInBits) != 0);
3682 SkipIvars.push_back(skivar);
3683 } else {
3684 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3685 // Last field was unnamed. Must update skip info.
3686 unsigned FieldSize
3687 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3688 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3689 FieldSize / ByteSizeInBits));
3690 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003691 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003692
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003693 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003694 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003695 MaxUnionIvarSize));
3696 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003697 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003698 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003699}
3700
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003701/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3702/// the computations and returning the layout bitmap (for ivar or blocks) in
3703/// the given argument BitMap string container. Routine reads
3704/// two containers, IvarsInfo and SkipIvars which are assumed to be
3705/// filled already by the caller.
3706llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003707 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003708 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003709
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003710 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003711 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003712 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003713 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003714 if (IvarsInfo[0].ivar_bytepos == 0) {
3715 WordsToSkip = 0;
3716 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003717 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003718 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3719 WordsToScan = IvarsInfo[0].ivar_size;
3720 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003721 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003722 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003723 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003724 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003725 // consecutive 'scanned' object pointers.
3726 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003727 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003728 // Skip over 'gc'able object pointer which lay over each other.
3729 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3730 continue;
3731 // Must skip over 1 or more words. We save current skip/scan values
3732 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003733 SKIP_SCAN SkScan;
3734 SkScan.skip = WordsToSkip;
3735 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003736 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003737
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003738 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003739 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3740 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003741 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003742 WordsToSkip = 0;
3743 WordsToScan = IvarsInfo[i].ivar_size;
3744 }
3745 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003746 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003747 SKIP_SCAN SkScan;
3748 SkScan.skip = WordsToSkip;
3749 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003750 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003751 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003752
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003753 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003754 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003755 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003756 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003757 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003758 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003759 IvarsInfo[LastIndex].ivar_bytepos +
3760 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003761 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003762 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003763 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003764 SKIP_SCAN SkScan;
3765 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3766 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003767 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003768 }
3769 }
3770 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3771 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003772 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003773 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003774 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3775 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3776 // 0xM0 followed by 0x0N detected.
3777 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3778 for (int j = i+1; j < SkipScan; j++)
3779 SkipScanIvars[j] = SkipScanIvars[j+1];
3780 --SkipScan;
3781 }
3782 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003783
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003784 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003785 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003786 unsigned char byte;
3787 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3788 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3789 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3790 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003791
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003792 // first skip big.
3793 for (unsigned int ix = 0; ix < skip_big; ix++)
3794 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003795
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003796 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003797 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003798 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003799 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003800 byte |= 0xf;
3801 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003802 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003803 byte |= scan_small;
3804 scan_small = 0;
3805 }
3806 BitMap += byte;
3807 }
3808 // next scan big
3809 for (unsigned int ix = 0; ix < scan_big; ix++)
3810 BitMap += (unsigned char)(0x0f);
3811 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003812 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003813 byte = scan_small;
3814 BitMap += byte;
3815 }
3816 }
3817 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003818 unsigned char zero = 0;
3819 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003820
3821 llvm::GlobalVariable * Entry =
3822 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3823 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003824 ((ObjCABI == 2) ?
3825 "__TEXT,__objc_classname,cstring_literals" :
3826 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003827 1, true);
3828 return getConstantGEP(VMContext, Entry, 0, 0);
3829}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003830
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003831/// BuildIvarLayout - Builds ivar layout bitmap for the class
3832/// implementation for the __strong or __weak case.
3833/// The layout map displays which words in ivar list must be skipped
3834/// and which must be scanned by GC (see below). String is built of bytes.
3835/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3836/// of words to skip and right nibble is count of words to scan. So, each
3837/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3838/// represented by a 0x00 byte which also ends the string.
3839/// 1. when ForStrongLayout is true, following ivars are scanned:
3840/// - id, Class
3841/// - object *
3842/// - __strong anything
3843///
3844/// 2. When ForStrongLayout is false, following ivars are scanned:
3845/// - __weak anything
3846///
3847llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3848 const ObjCImplementationDecl *OMD,
3849 bool ForStrongLayout) {
3850 bool hasUnion = false;
3851
3852 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3853 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3854 return llvm::Constant::getNullValue(PtrTy);
3855
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003856 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003857 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003858 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003859
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003860 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003861 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3862 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3863
3864 if (RecFields.empty())
3865 return llvm::Constant::getNullValue(PtrTy);
3866
3867 SkipIvars.clear();
3868 IvarsInfo.clear();
3869
3870 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3871 if (IvarsInfo.empty())
3872 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003873 // Sort on byte position in case we encounterred a union nested in
3874 // the ivar list.
3875 if (hasUnion && !IvarsInfo.empty())
3876 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3877 if (hasUnion && !SkipIvars.empty())
3878 std::sort(SkipIvars.begin(), SkipIvars.end());
3879
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003880 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003881 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003882
3883 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003884 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003885 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003886 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003887 const unsigned char *s = (unsigned char*)BitMap.c_str();
3888 for (unsigned i = 0; i < BitMap.size(); i++)
3889 if (!(s[i] & 0xf0))
3890 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3891 else
3892 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3893 printf("\n");
3894 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003895 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003896}
3897
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003898llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003899 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3900
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003901 // FIXME: Avoid std::string copying.
3902 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003903 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003904 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003905 ((ObjCABI == 2) ?
3906 "__TEXT,__objc_methname,cstring_literals" :
3907 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003908 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003909
Owen Anderson170229f2009-07-14 23:10:40 +00003910 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003911}
3912
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003913// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003914llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003915 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3916}
3917
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003918llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003919 std::string TypeStr;
3920 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3921
3922 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003923
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003924 if (!Entry)
3925 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003926 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003927 ((ObjCABI == 2) ?
3928 "__TEXT,__objc_methtype,cstring_literals" :
3929 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003930 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003931
Owen Anderson170229f2009-07-14 23:10:40 +00003932 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003933}
3934
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003935llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003936 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003937 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3938 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003939
3940 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3941
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003942 if (!Entry)
3943 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003944 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003945 ((ObjCABI == 2) ?
3946 "__TEXT,__objc_methtype,cstring_literals" :
3947 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003948 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003949
Owen Anderson170229f2009-07-14 23:10:40 +00003950 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003951}
3952
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003953// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003954llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003955 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003956
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003957 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003958 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003959 llvm::ConstantArray::get(VMContext,
3960 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003961 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003962 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003963
Owen Anderson170229f2009-07-14 23:10:40 +00003964 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003965}
3966
3967// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003968// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003969llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003970CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3971 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003972 std::string TypeStr;
3973 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003974 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3975}
3976
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003977void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003978 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003979 llvm::SmallVectorImpl<char> &Name) {
3980 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003981 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003982 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3983 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003984 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003985 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003986 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003987 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003988}
3989
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003990void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003991 EmitModuleInfo();
3992
Daniel Dunbarc475d422008-10-29 22:36:39 +00003993 // Emit the dummy bodies for any protocols which were referenced but
3994 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003995 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003996 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3997 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003998 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003999
Daniel Dunbarc475d422008-10-29 22:36:39 +00004000 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004001 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004002 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004003 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004004 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004005 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004006 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004007 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004008 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004009 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004010 }
4011
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004012 // Add assembler directives to add lazy undefined symbol references
4013 // for classes which are referenced but not defined. This is
4014 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004015 //
4016 // FIXME: It would be nice if we had an LLVM construct for this.
4017 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4018 llvm::SmallString<256> Asm;
4019 Asm += CGM.getModule().getModuleInlineAsm();
4020 if (!Asm.empty() && Asm.back() != '\n')
4021 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004022
Daniel Dunbard027a922009-09-07 00:20:42 +00004023 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004024 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4025 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004026 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4027 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004028 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004029 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004030 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004031 }
4032
4033 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4034 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4035 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4036 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004037
Daniel Dunbard027a922009-09-07 00:20:42 +00004038 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004039 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004040}
4041
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004042CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004043 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004044 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004045 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004046 ObjCABI = 2;
4047}
4048
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004049/* *** */
4050
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004051ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004052 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004053 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4054 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004055
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004056 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004057 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004058 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004059 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004060 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004061
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004062 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004063 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004064 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004065
Mike Stump18bb9282009-05-16 07:57:57 +00004066 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4067 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004068 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004069 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004070
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004071 // I'm not sure I like this. The implicit coordination is a bit
4072 // gross. We should solve this in a reasonable fashion because this
4073 // is a pretty common task (match some runtime data structure with
4074 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004075
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004076 // FIXME: This is leaked.
4077 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004078
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004079 // struct _objc_super {
4080 // id self;
4081 // Class cls;
4082 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004083 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004084 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004085 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004086 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004087 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004088 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004089 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004090 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004091 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004092
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004093 SuperCTy = Ctx.getTagDeclType(RD);
4094 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004096 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004097 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4098
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004099 // struct _prop_t {
4100 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004101 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004102 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004103 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004104 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004105 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004106
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004107 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004108 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004109 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004110 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004111 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004112 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004113 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004114 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004115 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004116 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004117 PropertyListTy);
4118 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004119 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004120
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004121 // struct _objc_method {
4122 // SEL _cmd;
4123 // char *method_type;
4124 // char *_imp;
4125 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004126 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004127 Int8PtrTy,
4128 Int8PtrTy,
4129 NULL);
4130 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004131
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004132 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004133 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004134 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004135 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004136}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004137
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004139 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004140 // struct _objc_method_description {
4141 // SEL name;
4142 // char *types;
4143 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004144 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004145 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004146 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004147 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004149 MethodDescriptionTy);
4150
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004151 // struct _objc_method_description_list {
4152 // int count;
4153 // struct _objc_method_description[1];
4154 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004156 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004157 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004158 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004159 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004160 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004162 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004163 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004164 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004165
Daniel Dunbarb036db82008-08-13 03:21:16 +00004166 // Protocol description structures
4167
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004168 // struct _objc_protocol_extension {
4169 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4170 // struct _objc_method_description_list *optional_instance_methods;
4171 // struct _objc_method_description_list *optional_class_methods;
4172 // struct _objc_property_list *instance_properties;
4173 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004174 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004175 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004176 MethodDescriptionListPtrTy,
4177 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004178 PropertyListPtrTy,
4179 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004180 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004181 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004182
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004183 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004184 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004185
Daniel Dunbarc475d422008-10-29 22:36:39 +00004186 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004187
Owen Andersonc36edfe2009-08-13 23:27:53 +00004188 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4189 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004190
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004191 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004192 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004193 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004194 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004195 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004196 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004197 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4198
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004199 // struct _objc_protocol {
4200 // struct _objc_protocol_extension *isa;
4201 // char *protocol_name;
4202 // struct _objc_protocol **_objc_protocol_list;
4203 // struct _objc_method_description_list *instance_methods;
4204 // struct _objc_method_description_list *class_methods;
4205 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004206 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004207 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004209 MethodDescriptionListPtrTy,
4210 MethodDescriptionListPtrTy,
4211 NULL);
4212 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4213
4214 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004215 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004216 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004217 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004218 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004219
4220 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004221 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004222 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004223
4224 // Class description structures
4225
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004226 // struct _objc_ivar {
4227 // char *ivar_name;
4228 // char *ivar_type;
4229 // int ivar_offset;
4230 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004231 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004232 Int8PtrTy,
4233 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004234 NULL);
4235 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4236
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004237 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004238 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004239 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004240 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004241
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004242 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004243 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004244 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004245 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004246
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004247 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004248 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004249 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004250 Int8PtrTy,
4251 PropertyListPtrTy,
4252 NULL);
4253 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004254 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004255
Owen Andersonc36edfe2009-08-13 23:27:53 +00004256 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004257
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004258 // struct _objc_class {
4259 // Class isa;
4260 // Class super_class;
4261 // char *name;
4262 // long version;
4263 // long info;
4264 // long instance_size;
4265 // struct _objc_ivar_list *ivars;
4266 // struct _objc_method_list *methods;
4267 // struct _objc_cache *cache;
4268 // struct _objc_protocol_list *protocols;
4269 // char *ivar_layout;
4270 // struct _objc_class_ext *ext;
4271 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004272 T = llvm::StructType::get(VMContext,
4273 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004274 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004275 Int8PtrTy,
4276 LongTy,
4277 LongTy,
4278 LongTy,
4279 IvarListPtrTy,
4280 MethodListPtrTy,
4281 CachePtrTy,
4282 ProtocolListPtrTy,
4283 Int8PtrTy,
4284 ClassExtensionPtrTy,
4285 NULL);
4286 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004287
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004288 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4289 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004290 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004291
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004292 // struct _objc_category {
4293 // char *category_name;
4294 // char *class_name;
4295 // struct _objc_method_list *instance_method;
4296 // struct _objc_method_list *class_method;
4297 // uint32_t size; // sizeof(struct _objc_category)
4298 // struct _objc_property_list *instance_properties;// category's @property
4299 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004300 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004301 Int8PtrTy,
4302 MethodListPtrTy,
4303 MethodListPtrTy,
4304 ProtocolListPtrTy,
4305 IntTy,
4306 PropertyListPtrTy,
4307 NULL);
4308 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4309
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004310 // Global metadata structures
4311
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004312 // struct _objc_symtab {
4313 // long sel_ref_cnt;
4314 // SEL *refs;
4315 // short cls_def_cnt;
4316 // short cat_def_cnt;
4317 // char *defs[cls_def_cnt + cat_def_cnt];
4318 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004319 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004320 SelectorPtrTy,
4321 ShortTy,
4322 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004323 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004324 NULL);
4325 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004326 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004327
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004328 // struct _objc_module {
4329 // long version;
4330 // long size; // sizeof(struct _objc_module)
4331 // char *name;
4332 // struct _objc_symtab* symtab;
4333 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004334 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004335 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336 LongTy,
4337 Int8PtrTy,
4338 SymtabPtrTy,
4339 NULL);
4340 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004341
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004342
Mike Stump18bb9282009-05-16 07:57:57 +00004343 // FIXME: This is the size of the setjmp buffer and should be target
4344 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004345 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004346
Anders Carlsson9ff22482008-09-09 10:10:21 +00004347 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004348 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004349 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004350
4351 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004352 llvm::StructType::get(VMContext,
4353 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4354 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004355 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004356 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004357 ExceptionDataTy);
4358
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004359}
4360
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004361ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004362 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004363 // struct _method_list_t {
4364 // uint32_t entsize; // sizeof(struct _objc_method)
4365 // uint32_t method_count;
4366 // struct _objc_method method_list[method_count];
4367 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004368 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004369 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004370 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004371 NULL);
4372 CGM.getModule().addTypeName("struct.__method_list_t",
4373 MethodListnfABITy);
4374 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004375 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004376
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004377 // struct _protocol_t {
4378 // id isa; // NULL
4379 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004380 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004381 // const struct method_list_t * const instance_methods;
4382 // const struct method_list_t * const class_methods;
4383 // const struct method_list_t *optionalInstanceMethods;
4384 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004385 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004386 // const uint32_t size; // sizeof(struct _protocol_t)
4387 // const uint32_t flags; // = 0
4388 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004389
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004390 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004391 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004392
Owen Anderson758428f2009-08-05 23:18:46 +00004393 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004394 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004395 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004396 ProtocolListTyHolder),
4397 MethodListnfABIPtrTy,
4398 MethodListnfABIPtrTy,
4399 MethodListnfABIPtrTy,
4400 MethodListnfABIPtrTy,
4401 PropertyListPtrTy,
4402 IntTy,
4403 IntTy,
4404 NULL);
4405 CGM.getModule().addTypeName("struct._protocol_t",
4406 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004407
4408 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004409 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004410
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004411 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004412 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004413 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004414 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004415 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004416 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004417 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004418 NULL);
4419 CGM.getModule().addTypeName("struct._objc_protocol_list",
4420 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004421 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004422 ProtocolListnfABITy);
4423
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004424 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004425 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004426
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004427 // struct _ivar_t {
4428 // unsigned long int *offset; // pointer to ivar offset location
4429 // char *name;
4430 // char *type;
4431 // uint32_t alignment;
4432 // uint32_t size;
4433 // }
Mike Stump11289f42009-09-09 15:08:12 +00004434 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004435 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004436 Int8PtrTy,
4437 Int8PtrTy,
4438 IntTy,
4439 IntTy,
4440 NULL);
4441 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004443 // struct _ivar_list_t {
4444 // uint32 entsize; // sizeof(struct _ivar_t)
4445 // uint32 count;
4446 // struct _iver_t list[count];
4447 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004448 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004449 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004450 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004451 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004452 NULL);
4453 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004454
Owen Anderson9793f0e2009-07-29 22:16:19 +00004455 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004456
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004457 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004458 // uint32_t const flags;
4459 // uint32_t const instanceStart;
4460 // uint32_t const instanceSize;
4461 // uint32_t const reserved; // only when building for 64bit targets
4462 // const uint8_t * const ivarLayout;
4463 // const char *const name;
4464 // const struct _method_list_t * const baseMethods;
4465 // const struct _objc_protocol_list *const baseProtocols;
4466 // const struct _ivar_list_t *const ivars;
4467 // const uint8_t * const weakIvarLayout;
4468 // const struct _prop_list_t * const properties;
4469 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004470
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004471 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004472 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004473 IntTy,
4474 IntTy,
4475 Int8PtrTy,
4476 Int8PtrTy,
4477 MethodListnfABIPtrTy,
4478 ProtocolListnfABIPtrTy,
4479 IvarListnfABIPtrTy,
4480 Int8PtrTy,
4481 PropertyListPtrTy,
4482 NULL);
4483 CGM.getModule().addTypeName("struct._class_ro_t",
4484 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004485
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4487 std::vector<const llvm::Type*> Params;
4488 Params.push_back(ObjectPtrTy);
4489 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004490 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004491 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4492
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004493 // struct _class_t {
4494 // struct _class_t *isa;
4495 // struct _class_t * const superclass;
4496 // void *cache;
4497 // IMP *vtable;
4498 // struct class_ro_t *ro;
4499 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004500
Owen Andersonc36edfe2009-08-13 23:27:53 +00004501 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004502 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004503 llvm::StructType::get(VMContext,
4504 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004505 llvm::PointerType::getUnqual(ClassTyHolder),
4506 CachePtrTy,
4507 llvm::PointerType::getUnqual(ImpnfABITy),
4508 llvm::PointerType::getUnqual(ClassRonfABITy),
4509 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004510 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4511
4512 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004513 ClassnfABITy);
4514
Fariborz Jahanian71394042009-01-23 23:53:38 +00004515 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004516 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004518 // struct _category_t {
4519 // const char * const name;
4520 // struct _class_t *const cls;
4521 // const struct _method_list_t * const instance_methods;
4522 // const struct _method_list_t * const class_methods;
4523 // const struct _protocol_list_t * const protocols;
4524 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004525 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004526 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004527 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004528 MethodListnfABIPtrTy,
4529 MethodListnfABIPtrTy,
4530 ProtocolListnfABIPtrTy,
4531 PropertyListPtrTy,
4532 NULL);
4533 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004534
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004535 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004536 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4537 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004538
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004539 // MessageRefTy - LLVM for:
4540 // struct _message_ref_t {
4541 // IMP messenger;
4542 // SEL name;
4543 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004544
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004545 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004546 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004547 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004548 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004549 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004550 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004551 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004552 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004553 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004554 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004555
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004556 MessageRefCTy = Ctx.getTagDeclType(RD);
4557 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4558 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004559
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004560 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004561 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004562
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004563 // SuperMessageRefTy - LLVM for:
4564 // struct _super_message_ref_t {
4565 // SUPER_IMP messenger;
4566 // SEL name;
4567 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004568 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004569 SelectorPtrTy,
4570 NULL);
4571 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004573 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004574 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4575
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004576
4577 // struct objc_typeinfo {
4578 // const void** vtable; // objc_ehtype_vtable + 2
4579 // const char* name; // c++ typeinfo string
4580 // Class cls;
4581 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004582 EHTypeTy = llvm::StructType::get(VMContext,
4583 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004584 Int8PtrTy,
4585 ClassnfABIPtrTy,
4586 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004587 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004588 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004589}
4590
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004591llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004592 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004593
Fariborz Jahanian71394042009-01-23 23:53:38 +00004594 return NULL;
4595}
4596
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597void CGObjCNonFragileABIMac::AddModuleClassList(const
4598 std::vector<llvm::GlobalValue*>
4599 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004600 const char *SymbolName,
4601 const char *SectionName) {
4602 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603
Daniel Dunbar19573e72009-05-15 21:48:48 +00004604 if (!NumClasses)
4605 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606
Daniel Dunbar19573e72009-05-15 21:48:48 +00004607 std::vector<llvm::Constant*> Symbols(NumClasses);
4608 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004609 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004610 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004611 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004612 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004613 NumClasses),
4614 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615
Daniel Dunbar19573e72009-05-15 21:48:48 +00004616 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004617 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004618 llvm::GlobalValue::InternalLinkage,
4619 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004620 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004621 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004622 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004623 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004624}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004625
Fariborz Jahanian71394042009-01-23 23:53:38 +00004626void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4627 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004628
Daniel Dunbar19573e72009-05-15 21:48:48 +00004629 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004630 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004631 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004632 "\01L_OBJC_LABEL_CLASS_$",
4633 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004634
Fariborz Jahanian67260552009-11-17 21:37:35 +00004635 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4636 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4637 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4638 continue;
4639 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004640 }
4641
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004642 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4643 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4644 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4645 continue;
4646 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4647 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004648
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004649 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004650 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4651 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004653 // Build list of all implemented category addresses in array
4654 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004655 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004656 "\01L_OBJC_LABEL_CATEGORY_$",
4657 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004658 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004659 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4660 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004661
Daniel Dunbar5e639272010-04-25 20:39:01 +00004662 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004663}
4664
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004665/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004666/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004667/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004668/// message dispatch call for all the rest.
4669///
4670bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004671 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4672 default:
4673 assert(0 && "Invalid dispatch method!");
4674 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004675 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004676 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004677 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004678 case CodeGenOptions::Mixed:
4679 break;
4680 }
4681
4682 // If so, see whether this selector is in the white-list of things which must
4683 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004684 if (NonLegacyDispatchMethods.empty()) {
4685 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4686 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4687 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4688 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4689 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4690 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4691 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4692 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4693 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4694 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004695
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004696 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4697 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4698 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4699 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4700 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4701 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4702 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4703 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004704 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004705 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004706 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4707 &CGM.getContext().Idents.get("objects"),
4708 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004709 };
4710 NonLegacyDispatchMethods.insert(
4711 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004712 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004713
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004714 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004715}
4716
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004717// Metadata flags
4718enum MetaDataDlags {
4719 CLS = 0x0,
4720 CLS_META = 0x1,
4721 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004722 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004723 CLS_EXCEPTION = 0x20
4724};
4725/// BuildClassRoTInitializer - generate meta-data for:
4726/// struct _class_ro_t {
4727/// uint32_t const flags;
4728/// uint32_t const instanceStart;
4729/// uint32_t const instanceSize;
4730/// uint32_t const reserved; // only when building for 64bit targets
4731/// const uint8_t * const ivarLayout;
4732/// const char *const name;
4733/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004734/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004735/// const struct _ivar_list_t *const ivars;
4736/// const uint8_t * const weakIvarLayout;
4737/// const struct _prop_list_t * const properties;
4738/// }
4739///
4740llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004741 unsigned flags,
4742 unsigned InstanceStart,
4743 unsigned InstanceSize,
4744 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004745 std::string ClassName = ID->getNameAsString();
4746 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004747 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4748 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4749 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004750 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004751 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4752 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004753 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004754 // const struct _method_list_t * const baseMethods;
4755 std::vector<llvm::Constant*> Methods;
4756 std::string MethodListName("\01l_OBJC_$_");
4757 if (flags & CLS_META) {
4758 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004760 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004761 // Class methods should always be defined.
4762 Methods.push_back(GetMethodConstant(*i));
4763 }
4764 } else {
4765 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004766 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004767 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004768 // Instance methods should always be defined.
4769 Methods.push_back(GetMethodConstant(*i));
4770 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004771 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004772 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004773 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004774
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004775 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4776 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004777
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004778 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4779 if (llvm::Constant *C = GetMethodConstant(MD))
4780 Methods.push_back(C);
4781 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4782 if (llvm::Constant *C = GetMethodConstant(MD))
4783 Methods.push_back(C);
4784 }
4785 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004786 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004787 Values[ 5] = EmitMethodList(MethodListName,
4788 "__DATA, __objc_const", Methods);
4789
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004790 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4791 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004792 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004793 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004794 OID->all_referenced_protocol_begin(),
4795 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004796
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004797 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004798 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004799 else
4800 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004801 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4802 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004803 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004804 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004805 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004806 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4807 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004808 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004809 Values);
4810 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004811 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4812 llvm::GlobalValue::InternalLinkage,
4813 Init,
4814 (flags & CLS_META) ?
4815 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4816 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004817 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004818 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004819 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004820 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004821
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004822}
4823
4824/// BuildClassMetaData - This routine defines that to-level meta-data
4825/// for the given ClassName for:
4826/// struct _class_t {
4827/// struct _class_t *isa;
4828/// struct _class_t * const superclass;
4829/// void *cache;
4830/// IMP *vtable;
4831/// struct class_ro_t *ro;
4832/// }
4833///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004834llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004835 std::string &ClassName,
4836 llvm::Constant *IsAGV,
4837 llvm::Constant *SuperClassGV,
4838 llvm::Constant *ClassRoGV,
4839 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004840 std::vector<llvm::Constant*> Values(5);
4841 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842 Values[1] = SuperClassGV;
4843 if (!Values[1])
4844 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004845 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4846 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4847 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004848 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004849 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004850 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4851 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004852 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004853 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004854 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004855 if (HiddenVisibility)
4856 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004857 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004858}
4859
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004860bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004861CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004862 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004863}
4864
Daniel Dunbar961202372009-05-03 12:57:56 +00004865void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004866 uint32_t &InstanceStart,
4867 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004868 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004869 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004870
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004871 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004872 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004873
4874 // If there are no fields, the start is the same as the end.
4875 if (!RL.getFieldCount())
4876 InstanceStart = InstanceSize;
4877 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004878 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004879}
4880
Fariborz Jahanian71394042009-01-23 23:53:38 +00004881void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4882 std::string ClassName = ID->getNameAsString();
4883 if (!ObjCEmptyCacheVar) {
4884 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004885 CGM.getModule(),
4886 ObjCTypes.CacheTy,
4887 false,
4888 llvm::GlobalValue::ExternalLinkage,
4889 0,
4890 "_objc_empty_cache");
4891
Fariborz Jahanian71394042009-01-23 23:53:38 +00004892 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004893 CGM.getModule(),
4894 ObjCTypes.ImpnfABITy,
4895 false,
4896 llvm::GlobalValue::ExternalLinkage,
4897 0,
4898 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004899 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004900 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004901 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004902 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004903 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004904 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004905 uint32_t InstanceSize = InstanceStart;
4906 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004907 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4908 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004909
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004910 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004911
4912 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004913 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004914 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004915 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004916 if (ID->getNumIvarInitializers())
4917 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004918 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004919 // class is root
4920 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004921 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004922 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004923 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004924 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004925 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4926 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4927 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004928 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004929 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004930 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004931 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004932 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004933 ObjCMetaClassName +
4934 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004935 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004936 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004937 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004938 }
4939 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4940 InstanceStart,
4941 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004942 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004943 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004944 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4945 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004946 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004947
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004948 // Metadata for the class
4949 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004950 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004951 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004952 if (ID->getNumIvarInitializers())
4953 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004954
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004955 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004956 flags |= CLS_EXCEPTION;
4957
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004958 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004959 flags |= CLS_ROOT;
4960 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004961 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004962 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004963 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004964 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004965 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004966 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004967 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004968 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004969 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004970 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004971 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004972 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004973 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004974
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004975 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004977 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4978 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004979 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004980
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004981 // Determine if this class is also "non-lazy".
4982 if (ImplementationIsNonLazy(ID))
4983 DefinedNonLazyClasses.push_back(ClassMD);
4984
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004985 // Force the definition of the EHType if necessary.
4986 if (flags & CLS_EXCEPTION)
4987 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00004988 // Make sure method definition entries are all clear for next implementation.
4989 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004990}
4991
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004992/// GenerateProtocolRef - This routine is called to generate code for
4993/// a protocol reference expression; as in:
4994/// @code
4995/// @protocol(Proto1);
4996/// @endcode
4997/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4998/// which will hold address of the protocol meta-data.
4999///
5000llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005001 const ObjCProtocolDecl *PD) {
5002
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005003 // This routine is called for @protocol only. So, we must build definition
5004 // of protocol's meta-data (not a reference to it!)
5005 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005006 llvm::Constant *Init =
5007 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5008 ObjCTypes.ExternalProtocolPtrTy);
5009
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005010 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005011 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005012
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005013 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5014 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005015 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005016 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005017 CGM.getModule(),
5018 Init->getType(), false,
5019 llvm::GlobalValue::WeakAnyLinkage,
5020 Init,
5021 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005022 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5023 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005024 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005025 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005026}
5027
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005028/// GenerateCategory - Build metadata for a category implementation.
5029/// struct _category_t {
5030/// const char * const name;
5031/// struct _class_t *const cls;
5032/// const struct _method_list_t * const instance_methods;
5033/// const struct _method_list_t * const class_methods;
5034/// const struct _protocol_list_t * const protocols;
5035/// const struct _prop_list_t * const properties;
5036/// }
5037///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005038void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005039 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005040 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005041 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5042 "_$_" + OCD->getNameAsString());
5043 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005044 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005045
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005046 std::vector<llvm::Constant*> Values(6);
5047 Values[0] = GetClassName(OCD->getIdentifier());
5048 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005049 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005050 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005051 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5052
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005053 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005054 std::vector<llvm::Constant*> Methods;
5055 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005056 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005057 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005058
5059 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005060 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005061 // Instance methods should always be defined.
5062 Methods.push_back(GetMethodConstant(*i));
5063 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064
5065 Values[2] = EmitMethodList(MethodListName,
5066 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005067 Methods);
5068
5069 MethodListName = Prefix;
5070 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5071 OCD->getNameAsString();
5072 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005074 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005075 // Class methods should always be defined.
5076 Methods.push_back(GetMethodConstant(*i));
5077 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005078
5079 Values[3] = EmitMethodList(MethodListName,
5080 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005081 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005083 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005084 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005085 llvm::SmallString<256> ExtName;
5086 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5087 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005088 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005089 + Interface->getName() + "_$_"
5090 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005091 Category->protocol_begin(),
5092 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005093 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5094 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005095 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005096 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5097 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005098 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005099
5100 llvm::Constant *Init =
5101 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005102 Values);
5103 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005104 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005105 false,
5106 llvm::GlobalValue::InternalLinkage,
5107 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005108 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005109 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005110 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005111 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005112 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005113 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005114
5115 // Determine if this category is also "non-lazy".
5116 if (ImplementationIsNonLazy(OCD))
5117 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005118 // method definition entries must be clear for next implementation.
5119 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005120}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005121
5122/// GetMethodConstant - Return a struct objc_method constant for the
5123/// given method if it has been defined. The result is null if the
5124/// method has not been defined. The return value has type MethodPtrTy.
5125llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005126 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005127 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005128 if (!Fn)
5129 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005130
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005131 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005132 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005133 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005135 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005136 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005137 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005138}
5139
5140/// EmitMethodList - Build meta-data for method declarations
5141/// struct _method_list_t {
5142/// uint32_t entsize; // sizeof(struct _objc_method)
5143/// uint32_t method_count;
5144/// struct _objc_method method_list[method_count];
5145/// }
5146///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005147llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5148 const char *Section,
5149 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005150 // Return null for empty list.
5151 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005152 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005153
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005154 std::vector<llvm::Constant*> Values(3);
5155 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005156 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005157 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005158 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005159 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005160 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005161 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005162 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005163 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005164
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005165 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005166 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005167 llvm::GlobalValue::InternalLinkage,
5168 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005169 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005170 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005171 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005172 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005173 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005174 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005175 ObjCTypes.MethodListnfABIPtrTy);
5176}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005177
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005178/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5179/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005180llvm::GlobalVariable *
5181CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5182 const ObjCIvarDecl *Ivar) {
5183 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005184 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005185 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005186 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005187 CGM.getModule().getGlobalVariable(Name);
5188 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005189 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005190 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005191 false,
5192 llvm::GlobalValue::ExternalLinkage,
5193 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005194 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005195 return IvarOffsetGV;
5196}
5197
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005198llvm::Constant *
5199CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5200 const ObjCIvarDecl *Ivar,
5201 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005202 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005203 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005204 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005205 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005206 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005207
Mike Stump18bb9282009-05-16 07:57:57 +00005208 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5209 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005210 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5211 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005212 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005213 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005214 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005215 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005216 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005217 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005218}
5219
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005220/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005221/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005222/// IvarListnfABIPtrTy.
5223/// struct _ivar_t {
5224/// unsigned long int *offset; // pointer to ivar offset location
5225/// char *name;
5226/// char *type;
5227/// uint32_t alignment;
5228/// uint32_t size;
5229/// }
5230/// struct _ivar_list_t {
5231/// uint32 entsize; // sizeof(struct _ivar_t)
5232/// uint32 count;
5233/// struct _iver_t list[count];
5234/// }
5235///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005236
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005237llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005238 const ObjCImplementationDecl *ID) {
5239
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005240 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005241
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005242 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5243 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005244
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005245 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005246
Daniel Dunbarae032262009-04-20 00:33:43 +00005247 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005248 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005249 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005250
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005251 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5252 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005253 // Ignore unnamed bit-fields.
5254 if (!IVD->getDeclName())
5255 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005256 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005257 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005258 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5259 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005260 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005261 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005262 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005263 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005264 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005265 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005266 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005267 // NOTE. Size of a bitfield does not match gcc's, because of the
5268 // way bitfields are treated special in each. But I am told that
5269 // 'size' for bitfield ivars is ignored by the runtime so it does
5270 // not matter. If it matters, there is enough info to get the
5271 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005272 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005273 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005274 }
5275 // Return null for empty list.
5276 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005277 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005278 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005279 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005280 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5281 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005282 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005283 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005284 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005285 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005286 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5287 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005288 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005289 llvm::GlobalValue::InternalLinkage,
5290 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005291 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005292 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005293 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005294 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005295
Chris Lattnerf56501c2009-07-17 23:57:13 +00005296 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005297 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005298}
5299
5300llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005301 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005302 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005304 if (!Entry) {
5305 // We use the initializer as a marker of whether this is a forward
5306 // reference or not. At module finalization we add the empty
5307 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005308 Entry =
5309 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5310 llvm::GlobalValue::ExternalLinkage,
5311 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005312 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005313 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005314 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005315
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005316 return Entry;
5317}
5318
5319/// GetOrEmitProtocol - Generate the protocol meta-data:
5320/// @code
5321/// struct _protocol_t {
5322/// id isa; // NULL
5323/// const char * const protocol_name;
5324/// const struct _protocol_list_t * protocol_list; // super protocols
5325/// const struct method_list_t * const instance_methods;
5326/// const struct method_list_t * const class_methods;
5327/// const struct method_list_t *optionalInstanceMethods;
5328/// const struct method_list_t *optionalClassMethods;
5329/// const struct _prop_list_t * properties;
5330/// const uint32_t size; // sizeof(struct _protocol_t)
5331/// const uint32_t flags; // = 0
5332/// }
5333/// @endcode
5334///
5335
5336llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005337 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005338 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005339
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005340 // Early exit if a defining object has already been generated.
5341 if (Entry && Entry->hasInitializer())
5342 return Entry;
5343
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005344 // Construct method lists.
5345 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5346 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005347 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005348 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005349 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005350 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005351 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5352 OptInstanceMethods.push_back(C);
5353 } else {
5354 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005355 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005356 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005357
5358 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005359 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005360 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005361 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005362 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5363 OptClassMethods.push_back(C);
5364 } else {
5365 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005366 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005367 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005368
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005369 std::vector<llvm::Constant*> Values(10);
5370 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005371 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005372 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005373 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5374 PD->protocol_begin(),
5375 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005376
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005377 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005378 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005379 "__DATA, __objc_const",
5380 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005381 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005382 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 "__DATA, __objc_const",
5384 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005385 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005386 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005387 "__DATA, __objc_const",
5388 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005389 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005390 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 "__DATA, __objc_const",
5392 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005393 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005394 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005395 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005396 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005397 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005398 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005399 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005401
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 if (Entry) {
5403 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005404 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005405 Entry->setInitializer(Init);
5406 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005407 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005408 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5409 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5410 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005411 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005412 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005413 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005415 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005416 CGM.AddUsedGlobal(Entry);
5417
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005418 // Use this protocol meta-data to build protocol list table in section
5419 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005420 llvm::GlobalVariable *PTGV =
5421 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5422 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5423 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005424 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005425 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005426 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005427 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005428 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005429 return Entry;
5430}
5431
5432/// EmitProtocolList - Generate protocol list meta-data:
5433/// @code
5434/// struct _protocol_list_t {
5435/// long protocol_count; // Note, this is 32/64 bit
5436/// struct _protocol_t[protocol_count];
5437/// }
5438/// @endcode
5439///
5440llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005441CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5442 ObjCProtocolDecl::protocol_iterator begin,
5443 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005444 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005445
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005446 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005447 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005448 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005449
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005450 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005451 llvm::SmallString<256> TmpName;
5452 Name.toVector(TmpName);
5453 llvm::GlobalVariable *GV =
5454 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005455 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005456 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005458 for (; begin != end; ++begin)
5459 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5460
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005461 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005462 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005463 ObjCTypes.ProtocolnfABIPtrTy));
5464
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005465 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005466 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005467 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005469 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005470 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471 ProtocolRefs.size()),
5472 ProtocolRefs);
5473
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005474 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005475 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005476 llvm::GlobalValue::InternalLinkage,
5477 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005478 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005479 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005480 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005481 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005482 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005484 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005485}
5486
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005487/// GetMethodDescriptionConstant - This routine build following meta-data:
5488/// struct _objc_method {
5489/// SEL _cmd;
5490/// char *method_type;
5491/// char *_imp;
5492/// }
5493
5494llvm::Constant *
5495CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5496 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005497 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005498 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5499 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005500 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005501 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005502 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005503 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005504}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005505
5506/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5507/// This code gen. amounts to generating code for:
5508/// @code
5509/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5510/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005511///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005512LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005513 CodeGen::CodeGenFunction &CGF,
5514 QualType ObjectTy,
5515 llvm::Value *BaseValue,
5516 const ObjCIvarDecl *Ivar,
5517 unsigned CVRQualifiers) {
5518 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005519 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5520 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005521}
5522
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005523llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005524 CodeGen::CodeGenFunction &CGF,
5525 const ObjCInterfaceDecl *Interface,
5526 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005527 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005528}
5529
John McCall234eac82011-05-13 23:16:18 +00005530static void appendSelectorForMessageRefTable(std::string &buffer,
5531 Selector selector) {
5532 if (selector.isUnarySelector()) {
5533 buffer += selector.getNameForSlot(0);
5534 return;
5535 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005536
John McCall234eac82011-05-13 23:16:18 +00005537 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5538 buffer += selector.getNameForSlot(i);
5539 buffer += '_';
5540 }
5541}
5542
5543/// Emit a message send for the non-fragile ABI.
5544///
5545/// Note that we intentionally don't emit a call to objc_msgSend*
5546/// directly because doing so will require the call to go through a
5547/// lazy call stub. In general, that overhead is considered
5548/// worthwhile because it
5549RValue CGObjCNonFragileABIMac::EmitMessageSend(CodeGenFunction &CGF,
5550 ReturnValueSlot returnSlot,
5551 QualType resultType,
5552 Selector selector,
5553 llvm::Value *receiver,
5554 QualType receiverType,
5555 bool isSuper,
5556 const CallArgList &formalArgs,
5557 const ObjCMethodDecl *method) {
5558 // Compute the actual arguments.
5559 CallArgList args;
5560
5561 // First argument: the receiver.
5562 // FIXME: this method doesn't really support super receivers anyway.
5563 if (!isSuper)
5564 receiver = CGF.Builder.CreateBitCast(receiver, ObjCTypes.ObjectPtrTy);
5565 args.add(RValue::get(receiver), receiverType);
5566
5567 // Second argument: a message reference pointer. Leave the actual
5568 // r-value blank for now.
5569 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5570
5571 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5572
5573 const CGFunctionInfo &fnInfo =
5574 CGM.getTypes().getFunctionInfo(resultType, args,
5575 FunctionType::ExtInfo());
5576
5577 // Find the function to call and the name of the message ref table
5578 // entry.
5579 llvm::Constant *fn = 0;
5580 std::string messageRefName("\01l_");
5581 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
5582 EmitNullReturnInitialization(CGF, returnSlot, resultType);
5583 if (isSuper) {
5584 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5585 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005586 } else {
John McCall234eac82011-05-13 23:16:18 +00005587 fn = ObjCTypes.getMessageSendStretFixupFn();
5588 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005589 }
John McCall234eac82011-05-13 23:16:18 +00005590 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5591 fn = ObjCTypes.getMessageSendFpretFixupFn();
5592 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005593 } else {
John McCall234eac82011-05-13 23:16:18 +00005594 if (isSuper) {
5595 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5596 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005597 } else {
John McCall234eac82011-05-13 23:16:18 +00005598 fn = ObjCTypes.getMessageSendFixupFn();
5599 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005600 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005601 }
John McCall234eac82011-05-13 23:16:18 +00005602 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5603 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005604
John McCall234eac82011-05-13 23:16:18 +00005605 // Append the selector name, except use underscores anywhere we
5606 // would have used colons.
5607 appendSelectorForMessageRefTable(messageRefName, selector);
5608
5609 llvm::GlobalVariable *messageRef
5610 = CGM.getModule().getGlobalVariable(messageRefName);
5611 if (!messageRef) {
5612 // Build the message ref table entry. It needs to be non-constant
5613 // because otherwise the optimizer will elide the load, which we
5614 // don't want --- in fact, the whole point of this exercise is to
5615 // get an indirect call.
5616 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
5617 llvm::Constant *init =
5618 llvm::ConstantStruct::get(VMContext, values, 2, false);
5619 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5620 init->getType(),
5621 /*constant*/ false,
5622 llvm::GlobalValue::WeakAnyLinkage,
5623 init,
5624 messageRefName);
5625 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5626 messageRef->setAlignment(16);
5627 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5628 }
5629 llvm::Value *mref =
5630 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5631
5632 // Update the message reference in the arguments.
5633 args[1].RV = RValue::get(mref);
5634
5635 // Load the function to call from the message ref table.
5636 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5637 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5638
5639 bool variadic = method ? method->isVariadic() : false;
5640 const llvm::FunctionType *fnType =
5641 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5642 callee = CGF.Builder.CreateBitCast(callee,
5643 llvm::PointerType::getUnqual(fnType));
5644
5645 return CGF.EmitCall(fnInfo, callee, returnSlot, args);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005646}
5647
5648/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005649CodeGen::RValue
5650CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005651 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005652 QualType ResultType,
5653 Selector Sel,
5654 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005655 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005656 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005657 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005658 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005659 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5660 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005661 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005662 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005663 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005664 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005665 false, CallArgs, Method);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005666}
5667
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005668llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005669CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005670 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5671
Daniel Dunbara6468342009-03-02 05:18:14 +00005672 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005673 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005674 false, llvm::GlobalValue::ExternalLinkage,
5675 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005676 }
5677
5678 return GV;
5679}
5680
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005681llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5682 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005683 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005684
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005685 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005686 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005687 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005688 Entry =
5689 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005690 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005691 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005692 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005693 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005694 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005695 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005696 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005697 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005698 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005700 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005701}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005702
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005703llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005704CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005705 const ObjCInterfaceDecl *ID) {
5706 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005707
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005708 if (!Entry) {
5709 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5710 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005711 Entry =
5712 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005713 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005714 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005715 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005716 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005717 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005719 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005720 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005721 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005722
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005723 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005724}
5725
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005726/// EmitMetaClassRef - Return a Value * of the address of _class_t
5727/// meta-data
5728///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005729llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5730 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005731 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5732 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005733 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005734
Daniel Dunbar15894b72009-04-07 05:48:37 +00005735 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005736 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005737 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005738 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005739 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005740 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005741 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005742 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005743 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005744 ObjCTypes.ClassnfABIPtrTy));
5745
Daniel Dunbare60aa052009-04-15 19:03:14 +00005746 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005747 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005748
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005749 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005750}
5751
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005752/// GetClass - Return a reference to the class for the given interface
5753/// decl.
5754llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5755 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005756 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005757 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5758 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5759 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5760 }
5761
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005762 return EmitClassRef(Builder, ID);
5763}
5764
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005765/// Generates a message send where the super is the receiver. This is
5766/// a message send to self with special delivery semantics indicating
5767/// which class's method should be called.
5768CodeGen::RValue
5769CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005770 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005771 QualType ResultType,
5772 Selector Sel,
5773 const ObjCInterfaceDecl *Class,
5774 bool isCategoryImpl,
5775 llvm::Value *Receiver,
5776 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005777 const CodeGen::CallArgList &CallArgs,
5778 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005779 // ...
5780 // Create and init a super structure; this is a (receiver, class)
5781 // pair we will pass to objc_msgSendSuper.
5782 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005783 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005784
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005785 llvm::Value *ReceiverAsObject =
5786 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5787 CGF.Builder.CreateStore(ReceiverAsObject,
5788 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005789
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005790 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005791 llvm::Value *Target;
5792 if (IsClassMessage) {
5793 if (isCategoryImpl) {
5794 // Message sent to "super' in a class method defined in
5795 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005796 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005797 Target = CGF.Builder.CreateStructGEP(Target, 0);
5798 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005799 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005800 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005801 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005802 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005803
Mike Stump18bb9282009-05-16 07:57:57 +00005804 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5805 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005806 const llvm::Type *ClassTy =
5807 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5808 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5809 CGF.Builder.CreateStore(Target,
5810 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005811
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005812 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005813 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5814 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005815 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005816 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005817 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005818 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005819 true, CallArgs, Method);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005820}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005821
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005822llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005823 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005824 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005825
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005826 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005827 llvm::Constant *Casted =
5828 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5829 ObjCTypes.SelectorPtrTy);
5830 Entry =
5831 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5832 llvm::GlobalValue::InternalLinkage,
5833 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005834 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005835 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005836 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005837
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005838 if (lval)
5839 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005840 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005841}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005842/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005843/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005844///
5845void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005846 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005847 llvm::Value *dst,
5848 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005849 const llvm::Type * SrcTy = src->getType();
5850 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005851 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005852 assert(Size <= 8 && "does not support size > 8");
5853 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5854 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005855 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5856 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005857 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5858 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005859 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5860 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005861 return;
5862}
5863
5864/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5865/// objc_assign_strongCast (id src, id *dst)
5866///
5867void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005868 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005869 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005870 const llvm::Type * SrcTy = src->getType();
5871 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005872 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005873 assert(Size <= 8 && "does not support size > 8");
5874 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005876 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5877 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005878 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5879 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005880 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005881 src, dst, "weakassign");
5882 return;
5883}
5884
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005885void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005886 CodeGen::CodeGenFunction &CGF,
5887 llvm::Value *DestPtr,
5888 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005889 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005890 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5891 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005892 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005893 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005894 return;
5895}
5896
Fariborz Jahanian06292952009-02-16 22:52:32 +00005897/// EmitObjCWeakRead - Code gen for loading value of a __weak
5898/// object: objc_read_weak (id *src)
5899///
5900llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005901 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005902 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005903 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005904 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5905 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005906 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005907 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005908 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005909 return read_weak;
5910}
5911
5912/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5913/// objc_assign_weak (id src, id *dst)
5914///
5915void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005916 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005917 const llvm::Type * SrcTy = src->getType();
5918 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005919 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005920 assert(Size <= 8 && "does not support size > 8");
5921 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5922 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005923 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5924 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005925 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5926 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005927 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005928 src, dst, "weakassign");
5929 return;
5930}
5931
5932/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5933/// objc_assign_global (id src, id *dst)
5934///
5935void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005936 llvm::Value *src, llvm::Value *dst,
5937 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005938 const llvm::Type * SrcTy = src->getType();
5939 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005940 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005941 assert(Size <= 8 && "does not support size > 8");
5942 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5943 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005944 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5945 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005946 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5947 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005948 if (!threadlocal)
5949 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5950 src, dst, "globalassign");
5951 else
5952 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5953 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005954 return;
5955}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005956
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005957void
John McCallbd309292010-07-06 01:34:17 +00005958CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5959 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005960 EmitAtSynchronizedStmt(CGF, S,
5961 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5962 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00005963}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005964
John McCall2ca705e2010-07-24 00:37:23 +00005965llvm::Constant *
5966CGObjCNonFragileABIMac::GetEHType(QualType T) {
5967 // There's a particular fixed type info for 'id'.
5968 if (T->isObjCIdType() ||
5969 T->isObjCQualifiedIdType()) {
5970 llvm::Constant *IDEHType =
5971 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5972 if (!IDEHType)
5973 IDEHType =
5974 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5975 false,
5976 llvm::GlobalValue::ExternalLinkage,
5977 0, "OBJC_EHTYPE_id");
5978 return IDEHType;
5979 }
5980
5981 // All other types should be Objective-C interface pointer types.
5982 const ObjCObjectPointerType *PT =
5983 T->getAs<ObjCObjectPointerType>();
5984 assert(PT && "Invalid @catch type.");
5985 const ObjCInterfaceType *IT = PT->getInterfaceType();
5986 assert(IT && "Invalid @catch type.");
5987 return GetInterfaceEHType(IT->getDecl(), false);
5988}
5989
John McCallbd309292010-07-06 01:34:17 +00005990void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5991 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005992 EmitTryCatchStmt(CGF, S,
5993 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5994 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5995 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005996}
5997
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005998/// EmitThrowStmt - Generate code for a throw statement.
5999void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6000 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006001 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006002 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006003 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6004 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006005 llvm::Value *Args[] = { Exception };
6006 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6007 Args, Args+1)
6008 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006009 } else {
John McCall17afe452010-10-16 08:21:07 +00006010 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6011 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006012 }
6013
John McCall17afe452010-10-16 08:21:07 +00006014 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006015 CGF.Builder.ClearInsertionPoint();
6016}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006017
John McCall2ca705e2010-07-24 00:37:23 +00006018llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006019CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006020 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006021 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006022
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006023 // If we don't need a definition, return the entry if found or check
6024 // if we use an external reference.
6025 if (!ForDefinition) {
6026 if (Entry)
6027 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006028
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006029 // If this type (or a super class) has the __objc_exception__
6030 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006031 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006032 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006033 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006034 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006035 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006036 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006037 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006038 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006039
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006040 // Otherwise we need to either make a new entry or fill in the
6041 // initializer.
6042 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006043 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006044 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006045 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006046 CGM.getModule().getGlobalVariable(VTableName);
6047 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006048 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6049 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006050 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006051 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006052
Chris Lattner5e016ae2010-06-27 07:15:29 +00006053 llvm::Value *VTableIdx =
6054 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006055
6056 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006057 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006058 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006059 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006060 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006061 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006062
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006063 if (Entry) {
6064 Entry->setInitializer(Init);
6065 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006066 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006067 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006068 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006069 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006070 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006071 }
6072
John McCall457a04e2010-10-22 21:05:15 +00006073 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006074 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006075 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6076 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006077
6078 if (ForDefinition) {
6079 Entry->setSection("__DATA,__objc_const");
6080 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6081 } else {
6082 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6083 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006084
6085 return Entry;
6086}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006087
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006088/* *** */
6089
Daniel Dunbarb036db82008-08-13 03:21:16 +00006090CodeGen::CGObjCRuntime *
6091CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006092 if (CGM.getLangOptions().ObjCNonFragileABI)
6093 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006094 return new CGObjCMac(CGM);
6095}