blob: 50f2c78b5248a395f6cbef3a279a299032f29c62 [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
Daniel Dunbar303e2c22008-08-11 02:45:11 +000046namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000047
Daniel Dunbar59e476b2009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000049
Daniel Dunbar59e476b2009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000052
Fariborz Jahanian279eda62009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000056
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000057private:
John McCall9dc0db22011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCall9dc0db22011-05-15 01:53:33 +000066 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
67 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
68 params, true),
69 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000070 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000071
John McCall9dc0db22011-05-15 01:53:33 +000072 /// void objc_msgSend_stret (id, SEL, ...)
73 ///
74 /// The messenger used when the return value is an aggregate returned
75 /// by indirect reference in the first argument, and therefore the
76 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000077 llvm::Constant *getMessageSendStretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +000078 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
79 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
80 params, true),
81 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000082
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000083 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000084
John McCall9dc0db22011-05-15 01:53:33 +000085 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
86 ///
87 /// The messenger used when the return value is returned on the x87
88 /// floating-point stack; without a special entrypoint, the nil case
89 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000090 llvm::Constant *getMessageSendFpretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +000091 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
92 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +000093 llvm::Type::getDoubleTy(VMContext),
John McCall9dc0db22011-05-15 01:53:33 +000094 params, true),
95 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000096
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000097 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000098
John McCall9dc0db22011-05-15 01:53:33 +000099 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
100 ///
101 /// The messenger used for super calls, which have different dispatch
102 /// semantics. The class passed is the superclass of the current
103 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000104 llvm::Constant *getMessageSendSuperFn() const {
John McCall9dc0db22011-05-15 01:53:33 +0000105 const llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000106 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000107 params, true),
108 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000109 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000110
John McCall9dc0db22011-05-15 01:53:33 +0000111 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
112 ///
113 /// A slightly different messenger used for super calls. The class
114 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000115 llvm::Constant *getMessageSendSuperFn2() const {
John McCall9dc0db22011-05-15 01:53:33 +0000116 const llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000117 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000118 params, true),
119 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000120 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000121
John McCall9dc0db22011-05-15 01:53:33 +0000122 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
123 /// SEL op, ...)
124 ///
125 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000126 llvm::Constant *getMessageSendSuperStretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +0000127 const llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000128 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000129 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000130 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000131 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000132
John McCall9dc0db22011-05-15 01:53:33 +0000133 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
134 /// SEL op, ...)
135 ///
136 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000137 llvm::Constant *getMessageSendSuperStretFn2() const {
John McCall9dc0db22011-05-15 01:53:33 +0000138 const llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000139 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000140 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000141 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000142 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000143
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000144 llvm::Constant *getMessageSendSuperFpretFn() const {
145 // There is no objc_msgSendSuper_fpret? How can that work?
146 return getMessageSendSuperFn();
147 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000148
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000149 llvm::Constant *getMessageSendSuperFpretFn2() const {
150 // There is no objc_msgSendSuper_fpret? How can that work?
151 return getMessageSendSuperFn2();
152 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000153
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000154protected:
155 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000157public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000158 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000159 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160
Daniel Dunbar5d715592008-08-12 05:28:47 +0000161 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
162 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000163
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000164 /// PtrObjectPtrTy - LLVM type for id *
165 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000166
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000167 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000168 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000169 /// ProtocolPtrTy - LLVM type for external protocol handles
170 /// (typeof(Protocol))
171 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Daniel Dunbarc722b852008-08-30 03:02:31 +0000173 // SuperCTy - clang type for struct objc_super.
174 QualType SuperCTy;
175 // SuperPtrCTy - clang type for struct objc_super *.
176 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000177
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000178 /// SuperTy - LLVM type for struct objc_super.
179 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000180 /// SuperPtrTy - LLVM type for struct objc_super *.
181 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000183 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
184 /// in GCC parlance).
185 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000186
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000187 /// PropertyListTy - LLVM type for struct objc_property_list
188 /// (_prop_list_t in GCC parlance).
189 const llvm::StructType *PropertyListTy;
190 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
191 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000192
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000193 // MethodTy - LLVM type for struct objc_method.
194 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000195
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000196 /// CacheTy - LLVM type for struct objc_cache.
197 const llvm::Type *CacheTy;
198 /// CachePtrTy - LLVM type for struct objc_cache *.
199 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000200
Chris Lattnerce8754e2009-04-22 02:44:54 +0000201 llvm::Constant *getGetPropertyFn() {
202 CodeGen::CodeGenTypes &Types = CGM.getTypes();
203 ASTContext &Ctx = CGM.getContext();
204 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000205 llvm::SmallVector<CanQualType,4> Params;
206 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
207 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000208 Params.push_back(IdType);
209 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000210 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000211 Params.push_back(Ctx.BoolTy);
212 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000213 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000214 FunctionType::ExtInfo()),
215 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000216 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
217 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000218
Chris Lattnerce8754e2009-04-22 02:44:54 +0000219 llvm::Constant *getSetPropertyFn() {
220 CodeGen::CodeGenTypes &Types = CGM.getTypes();
221 ASTContext &Ctx = CGM.getContext();
222 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000223 llvm::SmallVector<CanQualType,6> Params;
224 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
225 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000226 Params.push_back(IdType);
227 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000228 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000229 Params.push_back(IdType);
230 Params.push_back(Ctx.BoolTy);
231 Params.push_back(Ctx.BoolTy);
232 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000233 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000234 FunctionType::ExtInfo()),
235 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000236 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
237 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000238
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000239
240 llvm::Constant *getCopyStructFn() {
241 CodeGen::CodeGenTypes &Types = CGM.getTypes();
242 ASTContext &Ctx = CGM.getContext();
243 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
244 llvm::SmallVector<CanQualType,5> Params;
245 Params.push_back(Ctx.VoidPtrTy);
246 Params.push_back(Ctx.VoidPtrTy);
247 Params.push_back(Ctx.LongTy);
248 Params.push_back(Ctx.BoolTy);
249 Params.push_back(Ctx.BoolTy);
250 const llvm::FunctionType *FTy =
251 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
252 FunctionType::ExtInfo()),
253 false);
254 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
255 }
256
Chris Lattnerce8754e2009-04-22 02:44:54 +0000257 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000258 CodeGen::CodeGenTypes &Types = CGM.getTypes();
259 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000260 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000261 llvm::SmallVector<CanQualType,1> Params;
262 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000263 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000264 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000265 FunctionType::ExtInfo()),
266 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000267 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000269
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000270 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000271 llvm::Constant *getGcReadWeakFn() {
272 // id objc_read_weak (id *)
John McCall9dc0db22011-05-15 01:53:33 +0000273 const llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000274 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000275 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000276 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277 }
278
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000279 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000280 llvm::Constant *getGcAssignWeakFn() {
281 // id objc_assign_weak (id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000282 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000283 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000284 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000285 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
286 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000287
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000288 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000289 llvm::Constant *getGcAssignGlobalFn() {
290 // id objc_assign_global(id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000291 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000292 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000293 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000294 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
295 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000296
Fariborz Jahanian217af242010-07-20 20:30:03 +0000297 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
298 llvm::Constant *getGcAssignThreadLocalFn() {
299 // id objc_assign_threadlocal(id src, id * dest)
John McCall9dc0db22011-05-15 01:53:33 +0000300 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000301 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000302 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000303 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
304 }
305
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000306 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000307 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000308 // id objc_assign_ivar(id, id *, ptrdiff_t)
John McCall9dc0db22011-05-15 01:53:33 +0000309 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
310 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000311 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000312 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000313 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
314 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000315
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000316 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
317 llvm::Constant *GcMemmoveCollectableFn() {
318 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
John McCall9dc0db22011-05-15 01:53:33 +0000319 const llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
320 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
322 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000323
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000324 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000325 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000326 // id objc_assign_strongCast(id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000327 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000328 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000329 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
331 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000332
333 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000334 llvm::Constant *getExceptionThrowFn() {
335 // void objc_exception_throw(id)
John McCall9dc0db22011-05-15 01:53:33 +0000336 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000337 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000338 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
340 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000341
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000342 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
343 llvm::Constant *getExceptionRethrowFn() {
344 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000345 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000346 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
347 }
348
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000349 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000350 llvm::Constant *getSyncEnterFn() {
351 // void objc_sync_enter (id)
John McCall9dc0db22011-05-15 01:53:33 +0000352 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000353 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000354 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000355 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
356 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000357
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000358 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000359 llvm::Constant *getSyncExitFn() {
360 // void objc_sync_exit (id)
John McCall9dc0db22011-05-15 01:53:33 +0000361 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000362 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000363 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000364 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
365 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000366
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000367 llvm::Constant *getSendFn(bool IsSuper) const {
368 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
369 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000370
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000371 llvm::Constant *getSendFn2(bool IsSuper) const {
372 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
373 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000374
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000375 llvm::Constant *getSendStretFn(bool IsSuper) const {
376 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
377 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000378
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000379 llvm::Constant *getSendStretFn2(bool IsSuper) const {
380 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
381 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000382
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000383 llvm::Constant *getSendFpretFn(bool IsSuper) const {
384 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
385 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000386
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000387 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
388 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
389 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000390
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000391 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
392 ~ObjCCommonTypesHelper(){}
393};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000394
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000395/// ObjCTypesHelper - Helper class that encapsulates lazy
396/// construction of varies types used during ObjC generation.
397class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000398public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000399 /// SymtabTy - LLVM type for struct objc_symtab.
400 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000401 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
402 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000403 /// ModuleTy - LLVM type for struct objc_module.
404 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000405
Daniel Dunbarb036db82008-08-13 03:21:16 +0000406 /// ProtocolTy - LLVM type for struct objc_protocol.
407 const llvm::StructType *ProtocolTy;
408 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
409 const llvm::Type *ProtocolPtrTy;
410 /// ProtocolExtensionTy - LLVM type for struct
411 /// objc_protocol_extension.
412 const llvm::StructType *ProtocolExtensionTy;
413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension *.
415 const llvm::Type *ProtocolExtensionPtrTy;
416 /// MethodDescriptionTy - LLVM type for struct
417 /// objc_method_description.
418 const llvm::StructType *MethodDescriptionTy;
419 /// MethodDescriptionListTy - LLVM type for struct
420 /// objc_method_description_list.
421 const llvm::StructType *MethodDescriptionListTy;
422 /// MethodDescriptionListPtrTy - LLVM type for struct
423 /// objc_method_description_list *.
424 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000425 /// ProtocolListTy - LLVM type for struct objc_property_list.
426 const llvm::Type *ProtocolListTy;
427 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
428 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000429 /// CategoryTy - LLVM type for struct objc_category.
430 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000431 /// ClassTy - LLVM type for struct objc_class.
432 const llvm::StructType *ClassTy;
433 /// ClassPtrTy - LLVM type for struct objc_class *.
434 const llvm::Type *ClassPtrTy;
435 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
436 const llvm::StructType *ClassExtensionTy;
437 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
438 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000439 // IvarTy - LLVM type for struct objc_ivar.
440 const llvm::StructType *IvarTy;
441 /// IvarListTy - LLVM type for struct objc_ivar_list.
442 const llvm::Type *IvarListTy;
443 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
444 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000445 /// MethodListTy - LLVM type for struct objc_method_list.
446 const llvm::Type *MethodListTy;
447 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
448 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000449
Anders Carlsson9ff22482008-09-09 10:10:21 +0000450 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
451 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000452
Anders Carlsson9ff22482008-09-09 10:10:21 +0000453 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000454 llvm::Constant *getExceptionTryEnterFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000455 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000456 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000457 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000458 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000459 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000460
461 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000462 llvm::Constant *getExceptionTryExitFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000463 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000464 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000465 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000466 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000467 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000468
469 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000470 llvm::Constant *getExceptionExtractFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000471 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000472 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000473 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000474 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000475 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000476
Anders Carlsson9ff22482008-09-09 10:10:21 +0000477 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000478 llvm::Constant *getExceptionMatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000479 const llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000480 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000481 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000482 "objc_exception_match");
483
Chris Lattnerc6406db2009-04-22 02:26:14 +0000484 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485
Anders Carlsson9ff22482008-09-09 10:10:21 +0000486 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000487 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000488 // This is specifically the prototype for x86.
489 const llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
490 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
491 params, false),
492 "_setjmp");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000494
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000495public:
496 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000497 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000498};
499
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000500/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000501/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000502class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000503public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000504
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000505 // MethodListnfABITy - LLVM for struct _method_list_t
506 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000508 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
509 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000510
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000511 // ProtocolnfABITy = LLVM for struct _protocol_t
512 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000513
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000514 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
515 const llvm::Type *ProtocolnfABIPtrTy;
516
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000517 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
518 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000519
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000520 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
521 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000522
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000523 // ClassnfABITy - LLVM for struct _class_t
524 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000525
Fariborz Jahanian71394042009-01-23 23:53:38 +0000526 // ClassnfABIPtrTy - LLVM for struct _class_t*
527 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000528
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000529 // IvarnfABITy - LLVM for struct _ivar_t
530 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000532 // IvarListnfABITy - LLVM for struct _ivar_list_t
533 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000534
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000535 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
536 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000537
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000538 // ClassRonfABITy - LLVM for struct _class_ro_t
539 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000541 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
542 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000544 // CategorynfABITy - LLVM for struct _category_t
545 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000546
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000547 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000548
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000549 // MessageRefTy - LLVM for:
550 // struct _message_ref_t {
551 // IMP messenger;
552 // SEL name;
553 // };
554 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000555 // MessageRefCTy - clang type for struct _message_ref_t
556 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000557
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000558 // MessageRefPtrTy - LLVM for struct _message_ref_t*
559 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000560 // MessageRefCPtrTy - clang type for struct _message_ref_t*
561 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000562
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000563 // MessengerTy - Type of the messenger (shown as IMP above)
564 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000566 // SuperMessageRefTy - LLVM for:
567 // struct _super_message_ref_t {
568 // SUPER_IMP messenger;
569 // SEL name;
570 // };
571 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000572
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000573 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
574 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000575
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000576 llvm::Constant *getMessageSendFixupFn() {
577 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000578 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000579 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000580 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000581 "objc_msgSend_fixup");
582 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000583
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000584 llvm::Constant *getMessageSendFpretFixupFn() {
585 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000586 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000587 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000588 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000589 "objc_msgSend_fpret_fixup");
590 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000591
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000592 llvm::Constant *getMessageSendStretFixupFn() {
593 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000594 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000595 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000596 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000597 "objc_msgSend_stret_fixup");
598 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000599
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000600 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000601 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000602 // struct _super_message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000603 const llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000604 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000605 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000606 "objc_msgSendSuper2_fixup");
607 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000608
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000609 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000610 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000611 // struct _super_message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000612 const llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000613 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000614 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000615 "objc_msgSendSuper2_stret_fixup");
616 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000617
Chris Lattnera7c00b42009-04-22 02:15:23 +0000618 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000619 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000620 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621
Chris Lattnera7c00b42009-04-22 02:15:23 +0000622 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000623
Chris Lattnera7c00b42009-04-22 02:15:23 +0000624 llvm::Constant *getObjCBeginCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000625 const llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000626 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000627 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000628 "objc_begin_catch");
629 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000630
631 const llvm::StructType *EHTypeTy;
632 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000633
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000634 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
635 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000636};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000638class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000639public:
640 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000641 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000642 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000643 unsigned ivar_bytepos;
644 unsigned ivar_size;
645 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000647
648 // Allow sorting based on byte pos.
649 bool operator<(const GC_IVAR &b) const {
650 return ivar_bytepos < b.ivar_bytepos;
651 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000652 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000653
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000654 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000655 public:
656 unsigned skip;
657 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000659 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000660 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000662protected:
663 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000664 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000665 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000666 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000668 // gc ivar layout bitmap calculation helper caches.
669 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
670 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000671
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000672 /// LazySymbols - Symbols to generate a lazy reference for. See
673 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000674 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000675
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000676 /// DefinedSymbols - External symbols which are defined by this
677 /// module. The symbols in this list and LazySymbols are used to add
678 /// special linker symbols which ensure that Objective-C modules are
679 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000680 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000681
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000682 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000683 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000685 /// MethodVarNames - uniqued method variable names.
686 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000688 /// DefinedCategoryNames - list of category names in form Class_Category.
689 llvm::SetVector<std::string> DefinedCategoryNames;
690
Daniel Dunbarb036db82008-08-13 03:21:16 +0000691 /// MethodVarTypes - uniqued method type signatures. We have to use
692 /// a StringMap here because have no other unique reference.
693 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000694
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000695 /// MethodDefinitions - map of methods which have been defined in
696 /// this translation unit.
697 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000698
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000699 /// PropertyNames - uniqued method variable names.
700 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000701
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000702 /// ClassReferences - uniqued class references.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000705 /// SelectorReferences - uniqued selector references.
706 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000707
Daniel Dunbarb036db82008-08-13 03:21:16 +0000708 /// Protocols - Protocols for which an objc_protocol structure has
709 /// been emitted. Forward declarations are handled by creating an
710 /// empty structure whose initializer is filled in when/if defined.
711 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Daniel Dunbarc475d422008-10-29 22:36:39 +0000713 /// DefinedProtocols - Protocols which have actually been
714 /// defined. We should not need this, see FIXME in GenerateProtocol.
715 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000716
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000717 /// DefinedClasses - List of defined classes.
718 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000719
720 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
721 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000722
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000723 /// DefinedCategories - List of defined categories.
724 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000726 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
727 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000729 /// GetNameForMethod - Return a name for the given method.
730 /// \param[out] NameOut - The return value.
731 void GetNameForMethod(const ObjCMethodDecl *OMD,
732 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000733 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000734
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000735 /// GetMethodVarName - Return a unique constant for the given
736 /// selector's name. The return value has type char *.
737 llvm::Constant *GetMethodVarName(Selector Sel);
738 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000739
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000740 /// GetMethodVarType - Return a unique constant for the given
741 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000743 // FIXME: This is a horrible name.
744 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000745 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000746
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000747 /// GetPropertyName - Return a unique constant for the given
748 /// name. The return value has type char *.
749 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000751 // FIXME: This can be dropped once string functions are unified.
752 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
753 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000754
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000755 /// GetClassName - Return a unique constant for the given selector's
756 /// name. The return value has type char *.
757 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000758
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000759 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
760
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000761 /// BuildIvarLayout - Builds ivar layout bitmap for the class
762 /// implementation for the __strong or __weak case.
763 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000764 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
765 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000766
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000767 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000768
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000769 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770 unsigned int BytePos, bool ForStrongLayout,
771 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000772 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000773 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000774 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000775 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000776 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000777 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000778
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000779 /// GetIvarLayoutName - Returns a unique constant for the given
780 /// ivar layout bitmap.
781 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
782 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000783
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000784 /// EmitPropertyList - Emit the given property list. The return
785 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000786 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000787 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000788 const ObjCContainerDecl *OCD,
789 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000791 /// PushProtocolProperties - Push protocol's property on the input stack.
792 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
793 std::vector<llvm::Constant*> &Properties,
794 const Decl *Container,
795 const ObjCProtocolDecl *PROTO,
796 const ObjCCommonTypesHelper &ObjCTypes);
797
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000798 /// GetProtocolRef - Return a reference to the internal protocol
799 /// description, creating an empty one if it has not been
800 /// defined. The return value has type ProtocolPtrTy.
801 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000802
Daniel Dunbar30c65362009-03-09 20:09:19 +0000803 /// CreateMetadataVar - Create a global variable with internal
804 /// linkage for use by the Objective-C runtime.
805 ///
806 /// This is a convenience wrapper which not only creates the
807 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000808 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000809 ///
810 /// \param Name - The variable name.
811 /// \param Init - The variable initializer; this is also used to
812 /// define the type of the variable.
813 /// \param Section - The section the variable should go into, or 0.
814 /// \param Align - The alignment for the variable, or 0.
815 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000816 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000817 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000818 llvm::Constant *Init,
819 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000820 unsigned Align,
821 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000822
John McCall9e8bb002011-05-14 03:10:52 +0000823 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
824 ReturnValueSlot Return,
825 QualType ResultType,
826 llvm::Value *Sel,
827 llvm::Value *Arg0,
828 QualType Arg0Ty,
829 bool IsSuper,
830 const CallArgList &CallArgs,
831 const ObjCMethodDecl *OMD,
832 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000833
Daniel Dunbar5e639272010-04-25 20:39:01 +0000834 /// EmitImageInfo - Emit the image info marker used to encode some module
835 /// level information.
836 void EmitImageInfo();
837
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000838public:
Owen Andersonae86c192009-07-13 04:10:07 +0000839 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000840 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000841
David Chisnall481e3a82010-01-23 02:40:42 +0000842 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000843
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000844 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
845 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000847 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000848
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000849 /// GetOrEmitProtocol - Get the protocol object for the given
850 /// declaration, emitting it if necessary. The return value has type
851 /// ProtocolPtrTy.
852 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000853
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000854 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
855 /// object for the given declaration, emitting it if needed. These
856 /// forward references will be filled in with empty bodies if no
857 /// definition is seen. The return value has type ProtocolPtrTy.
858 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000859 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
860 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000861
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000862};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000863
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000864class CGObjCMac : public CGObjCCommonMac {
865private:
866 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000867
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000868 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000869 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000870 void EmitModuleInfo();
871
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000872 /// EmitModuleSymols - Emit module symbols, the list of defined
873 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000874 llvm::Constant *EmitModuleSymbols();
875
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000876 /// FinishModule - Write out global data structures at the end of
877 /// processing a translation unit.
878 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000879
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000880 /// EmitClassExtension - Generate the class extension structure used
881 /// to store the weak ivar layout and properties. The return value
882 /// has type ClassExtensionPtrTy.
883 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
884
885 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
886 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000887 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000888 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000889
890 /// EmitSuperClassRef - Emits reference to class's main metadata class.
891 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000892
893 /// EmitIvarList - Emit the ivar list for the given
894 /// implementation. If ForClass is true the list of class ivars
895 /// (i.e. metaclass ivars) is emitted, otherwise the list of
896 /// interface ivars will be emitted. The return value has type
897 /// IvarListPtrTy.
898 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000899 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000900
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000901 /// EmitMetaClass - Emit a forward reference to the class structure
902 /// for the metaclass of the given interface. The return value has
903 /// type ClassPtrTy.
904 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
905
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000906 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000907 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000908 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
909 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000910 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000911
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000912 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000914 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000915
916 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000917 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000918 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000919 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000920 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000921
922 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000924 /// - TypeName: The name for the type containing the methods.
925 /// - IsProtocol: True iff these methods are for a protocol.
926 /// - ClassMethds: True iff these are class methods.
927 /// - Required: When true, only "required" methods are
928 /// listed. Similarly, when false only "optional" methods are
929 /// listed. For classes this should always be true.
930 /// - begin, end: The method list to output.
931 ///
932 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000933 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000934 const char *Section,
935 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000936
Daniel Dunbarc475d422008-10-29 22:36:39 +0000937 /// GetOrEmitProtocol - Get the protocol object for the given
938 /// declaration, emitting it if necessary. The return value has type
939 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000940 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000941
942 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
943 /// object for the given declaration, emitting it if needed. These
944 /// forward references will be filled in with empty bodies if no
945 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000946 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000947
Daniel Dunbarb036db82008-08-13 03:21:16 +0000948 /// EmitProtocolExtension - Generate the protocol extension
949 /// structure used to store optional instance and class methods, and
950 /// protocol properties. The return value has type
951 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000952 llvm::Constant *
953 EmitProtocolExtension(const ObjCProtocolDecl *PD,
954 const ConstantVector &OptInstanceMethods,
955 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000956
957 /// EmitProtocolList - Generate the list of referenced
958 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000959 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +0000960 ObjCProtocolDecl::protocol_iterator begin,
961 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000962
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000963 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
964 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000965 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
966 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000967
968public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000969 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000970
Fariborz Jahanian71394042009-01-23 23:53:38 +0000971 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000972
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000973 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000974 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000975 QualType ResultType,
976 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000977 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000978 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +0000979 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000980 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000981
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000982 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000983 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000984 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000985 QualType ResultType,
986 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000987 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000988 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000989 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000990 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000991 const CallArgList &CallArgs,
992 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000993
Daniel Dunbarcb463852008-11-01 01:53:16 +0000994 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000995 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000996
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000997 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
998 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000999
1000 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1001 /// untyped one.
1002 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1003 const ObjCMethodDecl *Method);
1004
John McCall2ca705e2010-07-24 00:37:23 +00001005 virtual llvm::Constant *GetEHType(QualType T);
1006
Daniel Dunbar92992502008-08-15 22:20:32 +00001007 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001008
Daniel Dunbar92992502008-08-15 22:20:32 +00001009 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001010
Daniel Dunbarcb463852008-11-01 01:53:16 +00001011 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001012 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001013
Chris Lattnerd4808922009-03-22 21:03:39 +00001014 virtual llvm::Constant *GetPropertyGetFunction();
1015 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001016 virtual llvm::Constant *GetGetStructFunction();
1017 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001018 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001019
John McCallbd309292010-07-06 01:34:17 +00001020 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1021 const ObjCAtTryStmt &S);
1022 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1023 const ObjCAtSynchronizedStmt &S);
1024 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001025 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1026 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001027 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001028 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001029 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001030 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001031 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001032 llvm::Value *src, llvm::Value *dest,
1033 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001034 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001035 llvm::Value *src, llvm::Value *dest,
1036 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001037 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1038 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001039 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1040 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001041 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001042
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001043 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1044 QualType ObjectTy,
1045 llvm::Value *BaseValue,
1046 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001047 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001048 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001049 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001050 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001051};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001052
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001053class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001054private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001055 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001056 llvm::GlobalVariable* ObjCEmptyCacheVar;
1057 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001058
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001059 /// SuperClassReferences - uniqued super class references.
1060 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001061
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001062 /// MetaClassReferences - uniqued meta class references.
1063 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001064
1065 /// EHTypeReferences - uniqued class ehtype references.
1066 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001067
John McCall9e8bb002011-05-14 03:10:52 +00001068 /// VTableDispatchMethods - List of methods for which we generate
1069 /// vtable-based message dispatch.
1070 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001071
Fariborz Jahanian67260552009-11-17 21:37:35 +00001072 /// DefinedMetaClasses - List of defined meta-classes.
1073 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1074
John McCall9e8bb002011-05-14 03:10:52 +00001075 /// isVTableDispatchedSelector - Returns true if SEL is a
1076 /// vtable-based selector.
1077 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001078
Fariborz Jahanian71394042009-01-23 23:53:38 +00001079 /// FinishNonFragileABIModule - Write out global data structures at the end of
1080 /// processing a translation unit.
1081 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001082
Daniel Dunbar19573e72009-05-15 21:48:48 +00001083 /// AddModuleClassList - Add the given list of class pointers to the
1084 /// module with the provided symbol and section names.
1085 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1086 const char *SymbolName,
1087 const char *SectionName);
1088
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1090 unsigned InstanceStart,
1091 unsigned InstanceSize,
1092 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001093 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001094 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001095 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001096 llvm::Constant *ClassRoGV,
1097 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001098
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001099 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001100
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001101 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001102
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001103 /// EmitMethodList - Emit the method list for the given
1104 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001105 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001106 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001107 const ConstantVector &Methods);
1108 /// EmitIvarList - Emit the ivar list for the given
1109 /// implementation. If ForClass is true the list of class ivars
1110 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1111 /// interface ivars will be emitted. The return value has type
1112 /// IvarListnfABIPtrTy.
1113 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001114
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001115 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001116 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001117 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001118
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001119 /// GetOrEmitProtocol - Get the protocol object for the given
1120 /// declaration, emitting it if necessary. The return value has type
1121 /// ProtocolPtrTy.
1122 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001123
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001124 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1125 /// object for the given declaration, emitting it if needed. These
1126 /// forward references will be filled in with empty bodies if no
1127 /// definition is seen. The return value has type ProtocolPtrTy.
1128 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001129
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001130 /// EmitProtocolList - Generate the list of referenced
1131 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001132 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001133 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001134 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001135
John McCall9e8bb002011-05-14 03:10:52 +00001136 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1137 ReturnValueSlot Return,
1138 QualType ResultType,
1139 Selector Sel,
1140 llvm::Value *Receiver,
1141 QualType Arg0Ty,
1142 bool IsSuper,
1143 const CallArgList &CallArgs,
1144 const ObjCMethodDecl *Method);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001145
1146 /// GetClassGlobal - Return the global variable for the Objective-C
1147 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001148 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001149
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001150 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001151 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001152 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001153 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001154
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001155 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1156 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001157 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1158 const ObjCInterfaceDecl *ID);
1159
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001160 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1161 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001162 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001163 const ObjCInterfaceDecl *ID);
1164
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001165 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1166 /// the given ivar.
1167 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001168 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001169 const ObjCInterfaceDecl *ID,
1170 const ObjCIvarDecl *Ivar);
1171
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001172 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1173 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001174 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1175 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001176
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001177 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001178 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001179 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001180 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001181
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001183 return "OBJC_METACLASS_$_";
1184 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001185
Daniel Dunbar15894b72009-04-07 05:48:37 +00001186 const char *getClassSymbolPrefix() const {
1187 return "OBJC_CLASS_$_";
1188 }
1189
Daniel Dunbar961202372009-05-03 12:57:56 +00001190 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001191 uint32_t &InstanceStart,
1192 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001193
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001194 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001195 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001196 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1197 return CGM.getContext().Selectors.getSelector(0, &II);
1198 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001200 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001201 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1202 return CGM.getContext().Selectors.getSelector(1, &II);
1203 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001204
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001205 /// ImplementationIsNonLazy - Check whether the given category or
1206 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001207 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001208
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001209public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001210 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001211 // FIXME. All stubs for now!
1212 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001213
Fariborz Jahanian71394042009-01-23 23:53:38 +00001214 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001215 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001216 QualType ResultType,
1217 Selector Sel,
1218 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001219 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001220 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001221 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001222
1223 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001224 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001225 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001226 QualType ResultType,
1227 Selector Sel,
1228 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001229 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001230 llvm::Value *Receiver,
1231 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001232 const CallArgList &CallArgs,
1233 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234
Fariborz Jahanian71394042009-01-23 23:53:38 +00001235 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001236 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001237
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001238 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1239 bool lvalue = false)
1240 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001241
1242 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1243 /// untyped one.
1244 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1245 const ObjCMethodDecl *Method)
1246 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001247
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001248 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001249
Fariborz Jahanian71394042009-01-23 23:53:38 +00001250 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001251 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001252 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001253
John McCall2ca705e2010-07-24 00:37:23 +00001254 virtual llvm::Constant *GetEHType(QualType T);
1255
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001257 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001258 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001259 virtual llvm::Constant *GetPropertySetFunction() {
1260 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001261 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001262
David Chisnall168b80f2010-12-26 22:13:16 +00001263 virtual llvm::Constant *GetSetStructFunction() {
1264 return ObjCTypes.getCopyStructFn();
1265 }
1266 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001267 return ObjCTypes.getCopyStructFn();
1268 }
1269
Chris Lattnerd4808922009-03-22 21:03:39 +00001270 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001271 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001272 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
John McCallbd309292010-07-06 01:34:17 +00001274 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1275 const ObjCAtTryStmt &S);
1276 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1277 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001278 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001279 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001280 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001281 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001282 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001283 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001284 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001285 llvm::Value *src, llvm::Value *dest,
1286 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001287 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001288 llvm::Value *src, llvm::Value *dest,
1289 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001290 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001291 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001292 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1293 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001294 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001295 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1296 QualType ObjectTy,
1297 llvm::Value *BaseValue,
1298 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001299 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001300 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001301 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001302 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001303};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001304
John McCall5880fb82011-05-14 21:12:11 +00001305/// A helper class for performing the null-initialization of a return
1306/// value.
1307struct NullReturnState {
1308 llvm::BasicBlock *NullBB;
1309
1310 NullReturnState() : NullBB(0) {}
1311
1312 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1313 // Make blocks for the null-init and call edges.
1314 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1315 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1316
1317 // Check for a null receiver and, if there is one, jump to the
1318 // null-init test.
1319 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1320 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1321
1322 // Otherwise, start performing the call.
1323 CGF.EmitBlock(callBB);
1324 }
1325
1326 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1327 if (!NullBB) return result;
1328
1329 // Finish the call path.
1330 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1331 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1332
1333 // Emit the null-init block and perform the null-initialization there.
1334 CGF.EmitBlock(NullBB);
1335 assert(result.isAggregate() && "null init of non-aggregate result?");
1336 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1337
1338 // Jump to the continuation block.
1339 CGF.EmitBlock(contBB);
1340
1341 return result;
1342 }
1343};
1344
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001345} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001346
1347/* *** Helper Functions *** */
1348
1349/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001350static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001351 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001352 unsigned idx0,
1353 unsigned idx1) {
1354 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001355 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1356 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001357 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001358 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001359}
1360
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001361/// hasObjCExceptionAttribute - Return true if this class or any super
1362/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001363static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001364 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001365 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001366 return true;
1367 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001368 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001369 return false;
1370}
1371
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001372/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001373
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001374CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001375 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001376 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001377 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001378}
1379
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001380/// GetClass - Return a reference to the class for the given interface
1381/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001382llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001383 const ObjCInterfaceDecl *ID) {
1384 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001385}
1386
1387/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001388llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1389 bool lval) {
1390 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001391}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001392llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001394 return EmitSelector(Builder, Method->getSelector());
1395}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001396
John McCall2ca705e2010-07-24 00:37:23 +00001397llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1398 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1399 return 0;
1400}
1401
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001402/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001403/*
1404 struct __builtin_CFString {
1405 const int *isa; // point to __CFConstantStringClassReference
1406 int flags;
1407 const char *str;
1408 long length;
1409 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001410*/
1411
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001412/// or Generate a constant NSString object.
1413/*
1414 struct __builtin_NSString {
1415 const int *isa; // point to __NSConstantStringClassReference
1416 const char *str;
1417 unsigned int length;
1418 };
1419*/
1420
Fariborz Jahanian71394042009-01-23 23:53:38 +00001421llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001422 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001423 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1424 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001425 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001426}
1427
1428/// Generates a message send where the super is the receiver. This is
1429/// a message send to self with special delivery semantics indicating
1430/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001431CodeGen::RValue
1432CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001433 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001434 QualType ResultType,
1435 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001436 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001437 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001438 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001439 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001440 const CodeGen::CallArgList &CallArgs,
1441 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001442 // Create and init a super structure; this is a (receiver, class)
1443 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001444 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001445 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001446 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001447 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001448 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001449 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001450
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001451 // If this is a class message the metaclass is passed as the target.
1452 llvm::Value *Target;
1453 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001454 if (isCategoryImpl) {
1455 // Message sent to 'super' in a class method defined in a category
1456 // implementation requires an odd treatment.
1457 // If we are in a class method, we must retrieve the
1458 // _metaclass_ for the current class, pointed at by
1459 // the class's "isa" pointer. The following assumes that
1460 // isa" is the first ivar in a class (which it must be).
1461 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1462 Target = CGF.Builder.CreateStructGEP(Target, 0);
1463 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001464 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001465 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1466 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1467 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1468 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001469 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001470 }
1471 else if (isCategoryImpl)
1472 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1473 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001474 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1475 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1476 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001477 }
Mike Stump18bb9282009-05-16 07:57:57 +00001478 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1479 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001480 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001481 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001482 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001483 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001484 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001485 return EmitMessageSend(CGF, Return, ResultType,
1486 EmitSelector(CGF.Builder, Sel),
1487 ObjCSuper, ObjCTypes.SuperPtrCTy,
1488 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001489}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001490
1491/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001492CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001493 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001494 QualType ResultType,
1495 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001496 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001497 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001498 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001499 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001500 return EmitMessageSend(CGF, Return, ResultType,
1501 EmitSelector(CGF.Builder, Sel),
1502 Receiver, CGF.getContext().getObjCIdType(),
1503 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001504}
1505
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001506CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001507CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1508 ReturnValueSlot Return,
1509 QualType ResultType,
1510 llvm::Value *Sel,
1511 llvm::Value *Arg0,
1512 QualType Arg0Ty,
1513 bool IsSuper,
1514 const CallArgList &CallArgs,
1515 const ObjCMethodDecl *Method,
1516 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001517 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001518 if (!IsSuper)
1519 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman43dca6a2011-05-02 17:57:46 +00001520 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1521 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
Daniel Dunbarc722b852008-08-30 03:02:31 +00001522 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001523
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001524 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001525 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001526 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001527 const llvm::FunctionType *FTy =
1528 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001529
Anders Carlsson280e61f12010-06-21 20:59:55 +00001530 if (Method)
1531 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1532 CGM.getContext().getCanonicalType(ResultType) &&
1533 "Result type mismatch!");
1534
John McCall5880fb82011-05-14 21:12:11 +00001535 NullReturnState nullReturn;
1536
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001537 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001538 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001539 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001540 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001541 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001542 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1543 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1544 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001545 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001546 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001547 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001548 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001549 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001550 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1551 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001552}
1553
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001554static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1555 if (FQT.isObjCGCStrong())
1556 return Qualifiers::Strong;
1557
1558 if (FQT.isObjCGCWeak())
1559 return Qualifiers::Weak;
1560
1561 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1562 return Qualifiers::Strong;
1563
1564 if (const PointerType *PT = FQT->getAs<PointerType>())
1565 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1566
1567 return Qualifiers::GCNone;
1568}
1569
John McCall351762c2011-02-07 10:33:21 +00001570llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1571 const CGBlockInfo &blockInfo) {
1572 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001573 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001574
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001575 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001576 return nullPtr;
1577
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001578 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001579 SkipIvars.clear();
1580 IvarsInfo.clear();
1581 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1582 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1583
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001584 // __isa is the first field in block descriptor and must assume by runtime's
1585 // convention that it is GC'able.
1586 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001587
1588 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1589
1590 // Calculate the basic layout of the block structure.
1591 const llvm::StructLayout *layout =
1592 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1593
1594 // Ignore the optional 'this' capture: C++ objects are not assumed
1595 // to be GC'ed.
1596
1597 // Walk the captured variables.
1598 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1599 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1600 const VarDecl *variable = ci->getVariable();
1601 QualType type = variable->getType();
1602
1603 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1604
1605 // Ignore constant captures.
1606 if (capture.isConstant()) continue;
1607
1608 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1609
1610 // __block variables are passed by their descriptor address.
1611 if (ci->isByRef()) {
1612 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001613 continue;
John McCall351762c2011-02-07 10:33:21 +00001614 }
1615
1616 assert(!type->isArrayType() && "array variable should not be caught");
1617 if (const RecordType *record = type->getAs<RecordType>()) {
1618 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001619 continue;
1620 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001621
John McCall351762c2011-02-07 10:33:21 +00001622 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1623 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1624
1625 if (GCAttr == Qualifiers::Strong)
1626 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1627 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001628 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001629 SkipIvars.push_back(GC_IVAR(fieldOffset,
1630 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001631 }
1632
1633 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001634 return nullPtr;
1635
1636 // Sort on byte position; captures might not be allocated in order,
1637 // and unions can do funny things.
1638 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1639 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001640
1641 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001642 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001643 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1644 printf("\n block variable layout for block: ");
1645 const unsigned char *s = (unsigned char*)BitMap.c_str();
1646 for (unsigned i = 0; i < BitMap.size(); i++)
1647 if (!(s[i] & 0xf0))
1648 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1649 else
1650 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1651 printf("\n");
1652 }
1653
1654 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001655}
1656
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001657llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001658 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001659 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001660 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001661 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1662
Owen Andersonade90fd2009-07-29 18:54:39 +00001663 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001664 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001665}
1666
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001667void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001668 // FIXME: We shouldn't need this, the protocol decl should contain enough
1669 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001670 DefinedProtocols.insert(PD->getIdentifier());
1671
1672 // If we have generated a forward reference to this protocol, emit
1673 // it now. Otherwise do nothing, the protocol objects are lazily
1674 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001675 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001676 GetOrEmitProtocol(PD);
1677}
1678
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001679llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001680 if (DefinedProtocols.count(PD->getIdentifier()))
1681 return GetOrEmitProtocol(PD);
1682 return GetOrEmitProtocolRef(PD);
1683}
1684
Daniel Dunbarb036db82008-08-13 03:21:16 +00001685/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001686// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1687struct _objc_protocol {
1688struct _objc_protocol_extension *isa;
1689char *protocol_name;
1690struct _objc_protocol_list *protocol_list;
1691struct _objc__method_prototype_list *instance_methods;
1692struct _objc__method_prototype_list *class_methods
1693};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001694
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001695See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001696*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001697llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1698 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1699
1700 // Early exit if a defining object has already been generated.
1701 if (Entry && Entry->hasInitializer())
1702 return Entry;
1703
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001704 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001705 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001706 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1707
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001708 // Construct method lists.
1709 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1710 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001711 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001712 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001713 ObjCMethodDecl *MD = *i;
1714 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1715 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1716 OptInstanceMethods.push_back(C);
1717 } else {
1718 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001719 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001720 }
1721
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001722 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001723 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001724 ObjCMethodDecl *MD = *i;
1725 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1726 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1727 OptClassMethods.push_back(C);
1728 } else {
1729 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001730 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001731 }
1732
Daniel Dunbarb036db82008-08-13 03:21:16 +00001733 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001734 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001735 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001736 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001737 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001738 PD->protocol_begin(),
1739 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001740 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001741 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001742 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1743 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001744 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001745 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001746 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1747 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001748 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001749 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001750
Daniel Dunbarb036db82008-08-13 03:21:16 +00001751 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 // Already created, fix the linkage and update the initializer.
1753 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001754 Entry->setInitializer(Init);
1755 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001756 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001757 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001758 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001759 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001760 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001761 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001762 // FIXME: Is this necessary? Why only for protocol?
1763 Entry->setAlignment(4);
1764 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001765 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001766
1767 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001768}
1769
Daniel Dunbarc475d422008-10-29 22:36:39 +00001770llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001771 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1772
1773 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001774 // We use the initializer as a marker of whether this is a forward
1775 // reference or not. At module finalization we add the empty
1776 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001777 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001778 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001779 llvm::GlobalValue::ExternalLinkage,
1780 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001781 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001782 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001783 // FIXME: Is this necessary? Why only for protocol?
1784 Entry->setAlignment(4);
1785 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001786
Daniel Dunbarb036db82008-08-13 03:21:16 +00001787 return Entry;
1788}
1789
1790/*
1791 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001792 uint32_t size;
1793 struct objc_method_description_list *optional_instance_methods;
1794 struct objc_method_description_list *optional_class_methods;
1795 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001796 };
1797*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001798llvm::Constant *
1799CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1800 const ConstantVector &OptInstanceMethods,
1801 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001802 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001803 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001804 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001805 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001806 Values[1] =
1807 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001808 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001809 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1810 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001811 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001812 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001813 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1814 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001815 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001816 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001817
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001818 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001819 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001820 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001821 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001822
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001823 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001824 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001825
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001826 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001827 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001828 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001829 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001830}
1831
1832/*
1833 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001834 struct objc_protocol_list *next;
1835 long count;
1836 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001837 };
1838*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001839llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001840CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001841 ObjCProtocolDecl::protocol_iterator begin,
1842 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843 std::vector<llvm::Constant*> ProtocolRefs;
1844
Daniel Dunbardec75f82008-08-21 21:57:41 +00001845 for (; begin != end; ++begin)
1846 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001847
1848 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001849 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001850 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001851
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001852 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001853 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854
1855 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001856 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001857 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001858 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859 ProtocolRefs.size() - 1);
1860 Values[2] =
1861 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1862 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001864
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001865 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001866 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001867 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001868 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001869 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001870}
1871
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001872void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1873 std::vector<llvm::Constant*> &Properties,
1874 const Decl *Container,
1875 const ObjCProtocolDecl *PROTO,
1876 const ObjCCommonTypesHelper &ObjCTypes) {
1877 std::vector<llvm::Constant*> Prop(2);
1878 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1879 E = PROTO->protocol_end(); P != E; ++P)
1880 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1881 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1882 E = PROTO->prop_end(); I != E; ++I) {
1883 const ObjCPropertyDecl *PD = *I;
1884 if (!PropertySet.insert(PD->getIdentifier()))
1885 continue;
1886 Prop[0] = GetPropertyName(PD->getIdentifier());
1887 Prop[1] = GetPropertyTypeString(PD, Container);
1888 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1889 }
1890}
1891
Daniel Dunbarb036db82008-08-13 03:21:16 +00001892/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001893 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001894 const char * const name;
1895 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001896 };
1897
1898 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001899 uint32_t entsize; // sizeof (struct _objc_property)
1900 uint32_t prop_count;
1901 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001902 };
1903*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001904llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001905 const Decl *Container,
1906 const ObjCContainerDecl *OCD,
1907 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001908 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001909 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001910 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1911 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001912 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001913 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001914 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001915 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001916 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001917 Prop));
1918 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001919 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001920 for (ObjCInterfaceDecl::all_protocol_iterator
1921 P = OID->all_referenced_protocol_begin(),
1922 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001923 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1924 ObjCTypes);
1925 }
1926 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1927 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1928 E = CD->protocol_end(); P != E; ++P)
1929 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1930 ObjCTypes);
1931 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001932
1933 // Return null for empty list.
1934 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001935 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001936
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001937 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001938 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001939 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001940 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1941 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001942 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001943 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001944 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001945 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001946
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001947 llvm::GlobalVariable *GV =
1948 CreateMetadataVar(Name, Init,
1949 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001950 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001951 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001952 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001953 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001954}
1955
1956/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001957 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001958 int count;
1959 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001960 };
1961*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001962llvm::Constant *
1963CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1964 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001965 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001966 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1967 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001968 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001969 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001970 Desc);
1971}
Daniel Dunbarb036db82008-08-13 03:21:16 +00001972
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001973llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001974 const char *Section,
1975 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001976 // Return null for empty list.
1977 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001978 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001979
1980 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001981 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001982 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001983 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001984 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001985 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001986
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001987 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001989 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001990}
1991
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001992/*
1993 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 char *category_name;
1995 char *class_name;
1996 struct _objc_method_list *instance_methods;
1997 struct _objc_method_list *class_methods;
1998 struct _objc_protocol_list *protocols;
1999 uint32_t size; // <rdar://4585769>
2000 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002001 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002002*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002003void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002004 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002005
Mike Stump18bb9282009-05-16 07:57:57 +00002006 // FIXME: This is poor design, the OCD should have a pointer to the category
2007 // decl. Additionally, note that Category can be null for the @implementation
2008 // w/o an @interface case. Sema should just create one for us as it does for
2009 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002010 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002011 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002012 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002013
2014 llvm::SmallString<256> ExtName;
2015 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2016 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002017
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002018 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002019 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002020 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002021 // Instance methods should always be defined.
2022 InstanceMethods.push_back(GetMethodConstant(*i));
2023 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002024 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002025 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002026 // Class methods should always be defined.
2027 ClassMethods.push_back(GetMethodConstant(*i));
2028 }
2029
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002030 std::vector<llvm::Constant*> Values(7);
2031 Values[0] = GetClassName(OCD->getIdentifier());
2032 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002033 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002034 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002035 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002036 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002037 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002038 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002039 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002040 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002041 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002042 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002043 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002044 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002045 Category->protocol_begin(),
2046 Category->protocol_end());
2047 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002048 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002049 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002050 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002051
2052 // If there is no category @interface then there can be no properties.
2053 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002054 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002055 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002056 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002057 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002058 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002059
Owen Anderson0e0189d2009-07-27 22:29:56 +00002060 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002061 Values);
2062
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002064 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002065 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002066 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002067 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002068 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002069 // method definition entries must be clear for next implementation.
2070 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002071}
2072
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002073// FIXME: Get from somewhere?
2074enum ClassFlags {
2075 eClassFlags_Factory = 0x00001,
2076 eClassFlags_Meta = 0x00002,
2077 // <rdr://5142207>
2078 eClassFlags_HasCXXStructors = 0x02000,
2079 eClassFlags_Hidden = 0x20000,
2080 eClassFlags_ABI2_Hidden = 0x00010,
2081 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2082};
2083
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002084/*
2085 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002086 Class isa;
2087 Class super_class;
2088 const char *name;
2089 long version;
2090 long info;
2091 long instance_size;
2092 struct _objc_ivar_list *ivars;
2093 struct _objc_method_list *methods;
2094 struct _objc_cache *cache;
2095 struct _objc_protocol_list *protocols;
2096 // Objective-C 1.0 extensions (<rdr://4585769>)
2097 const char *ivar_layout;
2098 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002099 };
2100
2101 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002102*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002103void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002104 DefinedSymbols.insert(ID->getIdentifier());
2105
Chris Lattner86d7d912008-11-24 03:54:41 +00002106 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002107 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002108 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002109 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002110 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002111 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002112 Interface->all_referenced_protocol_begin(),
2113 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002114 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002115 if (ID->getNumIvarInitializers())
2116 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002117 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002118 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002119
2120 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002121 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002122 Flags |= eClassFlags_Hidden;
2123
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002124 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002125 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002126 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002127 // Instance methods should always be defined.
2128 InstanceMethods.push_back(GetMethodConstant(*i));
2129 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002131 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002132 // Class methods should always be defined.
2133 ClassMethods.push_back(GetMethodConstant(*i));
2134 }
2135
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002136 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002137 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002138 ObjCPropertyImplDecl *PID = *i;
2139
2140 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2141 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2142
2143 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2144 if (llvm::Constant *C = GetMethodConstant(MD))
2145 InstanceMethods.push_back(C);
2146 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2147 if (llvm::Constant *C = GetMethodConstant(MD))
2148 InstanceMethods.push_back(C);
2149 }
2150 }
2151
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002152 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002153 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002154 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002155 // Record a reference to the super class.
2156 LazySymbols.insert(Super->getIdentifier());
2157
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002158 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002159 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002160 ObjCTypes.ClassPtrTy);
2161 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002162 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002163 }
2164 Values[ 2] = GetClassName(ID->getIdentifier());
2165 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002166 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2167 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2168 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002169 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002170 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002171 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002172 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002173 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002174 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002175 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002176 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002177 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002178 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002179 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002180 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002181 std::string Name("\01L_OBJC_CLASS_");
2182 Name += ClassName;
2183 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2184 // Check for a forward reference.
2185 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2186 if (GV) {
2187 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2188 "Forward metaclass reference has incorrect type.");
2189 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2190 GV->setInitializer(Init);
2191 GV->setSection(Section);
2192 GV->setAlignment(4);
2193 CGM.AddUsedGlobal(GV);
2194 }
2195 else
2196 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002197 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002198 // method definition entries must be clear for next implementation.
2199 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002200}
2201
2202llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2203 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002204 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002205 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002206 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002207
John McCall457a04e2010-10-22 21:05:15 +00002208 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002209 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002210
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002211 std::vector<llvm::Constant*> Values(12);
2212 // The isa for the metaclass is the root of the hierarchy.
2213 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2214 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2215 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002216 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002217 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002218 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002219 // The super class for the metaclass is emitted as the name of the
2220 // super class. The runtime fixes this up to point to the
2221 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002222 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002223 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002224 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002225 ObjCTypes.ClassPtrTy);
2226 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002227 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002228 }
2229 Values[ 2] = GetClassName(ID->getIdentifier());
2230 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002231 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2232 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2233 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002234 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002235 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002236 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002237 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002238 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002239 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002240 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002241 Values[ 9] = Protocols;
2242 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002243 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002244 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002245 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002246 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002247 Values);
2248
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002249 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002250 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002251
2252 // Check for a forward reference.
2253 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2254 if (GV) {
2255 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2256 "Forward metaclass reference has incorrect type.");
2257 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2258 GV->setInitializer(Init);
2259 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002260 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002261 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002262 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002263 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002264 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002265 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002266 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002267
2268 return GV;
2269}
2270
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002271llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002272 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002273
Mike Stump18bb9282009-05-16 07:57:57 +00002274 // FIXME: Should we look these up somewhere other than the module. Its a bit
2275 // silly since we only generate these while processing an implementation, so
2276 // exactly one pointer would work if know when we entered/exitted an
2277 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002278
2279 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002280 // Previously, metaclass with internal linkage may have been defined.
2281 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002282 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2283 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002284 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2285 "Forward metaclass reference has incorrect type.");
2286 return GV;
2287 } else {
2288 // Generate as an external reference to keep a consistent
2289 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002290 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002291 llvm::GlobalValue::ExternalLinkage,
2292 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002293 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002294 }
2295}
2296
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002297llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2298 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2299
2300 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2301 true)) {
2302 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2303 "Forward class metadata reference has incorrect type.");
2304 return GV;
2305 } else {
2306 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2307 llvm::GlobalValue::ExternalLinkage,
2308 0,
2309 Name);
2310 }
2311}
2312
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002313/*
2314 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002315 uint32_t size;
2316 const char *weak_ivar_layout;
2317 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002318 };
2319*/
2320llvm::Constant *
2321CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002322 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002323 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002324
2325 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002326 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002327 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002328 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002329 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002330
2331 // Return null if no extension bits are used.
2332 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002333 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002334
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002335 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002336 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002337 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002338 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002339 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340}
2341
2342/*
2343 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002344 char *ivar_name;
2345 char *ivar_type;
2346 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002347 };
2348
2349 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002350 int ivar_count;
2351 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002352 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002353*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002354llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002355 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356 std::vector<llvm::Constant*> Ivars, Ivar(3);
2357
2358 // When emitting the root class GCC emits ivar entries for the
2359 // actual class structure. It is not clear if we need to follow this
2360 // behavior; for now lets try and get away with not doing it. If so,
2361 // the cleanest solution would be to make up an ObjCInterfaceDecl
2362 // for the class.
2363 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002364 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002365
2366 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002367 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002368
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002369 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002370 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002371
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002372 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2373 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002374 // Ignore unnamed bit-fields.
2375 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002376 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002377 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2378 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002379 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002380 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002381 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002382 }
2383
2384 // Return null for empty list.
2385 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002386 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002387
2388 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002389 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002390 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002391 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002392 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002393 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002394
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002395 llvm::GlobalVariable *GV;
2396 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002397 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002398 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002399 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002400 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002401 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002402 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002403 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002404 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002405}
2406
2407/*
2408 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002409 SEL method_name;
2410 char *method_types;
2411 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002412 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002413
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002414 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002415 struct objc_method_list *obsolete;
2416 int count;
2417 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002418 };
2419*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002420
2421/// GetMethodConstant - Return a struct objc_method constant for the
2422/// given method if it has been defined. The result is null if the
2423/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002424llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002425 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002426 if (!Fn)
2427 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002428
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002429 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002430 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002431 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002432 ObjCTypes.SelectorPtrTy);
2433 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002434 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002435 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002436}
2437
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002438llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002439 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002440 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002441 // Return null for empty list.
2442 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002443 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444
2445 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002446 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002447 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002448 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002450 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002451 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002452
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002453 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002454 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002456}
2457
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002458llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002459 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002460 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002461 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002462
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002463 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002464 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002465 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002466 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002467 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002468 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002469 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002470 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002471 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002472
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002473 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002474}
2475
Daniel Dunbar30c65362009-03-09 20:09:19 +00002476llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002477CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002478 llvm::Constant *Init,
2479 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002480 unsigned Align,
2481 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002482 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002483 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002484 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002485 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002486 if (Section)
2487 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002488 if (Align)
2489 GV->setAlignment(Align);
2490 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002491 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002492 return GV;
2493}
2494
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002495llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002496 // Abuse this interface function as a place to finalize.
2497 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002498 return NULL;
2499}
2500
Chris Lattnerd4808922009-03-22 21:03:39 +00002501llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002502 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002503}
2504
Chris Lattnerd4808922009-03-22 21:03:39 +00002505llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002506 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002507}
2508
David Chisnall168b80f2010-12-26 22:13:16 +00002509llvm::Constant *CGObjCMac::GetGetStructFunction() {
2510 return ObjCTypes.getCopyStructFn();
2511}
2512llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002513 return ObjCTypes.getCopyStructFn();
2514}
2515
Chris Lattnerd4808922009-03-22 21:03:39 +00002516llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002517 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002518}
2519
John McCallbd309292010-07-06 01:34:17 +00002520void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2521 return EmitTryOrSynchronizedStmt(CGF, S);
2522}
2523
2524void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2525 const ObjCAtSynchronizedStmt &S) {
2526 return EmitTryOrSynchronizedStmt(CGF, S);
2527}
2528
John McCall65bea082010-07-21 06:59:36 +00002529namespace {
John McCallcda666c2010-07-21 07:22:38 +00002530 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002531 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002532 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002533 llvm::Value *CallTryExitVar;
2534 llvm::Value *ExceptionData;
2535 ObjCTypesHelper &ObjCTypes;
2536 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002537 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002538 llvm::Value *CallTryExitVar,
2539 llvm::Value *ExceptionData,
2540 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002541 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002542 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2543
2544 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2545 // Check whether we need to call objc_exception_try_exit.
2546 // In optimized code, this branch will always be folded.
2547 llvm::BasicBlock *FinallyCallExit =
2548 CGF.createBasicBlock("finally.call_exit");
2549 llvm::BasicBlock *FinallyNoCallExit =
2550 CGF.createBasicBlock("finally.no_call_exit");
2551 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2552 FinallyCallExit, FinallyNoCallExit);
2553
2554 CGF.EmitBlock(FinallyCallExit);
2555 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2556 ->setDoesNotThrow();
2557
2558 CGF.EmitBlock(FinallyNoCallExit);
2559
2560 if (isa<ObjCAtTryStmt>(S)) {
2561 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002562 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2563 // Save the current cleanup destination in case there's
2564 // control flow inside the finally statement.
2565 llvm::Value *CurCleanupDest =
2566 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2567
John McCall65bea082010-07-21 06:59:36 +00002568 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2569
John McCallcebe0ca2010-08-11 00:16:14 +00002570 if (CGF.HaveInsertPoint()) {
2571 CGF.Builder.CreateStore(CurCleanupDest,
2572 CGF.getNormalCleanupDestSlot());
2573 } else {
2574 // Currently, the end of the cleanup must always exist.
2575 CGF.EnsureInsertPoint();
2576 }
2577 }
John McCall65bea082010-07-21 06:59:36 +00002578 } else {
2579 // Emit objc_sync_exit(expr); as finally's sole statement for
2580 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002581 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002582 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2583 ->setDoesNotThrow();
2584 }
2585 }
2586 };
John McCall42227ed2010-07-31 23:20:56 +00002587
2588 class FragileHazards {
2589 CodeGenFunction &CGF;
2590 llvm::SmallVector<llvm::Value*, 20> Locals;
2591 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2592
2593 llvm::InlineAsm *ReadHazard;
2594 llvm::InlineAsm *WriteHazard;
2595
2596 llvm::FunctionType *GetAsmFnType();
2597
2598 void collectLocals();
2599 void emitReadHazard(CGBuilderTy &Builder);
2600
2601 public:
2602 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002603
John McCall42227ed2010-07-31 23:20:56 +00002604 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002605 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002606 };
2607}
2608
2609/// Create the fragile-ABI read and write hazards based on the current
2610/// state of the function, which is presumed to be immediately prior
2611/// to a @try block. These hazards are used to maintain correct
2612/// semantics in the face of optimization and the fragile ABI's
2613/// cavalier use of setjmp/longjmp.
2614FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2615 collectLocals();
2616
2617 if (Locals.empty()) return;
2618
2619 // Collect all the blocks in the function.
2620 for (llvm::Function::iterator
2621 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2622 BlocksBeforeTry.insert(&*I);
2623
2624 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2625
2626 // Create a read hazard for the allocas. This inhibits dead-store
2627 // optimizations and forces the values to memory. This hazard is
2628 // inserted before any 'throwing' calls in the protected scope to
2629 // reflect the possibility that the variables might be read from the
2630 // catch block if the call throws.
2631 {
2632 std::string Constraint;
2633 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2634 if (I) Constraint += ',';
2635 Constraint += "*m";
2636 }
2637
2638 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2639 }
2640
2641 // Create a write hazard for the allocas. This inhibits folding
2642 // loads across the hazard. This hazard is inserted at the
2643 // beginning of the catch path to reflect the possibility that the
2644 // variables might have been written within the protected scope.
2645 {
2646 std::string Constraint;
2647 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2648 if (I) Constraint += ',';
2649 Constraint += "=*m";
2650 }
2651
2652 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2653 }
2654}
2655
2656/// Emit a write hazard at the current location.
2657void FragileHazards::emitWriteHazard() {
2658 if (Locals.empty()) return;
2659
2660 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2661 ->setDoesNotThrow();
2662}
2663
John McCall42227ed2010-07-31 23:20:56 +00002664void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2665 assert(!Locals.empty());
2666 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2667 ->setDoesNotThrow();
2668}
2669
2670/// Emit read hazards in all the protected blocks, i.e. all the blocks
2671/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002672void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002673 if (Locals.empty()) return;
2674
2675 CGBuilderTy Builder(CGF.getLLVMContext());
2676
2677 // Iterate through all blocks, skipping those prior to the try.
2678 for (llvm::Function::iterator
2679 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2680 llvm::BasicBlock &BB = *FI;
2681 if (BlocksBeforeTry.count(&BB)) continue;
2682
2683 // Walk through all the calls in the block.
2684 for (llvm::BasicBlock::iterator
2685 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2686 llvm::Instruction &I = *BI;
2687
2688 // Ignore instructions that aren't non-intrinsic calls.
2689 // These are the only calls that can possibly call longjmp.
2690 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2691 if (isa<llvm::IntrinsicInst>(I))
2692 continue;
2693
2694 // Ignore call sites marked nounwind. This may be questionable,
2695 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2696 llvm::CallSite CS(&I);
2697 if (CS.doesNotThrow()) continue;
2698
John McCall2dd7d442010-08-04 05:59:32 +00002699 // Insert a read hazard before the call. This will ensure that
2700 // any writes to the locals are performed before making the
2701 // call. If the call throws, then this is sufficient to
2702 // guarantee correctness as long as it doesn't also write to any
2703 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002704 Builder.SetInsertPoint(&BB, BI);
2705 emitReadHazard(Builder);
2706 }
2707 }
2708}
2709
2710static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2711 if (V) S.insert(V);
2712}
2713
2714void FragileHazards::collectLocals() {
2715 // Compute a set of allocas to ignore.
2716 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2717 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2718 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2719 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2720
2721 // Collect all the allocas currently in the function. This is
2722 // probably way too aggressive.
2723 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2724 for (llvm::BasicBlock::iterator
2725 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2726 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2727 Locals.push_back(&*I);
2728}
2729
2730llvm::FunctionType *FragileHazards::GetAsmFnType() {
John McCall9dc0db22011-05-15 01:53:33 +00002731 llvm::SmallVector<const llvm::Type *, 16> tys(Locals.size());
2732 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2733 tys[i] = Locals[i]->getType();
2734 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002735}
2736
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002737/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002738
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002739 Objective-C setjmp-longjmp (sjlj) Exception Handling
2740 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002741
John McCallbd309292010-07-06 01:34:17 +00002742 A catch buffer is a setjmp buffer plus:
2743 - a pointer to the exception that was caught
2744 - a pointer to the previous exception data buffer
2745 - two pointers of reserved storage
2746 Therefore catch buffers form a stack, with a pointer to the top
2747 of the stack kept in thread-local storage.
2748
2749 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2750 objc_exception_try_exit pops the given catch buffer, which is
2751 required to be the top of the EH stack.
2752 objc_exception_throw pops the top of the EH stack, writes the
2753 thrown exception into the appropriate field, and longjmps
2754 to the setjmp buffer. It crashes the process (with a printf
2755 and an abort()) if there are no catch buffers on the stack.
2756 objc_exception_extract just reads the exception pointer out of the
2757 catch buffer.
2758
2759 There's no reason an implementation couldn't use a light-weight
2760 setjmp here --- something like __builtin_setjmp, but API-compatible
2761 with the heavyweight setjmp. This will be more important if we ever
2762 want to implement correct ObjC/C++ exception interactions for the
2763 fragile ABI.
2764
2765 Note that for this use of setjmp/longjmp to be correct, we may need
2766 to mark some local variables volatile: if a non-volatile local
2767 variable is modified between the setjmp and the longjmp, it has
2768 indeterminate value. For the purposes of LLVM IR, it may be
2769 sufficient to make loads and stores within the @try (to variables
2770 declared outside the @try) volatile. This is necessary for
2771 optimized correctness, but is not currently being done; this is
2772 being tracked as rdar://problem/8160285
2773
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002774 The basic framework for a @try-catch-finally is as follows:
2775 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002776 objc_exception_data d;
2777 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002778 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002779
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002780 objc_exception_try_enter(&d);
2781 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002782 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002783 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002784 // exception path
2785 id _caught = objc_exception_extract(&d);
2786
2787 // enter new try scope for handlers
2788 if (!setjmp(d.jmp_buf)) {
2789 ... match exception and execute catch blocks ...
2790
2791 // fell off end, rethrow.
2792 _rethrow = _caught;
2793 ... jump-through-finally to finally_rethrow ...
2794 } else {
2795 // exception in catch block
2796 _rethrow = objc_exception_extract(&d);
2797 _call_try_exit = false;
2798 ... jump-through-finally to finally_rethrow ...
2799 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002800 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002801 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002802
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002803 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002804 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002805 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002806
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002807 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002808 ... dispatch to finally destination ...
2809
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002810 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002811 objc_exception_throw(_rethrow);
2812
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002813 finally_end:
2814 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002815
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002816 This framework differs slightly from the one gcc uses, in that gcc
2817 uses _rethrow to determine if objc_exception_try_exit should be called
2818 and if the object should be rethrown. This breaks in the face of
2819 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002820
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002821 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002822
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002823 - If there are no catch blocks, then we avoid emitting the second
2824 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002825
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002826 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2827 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002829 - FIXME: If there is no @finally block we can do a few more
2830 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002831
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002832 Rethrows and Jumps-Through-Finally
2833 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002834
John McCallbd309292010-07-06 01:34:17 +00002835 '@throw;' is supported by pushing the currently-caught exception
2836 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002837
John McCallbd309292010-07-06 01:34:17 +00002838 Branches through the @finally block are handled with an ordinary
2839 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2840 exceptions are not compatible with C++ exceptions, and this is
2841 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002842
John McCallbd309292010-07-06 01:34:17 +00002843 @synchronized(expr) { stmt; } is emitted as if it were:
2844 id synch_value = expr;
2845 objc_sync_enter(synch_value);
2846 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002847*/
2848
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002849void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2850 const Stmt &S) {
2851 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002852
2853 // A destination for the fall-through edges of the catch handlers to
2854 // jump to.
2855 CodeGenFunction::JumpDest FinallyEnd =
2856 CGF.getJumpDestInCurrentScope("finally.end");
2857
2858 // A destination for the rethrow edge of the catch handlers to jump
2859 // to.
2860 CodeGenFunction::JumpDest FinallyRethrow =
2861 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002862
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002863 // For @synchronized, call objc_sync_enter(sync.expr). The
2864 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002865 // @synchronized. We can't avoid a temp here because we need the
2866 // value to be preserved. If the backend ever does liveness
2867 // correctly after setjmp, this will be unnecessary.
2868 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002869 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002870 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002871 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2872 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002873 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2874 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002875
2876 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2877 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002878 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002879
John McCall2dd7d442010-08-04 05:59:32 +00002880 // Allocate memory for the setjmp buffer. This needs to be kept
2881 // live throughout the try and catch blocks.
2882 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2883 "exceptiondata.ptr");
2884
John McCall42227ed2010-07-31 23:20:56 +00002885 // Create the fragile hazards. Note that this will not capture any
2886 // of the allocas required for exception processing, but will
2887 // capture the current basic block (which extends all the way to the
2888 // setjmp call) as "before the @try".
2889 FragileHazards Hazards(CGF);
2890
John McCallbd309292010-07-06 01:34:17 +00002891 // Create a flag indicating whether the cleanup needs to call
2892 // objc_exception_try_exit. This is true except when
2893 // - no catches match and we're branching through the cleanup
2894 // just to rethrow the exception, or
2895 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002896 // The setjmp-safety rule here is that we should always store to this
2897 // variable in a place that dominates the branch through the cleanup
2898 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002899 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002900 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002901
John McCall9916e3f2010-10-04 23:42:51 +00002902 // A slot containing the exception to rethrow. Only needed when we
2903 // have both a @catch and a @finally.
2904 llvm::Value *PropagatingExnVar = 0;
2905
John McCallbd309292010-07-06 01:34:17 +00002906 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002907 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002908 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002909 CallTryExitVar,
2910 ExceptionData,
2911 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002912
2913 // Enter a try block:
2914 // - Call objc_exception_try_enter to push ExceptionData on top of
2915 // the EH stack.
2916 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2917 ->setDoesNotThrow();
2918
2919 // - Call setjmp on the exception data buffer.
2920 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2921 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2922 llvm::Value *SetJmpBuffer =
2923 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2924 llvm::CallInst *SetJmpResult =
2925 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2926 SetJmpResult->setDoesNotThrow();
2927
2928 // If setjmp returned 0, enter the protected block; otherwise,
2929 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002930 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2931 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002932 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002933 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2934 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002935
John McCallbd309292010-07-06 01:34:17 +00002936 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002937 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002938 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002939 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002940 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002941
2942 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002943
John McCallbd309292010-07-06 01:34:17 +00002944 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002945 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002946
John McCall42227ed2010-07-31 23:20:56 +00002947 // Don't optimize loads of the in-scope locals across this point.
2948 Hazards.emitWriteHazard();
2949
John McCallbd309292010-07-06 01:34:17 +00002950 // For a @synchronized (or a @try with no catches), just branch
2951 // through the cleanup to the rethrow block.
2952 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2953 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002954 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002955 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002956
2957 // Otherwise, we have to match against the caught exceptions.
2958 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002959 // Retrieve the exception object. We may emit multiple blocks but
2960 // nothing can cross this so the value is already in SSA form.
2961 llvm::CallInst *Caught =
2962 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2963 ExceptionData, "caught");
2964 Caught->setDoesNotThrow();
2965
John McCallbd309292010-07-06 01:34:17 +00002966 // Push the exception to rethrow onto the EH value stack for the
2967 // benefit of any @throws in the handlers.
2968 CGF.ObjCEHValueStack.push_back(Caught);
2969
Douglas Gregor96c79492010-04-23 22:50:49 +00002970 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002971
John McCall2dd7d442010-08-04 05:59:32 +00002972 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00002973
John McCall2dd7d442010-08-04 05:59:32 +00002974 llvm::BasicBlock *CatchBlock = 0;
2975 llvm::BasicBlock *CatchHandler = 0;
2976 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00002977 // Save the currently-propagating exception before
2978 // objc_exception_try_enter clears the exception slot.
2979 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2980 "propagating_exception");
2981 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2982
John McCall2dd7d442010-08-04 05:59:32 +00002983 // Enter a new exception try block (in case a @catch block
2984 // throws an exception).
2985 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2986 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002987
John McCall2dd7d442010-08-04 05:59:32 +00002988 llvm::CallInst *SetJmpResult =
2989 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2990 "setjmp.result");
2991 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002992
John McCall2dd7d442010-08-04 05:59:32 +00002993 llvm::Value *Threw =
2994 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2995
2996 CatchBlock = CGF.createBasicBlock("catch");
2997 CatchHandler = CGF.createBasicBlock("catch_for_catch");
2998 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
2999
3000 CGF.EmitBlock(CatchBlock);
3001 }
3002
3003 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003004
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003005 // Handle catch list. As a special case we check if everything is
3006 // matched and avoid generating code for falling off the end if
3007 // so.
3008 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003009 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3010 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003011
Douglas Gregor46a572b2010-04-26 16:46:50 +00003012 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003013 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003014
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003015 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003016 if (!CatchParam) {
3017 AllMatched = true;
3018 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003019 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003020
John McCallbd309292010-07-06 01:34:17 +00003021 // catch(id e) always matches under this ABI, since only
3022 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003023 // FIXME: For the time being we also match id<X>; this should
3024 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003025 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003026 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003027 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003028
John McCallbd309292010-07-06 01:34:17 +00003029 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003030 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003031 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3032
Anders Carlsson9396a892008-09-11 09:15:33 +00003033 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003034 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003035 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003036
3037 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003038 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003039 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003040
Anders Carlsson9396a892008-09-11 09:15:33 +00003041 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003042
3043 // The scope of the catch variable ends right here.
3044 CatchVarCleanups.ForceCleanup();
3045
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003046 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003047 break;
3048 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003049
Steve Naroff7cae42b2009-07-10 23:34:53 +00003050 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003051 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003052
3053 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003054 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3055 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003056
3057 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003058 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003059
John McCallbd309292010-07-06 01:34:17 +00003060 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003061 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3062 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003063 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003064
John McCallbd309292010-07-06 01:34:17 +00003065 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3066 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003067
3068 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003069 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003070
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003071 // Emit the @catch block.
3072 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003073
3074 // Collect any cleanups for the catch variable. The scope lasts until
3075 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003076 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003077
John McCall1c9c3fd2010-10-15 04:57:14 +00003078 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003079 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003080
John McCallbd309292010-07-06 01:34:17 +00003081 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003082 llvm::Value *Tmp =
3083 CGF.Builder.CreateBitCast(Caught,
3084 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003085 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003086 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003087
Anders Carlsson9396a892008-09-11 09:15:33 +00003088 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003089
3090 // We're done with the catch variable.
3091 CatchVarCleanups.ForceCleanup();
3092
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003093 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003094
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003095 CGF.EmitBlock(NextCatchBlock);
3096 }
3097
John McCallbd309292010-07-06 01:34:17 +00003098 CGF.ObjCEHValueStack.pop_back();
3099
John McCall2dd7d442010-08-04 05:59:32 +00003100 // If nothing wanted anything to do with the caught exception,
3101 // kill the extract call.
3102 if (Caught->use_empty())
3103 Caught->eraseFromParent();
3104
3105 if (!AllMatched)
3106 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3107
3108 if (HasFinally) {
3109 // Emit the exception handler for the @catch blocks.
3110 CGF.EmitBlock(CatchHandler);
3111
3112 // In theory we might now need a write hazard, but actually it's
3113 // unnecessary because there's no local-accessing code between
3114 // the try's write hazard and here.
3115 //Hazards.emitWriteHazard();
3116
John McCall9916e3f2010-10-04 23:42:51 +00003117 // Extract the new exception and save it to the
3118 // propagating-exception slot.
3119 assert(PropagatingExnVar);
3120 llvm::CallInst *NewCaught =
3121 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3122 ExceptionData, "caught");
3123 NewCaught->setDoesNotThrow();
3124 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3125
John McCall2dd7d442010-08-04 05:59:32 +00003126 // Don't pop the catch handler; the throw already did.
3127 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003128 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003129 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003130 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003131
John McCall42227ed2010-07-31 23:20:56 +00003132 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003133 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003134
John McCallbd309292010-07-06 01:34:17 +00003135 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003136 CGF.Builder.restoreIP(TryFallthroughIP);
3137 if (CGF.HaveInsertPoint())
3138 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003139 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003140 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003141
John McCallbd309292010-07-06 01:34:17 +00003142 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003143 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003144 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003145 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003146 // If we have a propagating-exception variable, check it.
3147 llvm::Value *PropagatingExn;
3148 if (PropagatingExnVar) {
3149 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003150
John McCall9916e3f2010-10-04 23:42:51 +00003151 // Otherwise, just look in the buffer for the exception to throw.
3152 } else {
3153 llvm::CallInst *Caught =
3154 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3155 ExceptionData);
3156 Caught->setDoesNotThrow();
3157 PropagatingExn = Caught;
3158 }
3159
3160 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003161 ->setDoesNotThrow();
3162 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003163 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003164
John McCall42227ed2010-07-31 23:20:56 +00003165 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003166}
3167
3168void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003169 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003170 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003171
Anders Carlssone005aa12008-09-09 16:16:55 +00003172 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3173 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003175 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3176 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003177 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003178 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003179 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003180 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003181
John McCallbd309292010-07-06 01:34:17 +00003182 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3183 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003184 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003185
3186 // Clear the insertion point to indicate we are in unreachable code.
3187 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003188}
3189
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003190/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003191/// object: objc_read_weak (id *src)
3192///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003193llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003194 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003195 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003196 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3197 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3198 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003199 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003200 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003201 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003202 return read_weak;
3203}
3204
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003205/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3206/// objc_assign_weak (id src, id *dst)
3207///
3208void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003209 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003210 const llvm::Type * SrcTy = src->getType();
3211 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003212 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003213 assert(Size <= 8 && "does not support size > 8");
3214 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003215 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003216 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3217 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003218 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3219 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003220 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003221 src, dst, "weakassign");
3222 return;
3223}
3224
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003225/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3226/// objc_assign_global (id src, id *dst)
3227///
3228void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003229 llvm::Value *src, llvm::Value *dst,
3230 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003231 const llvm::Type * SrcTy = src->getType();
3232 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003233 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003234 assert(Size <= 8 && "does not support size > 8");
3235 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003236 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003237 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3238 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003239 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3240 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003241 if (!threadlocal)
3242 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3243 src, dst, "globalassign");
3244 else
3245 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3246 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003247 return;
3248}
3249
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003250/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003251/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003252///
3253void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003254 llvm::Value *src, llvm::Value *dst,
3255 llvm::Value *ivarOffset) {
3256 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003257 const llvm::Type * SrcTy = src->getType();
3258 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003259 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003260 assert(Size <= 8 && "does not support size > 8");
3261 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003262 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003263 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3264 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003265 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3266 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003267 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3268 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003269 return;
3270}
3271
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003272/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3273/// objc_assign_strongCast (id src, id *dst)
3274///
3275void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003276 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003277 const llvm::Type * SrcTy = src->getType();
3278 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003279 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003280 assert(Size <= 8 && "does not support size > 8");
3281 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003282 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003283 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3284 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003285 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3286 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003287 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003288 src, dst, "weakassign");
3289 return;
3290}
3291
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003292void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003293 llvm::Value *DestPtr,
3294 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003295 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003296 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3297 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003298 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003299 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003300 return;
3301}
3302
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003303/// EmitObjCValueForIvar - Code Gen for ivar reference.
3304///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003305LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3306 QualType ObjectTy,
3307 llvm::Value *BaseValue,
3308 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003309 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003310 const ObjCInterfaceDecl *ID =
3311 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003312 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3313 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003314}
3315
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003316llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003317 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003318 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003319 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003320 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003321 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3322 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003323}
3324
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003325/* *** Private Interface *** */
3326
3327/// EmitImageInfo - Emit the image info marker used to encode some module
3328/// level information.
3329///
3330/// See: <rdr://4810609&4810587&4810587>
3331/// struct IMAGE_INFO {
3332/// unsigned version;
3333/// unsigned flags;
3334/// };
3335enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003336 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003337 eImageInfo_GarbageCollected = (1 << 1),
3338 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003339 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3340
Daniel Dunbar5e639272010-04-25 20:39:01 +00003341 // A flag indicating that the module has no instances of a @synthesize of a
3342 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003343 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003344};
3345
Daniel Dunbar5e639272010-04-25 20:39:01 +00003346void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003347 unsigned version = 0; // Version is unused?
3348 unsigned flags = 0;
3349
3350 // FIXME: Fix and continue?
3351 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3352 flags |= eImageInfo_GarbageCollected;
3353 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3354 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003355
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003356 // We never allow @synthesize of a superclass property.
3357 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003358
Chris Lattner5e016ae2010-06-27 07:15:29 +00003359 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3360
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003361 // Emitted as int[2];
3362 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003363 llvm::ConstantInt::get(Int32Ty, version),
3364 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003365 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003366 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003367
3368 const char *Section;
3369 if (ObjCABI == 1)
3370 Section = "__OBJC, __image_info,regular";
3371 else
3372 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003373 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003374 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003375 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003376 Section,
3377 0,
3378 true);
3379 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003380}
3381
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003382
3383// struct objc_module {
3384// unsigned long version;
3385// unsigned long size;
3386// const char *name;
3387// Symtab symtab;
3388// };
3389
3390// FIXME: Get from somewhere
3391static const int ModuleVersion = 7;
3392
3393void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003394 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003395
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003396 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003397 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3398 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003399 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003400 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003401 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003402 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003403 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003404 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003405 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003406}
3407
3408llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003409 unsigned NumClasses = DefinedClasses.size();
3410 unsigned NumCategories = DefinedCategories.size();
3411
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003412 // Return null if no symbols were defined.
3413 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003414 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003415
3416 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003417 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003418 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003419 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3420 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003421
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003422 // The runtime expects exactly the list of defined classes followed
3423 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003424 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003425 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003426 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003427 ObjCTypes.Int8PtrTy);
3428 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003429 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003430 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003431 ObjCTypes.Int8PtrTy);
3432
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003433 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003434 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003435 NumClasses + NumCategories),
3436 Symbols);
3437
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003438 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003439
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003440 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003441 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3442 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003443 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003444 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003445}
3446
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003447llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003448 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003449 LazySymbols.insert(ID->getIdentifier());
3450
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003451 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003452
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003453 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003454 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003455 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003456 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003457 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003458 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3459 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003460 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003461 }
3462
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003463 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003464}
3465
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003466llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3467 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003468 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003469
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003470 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003471 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003472 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003473 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003474 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003475 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3476 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003477 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003478 }
3479
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003480 if (lvalue)
3481 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003482 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003483}
3484
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003485llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003486 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003487
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003488 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003490 llvm::ConstantArray::get(VMContext,
3491 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003492 ((ObjCABI == 2) ?
3493 "__TEXT,__objc_classname,cstring_literals" :
3494 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003495 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003496
Owen Anderson170229f2009-07-14 23:10:40 +00003497 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003498}
3499
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003500llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3501 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3502 I = MethodDefinitions.find(MD);
3503 if (I != MethodDefinitions.end())
3504 return I->second;
3505
3506 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3507 // MD isn't emitted yet because it comes from PCH.
3508 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3509 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3510 return MethodDefinitions[MD];
3511 }
3512
3513 return NULL;
3514}
3515
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003516/// GetIvarLayoutName - Returns a unique constant for the given
3517/// ivar layout bitmap.
3518llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003519 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003520 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003521}
3522
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003523void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003524 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003525 bool ForStrongLayout,
3526 bool &HasUnion) {
3527 const RecordDecl *RD = RT->getDecl();
3528 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003529 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003530 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003531 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003532 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003533
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003534 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3535 ForStrongLayout, HasUnion);
3536}
3537
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003538void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003539 const llvm::StructLayout *Layout,
3540 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003541 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003542 unsigned int BytePos, bool ForStrongLayout,
3543 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003544 bool IsUnion = (RD && RD->isUnion());
3545 uint64_t MaxUnionIvarSize = 0;
3546 uint64_t MaxSkippedUnionIvarSize = 0;
3547 FieldDecl *MaxField = 0;
3548 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003549 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003550 uint64_t MaxFieldOffset = 0;
3551 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003552 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003553
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003554 if (RecFields.empty())
3555 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003556 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3557 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3558
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003559 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003560 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003561 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003562 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003563 // Note that 'i' here is actually the field index inside RD of Field,
3564 // although this dependency is hidden.
3565 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckc5ca8762011-04-14 00:43:09 +00003566 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003567 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003568 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003569
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003570 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003571 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003572 LastFieldBitfieldOrUnnamed = Field;
3573 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003574 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003575 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003576
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003577 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003578 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003579 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003580 if (FQT->isUnionType())
3581 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003582
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003583 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003584 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003585 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003586 continue;
3587 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003588
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003589 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003590 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003591 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003592 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003593 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003594 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003595 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3596 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 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003599 FQT = CArray->getElementType();
3600 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003601
3602 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003603 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003604 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003605 int OldIndex = IvarsInfo.size() - 1;
3606 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003607
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003608 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003609 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003610 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003611
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003612 // Replicate layout information for each array element. Note that
3613 // one element is already done.
3614 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003615 for (int FirstIndex = IvarsInfo.size() - 1,
3616 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003617 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003618 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3619 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3620 IvarsInfo[i].ivar_size));
3621 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3622 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3623 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003624 }
3625 continue;
3626 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003627 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003628 // At this point, we are done with Record/Union and array there of.
3629 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003630 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003631
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003632 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003633 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3634 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003635 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003636 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003637 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003638 MaxUnionIvarSize = UnionIvarSize;
3639 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003640 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003641 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003642 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003643 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003644 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003646 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003647 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3648 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003649 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003650 // FIXME: Why the asymmetry? We divide by word size in bits on other
3651 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003652 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003653 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003654 MaxSkippedUnionIvarSize = UnionIvarSize;
3655 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003656 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003657 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003658 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003659 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003660 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003661 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003662 }
3663 }
3664 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003665
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003666 if (LastFieldBitfieldOrUnnamed) {
3667 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3668 // Last field was a bitfield. Must update skip info.
3669 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3670 uint64_t BitFieldSize =
3671 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3672 GC_IVAR skivar;
3673 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3674 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3675 + ((BitFieldSize % ByteSizeInBits) != 0);
3676 SkipIvars.push_back(skivar);
3677 } else {
3678 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3679 // Last field was unnamed. Must update skip info.
3680 unsigned FieldSize
3681 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3682 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3683 FieldSize / ByteSizeInBits));
3684 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003685 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003686
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003687 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003688 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003689 MaxUnionIvarSize));
3690 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003691 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003692 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003693}
3694
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003695/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3696/// the computations and returning the layout bitmap (for ivar or blocks) in
3697/// the given argument BitMap string container. Routine reads
3698/// two containers, IvarsInfo and SkipIvars which are assumed to be
3699/// filled already by the caller.
3700llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003701 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003702 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003703
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003704 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003705 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003707 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003708 if (IvarsInfo[0].ivar_bytepos == 0) {
3709 WordsToSkip = 0;
3710 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003711 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003712 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3713 WordsToScan = IvarsInfo[0].ivar_size;
3714 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003715 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003716 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003717 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003718 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003719 // consecutive 'scanned' object pointers.
3720 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003721 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003722 // Skip over 'gc'able object pointer which lay over each other.
3723 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3724 continue;
3725 // Must skip over 1 or more words. We save current skip/scan values
3726 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003727 SKIP_SCAN SkScan;
3728 SkScan.skip = WordsToSkip;
3729 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003730 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003731
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003732 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003733 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3734 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003735 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003736 WordsToSkip = 0;
3737 WordsToScan = IvarsInfo[i].ivar_size;
3738 }
3739 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003740 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003741 SKIP_SCAN SkScan;
3742 SkScan.skip = WordsToSkip;
3743 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003744 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003745 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003746
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003747 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003748 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003749 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003750 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003751 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003753 IvarsInfo[LastIndex].ivar_bytepos +
3754 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003755 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003756 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003757 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003758 SKIP_SCAN SkScan;
3759 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3760 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003761 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003762 }
3763 }
3764 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3765 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003766 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003767 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003768 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3769 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3770 // 0xM0 followed by 0x0N detected.
3771 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3772 for (int j = i+1; j < SkipScan; j++)
3773 SkipScanIvars[j] = SkipScanIvars[j+1];
3774 --SkipScan;
3775 }
3776 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003777
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003778 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003779 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003780 unsigned char byte;
3781 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3782 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3783 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3784 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003785
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003786 // first skip big.
3787 for (unsigned int ix = 0; ix < skip_big; ix++)
3788 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003789
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003790 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003791 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003792 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003793 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003794 byte |= 0xf;
3795 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003796 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003797 byte |= scan_small;
3798 scan_small = 0;
3799 }
3800 BitMap += byte;
3801 }
3802 // next scan big
3803 for (unsigned int ix = 0; ix < scan_big; ix++)
3804 BitMap += (unsigned char)(0x0f);
3805 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003806 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003807 byte = scan_small;
3808 BitMap += byte;
3809 }
3810 }
3811 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003812 unsigned char zero = 0;
3813 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003814
3815 llvm::GlobalVariable * Entry =
3816 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3817 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003818 ((ObjCABI == 2) ?
3819 "__TEXT,__objc_classname,cstring_literals" :
3820 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003821 1, true);
3822 return getConstantGEP(VMContext, Entry, 0, 0);
3823}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003824
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003825/// BuildIvarLayout - Builds ivar layout bitmap for the class
3826/// implementation for the __strong or __weak case.
3827/// The layout map displays which words in ivar list must be skipped
3828/// and which must be scanned by GC (see below). String is built of bytes.
3829/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3830/// of words to skip and right nibble is count of words to scan. So, each
3831/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3832/// represented by a 0x00 byte which also ends the string.
3833/// 1. when ForStrongLayout is true, following ivars are scanned:
3834/// - id, Class
3835/// - object *
3836/// - __strong anything
3837///
3838/// 2. When ForStrongLayout is false, following ivars are scanned:
3839/// - __weak anything
3840///
3841llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3842 const ObjCImplementationDecl *OMD,
3843 bool ForStrongLayout) {
3844 bool hasUnion = false;
3845
3846 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3847 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3848 return llvm::Constant::getNullValue(PtrTy);
3849
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003850 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003851 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003852 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003853
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003854 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003855 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3856 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3857
3858 if (RecFields.empty())
3859 return llvm::Constant::getNullValue(PtrTy);
3860
3861 SkipIvars.clear();
3862 IvarsInfo.clear();
3863
3864 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3865 if (IvarsInfo.empty())
3866 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003867 // Sort on byte position in case we encounterred a union nested in
3868 // the ivar list.
3869 if (hasUnion && !IvarsInfo.empty())
3870 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3871 if (hasUnion && !SkipIvars.empty())
3872 std::sort(SkipIvars.begin(), SkipIvars.end());
3873
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003874 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003875 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003876
3877 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003878 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003879 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003880 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003881 const unsigned char *s = (unsigned char*)BitMap.c_str();
3882 for (unsigned i = 0; i < BitMap.size(); i++)
3883 if (!(s[i] & 0xf0))
3884 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3885 else
3886 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3887 printf("\n");
3888 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003889 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003890}
3891
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003892llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003893 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3894
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003895 // FIXME: Avoid std::string copying.
3896 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003897 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003898 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003899 ((ObjCABI == 2) ?
3900 "__TEXT,__objc_methname,cstring_literals" :
3901 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003902 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003903
Owen Anderson170229f2009-07-14 23:10:40 +00003904 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003905}
3906
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003907// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003908llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003909 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3910}
3911
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003912llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003913 std::string TypeStr;
3914 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3915
3916 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003917
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003918 if (!Entry)
3919 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003920 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003921 ((ObjCABI == 2) ?
3922 "__TEXT,__objc_methtype,cstring_literals" :
3923 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003924 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003925
Owen Anderson170229f2009-07-14 23:10:40 +00003926 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003927}
3928
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003929llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003930 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003931 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3932 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003933
3934 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3935
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003936 if (!Entry)
3937 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003938 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003939 ((ObjCABI == 2) ?
3940 "__TEXT,__objc_methtype,cstring_literals" :
3941 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003942 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003943
Owen Anderson170229f2009-07-14 23:10:40 +00003944 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003945}
3946
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003947// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003948llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003949 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003950
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003951 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003952 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003953 llvm::ConstantArray::get(VMContext,
3954 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003955 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003956 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003957
Owen Anderson170229f2009-07-14 23:10:40 +00003958 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003959}
3960
3961// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003962// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003963llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003964CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3965 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003966 std::string TypeStr;
3967 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003968 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3969}
3970
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003971void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003972 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003973 llvm::SmallVectorImpl<char> &Name) {
3974 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003975 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003976 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3977 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003978 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003979 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003980 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003981 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003982}
3983
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003984void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003985 EmitModuleInfo();
3986
Daniel Dunbarc475d422008-10-29 22:36:39 +00003987 // Emit the dummy bodies for any protocols which were referenced but
3988 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003989 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003990 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3991 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003992 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003993
Daniel Dunbarc475d422008-10-29 22:36:39 +00003994 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00003995 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003996 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00003997 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003998 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00003999 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004000 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004001 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004002 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004003 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004004 }
4005
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004006 // Add assembler directives to add lazy undefined symbol references
4007 // for classes which are referenced but not defined. This is
4008 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004009 //
4010 // FIXME: It would be nice if we had an LLVM construct for this.
4011 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4012 llvm::SmallString<256> Asm;
4013 Asm += CGM.getModule().getModuleInlineAsm();
4014 if (!Asm.empty() && Asm.back() != '\n')
4015 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004016
Daniel Dunbard027a922009-09-07 00:20:42 +00004017 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004018 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4019 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004020 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4021 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004022 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004023 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004024 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004025 }
4026
4027 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4028 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4029 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4030 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004031
Daniel Dunbard027a922009-09-07 00:20:42 +00004032 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004033 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004034}
4035
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004036CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004037 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004038 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004039 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004040 ObjCABI = 2;
4041}
4042
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004043/* *** */
4044
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004045ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004046 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004047 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4048 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004049
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004050 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004051 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004052 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004053 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004054 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004055
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004056 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004057 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004058 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004059
Mike Stump18bb9282009-05-16 07:57:57 +00004060 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4061 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004062 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004063 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004064
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004065 // I'm not sure I like this. The implicit coordination is a bit
4066 // gross. We should solve this in a reasonable fashion because this
4067 // is a pretty common task (match some runtime data structure with
4068 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004069
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004070 // FIXME: This is leaked.
4071 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004072
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004073 // struct _objc_super {
4074 // id self;
4075 // Class cls;
4076 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004077 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004078 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004079 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004080 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004081 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004082 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004083 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004084 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004085 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004086
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004087 SuperCTy = Ctx.getTagDeclType(RD);
4088 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004089
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004090 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004091 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4092
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004093 // struct _prop_t {
4094 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004096 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004097 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004098 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004099 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004100
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004101 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004102 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004103 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004104 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004105 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004106 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004107 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004108 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004109 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004110 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004111 PropertyListTy);
4112 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004113 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004114
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004115 // struct _objc_method {
4116 // SEL _cmd;
4117 // char *method_type;
4118 // char *_imp;
4119 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004120 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004121 Int8PtrTy,
4122 Int8PtrTy,
4123 NULL);
4124 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004126 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004127 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004128 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004129 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004130}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004131
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004132ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004133 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004134 // struct _objc_method_description {
4135 // SEL name;
4136 // char *types;
4137 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004139 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004140 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004141 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004142 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004143 MethodDescriptionTy);
4144
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004145 // struct _objc_method_description_list {
4146 // int count;
4147 // struct _objc_method_description[1];
4148 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004149 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004150 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004151 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004152 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004153 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004154 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004156 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004157 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004158 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004159
Daniel Dunbarb036db82008-08-13 03:21:16 +00004160 // Protocol description structures
4161
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004162 // struct _objc_protocol_extension {
4163 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4164 // struct _objc_method_description_list *optional_instance_methods;
4165 // struct _objc_method_description_list *optional_class_methods;
4166 // struct _objc_property_list *instance_properties;
4167 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004168 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004169 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004170 MethodDescriptionListPtrTy,
4171 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004172 PropertyListPtrTy,
4173 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004174 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004175 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004176
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004177 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004178 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004179
Daniel Dunbarc475d422008-10-29 22:36:39 +00004180 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004181
Owen Andersonc36edfe2009-08-13 23:27:53 +00004182 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4183 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004184
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004185 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004186 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004187 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004188 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004189 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004190 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004191 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4192
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004193 // struct _objc_protocol {
4194 // struct _objc_protocol_extension *isa;
4195 // char *protocol_name;
4196 // struct _objc_protocol **_objc_protocol_list;
4197 // struct _objc_method_description_list *instance_methods;
4198 // struct _objc_method_description_list *class_methods;
4199 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004200 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004201 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004202 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004203 MethodDescriptionListPtrTy,
4204 MethodDescriptionListPtrTy,
4205 NULL);
4206 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4207
4208 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004209 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004210 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004211 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004212 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004213
4214 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004215 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004216 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004217
4218 // Class description structures
4219
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004220 // struct _objc_ivar {
4221 // char *ivar_name;
4222 // char *ivar_type;
4223 // int ivar_offset;
4224 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004225 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004226 Int8PtrTy,
4227 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004228 NULL);
4229 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4230
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004231 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004232 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004233 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004234 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004235
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004236 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004237 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004238 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004239 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004240
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004241 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004242 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004243 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004244 Int8PtrTy,
4245 PropertyListPtrTy,
4246 NULL);
4247 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004248 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004249
Owen Andersonc36edfe2009-08-13 23:27:53 +00004250 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004251
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004252 // struct _objc_class {
4253 // Class isa;
4254 // Class super_class;
4255 // char *name;
4256 // long version;
4257 // long info;
4258 // long instance_size;
4259 // struct _objc_ivar_list *ivars;
4260 // struct _objc_method_list *methods;
4261 // struct _objc_cache *cache;
4262 // struct _objc_protocol_list *protocols;
4263 // char *ivar_layout;
4264 // struct _objc_class_ext *ext;
4265 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004266 T = llvm::StructType::get(VMContext,
4267 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004268 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004269 Int8PtrTy,
4270 LongTy,
4271 LongTy,
4272 LongTy,
4273 IvarListPtrTy,
4274 MethodListPtrTy,
4275 CachePtrTy,
4276 ProtocolListPtrTy,
4277 Int8PtrTy,
4278 ClassExtensionPtrTy,
4279 NULL);
4280 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004281
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004282 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4283 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004284 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004285
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004286 // struct _objc_category {
4287 // char *category_name;
4288 // char *class_name;
4289 // struct _objc_method_list *instance_method;
4290 // struct _objc_method_list *class_method;
4291 // uint32_t size; // sizeof(struct _objc_category)
4292 // struct _objc_property_list *instance_properties;// category's @property
4293 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004294 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004295 Int8PtrTy,
4296 MethodListPtrTy,
4297 MethodListPtrTy,
4298 ProtocolListPtrTy,
4299 IntTy,
4300 PropertyListPtrTy,
4301 NULL);
4302 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4303
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004304 // Global metadata structures
4305
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004306 // struct _objc_symtab {
4307 // long sel_ref_cnt;
4308 // SEL *refs;
4309 // short cls_def_cnt;
4310 // short cat_def_cnt;
4311 // char *defs[cls_def_cnt + cat_def_cnt];
4312 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004313 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004314 SelectorPtrTy,
4315 ShortTy,
4316 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004317 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004318 NULL);
4319 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004320 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004321
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004322 // struct _objc_module {
4323 // long version;
4324 // long size; // sizeof(struct _objc_module)
4325 // char *name;
4326 // struct _objc_symtab* symtab;
4327 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004328 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004329 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004330 LongTy,
4331 Int8PtrTy,
4332 SymtabPtrTy,
4333 NULL);
4334 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004335
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004336
Mike Stump18bb9282009-05-16 07:57:57 +00004337 // FIXME: This is the size of the setjmp buffer and should be target
4338 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004339 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004340
Anders Carlsson9ff22482008-09-09 10:10:21 +00004341 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004342 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004343 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004344
4345 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004346 llvm::StructType::get(VMContext,
4347 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4348 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004349 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004350 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004351 ExceptionDataTy);
4352
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004353}
4354
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004355ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004356 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004357 // struct _method_list_t {
4358 // uint32_t entsize; // sizeof(struct _objc_method)
4359 // uint32_t method_count;
4360 // struct _objc_method method_list[method_count];
4361 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004362 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004363 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004364 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004365 NULL);
4366 CGM.getModule().addTypeName("struct.__method_list_t",
4367 MethodListnfABITy);
4368 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004369 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004370
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004371 // struct _protocol_t {
4372 // id isa; // NULL
4373 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004374 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004375 // const struct method_list_t * const instance_methods;
4376 // const struct method_list_t * const class_methods;
4377 // const struct method_list_t *optionalInstanceMethods;
4378 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004379 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004380 // const uint32_t size; // sizeof(struct _protocol_t)
4381 // const uint32_t flags; // = 0
4382 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004383
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004384 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004385 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004386
Owen Anderson758428f2009-08-05 23:18:46 +00004387 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004388 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004389 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004390 ProtocolListTyHolder),
4391 MethodListnfABIPtrTy,
4392 MethodListnfABIPtrTy,
4393 MethodListnfABIPtrTy,
4394 MethodListnfABIPtrTy,
4395 PropertyListPtrTy,
4396 IntTy,
4397 IntTy,
4398 NULL);
4399 CGM.getModule().addTypeName("struct._protocol_t",
4400 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004401
4402 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004403 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004404
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004405 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004406 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004407 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004408 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004409 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004410 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004411 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004412 NULL);
4413 CGM.getModule().addTypeName("struct._objc_protocol_list",
4414 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004415 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004416 ProtocolListnfABITy);
4417
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004418 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004419 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004420
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004421 // struct _ivar_t {
4422 // unsigned long int *offset; // pointer to ivar offset location
4423 // char *name;
4424 // char *type;
4425 // uint32_t alignment;
4426 // uint32_t size;
4427 // }
Mike Stump11289f42009-09-09 15:08:12 +00004428 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004429 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004430 Int8PtrTy,
4431 Int8PtrTy,
4432 IntTy,
4433 IntTy,
4434 NULL);
4435 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004436
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004437 // struct _ivar_list_t {
4438 // uint32 entsize; // sizeof(struct _ivar_t)
4439 // uint32 count;
4440 // struct _iver_t list[count];
4441 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004442 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004443 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004444 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004445 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004446 NULL);
4447 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448
Owen Anderson9793f0e2009-07-29 22:16:19 +00004449 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004450
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004451 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004452 // uint32_t const flags;
4453 // uint32_t const instanceStart;
4454 // uint32_t const instanceSize;
4455 // uint32_t const reserved; // only when building for 64bit targets
4456 // const uint8_t * const ivarLayout;
4457 // const char *const name;
4458 // const struct _method_list_t * const baseMethods;
4459 // const struct _objc_protocol_list *const baseProtocols;
4460 // const struct _ivar_list_t *const ivars;
4461 // const uint8_t * const weakIvarLayout;
4462 // const struct _prop_list_t * const properties;
4463 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004464
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004465 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004466 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004467 IntTy,
4468 IntTy,
4469 Int8PtrTy,
4470 Int8PtrTy,
4471 MethodListnfABIPtrTy,
4472 ProtocolListnfABIPtrTy,
4473 IvarListnfABIPtrTy,
4474 Int8PtrTy,
4475 PropertyListPtrTy,
4476 NULL);
4477 CGM.getModule().addTypeName("struct._class_ro_t",
4478 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004479
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004480 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
John McCall9dc0db22011-05-15 01:53:33 +00004481 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
4482 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4483 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004484
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004485 // struct _class_t {
4486 // struct _class_t *isa;
4487 // struct _class_t * const superclass;
4488 // void *cache;
4489 // IMP *vtable;
4490 // struct class_ro_t *ro;
4491 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004492
Owen Andersonc36edfe2009-08-13 23:27:53 +00004493 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004494 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004495 llvm::StructType::get(VMContext,
4496 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004497 llvm::PointerType::getUnqual(ClassTyHolder),
4498 CachePtrTy,
4499 llvm::PointerType::getUnqual(ImpnfABITy),
4500 llvm::PointerType::getUnqual(ClassRonfABITy),
4501 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004502 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4503
4504 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004505 ClassnfABITy);
4506
Fariborz Jahanian71394042009-01-23 23:53:38 +00004507 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004508 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004509
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004510 // struct _category_t {
4511 // const char * const name;
4512 // struct _class_t *const cls;
4513 // const struct _method_list_t * const instance_methods;
4514 // const struct _method_list_t * const class_methods;
4515 // const struct _protocol_list_t * const protocols;
4516 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004517 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004518 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004519 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004520 MethodListnfABIPtrTy,
4521 MethodListnfABIPtrTy,
4522 ProtocolListnfABIPtrTy,
4523 PropertyListPtrTy,
4524 NULL);
4525 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004526
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004527 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004528 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4529 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004530
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004531 // MessageRefTy - LLVM for:
4532 // struct _message_ref_t {
4533 // IMP messenger;
4534 // SEL name;
4535 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004536
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004537 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004538 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004539 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004540 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004541 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004542 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004543 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004544 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004545 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004546 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004547
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004548 MessageRefCTy = Ctx.getTagDeclType(RD);
4549 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4550 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004551
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004552 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004553 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004554
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004555 // SuperMessageRefTy - LLVM for:
4556 // struct _super_message_ref_t {
4557 // SUPER_IMP messenger;
4558 // SEL name;
4559 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004560 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004561 SelectorPtrTy,
4562 NULL);
4563 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004564
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004565 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004566 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4567
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004568
4569 // struct objc_typeinfo {
4570 // const void** vtable; // objc_ehtype_vtable + 2
4571 // const char* name; // c++ typeinfo string
4572 // Class cls;
4573 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004574 EHTypeTy = llvm::StructType::get(VMContext,
4575 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004576 Int8PtrTy,
4577 ClassnfABIPtrTy,
4578 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004579 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004580 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004581}
4582
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004584 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004585
Fariborz Jahanian71394042009-01-23 23:53:38 +00004586 return NULL;
4587}
4588
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004589void CGObjCNonFragileABIMac::AddModuleClassList(const
4590 std::vector<llvm::GlobalValue*>
4591 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004592 const char *SymbolName,
4593 const char *SectionName) {
4594 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004595
Daniel Dunbar19573e72009-05-15 21:48:48 +00004596 if (!NumClasses)
4597 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004598
Daniel Dunbar19573e72009-05-15 21:48:48 +00004599 std::vector<llvm::Constant*> Symbols(NumClasses);
4600 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004601 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004602 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004604 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004605 NumClasses),
4606 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004607
Daniel Dunbar19573e72009-05-15 21:48:48 +00004608 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004609 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004610 llvm::GlobalValue::InternalLinkage,
4611 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004612 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004613 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004614 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004615 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004616}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004617
Fariborz Jahanian71394042009-01-23 23:53:38 +00004618void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4619 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004620
Daniel Dunbar19573e72009-05-15 21:48:48 +00004621 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004622 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004623 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004624 "\01L_OBJC_LABEL_CLASS_$",
4625 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004626
Fariborz Jahanian67260552009-11-17 21:37:35 +00004627 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4628 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4629 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4630 continue;
4631 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004632 }
4633
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004634 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4635 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4636 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4637 continue;
4638 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4639 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004640
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004641 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004642 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4643 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004644
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004645 // Build list of all implemented category addresses in array
4646 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004647 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004648 "\01L_OBJC_LABEL_CATEGORY_$",
4649 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004651 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4652 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Daniel Dunbar5e639272010-04-25 20:39:01 +00004654 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004655}
4656
John McCall9e8bb002011-05-14 03:10:52 +00004657/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4658/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004659/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004660/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004661bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4662 // At various points we've experimented with using vtable-based
4663 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004664 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4665 default:
John McCall9e8bb002011-05-14 03:10:52 +00004666 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004667 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004668 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004669 case CodeGenOptions::NonLegacy:
4670 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004671 case CodeGenOptions::Mixed:
4672 break;
4673 }
4674
4675 // If so, see whether this selector is in the white-list of things which must
4676 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004677 if (VTableDispatchMethods.empty()) {
4678 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4679 VTableDispatchMethods.insert(GetNullarySelector("class"));
4680 VTableDispatchMethods.insert(GetNullarySelector("self"));
4681 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4682 VTableDispatchMethods.insert(GetNullarySelector("length"));
4683 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684
John McCall9e8bb002011-05-14 03:10:52 +00004685 // These are vtable-based if GC is disabled.
4686 // Optimistically use vtable dispatch for hybrid compiles.
4687 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
4688 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4689 VTableDispatchMethods.insert(GetNullarySelector("release"));
4690 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4691 }
4692
4693 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4694 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4695 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4696 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4697 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4698 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4699 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4700
4701 // These are vtable-based if GC is enabled.
4702 // Optimistically use vtable dispatch for hybrid compiles.
4703 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
4704 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4705 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4706
4707 // "countByEnumeratingWithState:objects:count"
4708 IdentifierInfo *KeyIdents[] = {
4709 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4710 &CGM.getContext().Idents.get("objects"),
4711 &CGM.getContext().Idents.get("count")
4712 };
4713 VTableDispatchMethods.insert(
4714 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4715 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004716 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004717
John McCall9e8bb002011-05-14 03:10:52 +00004718 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004719}
4720
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004721// Metadata flags
4722enum MetaDataDlags {
4723 CLS = 0x0,
4724 CLS_META = 0x1,
4725 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004726 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004727 CLS_EXCEPTION = 0x20
4728};
4729/// BuildClassRoTInitializer - generate meta-data for:
4730/// struct _class_ro_t {
4731/// uint32_t const flags;
4732/// uint32_t const instanceStart;
4733/// uint32_t const instanceSize;
4734/// uint32_t const reserved; // only when building for 64bit targets
4735/// const uint8_t * const ivarLayout;
4736/// const char *const name;
4737/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004738/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004739/// const struct _ivar_list_t *const ivars;
4740/// const uint8_t * const weakIvarLayout;
4741/// const struct _prop_list_t * const properties;
4742/// }
4743///
4744llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004745 unsigned flags,
4746 unsigned InstanceStart,
4747 unsigned InstanceSize,
4748 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004749 std::string ClassName = ID->getNameAsString();
4750 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004751 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4752 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4753 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004754 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004755 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4756 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004757 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004758 // const struct _method_list_t * const baseMethods;
4759 std::vector<llvm::Constant*> Methods;
4760 std::string MethodListName("\01l_OBJC_$_");
4761 if (flags & CLS_META) {
4762 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004763 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004764 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004765 // Class methods should always be defined.
4766 Methods.push_back(GetMethodConstant(*i));
4767 }
4768 } else {
4769 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004770 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004771 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004772 // Instance methods should always be defined.
4773 Methods.push_back(GetMethodConstant(*i));
4774 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004775 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004776 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004777 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004778
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004779 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4780 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004781
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004782 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4783 if (llvm::Constant *C = GetMethodConstant(MD))
4784 Methods.push_back(C);
4785 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4786 if (llvm::Constant *C = GetMethodConstant(MD))
4787 Methods.push_back(C);
4788 }
4789 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004790 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004791 Values[ 5] = EmitMethodList(MethodListName,
4792 "__DATA, __objc_const", Methods);
4793
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004794 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4795 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004796 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004797 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004798 OID->all_referenced_protocol_begin(),
4799 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004800
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004801 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004802 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004803 else
4804 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004805 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4806 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004807 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004808 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004809 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004810 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4811 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004812 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004813 Values);
4814 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004815 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4816 llvm::GlobalValue::InternalLinkage,
4817 Init,
4818 (flags & CLS_META) ?
4819 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4820 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004821 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004822 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004823 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004824 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004825
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004826}
4827
4828/// BuildClassMetaData - This routine defines that to-level meta-data
4829/// for the given ClassName for:
4830/// struct _class_t {
4831/// struct _class_t *isa;
4832/// struct _class_t * const superclass;
4833/// void *cache;
4834/// IMP *vtable;
4835/// struct class_ro_t *ro;
4836/// }
4837///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004838llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004839 std::string &ClassName,
4840 llvm::Constant *IsAGV,
4841 llvm::Constant *SuperClassGV,
4842 llvm::Constant *ClassRoGV,
4843 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004844 std::vector<llvm::Constant*> Values(5);
4845 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004846 Values[1] = SuperClassGV;
4847 if (!Values[1])
4848 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004849 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4850 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4851 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004852 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004853 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004854 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4855 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004856 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004857 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004858 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004859 if (HiddenVisibility)
4860 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004861 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004862}
4863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004864bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004865CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004866 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004867}
4868
Daniel Dunbar961202372009-05-03 12:57:56 +00004869void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004870 uint32_t &InstanceStart,
4871 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004872 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004873 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004874
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004875 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004876 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004877
4878 // If there are no fields, the start is the same as the end.
4879 if (!RL.getFieldCount())
4880 InstanceStart = InstanceSize;
4881 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004882 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004883}
4884
Fariborz Jahanian71394042009-01-23 23:53:38 +00004885void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4886 std::string ClassName = ID->getNameAsString();
4887 if (!ObjCEmptyCacheVar) {
4888 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004889 CGM.getModule(),
4890 ObjCTypes.CacheTy,
4891 false,
4892 llvm::GlobalValue::ExternalLinkage,
4893 0,
4894 "_objc_empty_cache");
4895
Fariborz Jahanian71394042009-01-23 23:53:38 +00004896 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004897 CGM.getModule(),
4898 ObjCTypes.ImpnfABITy,
4899 false,
4900 llvm::GlobalValue::ExternalLinkage,
4901 0,
4902 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004903 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004904 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004905 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004906 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004907 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004908 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004909 uint32_t InstanceSize = InstanceStart;
4910 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004911 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4912 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004913
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004914 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004915
4916 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004917 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004918 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004919 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004920 if (ID->getNumIvarInitializers())
4921 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004922 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004923 // class is root
4924 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004925 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004926 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004927 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004928 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004929 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4930 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4931 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004932 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004933 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004934 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004935 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004936 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004937 ObjCMetaClassName +
4938 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004939 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004940 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004941 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004942 }
4943 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4944 InstanceStart,
4945 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004946 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004947 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004948 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4949 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004950 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004951
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004952 // Metadata for the class
4953 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004954 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004955 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004956 if (ID->getNumIvarInitializers())
4957 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004958
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004959 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004960 flags |= CLS_EXCEPTION;
4961
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004962 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004963 flags |= CLS_ROOT;
4964 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004965 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004966 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004967 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004968 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004969 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004970 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004971 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004972 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004973 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004974 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004975 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004977 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004978
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004979 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004980 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004981 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4982 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004983 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004984
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004985 // Determine if this class is also "non-lazy".
4986 if (ImplementationIsNonLazy(ID))
4987 DefinedNonLazyClasses.push_back(ClassMD);
4988
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004989 // Force the definition of the EHType if necessary.
4990 if (flags & CLS_EXCEPTION)
4991 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00004992 // Make sure method definition entries are all clear for next implementation.
4993 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004994}
4995
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004996/// GenerateProtocolRef - This routine is called to generate code for
4997/// a protocol reference expression; as in:
4998/// @code
4999/// @protocol(Proto1);
5000/// @endcode
5001/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5002/// which will hold address of the protocol meta-data.
5003///
5004llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005005 const ObjCProtocolDecl *PD) {
5006
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005007 // This routine is called for @protocol only. So, we must build definition
5008 // of protocol's meta-data (not a reference to it!)
5009 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005010 llvm::Constant *Init =
5011 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5012 ObjCTypes.ExternalProtocolPtrTy);
5013
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005014 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005015 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005017 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5018 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005019 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005020 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005021 CGM.getModule(),
5022 Init->getType(), false,
5023 llvm::GlobalValue::WeakAnyLinkage,
5024 Init,
5025 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005026 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5027 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005028 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005029 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005030}
5031
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005032/// GenerateCategory - Build metadata for a category implementation.
5033/// struct _category_t {
5034/// const char * const name;
5035/// struct _class_t *const cls;
5036/// const struct _method_list_t * const instance_methods;
5037/// const struct _method_list_t * const class_methods;
5038/// const struct _protocol_list_t * const protocols;
5039/// const struct _prop_list_t * const properties;
5040/// }
5041///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005042void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005043 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005044 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005045 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5046 "_$_" + OCD->getNameAsString());
5047 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005048 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005049
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005050 std::vector<llvm::Constant*> Values(6);
5051 Values[0] = GetClassName(OCD->getIdentifier());
5052 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005053 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005054 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005055 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5056
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005057 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005058 std::vector<llvm::Constant*> Methods;
5059 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005060 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005061 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005062
5063 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005064 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005065 // Instance methods should always be defined.
5066 Methods.push_back(GetMethodConstant(*i));
5067 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005068
5069 Values[2] = EmitMethodList(MethodListName,
5070 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005071 Methods);
5072
5073 MethodListName = Prefix;
5074 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5075 OCD->getNameAsString();
5076 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005077 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005078 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005079 // Class methods should always be defined.
5080 Methods.push_back(GetMethodConstant(*i));
5081 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082
5083 Values[3] = EmitMethodList(MethodListName,
5084 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005085 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005086 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005087 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005088 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005089 llvm::SmallString<256> ExtName;
5090 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5091 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005092 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005093 + Interface->getName() + "_$_"
5094 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005095 Category->protocol_begin(),
5096 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005097 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5098 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005099 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005100 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5101 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005102 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005103
5104 llvm::Constant *Init =
5105 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005106 Values);
5107 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005108 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005109 false,
5110 llvm::GlobalValue::InternalLinkage,
5111 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005112 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005113 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005114 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005115 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005116 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005117 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005118
5119 // Determine if this category is also "non-lazy".
5120 if (ImplementationIsNonLazy(OCD))
5121 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005122 // method definition entries must be clear for next implementation.
5123 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005124}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005125
5126/// GetMethodConstant - Return a struct objc_method constant for the
5127/// given method if it has been defined. The result is null if the
5128/// method has not been defined. The return value has type MethodPtrTy.
5129llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005130 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005131 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005132 if (!Fn)
5133 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005135 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005136 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005137 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005138 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005139 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005140 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005141 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005142}
5143
5144/// EmitMethodList - Build meta-data for method declarations
5145/// struct _method_list_t {
5146/// uint32_t entsize; // sizeof(struct _objc_method)
5147/// uint32_t method_count;
5148/// struct _objc_method method_list[method_count];
5149/// }
5150///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005151llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5152 const char *Section,
5153 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005154 // Return null for empty list.
5155 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005156 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005157
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005158 std::vector<llvm::Constant*> Values(3);
5159 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005160 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005161 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005162 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005163 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005164 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005165 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005166 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005167 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005169 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005170 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005171 llvm::GlobalValue::InternalLinkage,
5172 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005173 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005174 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005175 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005176 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005177 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005178 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005179 ObjCTypes.MethodListnfABIPtrTy);
5180}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005181
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005182/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5183/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005184llvm::GlobalVariable *
5185CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5186 const ObjCIvarDecl *Ivar) {
5187 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005188 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005189 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005190 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005191 CGM.getModule().getGlobalVariable(Name);
5192 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005193 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005194 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005195 false,
5196 llvm::GlobalValue::ExternalLinkage,
5197 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005198 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005199 return IvarOffsetGV;
5200}
5201
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005202llvm::Constant *
5203CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5204 const ObjCIvarDecl *Ivar,
5205 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005206 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005207 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005208 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005209 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005210 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005211
Mike Stump18bb9282009-05-16 07:57:57 +00005212 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5213 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005214 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5215 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005216 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005217 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005218 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005219 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005220 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005221 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005222}
5223
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005224/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005225/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005226/// IvarListnfABIPtrTy.
5227/// struct _ivar_t {
5228/// unsigned long int *offset; // pointer to ivar offset location
5229/// char *name;
5230/// char *type;
5231/// uint32_t alignment;
5232/// uint32_t size;
5233/// }
5234/// struct _ivar_list_t {
5235/// uint32 entsize; // sizeof(struct _ivar_t)
5236/// uint32 count;
5237/// struct _iver_t list[count];
5238/// }
5239///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005240
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005241llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005242 const ObjCImplementationDecl *ID) {
5243
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005244 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005245
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005246 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5247 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005248
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005249 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005250
Daniel Dunbarae032262009-04-20 00:33:43 +00005251 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005252 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005253 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005255 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5256 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005257 // Ignore unnamed bit-fields.
5258 if (!IVD->getDeclName())
5259 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005260 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005261 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005262 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5263 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005264 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005265 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005266 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005267 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005268 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005269 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005270 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005271 // NOTE. Size of a bitfield does not match gcc's, because of the
5272 // way bitfields are treated special in each. But I am told that
5273 // 'size' for bitfield ivars is ignored by the runtime so it does
5274 // not matter. If it matters, there is enough info to get the
5275 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005276 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005277 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005278 }
5279 // Return null for empty list.
5280 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005281 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005282 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005283 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005284 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5285 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005286 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005287 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005288 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005289 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005290 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5291 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005292 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005293 llvm::GlobalValue::InternalLinkage,
5294 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005295 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005296 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005297 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005298 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005299
Chris Lattnerf56501c2009-07-17 23:57:13 +00005300 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005301 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005302}
5303
5304llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005305 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005306 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005307
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005308 if (!Entry) {
5309 // We use the initializer as a marker of whether this is a forward
5310 // reference or not. At module finalization we add the empty
5311 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005312 Entry =
5313 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5314 llvm::GlobalValue::ExternalLinkage,
5315 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005316 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005317 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005318 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005319
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005320 return Entry;
5321}
5322
5323/// GetOrEmitProtocol - Generate the protocol meta-data:
5324/// @code
5325/// struct _protocol_t {
5326/// id isa; // NULL
5327/// const char * const protocol_name;
5328/// const struct _protocol_list_t * protocol_list; // super protocols
5329/// const struct method_list_t * const instance_methods;
5330/// const struct method_list_t * const class_methods;
5331/// const struct method_list_t *optionalInstanceMethods;
5332/// const struct method_list_t *optionalClassMethods;
5333/// const struct _prop_list_t * properties;
5334/// const uint32_t size; // sizeof(struct _protocol_t)
5335/// const uint32_t flags; // = 0
5336/// }
5337/// @endcode
5338///
5339
5340llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005341 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005342 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005343
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005344 // Early exit if a defining object has already been generated.
5345 if (Entry && Entry->hasInitializer())
5346 return Entry;
5347
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005348 // Construct method lists.
5349 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5350 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005351 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005352 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005353 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005354 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005355 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5356 OptInstanceMethods.push_back(C);
5357 } else {
5358 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005359 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005360 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005361
5362 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005363 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005364 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005365 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005366 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5367 OptClassMethods.push_back(C);
5368 } else {
5369 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005371 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005372
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005373 std::vector<llvm::Constant*> Values(10);
5374 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005375 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005376 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005377 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5378 PD->protocol_begin(),
5379 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005380
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005381 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005382 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 "__DATA, __objc_const",
5384 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005385 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005386 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005387 "__DATA, __objc_const",
5388 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005389 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005390 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 "__DATA, __objc_const",
5392 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005393 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005394 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005395 "__DATA, __objc_const",
5396 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005397 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005398 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005399 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005400 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005401 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005402 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005403 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005404 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005405
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005406 if (Entry) {
5407 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005408 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005409 Entry->setInitializer(Init);
5410 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005412 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5413 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5414 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005415 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005416 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005417 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005418 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005419 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005420 CGM.AddUsedGlobal(Entry);
5421
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005422 // Use this protocol meta-data to build protocol list table in section
5423 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005424 llvm::GlobalVariable *PTGV =
5425 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5426 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5427 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005428 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005429 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005430 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005431 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005432 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 return Entry;
5434}
5435
5436/// EmitProtocolList - Generate protocol list meta-data:
5437/// @code
5438/// struct _protocol_list_t {
5439/// long protocol_count; // Note, this is 32/64 bit
5440/// struct _protocol_t[protocol_count];
5441/// }
5442/// @endcode
5443///
5444llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005445CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5446 ObjCProtocolDecl::protocol_iterator begin,
5447 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005448 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005449
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005450 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005451 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005452 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005453
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005454 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005455 llvm::SmallString<256> TmpName;
5456 Name.toVector(TmpName);
5457 llvm::GlobalVariable *GV =
5458 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005460 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005461
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005462 for (; begin != end; ++begin)
5463 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5464
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005465 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005466 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005467 ObjCTypes.ProtocolnfABIPtrTy));
5468
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005469 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005470 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005471 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005472 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005473 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005474 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005475 ProtocolRefs.size()),
5476 ProtocolRefs);
5477
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005478 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005479 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005480 llvm::GlobalValue::InternalLinkage,
5481 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005482 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005483 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005484 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005485 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005486 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005487 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005488 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489}
5490
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005491/// GetMethodDescriptionConstant - This routine build following meta-data:
5492/// struct _objc_method {
5493/// SEL _cmd;
5494/// char *method_type;
5495/// char *_imp;
5496/// }
5497
5498llvm::Constant *
5499CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5500 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005501 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005502 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5503 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005504 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005505 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005506 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005507 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005508}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005509
5510/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5511/// This code gen. amounts to generating code for:
5512/// @code
5513/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5514/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005515///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005516LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005517 CodeGen::CodeGenFunction &CGF,
5518 QualType ObjectTy,
5519 llvm::Value *BaseValue,
5520 const ObjCIvarDecl *Ivar,
5521 unsigned CVRQualifiers) {
5522 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005523 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5524 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005525}
5526
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005527llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005528 CodeGen::CodeGenFunction &CGF,
5529 const ObjCInterfaceDecl *Interface,
5530 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005531 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005532}
5533
John McCall234eac82011-05-13 23:16:18 +00005534static void appendSelectorForMessageRefTable(std::string &buffer,
5535 Selector selector) {
5536 if (selector.isUnarySelector()) {
5537 buffer += selector.getNameForSlot(0);
5538 return;
5539 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005540
John McCall234eac82011-05-13 23:16:18 +00005541 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5542 buffer += selector.getNameForSlot(i);
5543 buffer += '_';
5544 }
5545}
5546
John McCall9e8bb002011-05-14 03:10:52 +00005547/// Emit a "v-table" message send. We emit a weak hidden-visibility
5548/// struct, initially containing the selector pointer and a pointer to
5549/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5550/// load and call the function pointer, passing the address of the
5551/// struct as the second parameter. The runtime determines whether
5552/// the selector is currently emitted using vtable dispatch; if so, it
5553/// substitutes a stub function which simply tail-calls through the
5554/// appropriate vtable slot, and if not, it substitues a stub function
5555/// which tail-calls objc_msgSend. Both stubs adjust the selector
5556/// argument to correctly point to the selector.
5557RValue
5558CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5559 ReturnValueSlot returnSlot,
5560 QualType resultType,
5561 Selector selector,
5562 llvm::Value *arg0,
5563 QualType arg0Type,
5564 bool isSuper,
5565 const CallArgList &formalArgs,
5566 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005567 // Compute the actual arguments.
5568 CallArgList args;
5569
John McCall9e8bb002011-05-14 03:10:52 +00005570 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005571 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005572 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5573 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005574
John McCall9e8bb002011-05-14 03:10:52 +00005575 // Second argument: a pointer to the message ref structure. Leave
5576 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005577 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5578
5579 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5580
5581 const CGFunctionInfo &fnInfo =
5582 CGM.getTypes().getFunctionInfo(resultType, args,
5583 FunctionType::ExtInfo());
5584
John McCall5880fb82011-05-14 21:12:11 +00005585 NullReturnState nullReturn;
5586
John McCall9e8bb002011-05-14 03:10:52 +00005587 // Find the function to call and the mangled name for the message
5588 // ref structure. Using a different mangled name wouldn't actually
5589 // be a problem; it would just be a waste.
5590 //
5591 // The runtime currently never uses vtable dispatch for anything
5592 // except normal, non-super message-sends.
5593 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005594 llvm::Constant *fn = 0;
5595 std::string messageRefName("\01l_");
5596 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005597 if (isSuper) {
5598 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5599 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005600 } else {
John McCall5880fb82011-05-14 21:12:11 +00005601 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005602 fn = ObjCTypes.getMessageSendStretFixupFn();
5603 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005604 }
John McCall234eac82011-05-13 23:16:18 +00005605 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5606 fn = ObjCTypes.getMessageSendFpretFixupFn();
5607 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005608 } else {
John McCall234eac82011-05-13 23:16:18 +00005609 if (isSuper) {
5610 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5611 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005612 } else {
John McCall234eac82011-05-13 23:16:18 +00005613 fn = ObjCTypes.getMessageSendFixupFn();
5614 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005615 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005616 }
John McCall234eac82011-05-13 23:16:18 +00005617 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5618 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005619
John McCall234eac82011-05-13 23:16:18 +00005620 // Append the selector name, except use underscores anywhere we
5621 // would have used colons.
5622 appendSelectorForMessageRefTable(messageRefName, selector);
5623
5624 llvm::GlobalVariable *messageRef
5625 = CGM.getModule().getGlobalVariable(messageRefName);
5626 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005627 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005628 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
5629 llvm::Constant *init =
5630 llvm::ConstantStruct::get(VMContext, values, 2, false);
5631 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5632 init->getType(),
5633 /*constant*/ false,
5634 llvm::GlobalValue::WeakAnyLinkage,
5635 init,
5636 messageRefName);
5637 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5638 messageRef->setAlignment(16);
5639 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5640 }
5641 llvm::Value *mref =
5642 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5643
John McCall9e8bb002011-05-14 03:10:52 +00005644 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005645 args[1].RV = RValue::get(mref);
5646
5647 // Load the function to call from the message ref table.
5648 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5649 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5650
5651 bool variadic = method ? method->isVariadic() : false;
5652 const llvm::FunctionType *fnType =
5653 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5654 callee = CGF.Builder.CreateBitCast(callee,
5655 llvm::PointerType::getUnqual(fnType));
5656
John McCall5880fb82011-05-14 21:12:11 +00005657 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5658 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005659}
5660
5661/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005662CodeGen::RValue
5663CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005664 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005665 QualType ResultType,
5666 Selector Sel,
5667 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005668 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005669 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005670 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005671 return isVTableDispatchedSelector(Sel)
5672 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005673 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005674 false, CallArgs, Method)
5675 : EmitMessageSend(CGF, Return, ResultType,
5676 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005677 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005678 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005679}
5680
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005681llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005682CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005683 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5684
Daniel Dunbara6468342009-03-02 05:18:14 +00005685 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005686 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005687 false, llvm::GlobalValue::ExternalLinkage,
5688 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005689 }
5690
5691 return GV;
5692}
5693
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005694llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5695 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005696 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005697
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005698 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005699 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005700 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701 Entry =
5702 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005703 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005704 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005705 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005706 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005707 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005708 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005709 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005710 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005711 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005712
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005713 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005714}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005715
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005716llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005717CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005718 const ObjCInterfaceDecl *ID) {
5719 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005720
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005721 if (!Entry) {
5722 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5723 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005724 Entry =
5725 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005726 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005728 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005729 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005730 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005731 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005732 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005733 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005734 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005735
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005736 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005737}
5738
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005739/// EmitMetaClassRef - Return a Value * of the address of _class_t
5740/// meta-data
5741///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005742llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5743 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005744 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5745 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005746 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005747
Daniel Dunbar15894b72009-04-07 05:48:37 +00005748 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005749 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005750 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005751 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005752 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005753 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005754 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005755 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005756 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005757 ObjCTypes.ClassnfABIPtrTy));
5758
Daniel Dunbare60aa052009-04-15 19:03:14 +00005759 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005760 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005762 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005763}
5764
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005765/// GetClass - Return a reference to the class for the given interface
5766/// decl.
5767llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5768 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005769 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005770 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5771 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5772 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5773 }
5774
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005775 return EmitClassRef(Builder, ID);
5776}
5777
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005778/// Generates a message send where the super is the receiver. This is
5779/// a message send to self with special delivery semantics indicating
5780/// which class's method should be called.
5781CodeGen::RValue
5782CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005783 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005784 QualType ResultType,
5785 Selector Sel,
5786 const ObjCInterfaceDecl *Class,
5787 bool isCategoryImpl,
5788 llvm::Value *Receiver,
5789 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005790 const CodeGen::CallArgList &CallArgs,
5791 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005792 // ...
5793 // Create and init a super structure; this is a (receiver, class)
5794 // pair we will pass to objc_msgSendSuper.
5795 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005796 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005797
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005798 llvm::Value *ReceiverAsObject =
5799 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5800 CGF.Builder.CreateStore(ReceiverAsObject,
5801 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005802
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005803 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005804 llvm::Value *Target;
5805 if (IsClassMessage) {
5806 if (isCategoryImpl) {
5807 // Message sent to "super' in a class method defined in
5808 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005809 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005810 Target = CGF.Builder.CreateStructGEP(Target, 0);
5811 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005812 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005813 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005814 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005815 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005816
Mike Stump18bb9282009-05-16 07:57:57 +00005817 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5818 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005819 const llvm::Type *ClassTy =
5820 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5821 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5822 CGF.Builder.CreateStore(Target,
5823 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005824
John McCall9e8bb002011-05-14 03:10:52 +00005825 return (isVTableDispatchedSelector(Sel))
5826 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005827 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005828 true, CallArgs, Method)
5829 : EmitMessageSend(CGF, Return, ResultType,
5830 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005831 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005832 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005833}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005834
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005835llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005836 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005837 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005838
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005839 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005840 llvm::Constant *Casted =
5841 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5842 ObjCTypes.SelectorPtrTy);
5843 Entry =
5844 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5845 llvm::GlobalValue::InternalLinkage,
5846 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005847 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005848 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005849 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005850
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005851 if (lval)
5852 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005853 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005854}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005855/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005856/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005857///
5858void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005859 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005860 llvm::Value *dst,
5861 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005862 const llvm::Type * SrcTy = src->getType();
5863 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005864 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005865 assert(Size <= 8 && "does not support size > 8");
5866 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5867 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005868 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5869 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005870 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5871 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005872 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5873 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005874 return;
5875}
5876
5877/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5878/// objc_assign_strongCast (id src, id *dst)
5879///
5880void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005881 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005882 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005883 const llvm::Type * SrcTy = src->getType();
5884 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005885 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005886 assert(Size <= 8 && "does not support size > 8");
5887 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005888 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005889 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5890 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005891 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5892 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005893 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005894 src, dst, "weakassign");
5895 return;
5896}
5897
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005898void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005899 CodeGen::CodeGenFunction &CGF,
5900 llvm::Value *DestPtr,
5901 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005902 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005903 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5904 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005905 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005906 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005907 return;
5908}
5909
Fariborz Jahanian06292952009-02-16 22:52:32 +00005910/// EmitObjCWeakRead - Code gen for loading value of a __weak
5911/// object: objc_read_weak (id *src)
5912///
5913llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005914 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005915 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005916 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005917 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5918 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005919 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005920 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005921 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005922 return read_weak;
5923}
5924
5925/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5926/// objc_assign_weak (id src, id *dst)
5927///
5928void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005929 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005930 const llvm::Type * SrcTy = src->getType();
5931 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005932 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005933 assert(Size <= 8 && "does not support size > 8");
5934 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5935 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005936 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5937 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005938 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5939 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005940 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005941 src, dst, "weakassign");
5942 return;
5943}
5944
5945/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5946/// objc_assign_global (id src, id *dst)
5947///
5948void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005949 llvm::Value *src, llvm::Value *dst,
5950 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005951 const llvm::Type * SrcTy = src->getType();
5952 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005953 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005954 assert(Size <= 8 && "does not support size > 8");
5955 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5956 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005957 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5958 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005959 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5960 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005961 if (!threadlocal)
5962 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5963 src, dst, "globalassign");
5964 else
5965 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5966 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005967 return;
5968}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005969
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005970void
John McCallbd309292010-07-06 01:34:17 +00005971CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5972 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005973 EmitAtSynchronizedStmt(CGF, S,
5974 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5975 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00005976}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005977
John McCall2ca705e2010-07-24 00:37:23 +00005978llvm::Constant *
5979CGObjCNonFragileABIMac::GetEHType(QualType T) {
5980 // There's a particular fixed type info for 'id'.
5981 if (T->isObjCIdType() ||
5982 T->isObjCQualifiedIdType()) {
5983 llvm::Constant *IDEHType =
5984 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5985 if (!IDEHType)
5986 IDEHType =
5987 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5988 false,
5989 llvm::GlobalValue::ExternalLinkage,
5990 0, "OBJC_EHTYPE_id");
5991 return IDEHType;
5992 }
5993
5994 // All other types should be Objective-C interface pointer types.
5995 const ObjCObjectPointerType *PT =
5996 T->getAs<ObjCObjectPointerType>();
5997 assert(PT && "Invalid @catch type.");
5998 const ObjCInterfaceType *IT = PT->getInterfaceType();
5999 assert(IT && "Invalid @catch type.");
6000 return GetInterfaceEHType(IT->getDecl(), false);
6001}
6002
John McCallbd309292010-07-06 01:34:17 +00006003void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6004 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006005 EmitTryCatchStmt(CGF, S,
6006 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6007 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6008 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006009}
6010
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006011/// EmitThrowStmt - Generate code for a throw statement.
6012void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6013 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006014 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006015 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006016 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6017 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006018 llvm::Value *Args[] = { Exception };
6019 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6020 Args, Args+1)
6021 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006022 } else {
John McCall17afe452010-10-16 08:21:07 +00006023 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6024 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006025 }
6026
John McCall17afe452010-10-16 08:21:07 +00006027 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006028 CGF.Builder.ClearInsertionPoint();
6029}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006030
John McCall2ca705e2010-07-24 00:37:23 +00006031llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006032CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006033 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006034 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006035
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006036 // If we don't need a definition, return the entry if found or check
6037 // if we use an external reference.
6038 if (!ForDefinition) {
6039 if (Entry)
6040 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006041
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006042 // If this type (or a super class) has the __objc_exception__
6043 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006044 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006045 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006046 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006047 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006048 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006049 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006050 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006051 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006052
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006053 // Otherwise we need to either make a new entry or fill in the
6054 // initializer.
6055 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006056 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006057 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006058 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006059 CGM.getModule().getGlobalVariable(VTableName);
6060 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006061 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6062 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006063 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006064 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006065
Chris Lattner5e016ae2010-06-27 07:15:29 +00006066 llvm::Value *VTableIdx =
6067 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006068
6069 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006070 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006071 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006072 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006073 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006074 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006075
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006076 if (Entry) {
6077 Entry->setInitializer(Init);
6078 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006079 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006080 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006081 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006082 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006083 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006084 }
6085
John McCall457a04e2010-10-22 21:05:15 +00006086 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006087 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006088 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6089 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006090
6091 if (ForDefinition) {
6092 Entry->setSection("__DATA,__objc_const");
6093 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6094 } else {
6095 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6096 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006097
6098 return Entry;
6099}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006100
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006101/* *** */
6102
Daniel Dunbarb036db82008-08-13 03:21:16 +00006103CodeGen::CGObjCRuntime *
6104CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006105 if (CGM.getLangOptions().ObjCNonFragileABI)
6106 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006107 return new CGObjCMac(CGM);
6108}