blob: d518372f9c99f40c15adac3a83e65ec8dd800b32 [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 McCall31168b02011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
John McCall9dc0db22011-05-15 01:53:33 +000068 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
69 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCall31168b02011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000073 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000074
John McCall9dc0db22011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +000081 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
82 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000085
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000087
John McCall9dc0db22011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +000094 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
95 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall9dc0db22011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000099
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000101
John McCall9dc0db22011-05-15 01:53:33 +0000102 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
103 ///
104 /// The messenger used for super calls, which have different dispatch
105 /// semantics. The class passed is the superclass of the current
106 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000107 llvm::Constant *getMessageSendSuperFn() const {
John McCall9dc0db22011-05-15 01:53:33 +0000108 const llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000110 params, true),
111 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000112 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000113
John McCall9dc0db22011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
115 ///
116 /// A slightly different messenger used for super calls. The class
117 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000118 llvm::Constant *getMessageSendSuperFn2() const {
John McCall9dc0db22011-05-15 01:53:33 +0000119 const llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000121 params, true),
122 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000123 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000124
John McCall9dc0db22011-05-15 01:53:33 +0000125 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
126 /// SEL op, ...)
127 ///
128 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000129 llvm::Constant *getMessageSendSuperStretFn() const {
John McCall9dc0db22011-05-15 01:53:33 +0000130 const llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000131 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000132 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000133 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000134 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000135
John McCall9dc0db22011-05-15 01:53:33 +0000136 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
137 /// SEL op, ...)
138 ///
139 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000140 llvm::Constant *getMessageSendSuperStretFn2() const {
John McCall9dc0db22011-05-15 01:53:33 +0000141 const llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000142 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000143 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000144 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000145 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000146
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000147 llvm::Constant *getMessageSendSuperFpretFn() const {
148 // There is no objc_msgSendSuper_fpret? How can that work?
149 return getMessageSendSuperFn();
150 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000151
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperFpretFn2() const {
153 // There is no objc_msgSendSuper_fpret? How can that work?
154 return getMessageSendSuperFn2();
155 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000157protected:
158 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000159
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000160public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000161 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000162 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000163
Daniel Dunbar5d715592008-08-12 05:28:47 +0000164 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
165 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000166
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000167 /// PtrObjectPtrTy - LLVM type for id *
168 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000169
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000170 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000171 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000172 /// ProtocolPtrTy - LLVM type for external protocol handles
173 /// (typeof(Protocol))
174 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Daniel Dunbarc722b852008-08-30 03:02:31 +0000176 // SuperCTy - clang type for struct objc_super.
177 QualType SuperCTy;
178 // SuperPtrCTy - clang type for struct objc_super *.
179 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000180
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000181 /// SuperTy - LLVM type for struct objc_super.
182 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000183 /// SuperPtrTy - LLVM type for struct objc_super *.
184 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000186 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
187 /// in GCC parlance).
188 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000189
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000190 /// PropertyListTy - LLVM type for struct objc_property_list
191 /// (_prop_list_t in GCC parlance).
192 const llvm::StructType *PropertyListTy;
193 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
194 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000195
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000196 // MethodTy - LLVM type for struct objc_method.
197 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000198
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000199 /// CacheTy - LLVM type for struct objc_cache.
200 const llvm::Type *CacheTy;
201 /// CachePtrTy - LLVM type for struct objc_cache *.
202 const llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000203
Chris Lattnerce8754e2009-04-22 02:44:54 +0000204 llvm::Constant *getGetPropertyFn() {
205 CodeGen::CodeGenTypes &Types = CGM.getTypes();
206 ASTContext &Ctx = CGM.getContext();
207 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000208 llvm::SmallVector<CanQualType,4> Params;
209 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
210 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000211 Params.push_back(IdType);
212 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000213 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000214 Params.push_back(Ctx.BoolTy);
215 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000216 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000217 FunctionType::ExtInfo()),
218 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000219 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
220 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000221
Chris Lattnerce8754e2009-04-22 02:44:54 +0000222 llvm::Constant *getSetPropertyFn() {
223 CodeGen::CodeGenTypes &Types = CGM.getTypes();
224 ASTContext &Ctx = CGM.getContext();
225 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000226 llvm::SmallVector<CanQualType,6> Params;
227 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
228 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000229 Params.push_back(IdType);
230 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000231 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000232 Params.push_back(IdType);
233 Params.push_back(Ctx.BoolTy);
234 Params.push_back(Ctx.BoolTy);
235 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000236 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000237 FunctionType::ExtInfo()),
238 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000239 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
240 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000241
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000242
243 llvm::Constant *getCopyStructFn() {
244 CodeGen::CodeGenTypes &Types = CGM.getTypes();
245 ASTContext &Ctx = CGM.getContext();
246 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
247 llvm::SmallVector<CanQualType,5> Params;
248 Params.push_back(Ctx.VoidPtrTy);
249 Params.push_back(Ctx.VoidPtrTy);
250 Params.push_back(Ctx.LongTy);
251 Params.push_back(Ctx.BoolTy);
252 Params.push_back(Ctx.BoolTy);
253 const llvm::FunctionType *FTy =
254 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
255 FunctionType::ExtInfo()),
256 false);
257 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
258 }
259
Chris Lattnerce8754e2009-04-22 02:44:54 +0000260 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000261 CodeGen::CodeGenTypes &Types = CGM.getTypes();
262 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000263 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000264 llvm::SmallVector<CanQualType,1> Params;
265 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000266 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000267 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000268 FunctionType::ExtInfo()),
269 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
271 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000273 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000274 llvm::Constant *getGcReadWeakFn() {
275 // id objc_read_weak (id *)
John McCall9dc0db22011-05-15 01:53:33 +0000276 const llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000278 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000280 }
281
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000282 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000283 llvm::Constant *getGcAssignWeakFn() {
284 // id objc_assign_weak (id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000285 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000286 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000287 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000288 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
289 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000290
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000291 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000292 llvm::Constant *getGcAssignGlobalFn() {
293 // id objc_assign_global(id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000294 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000295 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000296 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000297 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
298 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000299
Fariborz Jahanian217af242010-07-20 20:30:03 +0000300 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
301 llvm::Constant *getGcAssignThreadLocalFn() {
302 // id objc_assign_threadlocal(id src, id * dest)
John McCall9dc0db22011-05-15 01:53:33 +0000303 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000304 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000305 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000306 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
307 }
308
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000309 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000310 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000311 // id objc_assign_ivar(id, id *, ptrdiff_t)
John McCall9dc0db22011-05-15 01:53:33 +0000312 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
313 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000314 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000315 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000316 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
317 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000318
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000319 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
320 llvm::Constant *GcMemmoveCollectableFn() {
321 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
John McCall9dc0db22011-05-15 01:53:33 +0000322 const llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
323 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000324 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
325 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000326
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000327 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000328 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000329 // id objc_assign_strongCast(id, id *)
John McCall9dc0db22011-05-15 01:53:33 +0000330 const llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000331 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000332 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000333 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
334 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000335
336 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000337 llvm::Constant *getExceptionThrowFn() {
338 // void objc_exception_throw(id)
John McCall9dc0db22011-05-15 01:53:33 +0000339 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000340 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000341 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
343 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000344
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000345 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
346 llvm::Constant *getExceptionRethrowFn() {
347 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000348 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
350 }
351
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000352 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000353 llvm::Constant *getSyncEnterFn() {
354 // void objc_sync_enter (id)
John McCall9dc0db22011-05-15 01:53:33 +0000355 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000356 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
359 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000360
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000361 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000362 llvm::Constant *getSyncExitFn() {
363 // void objc_sync_exit (id)
John McCall9dc0db22011-05-15 01:53:33 +0000364 const llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000365 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000366 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
368 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000369
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000370 llvm::Constant *getSendFn(bool IsSuper) const {
371 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
372 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000373
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000374 llvm::Constant *getSendFn2(bool IsSuper) const {
375 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
376 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000377
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000378 llvm::Constant *getSendStretFn(bool IsSuper) const {
379 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
380 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000382 llvm::Constant *getSendStretFn2(bool IsSuper) const {
383 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000385
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFpretFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
388 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000389
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
392 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000393
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000394 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
395 ~ObjCCommonTypesHelper(){}
396};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000397
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000398/// ObjCTypesHelper - Helper class that encapsulates lazy
399/// construction of varies types used during ObjC generation.
400class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000401public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000402 /// SymtabTy - LLVM type for struct objc_symtab.
403 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000404 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
405 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000406 /// ModuleTy - LLVM type for struct objc_module.
407 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000408
Daniel Dunbarb036db82008-08-13 03:21:16 +0000409 /// ProtocolTy - LLVM type for struct objc_protocol.
410 const llvm::StructType *ProtocolTy;
411 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
412 const llvm::Type *ProtocolPtrTy;
413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension.
415 const llvm::StructType *ProtocolExtensionTy;
416 /// ProtocolExtensionTy - LLVM type for struct
417 /// objc_protocol_extension *.
418 const llvm::Type *ProtocolExtensionPtrTy;
419 /// MethodDescriptionTy - LLVM type for struct
420 /// objc_method_description.
421 const llvm::StructType *MethodDescriptionTy;
422 /// MethodDescriptionListTy - LLVM type for struct
423 /// objc_method_description_list.
424 const llvm::StructType *MethodDescriptionListTy;
425 /// MethodDescriptionListPtrTy - LLVM type for struct
426 /// objc_method_description_list *.
427 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000428 /// ProtocolListTy - LLVM type for struct objc_property_list.
429 const llvm::Type *ProtocolListTy;
430 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
431 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000432 /// CategoryTy - LLVM type for struct objc_category.
433 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000434 /// ClassTy - LLVM type for struct objc_class.
435 const llvm::StructType *ClassTy;
436 /// ClassPtrTy - LLVM type for struct objc_class *.
437 const llvm::Type *ClassPtrTy;
438 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
439 const llvm::StructType *ClassExtensionTy;
440 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
441 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000442 // IvarTy - LLVM type for struct objc_ivar.
443 const llvm::StructType *IvarTy;
444 /// IvarListTy - LLVM type for struct objc_ivar_list.
445 const llvm::Type *IvarListTy;
446 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
447 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000448 /// MethodListTy - LLVM type for struct objc_method_list.
449 const llvm::Type *MethodListTy;
450 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
451 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000452
Anders Carlsson9ff22482008-09-09 10:10:21 +0000453 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
454 const llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000455
Anders Carlsson9ff22482008-09-09 10:10:21 +0000456 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000457 llvm::Constant *getExceptionTryEnterFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000458 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000459 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000460 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000461 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000462 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000463
464 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000465 llvm::Constant *getExceptionTryExitFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000466 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000467 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000468 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000469 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000470 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000471
472 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000473 llvm::Constant *getExceptionExtractFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000474 const llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000475 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000476 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000477 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000478 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000479
Anders Carlsson9ff22482008-09-09 10:10:21 +0000480 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionMatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000482 const llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000483 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485 "objc_exception_match");
486
Chris Lattnerc6406db2009-04-22 02:26:14 +0000487 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000488
Anders Carlsson9ff22482008-09-09 10:10:21 +0000489 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000490 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000491 // This is specifically the prototype for x86.
492 const llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
493 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
494 params, false),
495 "_setjmp");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000496 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000497
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000501};
502
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000503/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000504/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000505class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000506public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000508 // MethodListnfABITy - LLVM for struct _method_list_t
509 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000510
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
512 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000513
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000514 // ProtocolnfABITy = LLVM for struct _protocol_t
515 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000516
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
518 const llvm::Type *ProtocolnfABIPtrTy;
519
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
521 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000522
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
524 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000525
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000526 // ClassnfABITy - LLVM for struct _class_t
527 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000528
Fariborz Jahanian71394042009-01-23 23:53:38 +0000529 // ClassnfABIPtrTy - LLVM for struct _class_t*
530 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000532 // IvarnfABITy - LLVM for struct _ivar_t
533 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000534
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000535 // IvarListnfABITy - LLVM for struct _ivar_list_t
536 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000537
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
539 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000541 // ClassRonfABITy - LLVM for struct _class_ro_t
542 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
545 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000546
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000547 // CategorynfABITy - LLVM for struct _category_t
548 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000549
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000550 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000551
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
554 // IMP messenger;
555 // SEL name;
556 // };
557 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000560
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
562 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000566 // MessengerTy - Type of the messenger (shown as IMP above)
567 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
572 // SEL name;
573 // };
574 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
577 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000578
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000581 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000583 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000584 "objc_msgSend_fixup");
585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000586
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000587 llvm::Constant *getMessageSendFpretFixupFn() {
588 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000589 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000590 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000591 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000592 "objc_msgSend_fpret_fixup");
593 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000594
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000595 llvm::Constant *getMessageSendStretFixupFn() {
596 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000597 const llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000599 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000600 "objc_msgSend_stret_fixup");
601 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000603 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000605 // struct _super_message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000606 const llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000608 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000609 "objc_msgSendSuper2_fixup");
610 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000613 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000614 // struct _super_message_ref_t*, ...)
John McCall9dc0db22011-05-15 01:53:33 +0000615 const llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000616 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000617 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000618 "objc_msgSendSuper2_stret_fixup");
619 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000620
Chris Lattnera7c00b42009-04-22 02:15:23 +0000621 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000623 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000624
Chris Lattnera7c00b42009-04-22 02:15:23 +0000625 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Chris Lattnera7c00b42009-04-22 02:15:23 +0000627 llvm::Constant *getObjCBeginCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000628 const llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000629 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000630 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000631 "objc_begin_catch");
632 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000633
634 const llvm::StructType *EHTypeTy;
635 const llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000636
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000637 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
638 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000639};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000641class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000642public:
643 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000644 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000645 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000646 unsigned ivar_bytepos;
647 unsigned ivar_size;
648 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000650
651 // Allow sorting based on byte pos.
652 bool operator<(const GC_IVAR &b) const {
653 return ivar_bytepos < b.ivar_bytepos;
654 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000655 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000656
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000657 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000658 public:
659 unsigned skip;
660 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000662 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000663 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000665protected:
666 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000667 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000668 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000669 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000671 // gc ivar layout bitmap calculation helper caches.
672 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
673 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000674
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000675 /// LazySymbols - Symbols to generate a lazy reference for. See
676 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000677 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000678
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000679 /// DefinedSymbols - External symbols which are defined by this
680 /// module. The symbols in this list and LazySymbols are used to add
681 /// special linker symbols which ensure that Objective-C modules are
682 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000683 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000685 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000686 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000688 /// MethodVarNames - uniqued method variable names.
689 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000690
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000691 /// DefinedCategoryNames - list of category names in form Class_Category.
692 llvm::SetVector<std::string> DefinedCategoryNames;
693
Daniel Dunbarb036db82008-08-13 03:21:16 +0000694 /// MethodVarTypes - uniqued method type signatures. We have to use
695 /// a StringMap here because have no other unique reference.
696 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000697
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000698 /// MethodDefinitions - map of methods which have been defined in
699 /// this translation unit.
700 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000701
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000702 /// PropertyNames - uniqued method variable names.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000705 /// ClassReferences - uniqued class references.
706 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000707
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000708 /// SelectorReferences - uniqued selector references.
709 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000710
Daniel Dunbarb036db82008-08-13 03:21:16 +0000711 /// Protocols - Protocols for which an objc_protocol structure has
712 /// been emitted. Forward declarations are handled by creating an
713 /// empty structure whose initializer is filled in when/if defined.
714 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000715
Daniel Dunbarc475d422008-10-29 22:36:39 +0000716 /// DefinedProtocols - Protocols which have actually been
717 /// defined. We should not need this, see FIXME in GenerateProtocol.
718 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000719
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000720 /// DefinedClasses - List of defined classes.
721 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000722
723 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
724 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000726 /// DefinedCategories - List of defined categories.
727 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000729 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
730 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000732 /// GetNameForMethod - Return a name for the given method.
733 /// \param[out] NameOut - The return value.
734 void GetNameForMethod(const ObjCMethodDecl *OMD,
735 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000736 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000737
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000738 /// GetMethodVarName - Return a unique constant for the given
739 /// selector's name. The return value has type char *.
740 llvm::Constant *GetMethodVarName(Selector Sel);
741 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000743 /// GetMethodVarType - Return a unique constant for the given
744 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000745
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000746 // FIXME: This is a horrible name.
747 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000748 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000750 /// GetPropertyName - Return a unique constant for the given
751 /// name. The return value has type char *.
752 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000753
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000754 // FIXME: This can be dropped once string functions are unified.
755 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
756 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000758 /// GetClassName - Return a unique constant for the given selector's
759 /// name. The return value has type char *.
760 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000762 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
763
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000764 /// BuildIvarLayout - Builds ivar layout bitmap for the class
765 /// implementation for the __strong or __weak case.
766 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000767 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
768 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000769
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000770 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000771
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000772 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000773 unsigned int BytePos, bool ForStrongLayout,
774 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000775 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000776 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000777 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000778 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000779 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000780 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000781
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000782 /// GetIvarLayoutName - Returns a unique constant for the given
783 /// ivar layout bitmap.
784 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
785 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000786
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000787 /// EmitPropertyList - Emit the given property list. The return
788 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000789 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000791 const ObjCContainerDecl *OCD,
792 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000793
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000794 /// PushProtocolProperties - Push protocol's property on the input stack.
795 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
796 std::vector<llvm::Constant*> &Properties,
797 const Decl *Container,
798 const ObjCProtocolDecl *PROTO,
799 const ObjCCommonTypesHelper &ObjCTypes);
800
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000801 /// GetProtocolRef - Return a reference to the internal protocol
802 /// description, creating an empty one if it has not been
803 /// defined. The return value has type ProtocolPtrTy.
804 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000805
Daniel Dunbar30c65362009-03-09 20:09:19 +0000806 /// CreateMetadataVar - Create a global variable with internal
807 /// linkage for use by the Objective-C runtime.
808 ///
809 /// This is a convenience wrapper which not only creates the
810 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000811 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000812 ///
813 /// \param Name - The variable name.
814 /// \param Init - The variable initializer; this is also used to
815 /// define the type of the variable.
816 /// \param Section - The section the variable should go into, or 0.
817 /// \param Align - The alignment for the variable, or 0.
818 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000819 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000820 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000821 llvm::Constant *Init,
822 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000823 unsigned Align,
824 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000825
John McCall9e8bb002011-05-14 03:10:52 +0000826 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
827 ReturnValueSlot Return,
828 QualType ResultType,
829 llvm::Value *Sel,
830 llvm::Value *Arg0,
831 QualType Arg0Ty,
832 bool IsSuper,
833 const CallArgList &CallArgs,
834 const ObjCMethodDecl *OMD,
835 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000836
Daniel Dunbar5e639272010-04-25 20:39:01 +0000837 /// EmitImageInfo - Emit the image info marker used to encode some module
838 /// level information.
839 void EmitImageInfo();
840
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000841public:
Owen Andersonae86c192009-07-13 04:10:07 +0000842 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000843 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000844
David Chisnall481e3a82010-01-23 02:40:42 +0000845 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000847 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
848 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000850 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000852 /// GetOrEmitProtocol - Get the protocol object for the given
853 /// declaration, emitting it if necessary. The return value has type
854 /// ProtocolPtrTy.
855 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000856
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000857 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
858 /// object for the given declaration, emitting it if needed. These
859 /// forward references will be filled in with empty bodies if no
860 /// definition is seen. The return value has type ProtocolPtrTy.
861 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000862 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
863 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000864
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000865};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000867class CGObjCMac : public CGObjCCommonMac {
868private:
869 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000870
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000871 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000873 void EmitModuleInfo();
874
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000875 /// EmitModuleSymols - Emit module symbols, the list of defined
876 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000877 llvm::Constant *EmitModuleSymbols();
878
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000879 /// FinishModule - Write out global data structures at the end of
880 /// processing a translation unit.
881 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000882
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000883 /// EmitClassExtension - Generate the class extension structure used
884 /// to store the weak ivar layout and properties. The return value
885 /// has type ClassExtensionPtrTy.
886 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
887
888 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
889 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000891 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000892
John McCall31168b02011-06-15 23:02:42 +0000893 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
894 IdentifierInfo *II);
895
896 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
897
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000898 /// EmitSuperClassRef - Emits reference to class's main metadata class.
899 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000900
901 /// EmitIvarList - Emit the ivar list for the given
902 /// implementation. If ForClass is true the list of class ivars
903 /// (i.e. metaclass ivars) is emitted, otherwise the list of
904 /// interface ivars will be emitted. The return value has type
905 /// IvarListPtrTy.
906 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000907 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000909 /// EmitMetaClass - Emit a forward reference to the class structure
910 /// for the metaclass of the given interface. The return value has
911 /// type ClassPtrTy.
912 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
913
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000914 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000915 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000916 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
917 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000918 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000920 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000921
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000922 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000923
924 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000925 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000926 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000927 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000928 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000929
930 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000931 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000932 /// - TypeName: The name for the type containing the methods.
933 /// - IsProtocol: True iff these methods are for a protocol.
934 /// - ClassMethds: True iff these are class methods.
935 /// - Required: When true, only "required" methods are
936 /// listed. Similarly, when false only "optional" methods are
937 /// listed. For classes this should always be true.
938 /// - begin, end: The method list to output.
939 ///
940 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000941 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000942 const char *Section,
943 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000944
Daniel Dunbarc475d422008-10-29 22:36:39 +0000945 /// GetOrEmitProtocol - Get the protocol object for the given
946 /// declaration, emitting it if necessary. The return value has type
947 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000948 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000949
950 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
951 /// object for the given declaration, emitting it if needed. These
952 /// forward references will be filled in with empty bodies if no
953 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000954 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000955
Daniel Dunbarb036db82008-08-13 03:21:16 +0000956 /// EmitProtocolExtension - Generate the protocol extension
957 /// structure used to store optional instance and class methods, and
958 /// protocol properties. The return value has type
959 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000960 llvm::Constant *
961 EmitProtocolExtension(const ObjCProtocolDecl *PD,
962 const ConstantVector &OptInstanceMethods,
963 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000964
965 /// EmitProtocolList - Generate the list of referenced
966 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000967 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +0000968 ObjCProtocolDecl::protocol_iterator begin,
969 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000970
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000971 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
972 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000973 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
974 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000975
976public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000977 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000978
Fariborz Jahanian71394042009-01-23 23:53:38 +0000979 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000980
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000981 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000982 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000983 QualType ResultType,
984 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000985 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000986 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +0000987 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000988 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000989
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000990 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000991 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000992 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000993 QualType ResultType,
994 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000995 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000996 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000997 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000998 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000999 const CallArgList &CallArgs,
1000 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001001
Daniel Dunbarcb463852008-11-01 01:53:16 +00001002 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001003 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001004
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001005 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1006 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001007
1008 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1009 /// untyped one.
1010 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1011 const ObjCMethodDecl *Method);
1012
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001013 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001014
Daniel Dunbar92992502008-08-15 22:20:32 +00001015 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001016
Daniel Dunbar92992502008-08-15 22:20:32 +00001017 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001018
Daniel Dunbarcb463852008-11-01 01:53:16 +00001019 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001020 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001021
Chris Lattnerd4808922009-03-22 21:03:39 +00001022 virtual llvm::Constant *GetPropertyGetFunction();
1023 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001024 virtual llvm::Constant *GetGetStructFunction();
1025 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001026 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001027
John McCallbd309292010-07-06 01:34:17 +00001028 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1029 const ObjCAtTryStmt &S);
1030 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1031 const ObjCAtSynchronizedStmt &S);
1032 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001033 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1034 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001035 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001036 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001037 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001038 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001039 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001040 llvm::Value *src, llvm::Value *dest,
1041 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001042 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001043 llvm::Value *src, llvm::Value *dest,
1044 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001045 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001047 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1048 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001049 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001050
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001051 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1052 QualType ObjectTy,
1053 llvm::Value *BaseValue,
1054 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001055 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001056 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001057 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001058 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001059
1060 /// GetClassGlobal - Return the global variable for the Objective-C
1061 /// class of the given name.
1062 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
1063 assert(false && "CGObjCMac::GetClassGlobal");
1064 return 0;
1065 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001066};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001067
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001068class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001069private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001070 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001071 llvm::GlobalVariable* ObjCEmptyCacheVar;
1072 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001073
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001074 /// SuperClassReferences - uniqued super class references.
1075 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001076
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001077 /// MetaClassReferences - uniqued meta class references.
1078 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001079
1080 /// EHTypeReferences - uniqued class ehtype references.
1081 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001082
John McCall9e8bb002011-05-14 03:10:52 +00001083 /// VTableDispatchMethods - List of methods for which we generate
1084 /// vtable-based message dispatch.
1085 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001086
Fariborz Jahanian67260552009-11-17 21:37:35 +00001087 /// DefinedMetaClasses - List of defined meta-classes.
1088 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1089
John McCall9e8bb002011-05-14 03:10:52 +00001090 /// isVTableDispatchedSelector - Returns true if SEL is a
1091 /// vtable-based selector.
1092 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001093
Fariborz Jahanian71394042009-01-23 23:53:38 +00001094 /// FinishNonFragileABIModule - Write out global data structures at the end of
1095 /// processing a translation unit.
1096 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001097
Daniel Dunbar19573e72009-05-15 21:48:48 +00001098 /// AddModuleClassList - Add the given list of class pointers to the
1099 /// module with the provided symbol and section names.
1100 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1101 const char *SymbolName,
1102 const char *SectionName);
1103
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001104 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1105 unsigned InstanceStart,
1106 unsigned InstanceSize,
1107 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001108 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001109 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001110 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001111 llvm::Constant *ClassRoGV,
1112 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001113
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001114 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001115
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001116 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001117
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001118 /// EmitMethodList - Emit the method list for the given
1119 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001120 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001121 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001122 const ConstantVector &Methods);
1123 /// EmitIvarList - Emit the ivar list for the given
1124 /// implementation. If ForClass is true the list of class ivars
1125 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1126 /// interface ivars will be emitted. The return value has type
1127 /// IvarListnfABIPtrTy.
1128 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001129
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001130 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001131 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001132 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001133
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001134 /// GetOrEmitProtocol - Get the protocol object for the given
1135 /// declaration, emitting it if necessary. The return value has type
1136 /// ProtocolPtrTy.
1137 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001139 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1140 /// object for the given declaration, emitting it if needed. These
1141 /// forward references will be filled in with empty bodies if no
1142 /// definition is seen. The return value has type ProtocolPtrTy.
1143 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001144
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001145 /// EmitProtocolList - Generate the list of referenced
1146 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001147 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001148 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001149 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001150
John McCall9e8bb002011-05-14 03:10:52 +00001151 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1152 ReturnValueSlot Return,
1153 QualType ResultType,
1154 Selector Sel,
1155 llvm::Value *Receiver,
1156 QualType Arg0Ty,
1157 bool IsSuper,
1158 const CallArgList &CallArgs,
1159 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001160
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001161 /// GetClassGlobal - Return the global variable for the Objective-C
1162 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001163 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001164
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001165 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001166 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001167 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001168 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001169
1170 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1171 IdentifierInfo *II);
1172
1173 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001174
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001175 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1176 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001177 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1178 const ObjCInterfaceDecl *ID);
1179
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001180 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1181 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001183 const ObjCInterfaceDecl *ID);
1184
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001185 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1186 /// the given ivar.
1187 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001188 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001189 const ObjCInterfaceDecl *ID,
1190 const ObjCIvarDecl *Ivar);
1191
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001192 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1193 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001194 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1195 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001196
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001197 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001198 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001199 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001200 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001201
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001203 return "OBJC_METACLASS_$_";
1204 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001205
Daniel Dunbar15894b72009-04-07 05:48:37 +00001206 const char *getClassSymbolPrefix() const {
1207 return "OBJC_CLASS_$_";
1208 }
1209
Daniel Dunbar961202372009-05-03 12:57:56 +00001210 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001211 uint32_t &InstanceStart,
1212 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001213
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001214 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001215 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001216 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1217 return CGM.getContext().Selectors.getSelector(0, &II);
1218 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001219
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001220 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001221 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1222 return CGM.getContext().Selectors.getSelector(1, &II);
1223 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001224
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001225 /// ImplementationIsNonLazy - Check whether the given category or
1226 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001227 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001228
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001229public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001230 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001231 // FIXME. All stubs for now!
1232 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001233
Fariborz Jahanian71394042009-01-23 23:53:38 +00001234 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001235 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001236 QualType ResultType,
1237 Selector Sel,
1238 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001239 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001240 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001241 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
1243 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001244 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001245 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001246 QualType ResultType,
1247 Selector Sel,
1248 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001249 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001250 llvm::Value *Receiver,
1251 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001252 const CallArgList &CallArgs,
1253 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001254
Fariborz Jahanian71394042009-01-23 23:53:38 +00001255 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001256 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001257
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001258 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1259 bool lvalue = false)
1260 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001261
1262 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1263 /// untyped one.
1264 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1265 const ObjCMethodDecl *Method)
1266 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001268 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001269
Fariborz Jahanian71394042009-01-23 23:53:38 +00001270 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001271 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001272 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001274 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001275
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001276 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001277 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001278 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279 virtual llvm::Constant *GetPropertySetFunction() {
1280 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001281 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001282
David Chisnall168b80f2010-12-26 22:13:16 +00001283 virtual llvm::Constant *GetSetStructFunction() {
1284 return ObjCTypes.getCopyStructFn();
1285 }
1286 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001287 return ObjCTypes.getCopyStructFn();
1288 }
1289
Chris Lattnerd4808922009-03-22 21:03:39 +00001290 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001291 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001292 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001293
John McCallbd309292010-07-06 01:34:17 +00001294 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1295 const ObjCAtTryStmt &S);
1296 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1297 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001298 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001299 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001300 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001301 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001302 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001303 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001304 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001305 llvm::Value *src, llvm::Value *dest,
1306 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001307 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001308 llvm::Value *src, llvm::Value *dest,
1309 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001310 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001311 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001312 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1313 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001314 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001315 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1316 QualType ObjectTy,
1317 llvm::Value *BaseValue,
1318 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001319 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001320 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001321 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001322 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001323};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001324
John McCall5880fb82011-05-14 21:12:11 +00001325/// A helper class for performing the null-initialization of a return
1326/// value.
1327struct NullReturnState {
1328 llvm::BasicBlock *NullBB;
1329
1330 NullReturnState() : NullBB(0) {}
1331
1332 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1333 // Make blocks for the null-init and call edges.
1334 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1335 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1336
1337 // Check for a null receiver and, if there is one, jump to the
1338 // null-init test.
1339 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1340 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1341
1342 // Otherwise, start performing the call.
1343 CGF.EmitBlock(callBB);
1344 }
1345
1346 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1347 if (!NullBB) return result;
1348
1349 // Finish the call path.
1350 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1351 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1352
1353 // Emit the null-init block and perform the null-initialization there.
1354 CGF.EmitBlock(NullBB);
1355 assert(result.isAggregate() && "null init of non-aggregate result?");
1356 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1357
1358 // Jump to the continuation block.
1359 CGF.EmitBlock(contBB);
1360
1361 return result;
1362 }
1363};
1364
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001365} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001366
1367/* *** Helper Functions *** */
1368
1369/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001370static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001371 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001372 unsigned idx0,
1373 unsigned idx1) {
1374 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001375 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1376 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001377 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001378 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001379}
1380
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001381/// hasObjCExceptionAttribute - Return true if this class or any super
1382/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001383static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001384 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001385 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001386 return true;
1387 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001388 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001389 return false;
1390}
1391
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001392/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001394CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001395 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001396 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001397 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001398}
1399
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001400/// GetClass - Return a reference to the class for the given interface
1401/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001402llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001403 const ObjCInterfaceDecl *ID) {
1404 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001405}
1406
1407/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001408llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1409 bool lval) {
1410 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001411}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001412llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001413 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001414 return EmitSelector(Builder, Method->getSelector());
1415}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001416
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001417llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001418 if (T->isObjCIdType() ||
1419 T->isObjCQualifiedIdType()) {
1420 return CGM.GetAddrOfRTTIDescriptor(
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001421 CGM.getContext().ObjCIdRedefinitionType, /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001422 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001423 if (T->isObjCClassType() ||
1424 T->isObjCQualifiedClassType()) {
1425 return CGM.GetAddrOfRTTIDescriptor(
1426 CGM.getContext().ObjCClassRedefinitionType, /*ForEH=*/true);
1427 }
1428 if (T->isObjCObjectPointerType())
1429 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1430
John McCall2ca705e2010-07-24 00:37:23 +00001431 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1432 return 0;
1433}
1434
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001435/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001436/*
1437 struct __builtin_CFString {
1438 const int *isa; // point to __CFConstantStringClassReference
1439 int flags;
1440 const char *str;
1441 long length;
1442 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001443*/
1444
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001445/// or Generate a constant NSString object.
1446/*
1447 struct __builtin_NSString {
1448 const int *isa; // point to __NSConstantStringClassReference
1449 const char *str;
1450 unsigned int length;
1451 };
1452*/
1453
Fariborz Jahanian71394042009-01-23 23:53:38 +00001454llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001455 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001456 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1457 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001458 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001459}
1460
1461/// Generates a message send where the super is the receiver. This is
1462/// a message send to self with special delivery semantics indicating
1463/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001464CodeGen::RValue
1465CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001466 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001467 QualType ResultType,
1468 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001469 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001470 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001471 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001472 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001473 const CodeGen::CallArgList &CallArgs,
1474 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001475 // Create and init a super structure; this is a (receiver, class)
1476 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001477 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001478 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001479 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001480 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001481 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001482 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001483
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001484 // If this is a class message the metaclass is passed as the target.
1485 llvm::Value *Target;
1486 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001487 if (isCategoryImpl) {
1488 // Message sent to 'super' in a class method defined in a category
1489 // implementation requires an odd treatment.
1490 // If we are in a class method, we must retrieve the
1491 // _metaclass_ for the current class, pointed at by
1492 // the class's "isa" pointer. The following assumes that
1493 // isa" is the first ivar in a class (which it must be).
1494 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1495 Target = CGF.Builder.CreateStructGEP(Target, 0);
1496 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001497 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001498 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1499 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1500 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1501 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001502 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001503 }
1504 else if (isCategoryImpl)
1505 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1506 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001507 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1508 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1509 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001510 }
Mike Stump18bb9282009-05-16 07:57:57 +00001511 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1512 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001513 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001514 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001515 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001516 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001517 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001518 return EmitMessageSend(CGF, Return, ResultType,
1519 EmitSelector(CGF.Builder, Sel),
1520 ObjCSuper, ObjCTypes.SuperPtrCTy,
1521 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001522}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001523
1524/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001525CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001526 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001527 QualType ResultType,
1528 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001529 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001530 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001531 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001532 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001533 return EmitMessageSend(CGF, Return, ResultType,
1534 EmitSelector(CGF.Builder, Sel),
1535 Receiver, CGF.getContext().getObjCIdType(),
1536 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001537}
1538
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001539CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001540CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1541 ReturnValueSlot Return,
1542 QualType ResultType,
1543 llvm::Value *Sel,
1544 llvm::Value *Arg0,
1545 QualType Arg0Ty,
1546 bool IsSuper,
1547 const CallArgList &CallArgs,
1548 const ObjCMethodDecl *Method,
1549 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001550 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001551 if (!IsSuper)
1552 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman43dca6a2011-05-02 17:57:46 +00001553 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1554 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001555 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001556
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001557 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001558 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001559 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001560 const llvm::FunctionType *FTy =
1561 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001562
Anders Carlsson280e61f12010-06-21 20:59:55 +00001563 if (Method)
1564 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1565 CGM.getContext().getCanonicalType(ResultType) &&
1566 "Result type mismatch!");
1567
John McCall5880fb82011-05-14 21:12:11 +00001568 NullReturnState nullReturn;
1569
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001570 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001571 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001572 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001573 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001574 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001575 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1576 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1577 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001578 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001579 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001580 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001581 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001582 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001583 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1584 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001585}
1586
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001587static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1588 if (FQT.isObjCGCStrong())
1589 return Qualifiers::Strong;
1590
John McCall31168b02011-06-15 23:02:42 +00001591 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001592 return Qualifiers::Weak;
1593
1594 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1595 return Qualifiers::Strong;
1596
1597 if (const PointerType *PT = FQT->getAs<PointerType>())
1598 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1599
1600 return Qualifiers::GCNone;
1601}
1602
John McCall351762c2011-02-07 10:33:21 +00001603llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1604 const CGBlockInfo &blockInfo) {
1605 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001606 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001607
John McCall31168b02011-06-15 23:02:42 +00001608 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
1609 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001610 return nullPtr;
1611
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001612 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001613 SkipIvars.clear();
1614 IvarsInfo.clear();
1615 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1616 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1617
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001618 // __isa is the first field in block descriptor and must assume by runtime's
1619 // convention that it is GC'able.
1620 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001621
1622 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1623
1624 // Calculate the basic layout of the block structure.
1625 const llvm::StructLayout *layout =
1626 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1627
1628 // Ignore the optional 'this' capture: C++ objects are not assumed
1629 // to be GC'ed.
1630
1631 // Walk the captured variables.
1632 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1633 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1634 const VarDecl *variable = ci->getVariable();
1635 QualType type = variable->getType();
1636
1637 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1638
1639 // Ignore constant captures.
1640 if (capture.isConstant()) continue;
1641
1642 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1643
1644 // __block variables are passed by their descriptor address.
1645 if (ci->isByRef()) {
1646 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001647 continue;
John McCall351762c2011-02-07 10:33:21 +00001648 }
1649
1650 assert(!type->isArrayType() && "array variable should not be caught");
1651 if (const RecordType *record = type->getAs<RecordType>()) {
1652 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001653 continue;
1654 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001655
John McCall351762c2011-02-07 10:33:21 +00001656 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1657 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1658
1659 if (GCAttr == Qualifiers::Strong)
1660 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1661 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001662 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001663 SkipIvars.push_back(GC_IVAR(fieldOffset,
1664 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001665 }
1666
1667 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001668 return nullPtr;
1669
1670 // Sort on byte position; captures might not be allocated in order,
1671 // and unions can do funny things.
1672 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1673 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001674
1675 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001676 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001677 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1678 printf("\n block variable layout for block: ");
1679 const unsigned char *s = (unsigned char*)BitMap.c_str();
1680 for (unsigned i = 0; i < BitMap.size(); i++)
1681 if (!(s[i] & 0xf0))
1682 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1683 else
1684 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1685 printf("\n");
1686 }
1687
1688 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001689}
1690
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001691llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001692 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001693 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001694 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001695 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1696
Owen Andersonade90fd2009-07-29 18:54:39 +00001697 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001698 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001699}
1700
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001701void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001702 // FIXME: We shouldn't need this, the protocol decl should contain enough
1703 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001704 DefinedProtocols.insert(PD->getIdentifier());
1705
1706 // If we have generated a forward reference to this protocol, emit
1707 // it now. Otherwise do nothing, the protocol objects are lazily
1708 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001709 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001710 GetOrEmitProtocol(PD);
1711}
1712
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001713llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001714 if (DefinedProtocols.count(PD->getIdentifier()))
1715 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001716
Daniel Dunbarc475d422008-10-29 22:36:39 +00001717 return GetOrEmitProtocolRef(PD);
1718}
1719
Daniel Dunbarb036db82008-08-13 03:21:16 +00001720/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001721// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1722struct _objc_protocol {
1723struct _objc_protocol_extension *isa;
1724char *protocol_name;
1725struct _objc_protocol_list *protocol_list;
1726struct _objc__method_prototype_list *instance_methods;
1727struct _objc__method_prototype_list *class_methods
1728};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001729
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001730See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001731*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001732llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1733 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1734
1735 // Early exit if a defining object has already been generated.
1736 if (Entry && Entry->hasInitializer())
1737 return Entry;
1738
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001739 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001740 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001741 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1742
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001743 // Construct method lists.
1744 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1745 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001746 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001747 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001748 ObjCMethodDecl *MD = *i;
1749 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001750 if (!C)
1751 return GetOrEmitProtocolRef(PD);
1752
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001753 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1754 OptInstanceMethods.push_back(C);
1755 } else {
1756 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001757 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001758 }
1759
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001760 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001761 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001762 ObjCMethodDecl *MD = *i;
1763 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001764 if (!C)
1765 return GetOrEmitProtocolRef(PD);
1766
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001767 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1768 OptClassMethods.push_back(C);
1769 } else {
1770 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001771 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001772 }
1773
Daniel Dunbarb036db82008-08-13 03:21:16 +00001774 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001775 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001776 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001777 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001778 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001779 PD->protocol_begin(),
1780 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001781 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001782 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001783 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1784 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001785 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001786 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001787 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1788 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001789 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001790 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001791
Daniel Dunbarb036db82008-08-13 03:21:16 +00001792 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001793 // Already created, fix the linkage and update the initializer.
1794 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001795 Entry->setInitializer(Init);
1796 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001797 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001798 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001799 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001800 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001801 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001802 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001803 // FIXME: Is this necessary? Why only for protocol?
1804 Entry->setAlignment(4);
1805 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001806 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001807
1808 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001809}
1810
Daniel Dunbarc475d422008-10-29 22:36:39 +00001811llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001812 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1813
1814 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001815 // We use the initializer as a marker of whether this is a forward
1816 // reference or not. At module finalization we add the empty
1817 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001818 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001819 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001820 llvm::GlobalValue::ExternalLinkage,
1821 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001822 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001823 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001824 // FIXME: Is this necessary? Why only for protocol?
1825 Entry->setAlignment(4);
1826 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001827
Daniel Dunbarb036db82008-08-13 03:21:16 +00001828 return Entry;
1829}
1830
1831/*
1832 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001833 uint32_t size;
1834 struct objc_method_description_list *optional_instance_methods;
1835 struct objc_method_description_list *optional_class_methods;
1836 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001837 };
1838*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001839llvm::Constant *
1840CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1841 const ConstantVector &OptInstanceMethods,
1842 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001843 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001844 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001845 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001846 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001847 Values[1] =
1848 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001849 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001850 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1851 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001852 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001853 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001854 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1855 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001856 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001857 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001859 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001860 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001862 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001864 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001865 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001866
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001867 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001868 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001869 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001870 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871}
1872
1873/*
1874 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001875 struct objc_protocol_list *next;
1876 long count;
1877 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001878 };
1879*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001880llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001881CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001882 ObjCProtocolDecl::protocol_iterator begin,
1883 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001884 std::vector<llvm::Constant*> ProtocolRefs;
1885
Daniel Dunbardec75f82008-08-21 21:57:41 +00001886 for (; begin != end; ++begin)
1887 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001888
1889 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001890 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001891 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001892
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001893 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001894 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001895
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001896 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001897 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001898 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001899 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001900 ProtocolRefs.size() - 1);
1901 Values[2] =
1902 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1903 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001904 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001905
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001906 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001907 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001908 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001909 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001910 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001911}
1912
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001913void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1914 std::vector<llvm::Constant*> &Properties,
1915 const Decl *Container,
1916 const ObjCProtocolDecl *PROTO,
1917 const ObjCCommonTypesHelper &ObjCTypes) {
1918 std::vector<llvm::Constant*> Prop(2);
1919 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1920 E = PROTO->protocol_end(); P != E; ++P)
1921 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1922 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1923 E = PROTO->prop_end(); I != E; ++I) {
1924 const ObjCPropertyDecl *PD = *I;
1925 if (!PropertySet.insert(PD->getIdentifier()))
1926 continue;
1927 Prop[0] = GetPropertyName(PD->getIdentifier());
1928 Prop[1] = GetPropertyTypeString(PD, Container);
1929 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1930 }
1931}
1932
Daniel Dunbarb036db82008-08-13 03:21:16 +00001933/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001934 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001935 const char * const name;
1936 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001937 };
1938
1939 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940 uint32_t entsize; // sizeof (struct _objc_property)
1941 uint32_t prop_count;
1942 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001943 };
1944*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001945llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001946 const Decl *Container,
1947 const ObjCContainerDecl *OCD,
1948 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001949 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001950 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001951 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1952 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001953 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001954 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001955 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001956 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001957 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001958 Prop));
1959 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001960 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001961 for (ObjCInterfaceDecl::all_protocol_iterator
1962 P = OID->all_referenced_protocol_begin(),
1963 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001964 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1965 ObjCTypes);
1966 }
1967 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1968 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1969 E = CD->protocol_end(); P != E; ++P)
1970 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1971 ObjCTypes);
1972 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001973
1974 // Return null for empty list.
1975 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001976 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001977
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001978 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001979 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001980 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001981 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1982 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001983 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001984 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001985 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001986 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001987
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988 llvm::GlobalVariable *GV =
1989 CreateMetadataVar(Name, Init,
1990 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001991 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001992 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001993 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001994 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001995}
1996
1997/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001998 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001999 int count;
2000 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002001 };
2002*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002003llvm::Constant *
2004CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2005 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00002006 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002007 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2008 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002009 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002010 if (!Desc[1])
2011 return 0;
2012
Owen Anderson0e0189d2009-07-27 22:29:56 +00002013 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002014 Desc);
2015}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002016
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002017llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002018 const char *Section,
2019 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002020 // Return null for empty list.
2021 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002022 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002023
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002024 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002025 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002026 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002027 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002028 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002029 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002030
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002031 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002032 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002033 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002034}
2035
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002036/*
2037 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002038 char *category_name;
2039 char *class_name;
2040 struct _objc_method_list *instance_methods;
2041 struct _objc_method_list *class_methods;
2042 struct _objc_protocol_list *protocols;
2043 uint32_t size; // <rdar://4585769>
2044 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002045 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002046*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002047void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002048 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002049
Mike Stump18bb9282009-05-16 07:57:57 +00002050 // FIXME: This is poor design, the OCD should have a pointer to the category
2051 // decl. Additionally, note that Category can be null for the @implementation
2052 // w/o an @interface case. Sema should just create one for us as it does for
2053 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002054 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002055 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002056 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002057
2058 llvm::SmallString<256> ExtName;
2059 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2060 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002061
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002062 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002064 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002065 // Instance methods should always be defined.
2066 InstanceMethods.push_back(GetMethodConstant(*i));
2067 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002068 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002069 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002070 // Class methods should always be defined.
2071 ClassMethods.push_back(GetMethodConstant(*i));
2072 }
2073
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002074 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002075 Values[0] = GetClassName(OCD->getIdentifier());
2076 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002077 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002078 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002079 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002080 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002081 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002082 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002083 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002084 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002085 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002086 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002087 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002088 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002089 Category->protocol_begin(),
2090 Category->protocol_end());
2091 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002092 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002093 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002094 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002095
2096 // If there is no category @interface then there can be no properties.
2097 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002098 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002099 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002100 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002101 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002102 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002103
Owen Anderson0e0189d2009-07-27 22:29:56 +00002104 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002105 Values);
2106
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002107 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002108 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002109 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002110 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002111 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002112 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002113 // method definition entries must be clear for next implementation.
2114 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002115}
2116
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002117// FIXME: Get from somewhere?
2118enum ClassFlags {
2119 eClassFlags_Factory = 0x00001,
2120 eClassFlags_Meta = 0x00002,
2121 // <rdr://5142207>
2122 eClassFlags_HasCXXStructors = 0x02000,
2123 eClassFlags_Hidden = 0x20000,
2124 eClassFlags_ABI2_Hidden = 0x00010,
2125 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2126};
2127
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002128/*
2129 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130 Class isa;
2131 Class super_class;
2132 const char *name;
2133 long version;
2134 long info;
2135 long instance_size;
2136 struct _objc_ivar_list *ivars;
2137 struct _objc_method_list *methods;
2138 struct _objc_cache *cache;
2139 struct _objc_protocol_list *protocols;
2140 // Objective-C 1.0 extensions (<rdr://4585769>)
2141 const char *ivar_layout;
2142 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002143 };
2144
2145 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002146*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002147void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002148 DefinedSymbols.insert(ID->getIdentifier());
2149
Chris Lattner86d7d912008-11-24 03:54:41 +00002150 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002151 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002152 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002153 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002154 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002155 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002156 Interface->all_referenced_protocol_begin(),
2157 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002158 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002159 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002160 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002161 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002162 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002163
2164 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002165 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002166 Flags |= eClassFlags_Hidden;
2167
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002168 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002169 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002170 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002171 // Instance methods should always be defined.
2172 InstanceMethods.push_back(GetMethodConstant(*i));
2173 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002174 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002175 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002176 // Class methods should always be defined.
2177 ClassMethods.push_back(GetMethodConstant(*i));
2178 }
2179
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002180 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002181 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002182 ObjCPropertyImplDecl *PID = *i;
2183
2184 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2185 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2186
2187 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2188 if (llvm::Constant *C = GetMethodConstant(MD))
2189 InstanceMethods.push_back(C);
2190 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2191 if (llvm::Constant *C = GetMethodConstant(MD))
2192 InstanceMethods.push_back(C);
2193 }
2194 }
2195
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002196 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002197 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002198 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002199 // Record a reference to the super class.
2200 LazySymbols.insert(Super->getIdentifier());
2201
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002202 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002203 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002204 ObjCTypes.ClassPtrTy);
2205 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002206 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002207 }
2208 Values[ 2] = GetClassName(ID->getIdentifier());
2209 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002210 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2211 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2212 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002213 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002214 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002215 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002216 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002217 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002218 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002219 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002220 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002221 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002222 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002223 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002224 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002225 std::string Name("\01L_OBJC_CLASS_");
2226 Name += ClassName;
2227 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2228 // Check for a forward reference.
2229 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2230 if (GV) {
2231 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2232 "Forward metaclass reference has incorrect type.");
2233 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2234 GV->setInitializer(Init);
2235 GV->setSection(Section);
2236 GV->setAlignment(4);
2237 CGM.AddUsedGlobal(GV);
2238 }
2239 else
2240 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002241 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002242 // method definition entries must be clear for next implementation.
2243 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002244}
2245
2246llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2247 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002248 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002249 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002250 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002251
John McCall457a04e2010-10-22 21:05:15 +00002252 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002254
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002255 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002256 // The isa for the metaclass is the root of the hierarchy.
2257 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2258 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2259 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002260 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002261 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002262 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002263 // The super class for the metaclass is emitted as the name of the
2264 // super class. The runtime fixes this up to point to the
2265 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002266 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002267 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002268 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002269 ObjCTypes.ClassPtrTy);
2270 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002271 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002272 }
2273 Values[ 2] = GetClassName(ID->getIdentifier());
2274 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002275 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2276 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2277 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002278 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002279 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002280 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002281 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002282 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002284 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002285 Values[ 9] = Protocols;
2286 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002287 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002288 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002289 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002290 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002291 Values);
2292
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002293 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002294 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002295
2296 // Check for a forward reference.
2297 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2298 if (GV) {
2299 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2300 "Forward metaclass reference has incorrect type.");
2301 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2302 GV->setInitializer(Init);
2303 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002304 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002305 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002306 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002307 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002309 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002310 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002311
2312 return GV;
2313}
2314
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002315llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002316 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002317
Mike Stump18bb9282009-05-16 07:57:57 +00002318 // FIXME: Should we look these up somewhere other than the module. Its a bit
2319 // silly since we only generate these while processing an implementation, so
2320 // exactly one pointer would work if know when we entered/exitted an
2321 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002322
2323 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002324 // Previously, metaclass with internal linkage may have been defined.
2325 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002326 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2327 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002328 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2329 "Forward metaclass reference has incorrect type.");
2330 return GV;
2331 } else {
2332 // Generate as an external reference to keep a consistent
2333 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002334 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002335 llvm::GlobalValue::ExternalLinkage,
2336 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002337 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002338 }
2339}
2340
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002341llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2342 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2343
2344 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2345 true)) {
2346 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2347 "Forward class metadata reference has incorrect type.");
2348 return GV;
2349 } else {
2350 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2351 llvm::GlobalValue::ExternalLinkage,
2352 0,
2353 Name);
2354 }
2355}
2356
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002357/*
2358 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002359 uint32_t size;
2360 const char *weak_ivar_layout;
2361 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002362 };
2363*/
2364llvm::Constant *
2365CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002366 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002367 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002368
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002369 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002370 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002371 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002372 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002373 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002374
2375 // Return null if no extension bits are used.
2376 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002377 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002378
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002379 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002380 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002381 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002382 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002383 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002384}
2385
2386/*
2387 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002388 char *ivar_name;
2389 char *ivar_type;
2390 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002391 };
2392
2393 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002394 int ivar_count;
2395 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002396 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002397*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002399 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002400 std::vector<llvm::Constant*> Ivars, Ivar(3);
2401
2402 // When emitting the root class GCC emits ivar entries for the
2403 // actual class structure. It is not clear if we need to follow this
2404 // behavior; for now lets try and get away with not doing it. If so,
2405 // the cleanest solution would be to make up an ObjCInterfaceDecl
2406 // for the class.
2407 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002408 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002409
2410 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002411 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002412
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002413 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002414 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002415
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002416 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2417 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002418 // Ignore unnamed bit-fields.
2419 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002420 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002421 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2422 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002423 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002424 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002425 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002426 }
2427
2428 // Return null for empty list.
2429 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002430 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002432 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002433 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002434 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002435 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002436 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002437 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002438
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002439 llvm::GlobalVariable *GV;
2440 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002441 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002442 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002443 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002444 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002445 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002446 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002447 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002448 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449}
2450
2451/*
2452 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002453 SEL method_name;
2454 char *method_types;
2455 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002456 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002457
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002458 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002459 struct objc_method_list *obsolete;
2460 int count;
2461 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002462 };
2463*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002464
2465/// GetMethodConstant - Return a struct objc_method constant for the
2466/// given method if it has been defined. The result is null if the
2467/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002468llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002469 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002470 if (!Fn)
2471 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002472
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002473 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002474 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002475 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002476 ObjCTypes.SelectorPtrTy);
2477 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002478 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002479 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002480}
2481
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002482llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002483 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002484 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485 // Return null for empty list.
2486 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002487 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002488
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002489 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002490 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002491 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002492 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002493 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002494 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002495 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002496
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002497 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002498 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002499}
2500
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002501llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002502 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002503 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002504 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002505
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002506 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002507 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002508 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002509 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002510 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002511 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002512 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002513 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002514 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002515
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002516 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002517}
2518
Daniel Dunbar30c65362009-03-09 20:09:19 +00002519llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002520CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002521 llvm::Constant *Init,
2522 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002523 unsigned Align,
2524 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002525 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002526 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002527 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002528 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002529 if (Section)
2530 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002531 if (Align)
2532 GV->setAlignment(Align);
2533 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002534 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002535 return GV;
2536}
2537
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002539 // Abuse this interface function as a place to finalize.
2540 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002541 return NULL;
2542}
2543
Chris Lattnerd4808922009-03-22 21:03:39 +00002544llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002545 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002546}
2547
Chris Lattnerd4808922009-03-22 21:03:39 +00002548llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002549 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002550}
2551
David Chisnall168b80f2010-12-26 22:13:16 +00002552llvm::Constant *CGObjCMac::GetGetStructFunction() {
2553 return ObjCTypes.getCopyStructFn();
2554}
2555llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002556 return ObjCTypes.getCopyStructFn();
2557}
2558
Chris Lattnerd4808922009-03-22 21:03:39 +00002559llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002560 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002561}
2562
John McCallbd309292010-07-06 01:34:17 +00002563void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2564 return EmitTryOrSynchronizedStmt(CGF, S);
2565}
2566
2567void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2568 const ObjCAtSynchronizedStmt &S) {
2569 return EmitTryOrSynchronizedStmt(CGF, S);
2570}
2571
John McCall65bea082010-07-21 06:59:36 +00002572namespace {
John McCallcda666c2010-07-21 07:22:38 +00002573 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002574 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002575 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002576 llvm::Value *CallTryExitVar;
2577 llvm::Value *ExceptionData;
2578 ObjCTypesHelper &ObjCTypes;
2579 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002580 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002581 llvm::Value *CallTryExitVar,
2582 llvm::Value *ExceptionData,
2583 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002584 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002585 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2586
2587 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2588 // Check whether we need to call objc_exception_try_exit.
2589 // In optimized code, this branch will always be folded.
2590 llvm::BasicBlock *FinallyCallExit =
2591 CGF.createBasicBlock("finally.call_exit");
2592 llvm::BasicBlock *FinallyNoCallExit =
2593 CGF.createBasicBlock("finally.no_call_exit");
2594 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2595 FinallyCallExit, FinallyNoCallExit);
2596
2597 CGF.EmitBlock(FinallyCallExit);
2598 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2599 ->setDoesNotThrow();
2600
2601 CGF.EmitBlock(FinallyNoCallExit);
2602
2603 if (isa<ObjCAtTryStmt>(S)) {
2604 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002605 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2606 // Save the current cleanup destination in case there's
2607 // control flow inside the finally statement.
2608 llvm::Value *CurCleanupDest =
2609 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2610
John McCall65bea082010-07-21 06:59:36 +00002611 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2612
John McCallcebe0ca2010-08-11 00:16:14 +00002613 if (CGF.HaveInsertPoint()) {
2614 CGF.Builder.CreateStore(CurCleanupDest,
2615 CGF.getNormalCleanupDestSlot());
2616 } else {
2617 // Currently, the end of the cleanup must always exist.
2618 CGF.EnsureInsertPoint();
2619 }
2620 }
John McCall65bea082010-07-21 06:59:36 +00002621 } else {
2622 // Emit objc_sync_exit(expr); as finally's sole statement for
2623 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002624 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002625 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2626 ->setDoesNotThrow();
2627 }
2628 }
2629 };
John McCall42227ed2010-07-31 23:20:56 +00002630
2631 class FragileHazards {
2632 CodeGenFunction &CGF;
2633 llvm::SmallVector<llvm::Value*, 20> Locals;
2634 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2635
2636 llvm::InlineAsm *ReadHazard;
2637 llvm::InlineAsm *WriteHazard;
2638
2639 llvm::FunctionType *GetAsmFnType();
2640
2641 void collectLocals();
2642 void emitReadHazard(CGBuilderTy &Builder);
2643
2644 public:
2645 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002646
John McCall42227ed2010-07-31 23:20:56 +00002647 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002648 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002649 };
2650}
2651
2652/// Create the fragile-ABI read and write hazards based on the current
2653/// state of the function, which is presumed to be immediately prior
2654/// to a @try block. These hazards are used to maintain correct
2655/// semantics in the face of optimization and the fragile ABI's
2656/// cavalier use of setjmp/longjmp.
2657FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2658 collectLocals();
2659
2660 if (Locals.empty()) return;
2661
2662 // Collect all the blocks in the function.
2663 for (llvm::Function::iterator
2664 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2665 BlocksBeforeTry.insert(&*I);
2666
2667 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2668
2669 // Create a read hazard for the allocas. This inhibits dead-store
2670 // optimizations and forces the values to memory. This hazard is
2671 // inserted before any 'throwing' calls in the protected scope to
2672 // reflect the possibility that the variables might be read from the
2673 // catch block if the call throws.
2674 {
2675 std::string Constraint;
2676 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2677 if (I) Constraint += ',';
2678 Constraint += "*m";
2679 }
2680
2681 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2682 }
2683
2684 // Create a write hazard for the allocas. This inhibits folding
2685 // loads across the hazard. This hazard is inserted at the
2686 // beginning of the catch path to reflect the possibility that the
2687 // variables might have been written within the protected scope.
2688 {
2689 std::string Constraint;
2690 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2691 if (I) Constraint += ',';
2692 Constraint += "=*m";
2693 }
2694
2695 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2696 }
2697}
2698
2699/// Emit a write hazard at the current location.
2700void FragileHazards::emitWriteHazard() {
2701 if (Locals.empty()) return;
2702
2703 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2704 ->setDoesNotThrow();
2705}
2706
John McCall42227ed2010-07-31 23:20:56 +00002707void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2708 assert(!Locals.empty());
2709 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2710 ->setDoesNotThrow();
2711}
2712
2713/// Emit read hazards in all the protected blocks, i.e. all the blocks
2714/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002715void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002716 if (Locals.empty()) return;
2717
2718 CGBuilderTy Builder(CGF.getLLVMContext());
2719
2720 // Iterate through all blocks, skipping those prior to the try.
2721 for (llvm::Function::iterator
2722 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2723 llvm::BasicBlock &BB = *FI;
2724 if (BlocksBeforeTry.count(&BB)) continue;
2725
2726 // Walk through all the calls in the block.
2727 for (llvm::BasicBlock::iterator
2728 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2729 llvm::Instruction &I = *BI;
2730
2731 // Ignore instructions that aren't non-intrinsic calls.
2732 // These are the only calls that can possibly call longjmp.
2733 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2734 if (isa<llvm::IntrinsicInst>(I))
2735 continue;
2736
2737 // Ignore call sites marked nounwind. This may be questionable,
2738 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2739 llvm::CallSite CS(&I);
2740 if (CS.doesNotThrow()) continue;
2741
John McCall2dd7d442010-08-04 05:59:32 +00002742 // Insert a read hazard before the call. This will ensure that
2743 // any writes to the locals are performed before making the
2744 // call. If the call throws, then this is sufficient to
2745 // guarantee correctness as long as it doesn't also write to any
2746 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002747 Builder.SetInsertPoint(&BB, BI);
2748 emitReadHazard(Builder);
2749 }
2750 }
2751}
2752
2753static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2754 if (V) S.insert(V);
2755}
2756
2757void FragileHazards::collectLocals() {
2758 // Compute a set of allocas to ignore.
2759 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2760 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2761 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2762 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2763
2764 // Collect all the allocas currently in the function. This is
2765 // probably way too aggressive.
2766 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2767 for (llvm::BasicBlock::iterator
2768 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2769 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2770 Locals.push_back(&*I);
2771}
2772
2773llvm::FunctionType *FragileHazards::GetAsmFnType() {
John McCall9dc0db22011-05-15 01:53:33 +00002774 llvm::SmallVector<const llvm::Type *, 16> tys(Locals.size());
2775 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2776 tys[i] = Locals[i]->getType();
2777 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002778}
2779
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002780/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002781
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002782 Objective-C setjmp-longjmp (sjlj) Exception Handling
2783 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002784
John McCallbd309292010-07-06 01:34:17 +00002785 A catch buffer is a setjmp buffer plus:
2786 - a pointer to the exception that was caught
2787 - a pointer to the previous exception data buffer
2788 - two pointers of reserved storage
2789 Therefore catch buffers form a stack, with a pointer to the top
2790 of the stack kept in thread-local storage.
2791
2792 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2793 objc_exception_try_exit pops the given catch buffer, which is
2794 required to be the top of the EH stack.
2795 objc_exception_throw pops the top of the EH stack, writes the
2796 thrown exception into the appropriate field, and longjmps
2797 to the setjmp buffer. It crashes the process (with a printf
2798 and an abort()) if there are no catch buffers on the stack.
2799 objc_exception_extract just reads the exception pointer out of the
2800 catch buffer.
2801
2802 There's no reason an implementation couldn't use a light-weight
2803 setjmp here --- something like __builtin_setjmp, but API-compatible
2804 with the heavyweight setjmp. This will be more important if we ever
2805 want to implement correct ObjC/C++ exception interactions for the
2806 fragile ABI.
2807
2808 Note that for this use of setjmp/longjmp to be correct, we may need
2809 to mark some local variables volatile: if a non-volatile local
2810 variable is modified between the setjmp and the longjmp, it has
2811 indeterminate value. For the purposes of LLVM IR, it may be
2812 sufficient to make loads and stores within the @try (to variables
2813 declared outside the @try) volatile. This is necessary for
2814 optimized correctness, but is not currently being done; this is
2815 being tracked as rdar://problem/8160285
2816
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002817 The basic framework for a @try-catch-finally is as follows:
2818 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002819 objc_exception_data d;
2820 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002821 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002822
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002823 objc_exception_try_enter(&d);
2824 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002825 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002826 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002827 // exception path
2828 id _caught = objc_exception_extract(&d);
2829
2830 // enter new try scope for handlers
2831 if (!setjmp(d.jmp_buf)) {
2832 ... match exception and execute catch blocks ...
2833
2834 // fell off end, rethrow.
2835 _rethrow = _caught;
2836 ... jump-through-finally to finally_rethrow ...
2837 } else {
2838 // exception in catch block
2839 _rethrow = objc_exception_extract(&d);
2840 _call_try_exit = false;
2841 ... jump-through-finally to finally_rethrow ...
2842 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002843 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002844 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002845
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002846 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002847 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002848 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002849
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002850 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002851 ... dispatch to finally destination ...
2852
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002853 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002854 objc_exception_throw(_rethrow);
2855
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002856 finally_end:
2857 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002858
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002859 This framework differs slightly from the one gcc uses, in that gcc
2860 uses _rethrow to determine if objc_exception_try_exit should be called
2861 and if the object should be rethrown. This breaks in the face of
2862 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002864 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002865
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002866 - If there are no catch blocks, then we avoid emitting the second
2867 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002868
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2870 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002871
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002872 - FIXME: If there is no @finally block we can do a few more
2873 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002874
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002875 Rethrows and Jumps-Through-Finally
2876 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002877
John McCallbd309292010-07-06 01:34:17 +00002878 '@throw;' is supported by pushing the currently-caught exception
2879 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002880
John McCallbd309292010-07-06 01:34:17 +00002881 Branches through the @finally block are handled with an ordinary
2882 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2883 exceptions are not compatible with C++ exceptions, and this is
2884 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002885
John McCallbd309292010-07-06 01:34:17 +00002886 @synchronized(expr) { stmt; } is emitted as if it were:
2887 id synch_value = expr;
2888 objc_sync_enter(synch_value);
2889 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002890*/
2891
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002892void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2893 const Stmt &S) {
2894 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002895
2896 // A destination for the fall-through edges of the catch handlers to
2897 // jump to.
2898 CodeGenFunction::JumpDest FinallyEnd =
2899 CGF.getJumpDestInCurrentScope("finally.end");
2900
2901 // A destination for the rethrow edge of the catch handlers to jump
2902 // to.
2903 CodeGenFunction::JumpDest FinallyRethrow =
2904 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002905
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002906 // For @synchronized, call objc_sync_enter(sync.expr). The
2907 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002908 // @synchronized. We can't avoid a temp here because we need the
2909 // value to be preserved. If the backend ever does liveness
2910 // correctly after setjmp, this will be unnecessary.
2911 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002912 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002913 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002914 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2915 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002916 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2917 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002918
2919 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2920 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002921 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002922
John McCall2dd7d442010-08-04 05:59:32 +00002923 // Allocate memory for the setjmp buffer. This needs to be kept
2924 // live throughout the try and catch blocks.
2925 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2926 "exceptiondata.ptr");
2927
John McCall42227ed2010-07-31 23:20:56 +00002928 // Create the fragile hazards. Note that this will not capture any
2929 // of the allocas required for exception processing, but will
2930 // capture the current basic block (which extends all the way to the
2931 // setjmp call) as "before the @try".
2932 FragileHazards Hazards(CGF);
2933
John McCallbd309292010-07-06 01:34:17 +00002934 // Create a flag indicating whether the cleanup needs to call
2935 // objc_exception_try_exit. This is true except when
2936 // - no catches match and we're branching through the cleanup
2937 // just to rethrow the exception, or
2938 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002939 // The setjmp-safety rule here is that we should always store to this
2940 // variable in a place that dominates the branch through the cleanup
2941 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002942 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002943 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002944
John McCall9916e3f2010-10-04 23:42:51 +00002945 // A slot containing the exception to rethrow. Only needed when we
2946 // have both a @catch and a @finally.
2947 llvm::Value *PropagatingExnVar = 0;
2948
John McCallbd309292010-07-06 01:34:17 +00002949 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002950 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002951 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002952 CallTryExitVar,
2953 ExceptionData,
2954 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002955
2956 // Enter a try block:
2957 // - Call objc_exception_try_enter to push ExceptionData on top of
2958 // the EH stack.
2959 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2960 ->setDoesNotThrow();
2961
2962 // - Call setjmp on the exception data buffer.
2963 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2964 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2965 llvm::Value *SetJmpBuffer =
2966 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2967 llvm::CallInst *SetJmpResult =
2968 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2969 SetJmpResult->setDoesNotThrow();
2970
2971 // If setjmp returned 0, enter the protected block; otherwise,
2972 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002973 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2974 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002975 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002976 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2977 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002978
John McCallbd309292010-07-06 01:34:17 +00002979 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002980 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002981 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002982 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002983 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002984
2985 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002986
John McCallbd309292010-07-06 01:34:17 +00002987 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002988 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002989
John McCall42227ed2010-07-31 23:20:56 +00002990 // Don't optimize loads of the in-scope locals across this point.
2991 Hazards.emitWriteHazard();
2992
John McCallbd309292010-07-06 01:34:17 +00002993 // For a @synchronized (or a @try with no catches), just branch
2994 // through the cleanup to the rethrow block.
2995 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2996 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002997 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002998 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002999
3000 // Otherwise, we have to match against the caught exceptions.
3001 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003002 // Retrieve the exception object. We may emit multiple blocks but
3003 // nothing can cross this so the value is already in SSA form.
3004 llvm::CallInst *Caught =
3005 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3006 ExceptionData, "caught");
3007 Caught->setDoesNotThrow();
3008
John McCallbd309292010-07-06 01:34:17 +00003009 // Push the exception to rethrow onto the EH value stack for the
3010 // benefit of any @throws in the handlers.
3011 CGF.ObjCEHValueStack.push_back(Caught);
3012
Douglas Gregor96c79492010-04-23 22:50:49 +00003013 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003014
John McCall2dd7d442010-08-04 05:59:32 +00003015 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003016
John McCall2dd7d442010-08-04 05:59:32 +00003017 llvm::BasicBlock *CatchBlock = 0;
3018 llvm::BasicBlock *CatchHandler = 0;
3019 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003020 // Save the currently-propagating exception before
3021 // objc_exception_try_enter clears the exception slot.
3022 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3023 "propagating_exception");
3024 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3025
John McCall2dd7d442010-08-04 05:59:32 +00003026 // Enter a new exception try block (in case a @catch block
3027 // throws an exception).
3028 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3029 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003030
John McCall2dd7d442010-08-04 05:59:32 +00003031 llvm::CallInst *SetJmpResult =
3032 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3033 "setjmp.result");
3034 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003035
John McCall2dd7d442010-08-04 05:59:32 +00003036 llvm::Value *Threw =
3037 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3038
3039 CatchBlock = CGF.createBasicBlock("catch");
3040 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3041 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3042
3043 CGF.EmitBlock(CatchBlock);
3044 }
3045
3046 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003047
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003048 // Handle catch list. As a special case we check if everything is
3049 // matched and avoid generating code for falling off the end if
3050 // so.
3051 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003052 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3053 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003054
Douglas Gregor46a572b2010-04-26 16:46:50 +00003055 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003056 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003057
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003058 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003059 if (!CatchParam) {
3060 AllMatched = true;
3061 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003062 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003063
John McCallbd309292010-07-06 01:34:17 +00003064 // catch(id e) always matches under this ABI, since only
3065 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003066 // FIXME: For the time being we also match id<X>; this should
3067 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003068 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003069 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003070 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003071
John McCallbd309292010-07-06 01:34:17 +00003072 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003073 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003074 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3075
Anders Carlsson9396a892008-09-11 09:15:33 +00003076 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003077 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003078 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003079
3080 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003081 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003082 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003083
Anders Carlsson9396a892008-09-11 09:15:33 +00003084 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003085
3086 // The scope of the catch variable ends right here.
3087 CatchVarCleanups.ForceCleanup();
3088
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003089 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003090 break;
3091 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003092
Steve Naroff7cae42b2009-07-10 23:34:53 +00003093 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003094 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003095
3096 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003097 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3098 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003099
3100 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003101 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003102
John McCallbd309292010-07-06 01:34:17 +00003103 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003104 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3105 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003106 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003107
John McCallbd309292010-07-06 01:34:17 +00003108 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3109 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003110
3111 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003112 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003113
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003114 // Emit the @catch block.
3115 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003116
3117 // Collect any cleanups for the catch variable. The scope lasts until
3118 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003119 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003120
John McCall1c9c3fd2010-10-15 04:57:14 +00003121 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003122 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003123
John McCallbd309292010-07-06 01:34:17 +00003124 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003125 llvm::Value *Tmp =
3126 CGF.Builder.CreateBitCast(Caught,
3127 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003128 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003129 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003130
Anders Carlsson9396a892008-09-11 09:15:33 +00003131 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // We're done with the catch variable.
3134 CatchVarCleanups.ForceCleanup();
3135
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003136 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003137
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003138 CGF.EmitBlock(NextCatchBlock);
3139 }
3140
John McCallbd309292010-07-06 01:34:17 +00003141 CGF.ObjCEHValueStack.pop_back();
3142
John McCall2dd7d442010-08-04 05:59:32 +00003143 // If nothing wanted anything to do with the caught exception,
3144 // kill the extract call.
3145 if (Caught->use_empty())
3146 Caught->eraseFromParent();
3147
3148 if (!AllMatched)
3149 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3150
3151 if (HasFinally) {
3152 // Emit the exception handler for the @catch blocks.
3153 CGF.EmitBlock(CatchHandler);
3154
3155 // In theory we might now need a write hazard, but actually it's
3156 // unnecessary because there's no local-accessing code between
3157 // the try's write hazard and here.
3158 //Hazards.emitWriteHazard();
3159
John McCall9916e3f2010-10-04 23:42:51 +00003160 // Extract the new exception and save it to the
3161 // propagating-exception slot.
3162 assert(PropagatingExnVar);
3163 llvm::CallInst *NewCaught =
3164 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3165 ExceptionData, "caught");
3166 NewCaught->setDoesNotThrow();
3167 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3168
John McCall2dd7d442010-08-04 05:59:32 +00003169 // Don't pop the catch handler; the throw already did.
3170 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003171 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003172 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003173 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174
John McCall42227ed2010-07-31 23:20:56 +00003175 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003176 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003177
John McCallbd309292010-07-06 01:34:17 +00003178 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003179 CGF.Builder.restoreIP(TryFallthroughIP);
3180 if (CGF.HaveInsertPoint())
3181 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003182 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003183 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003184
John McCallbd309292010-07-06 01:34:17 +00003185 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003186 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003187 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003188 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003189 // If we have a propagating-exception variable, check it.
3190 llvm::Value *PropagatingExn;
3191 if (PropagatingExnVar) {
3192 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003193
John McCall9916e3f2010-10-04 23:42:51 +00003194 // Otherwise, just look in the buffer for the exception to throw.
3195 } else {
3196 llvm::CallInst *Caught =
3197 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3198 ExceptionData);
3199 Caught->setDoesNotThrow();
3200 PropagatingExn = Caught;
3201 }
3202
3203 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003204 ->setDoesNotThrow();
3205 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003206 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003207
John McCall42227ed2010-07-31 23:20:56 +00003208 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003209}
3210
3211void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003212 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003213 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214
Anders Carlssone005aa12008-09-09 16:16:55 +00003215 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3216 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003217 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003218 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3219 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003220 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003221 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003222 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003223 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003224
John McCallbd309292010-07-06 01:34:17 +00003225 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3226 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003227 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003228
3229 // Clear the insertion point to indicate we are in unreachable code.
3230 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003231}
3232
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003233/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003234/// object: objc_read_weak (id *src)
3235///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003236llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003237 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003238 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003239 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3240 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3241 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003242 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003243 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003244 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003245 return read_weak;
3246}
3247
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003248/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3249/// objc_assign_weak (id src, id *dst)
3250///
3251void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003252 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003253 const llvm::Type * SrcTy = src->getType();
3254 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003255 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003256 assert(Size <= 8 && "does not support size > 8");
3257 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003258 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003259 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3260 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003261 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3262 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003263 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003264 src, dst, "weakassign");
3265 return;
3266}
3267
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003268/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3269/// objc_assign_global (id src, id *dst)
3270///
3271void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003272 llvm::Value *src, llvm::Value *dst,
3273 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003274 const llvm::Type * SrcTy = src->getType();
3275 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003276 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003277 assert(Size <= 8 && "does not support size > 8");
3278 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003279 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003280 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3281 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003282 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3283 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003284 if (!threadlocal)
3285 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3286 src, dst, "globalassign");
3287 else
3288 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3289 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003290 return;
3291}
3292
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003293/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003294/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003295///
3296void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003297 llvm::Value *src, llvm::Value *dst,
3298 llvm::Value *ivarOffset) {
3299 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003300 const llvm::Type * SrcTy = src->getType();
3301 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003302 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003303 assert(Size <= 8 && "does not support size > 8");
3304 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003305 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003306 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3307 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003308 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3309 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003310 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3311 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003312 return;
3313}
3314
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003315/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3316/// objc_assign_strongCast (id src, id *dst)
3317///
3318void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003319 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003320 const llvm::Type * SrcTy = src->getType();
3321 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003322 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003323 assert(Size <= 8 && "does not support size > 8");
3324 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003325 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003326 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3327 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003328 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3329 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003330 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003331 src, dst, "weakassign");
3332 return;
3333}
3334
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003335void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003336 llvm::Value *DestPtr,
3337 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003338 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003339 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3340 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003341 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003342 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003343 return;
3344}
3345
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003346/// EmitObjCValueForIvar - Code Gen for ivar reference.
3347///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003348LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3349 QualType ObjectTy,
3350 llvm::Value *BaseValue,
3351 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003352 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003353 const ObjCInterfaceDecl *ID =
3354 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003355 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3356 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003357}
3358
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003359llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003360 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003361 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003362 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003363 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003364 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3365 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003366}
3367
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003368/* *** Private Interface *** */
3369
3370/// EmitImageInfo - Emit the image info marker used to encode some module
3371/// level information.
3372///
3373/// See: <rdr://4810609&4810587&4810587>
3374/// struct IMAGE_INFO {
3375/// unsigned version;
3376/// unsigned flags;
3377/// };
3378enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003379 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003380 eImageInfo_GarbageCollected = (1 << 1),
3381 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003382 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3383
Daniel Dunbar5e639272010-04-25 20:39:01 +00003384 // A flag indicating that the module has no instances of a @synthesize of a
3385 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003386 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003387};
3388
Daniel Dunbar5e639272010-04-25 20:39:01 +00003389void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003390 unsigned version = 0; // Version is unused?
3391 unsigned flags = 0;
3392
3393 // FIXME: Fix and continue?
3394 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3395 flags |= eImageInfo_GarbageCollected;
3396 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3397 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003398
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003399 // We never allow @synthesize of a superclass property.
3400 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003401
Chris Lattner5e016ae2010-06-27 07:15:29 +00003402 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3403
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003404 // Emitted as int[2];
3405 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003406 llvm::ConstantInt::get(Int32Ty, version),
3407 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003408 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003409 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003410
3411 const char *Section;
3412 if (ObjCABI == 1)
3413 Section = "__OBJC, __image_info,regular";
3414 else
3415 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003416 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003417 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003418 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003419 Section,
3420 0,
3421 true);
3422 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003423}
3424
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003425
3426// struct objc_module {
3427// unsigned long version;
3428// unsigned long size;
3429// const char *name;
3430// Symtab symtab;
3431// };
3432
3433// FIXME: Get from somewhere
3434static const int ModuleVersion = 7;
3435
3436void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003437 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003438
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003439 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003440 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3441 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003442 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003443 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003444 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003445 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003446 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003447 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003448 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003449}
3450
3451llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003452 unsigned NumClasses = DefinedClasses.size();
3453 unsigned NumCategories = DefinedCategories.size();
3454
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003455 // Return null if no symbols were defined.
3456 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003457 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003458
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003459 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003460 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003461 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003462 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3463 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003464
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003465 // The runtime expects exactly the list of defined classes followed
3466 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003467 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003468 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003469 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003470 ObjCTypes.Int8PtrTy);
3471 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003472 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003473 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003474 ObjCTypes.Int8PtrTy);
3475
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003476 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003477 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003478 NumClasses + NumCategories),
3479 Symbols);
3480
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003481 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003482
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003483 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003484 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3485 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003486 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003487 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003488}
3489
John McCall31168b02011-06-15 23:02:42 +00003490llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3491 IdentifierInfo *II) {
3492 LazySymbols.insert(II);
3493
3494 llvm::GlobalVariable *&Entry = ClassReferences[II];
3495
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003496 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003497 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003498 llvm::ConstantExpr::getBitCast(GetClassName(II),
3499 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003500 Entry =
John McCall31168b02011-06-15 23:02:42 +00003501 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3502 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3503 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003504 }
John McCall31168b02011-06-15 23:02:42 +00003505
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003506 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003507}
3508
John McCall31168b02011-06-15 23:02:42 +00003509llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3510 const ObjCInterfaceDecl *ID) {
3511 return EmitClassRefFromId(Builder, ID->getIdentifier());
3512}
3513
3514llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3515 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3516 return EmitClassRefFromId(Builder, II);
3517}
3518
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003519llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3520 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003521 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003522
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003523 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003524 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003525 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003526 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003527 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003528 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3529 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003530 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003531 }
3532
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003533 if (lvalue)
3534 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003535 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003536}
3537
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003538llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003539 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003540
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003541 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003542 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003543 llvm::ConstantArray::get(VMContext,
3544 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003545 ((ObjCABI == 2) ?
3546 "__TEXT,__objc_classname,cstring_literals" :
3547 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003548 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003549
Owen Anderson170229f2009-07-14 23:10:40 +00003550 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003551}
3552
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003553llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3554 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3555 I = MethodDefinitions.find(MD);
3556 if (I != MethodDefinitions.end())
3557 return I->second;
3558
3559 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3560 // MD isn't emitted yet because it comes from PCH.
3561 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3562 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3563 return MethodDefinitions[MD];
3564 }
3565
3566 return NULL;
3567}
3568
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003569/// GetIvarLayoutName - Returns a unique constant for the given
3570/// ivar layout bitmap.
3571llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003572 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003573 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003574}
3575
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003576void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003577 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003578 bool ForStrongLayout,
3579 bool &HasUnion) {
3580 const RecordDecl *RD = RT->getDecl();
3581 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003582 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003583 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003584 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003585 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003586
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003587 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3588 ForStrongLayout, HasUnion);
3589}
3590
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003591void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003592 const llvm::StructLayout *Layout,
3593 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003594 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003595 unsigned int BytePos, bool ForStrongLayout,
3596 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003597 bool IsUnion = (RD && RD->isUnion());
3598 uint64_t MaxUnionIvarSize = 0;
3599 uint64_t MaxSkippedUnionIvarSize = 0;
3600 FieldDecl *MaxField = 0;
3601 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003602 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003603 uint64_t MaxFieldOffset = 0;
3604 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003605 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003606 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003607
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003608 if (RecFields.empty())
3609 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003610 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3611 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003612 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
3613 FieldDecl *FirstField = RecFields[0];
3614 FirstFieldDelta =
3615 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3616 }
3617
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003618 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003619 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003620 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003621 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003622 // Note that 'i' here is actually the field index inside RD of Field,
3623 // although this dependency is hidden.
3624 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003625 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003626 } else
John McCall31168b02011-06-15 23:02:42 +00003627 FieldOffset =
3628 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003629
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003630 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003631 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003632 LastFieldBitfieldOrUnnamed = Field;
3633 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003634 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003635 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003636
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003637 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003638 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003639 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003640 if (FQT->isUnionType())
3641 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003642
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003643 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003644 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003645 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003646 continue;
3647 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003648
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003649 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003650 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003651 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003652 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003653 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003654 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003655 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3656 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003657 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003658 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003659 FQT = CArray->getElementType();
3660 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003661
3662 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003663 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003664 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003665 int OldIndex = IvarsInfo.size() - 1;
3666 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003667
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003668 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003669 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003670 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003671
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003672 // Replicate layout information for each array element. Note that
3673 // one element is already done.
3674 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003675 for (int FirstIndex = IvarsInfo.size() - 1,
3676 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003677 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003678 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3679 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3680 IvarsInfo[i].ivar_size));
3681 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3682 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3683 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003684 }
3685 continue;
3686 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003687 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003688 // At this point, we are done with Record/Union and array there of.
3689 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003690 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003691
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003692 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003693 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3694 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003695 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003696 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003697 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003698 MaxUnionIvarSize = UnionIvarSize;
3699 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003700 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003701 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003702 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003703 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003704 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003705 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003707 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3708 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003709 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003710 // FIXME: Why the asymmetry? We divide by word size in bits on other
3711 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003712 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003713 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003714 MaxSkippedUnionIvarSize = UnionIvarSize;
3715 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003716 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003717 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003718 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003719 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003720 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003721 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003722 }
3723 }
3724 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003725
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003726 if (LastFieldBitfieldOrUnnamed) {
3727 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3728 // Last field was a bitfield. Must update skip info.
3729 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3730 uint64_t BitFieldSize =
3731 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3732 GC_IVAR skivar;
3733 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3734 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3735 + ((BitFieldSize % ByteSizeInBits) != 0);
3736 SkipIvars.push_back(skivar);
3737 } else {
3738 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3739 // Last field was unnamed. Must update skip info.
3740 unsigned FieldSize
3741 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3742 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3743 FieldSize / ByteSizeInBits));
3744 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003745 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003746
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003747 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003748 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003749 MaxUnionIvarSize));
3750 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003751 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003752 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003753}
3754
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003755/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3756/// the computations and returning the layout bitmap (for ivar or blocks) in
3757/// the given argument BitMap string container. Routine reads
3758/// two containers, IvarsInfo and SkipIvars which are assumed to be
3759/// filled already by the caller.
3760llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003761 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003762 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003763
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003764 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003765 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003766 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003767 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003768 if (IvarsInfo[0].ivar_bytepos == 0) {
3769 WordsToSkip = 0;
3770 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003771 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003772 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3773 WordsToScan = IvarsInfo[0].ivar_size;
3774 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003775 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003776 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003777 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003778 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003779 // consecutive 'scanned' object pointers.
3780 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003781 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003782 // Skip over 'gc'able object pointer which lay over each other.
3783 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3784 continue;
3785 // Must skip over 1 or more words. We save current skip/scan values
3786 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003787 SKIP_SCAN SkScan;
3788 SkScan.skip = WordsToSkip;
3789 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003790 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003791
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003792 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003793 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3794 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003795 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003796 WordsToSkip = 0;
3797 WordsToScan = IvarsInfo[i].ivar_size;
3798 }
3799 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003800 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003801 SKIP_SCAN SkScan;
3802 SkScan.skip = WordsToSkip;
3803 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003804 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003805 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003806
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003807 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003808 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003809 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003810 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003811 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003812 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003813 IvarsInfo[LastIndex].ivar_bytepos +
3814 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003815 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003816 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003817 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003818 SKIP_SCAN SkScan;
3819 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3820 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003821 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003822 }
3823 }
3824 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3825 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003826 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003827 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003828 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3829 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3830 // 0xM0 followed by 0x0N detected.
3831 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3832 for (int j = i+1; j < SkipScan; j++)
3833 SkipScanIvars[j] = SkipScanIvars[j+1];
3834 --SkipScan;
3835 }
3836 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003837
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003838 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003839 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003840 unsigned char byte;
3841 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3842 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3843 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3844 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003845
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003846 // first skip big.
3847 for (unsigned int ix = 0; ix < skip_big; ix++)
3848 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003849
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003850 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003851 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003852 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003853 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003854 byte |= 0xf;
3855 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003856 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003857 byte |= scan_small;
3858 scan_small = 0;
3859 }
3860 BitMap += byte;
3861 }
3862 // next scan big
3863 for (unsigned int ix = 0; ix < scan_big; ix++)
3864 BitMap += (unsigned char)(0x0f);
3865 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003866 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003867 byte = scan_small;
3868 BitMap += byte;
3869 }
3870 }
3871 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003872 unsigned char zero = 0;
3873 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003874
3875 llvm::GlobalVariable * Entry =
3876 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3877 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003878 ((ObjCABI == 2) ?
3879 "__TEXT,__objc_classname,cstring_literals" :
3880 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003881 1, true);
3882 return getConstantGEP(VMContext, Entry, 0, 0);
3883}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003884
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003885/// BuildIvarLayout - Builds ivar layout bitmap for the class
3886/// implementation for the __strong or __weak case.
3887/// The layout map displays which words in ivar list must be skipped
3888/// and which must be scanned by GC (see below). String is built of bytes.
3889/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3890/// of words to skip and right nibble is count of words to scan. So, each
3891/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3892/// represented by a 0x00 byte which also ends the string.
3893/// 1. when ForStrongLayout is true, following ivars are scanned:
3894/// - id, Class
3895/// - object *
3896/// - __strong anything
3897///
3898/// 2. When ForStrongLayout is false, following ivars are scanned:
3899/// - __weak anything
3900///
3901llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3902 const ObjCImplementationDecl *OMD,
3903 bool ForStrongLayout) {
3904 bool hasUnion = false;
3905
3906 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
John McCall31168b02011-06-15 23:02:42 +00003907 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
3908 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003909 return llvm::Constant::getNullValue(PtrTy);
3910
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003911 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003912 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
John McCall31168b02011-06-15 23:02:42 +00003913 if (CGM.getLangOptions().ObjCAutoRefCount)
3914 CGM.getContext().ShallowCollectObjCIvars(OI, Ivars);
3915 else
3916 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003917
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003918 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003919 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3920 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3921
3922 if (RecFields.empty())
3923 return llvm::Constant::getNullValue(PtrTy);
3924
3925 SkipIvars.clear();
3926 IvarsInfo.clear();
3927
3928 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3929 if (IvarsInfo.empty())
3930 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003931 // Sort on byte position in case we encounterred a union nested in
3932 // the ivar list.
3933 if (hasUnion && !IvarsInfo.empty())
3934 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3935 if (hasUnion && !SkipIvars.empty())
3936 std::sort(SkipIvars.begin(), SkipIvars.end());
3937
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003938 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003939 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003940
3941 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003942 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003943 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003944 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003945 const unsigned char *s = (unsigned char*)BitMap.c_str();
3946 for (unsigned i = 0; i < BitMap.size(); i++)
3947 if (!(s[i] & 0xf0))
3948 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3949 else
3950 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3951 printf("\n");
3952 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003953 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003954}
3955
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003956llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003957 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3958
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003959 // FIXME: Avoid std::string copying.
3960 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003961 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003962 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003963 ((ObjCABI == 2) ?
3964 "__TEXT,__objc_methname,cstring_literals" :
3965 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003966 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003967
Owen Anderson170229f2009-07-14 23:10:40 +00003968 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003969}
3970
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003971// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003972llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003973 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3974}
3975
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003976llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003977 std::string TypeStr;
3978 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3979
3980 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003981
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003982 if (!Entry)
3983 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003984 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003985 ((ObjCABI == 2) ?
3986 "__TEXT,__objc_methtype,cstring_literals" :
3987 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003988 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003989
Owen Anderson170229f2009-07-14 23:10:40 +00003990 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003991}
3992
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003993llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003994 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00003995 if (CGM.getContext().getObjCEncodingForMethodDecl(
3996 const_cast<ObjCMethodDecl*>(D),
3997 TypeStr))
3998 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003999
4000 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4001
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004002 if (!Entry)
4003 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004004 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004005 ((ObjCABI == 2) ?
4006 "__TEXT,__objc_methtype,cstring_literals" :
4007 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004008 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004009
Owen Anderson170229f2009-07-14 23:10:40 +00004010 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004011}
4012
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004013// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004014llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004015 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004016
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004017 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004018 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004019 llvm::ConstantArray::get(VMContext,
4020 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004021 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004022 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004023
Owen Anderson170229f2009-07-14 23:10:40 +00004024 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004025}
4026
4027// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004028// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004029llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004030CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4031 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004032 std::string TypeStr;
4033 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004034 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4035}
4036
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004037void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004038 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004039 llvm::SmallVectorImpl<char> &Name) {
4040 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004041 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004042 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4043 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004044 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004045 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004046 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004047 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004048}
4049
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004050void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004051 EmitModuleInfo();
4052
Daniel Dunbarc475d422008-10-29 22:36:39 +00004053 // Emit the dummy bodies for any protocols which were referenced but
4054 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004055 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004056 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4057 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004058 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004059
Daniel Dunbarc475d422008-10-29 22:36:39 +00004060 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004061 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004062 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004063 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004064 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004065 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004066 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004067 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004068 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004069 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004070 }
4071
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004072 // Add assembler directives to add lazy undefined symbol references
4073 // for classes which are referenced but not defined. This is
4074 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004075 //
4076 // FIXME: It would be nice if we had an LLVM construct for this.
4077 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4078 llvm::SmallString<256> Asm;
4079 Asm += CGM.getModule().getModuleInlineAsm();
4080 if (!Asm.empty() && Asm.back() != '\n')
4081 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004082
Daniel Dunbard027a922009-09-07 00:20:42 +00004083 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004084 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4085 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004086 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4087 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004088 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004089 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004090 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004091 }
4092
4093 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4094 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4095 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4096 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004097
Daniel Dunbard027a922009-09-07 00:20:42 +00004098 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004099 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004100}
4101
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004102CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004103 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004104 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004105 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004106 ObjCABI = 2;
4107}
4108
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004109/* *** */
4110
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004111ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004112 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004113 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4114 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004115
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004116 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004117 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004118 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004119 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004120 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004121
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004122 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004123 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004124 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125
Mike Stump18bb9282009-05-16 07:57:57 +00004126 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4127 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004128 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004129 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004130
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004131 // I'm not sure I like this. The implicit coordination is a bit
4132 // gross. We should solve this in a reasonable fashion because this
4133 // is a pretty common task (match some runtime data structure with
4134 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004136 // FIXME: This is leaked.
4137 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004139 // struct _objc_super {
4140 // id self;
4141 // Class cls;
4142 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004143 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004144 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004145 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004146 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004147 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004148 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004149 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004150 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004151 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004152
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004153 SuperCTy = Ctx.getTagDeclType(RD);
4154 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004156 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004157 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4158
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004159 // struct _prop_t {
4160 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004162 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004163 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004164 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004165 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004166
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004167 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004168 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004169 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004170 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004171 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004172 PropertyListTy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004173 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004174 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004175 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004176 PropertyListTy);
4177 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004178 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004179
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004180 // struct _objc_method {
4181 // SEL _cmd;
4182 // char *method_type;
4183 // char *_imp;
4184 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004185 MethodTy = llvm::StructType::get(SelectorPtrTy, Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004186 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004187
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004188 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004189 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004190 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004191 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004192
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004193}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004194
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004195ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004196 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004197 // struct _objc_method_description {
4198 // SEL name;
4199 // char *types;
4200 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004201 MethodDescriptionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004202 llvm::StructType::get(SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004203 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004204 MethodDescriptionTy);
4205
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004206 // struct _objc_method_description_list {
4207 // int count;
4208 // struct _objc_method_description[1];
4209 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004210 MethodDescriptionListTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004211 llvm::StructType::get(IntTy, llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004212 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004213 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004214 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004215
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004216 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004217 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004218 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004219
Daniel Dunbarb036db82008-08-13 03:21:16 +00004220 // Protocol description structures
4221
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004222 // struct _objc_protocol_extension {
4223 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4224 // struct _objc_method_description_list *optional_instance_methods;
4225 // struct _objc_method_description_list *optional_class_methods;
4226 // struct _objc_property_list *instance_properties;
4227 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004228 ProtocolExtensionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004229 llvm::StructType::get(IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004230 MethodDescriptionListPtrTy,
4231 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004232 PropertyListPtrTy,
4233 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004235 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004236
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004237 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004238 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004239
Daniel Dunbarc475d422008-10-29 22:36:39 +00004240 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004241
Owen Andersonc36edfe2009-08-13 23:27:53 +00004242 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4243 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004244
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004245 const llvm::Type *T =
Chris Lattner845511f2011-06-18 22:49:11 +00004246 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004247 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004248 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004249 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004250 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4251
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004252 // struct _objc_protocol {
4253 // struct _objc_protocol_extension *isa;
4254 // char *protocol_name;
4255 // struct _objc_protocol **_objc_protocol_list;
4256 // struct _objc_method_description_list *instance_methods;
4257 // struct _objc_method_description_list *class_methods;
4258 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004259 T = llvm::StructType::get(ProtocolExtensionPtrTy, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004260 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004261 MethodDescriptionListPtrTy,
4262 MethodDescriptionListPtrTy,
4263 NULL);
4264 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4265
4266 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004267 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004268 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004269 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004270 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004271
4272 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004273 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004274 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004275
4276 // Class description structures
4277
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004278 // struct _objc_ivar {
4279 // char *ivar_name;
4280 // char *ivar_type;
4281 // int ivar_offset;
4282 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004283 IvarTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004284 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4285
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004286 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004287 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004288 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004289 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004290
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004291 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004292 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004293 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004294 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004295
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004296 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004297 ClassExtensionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004298 llvm::StructType::get(IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004299 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004300 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004301
Owen Andersonc36edfe2009-08-13 23:27:53 +00004302 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004303
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004304 // struct _objc_class {
4305 // Class isa;
4306 // Class super_class;
4307 // char *name;
4308 // long version;
4309 // long info;
4310 // long instance_size;
4311 // struct _objc_ivar_list *ivars;
4312 // struct _objc_method_list *methods;
4313 // struct _objc_cache *cache;
4314 // struct _objc_protocol_list *protocols;
4315 // char *ivar_layout;
4316 // struct _objc_class_ext *ext;
4317 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004318 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004319 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004320 Int8PtrTy,
4321 LongTy,
4322 LongTy,
4323 LongTy,
4324 IvarListPtrTy,
4325 MethodListPtrTy,
4326 CachePtrTy,
4327 ProtocolListPtrTy,
4328 Int8PtrTy,
4329 ClassExtensionPtrTy,
4330 NULL);
4331 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004332
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004333 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4334 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004335 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004337 // struct _objc_category {
4338 // char *category_name;
4339 // char *class_name;
4340 // struct _objc_method_list *instance_method;
4341 // struct _objc_method_list *class_method;
4342 // uint32_t size; // sizeof(struct _objc_category)
4343 // struct _objc_property_list *instance_properties;// category's @property
4344 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004345 CategoryTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4346 MethodListPtrTy, ProtocolListPtrTy,
4347 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004348 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4349
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004350 // Global metadata structures
4351
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004352 // struct _objc_symtab {
4353 // long sel_ref_cnt;
4354 // SEL *refs;
4355 // short cls_def_cnt;
4356 // short cat_def_cnt;
4357 // char *defs[cls_def_cnt + cat_def_cnt];
4358 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004359 SymtabTy = llvm::StructType::get(LongTy, SelectorPtrTy, ShortTy, ShortTy,
4360 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004361 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004362 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004363
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004364 // struct _objc_module {
4365 // long version;
4366 // long size; // sizeof(struct _objc_module)
4367 // char *name;
4368 // struct _objc_symtab* symtab;
4369 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004370 ModuleTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004371 llvm::StructType::get(LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004372 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004373
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004374
Mike Stump18bb9282009-05-16 07:57:57 +00004375 // FIXME: This is the size of the setjmp buffer and should be target
4376 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004377 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004378
Anders Carlsson9ff22482008-09-09 10:10:21 +00004379 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004380 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004381 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004382
4383 ExceptionDataTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004384 llvm::StructType::get(
Chris Lattner5e016ae2010-06-27 07:15:29 +00004385 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4386 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004387 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004389 ExceptionDataTy);
4390
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004391}
4392
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004393ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004394 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004395 // struct _method_list_t {
4396 // uint32_t entsize; // sizeof(struct _objc_method)
4397 // uint32_t method_count;
4398 // struct _objc_method method_list[method_count];
4399 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004400 MethodListnfABITy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004401 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004402 NULL);
4403 CGM.getModule().addTypeName("struct.__method_list_t",
4404 MethodListnfABITy);
4405 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004406 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004407
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004408 // struct _protocol_t {
4409 // id isa; // NULL
4410 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004411 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004412 // const struct method_list_t * const instance_methods;
4413 // const struct method_list_t * const class_methods;
4414 // const struct method_list_t *optionalInstanceMethods;
4415 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004416 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004417 // const uint32_t size; // sizeof(struct _protocol_t)
4418 // const uint32_t flags; // = 0
4419 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004420
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004421 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004422 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004423
Chris Lattner845511f2011-06-18 22:49:11 +00004424 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, Int8PtrTy,
4425 ProtocolListTyHolder->getPointerTo(),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004426 MethodListnfABIPtrTy,
4427 MethodListnfABIPtrTy,
4428 MethodListnfABIPtrTy,
4429 MethodListnfABIPtrTy,
4430 PropertyListPtrTy,
4431 IntTy,
4432 IntTy,
4433 NULL);
4434 CGM.getModule().addTypeName("struct._protocol_t",
4435 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004436
4437 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004438 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004440 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004441 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004442 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004443 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004444 ProtocolListnfABITy = llvm::StructType::get(LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004445 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004446 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004447 NULL);
4448 CGM.getModule().addTypeName("struct._objc_protocol_list",
4449 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004450 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004451 ProtocolListnfABITy);
4452
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004453 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004454 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004455
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004456 // struct _ivar_t {
4457 // unsigned long int *offset; // pointer to ivar offset location
4458 // char *name;
4459 // char *type;
4460 // uint32_t alignment;
4461 // uint32_t size;
4462 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004463 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004464 Int8PtrTy,
4465 Int8PtrTy,
4466 IntTy,
4467 IntTy,
4468 NULL);
4469 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004470
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004471 // struct _ivar_list_t {
4472 // uint32 entsize; // sizeof(struct _ivar_t)
4473 // uint32 count;
4474 // struct _iver_t list[count];
4475 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004476 IvarListnfABITy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004477 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004478 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004479 NULL);
4480 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004481
Owen Anderson9793f0e2009-07-29 22:16:19 +00004482 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004483
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004484 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004485 // uint32_t const flags;
4486 // uint32_t const instanceStart;
4487 // uint32_t const instanceSize;
4488 // uint32_t const reserved; // only when building for 64bit targets
4489 // const uint8_t * const ivarLayout;
4490 // const char *const name;
4491 // const struct _method_list_t * const baseMethods;
4492 // const struct _objc_protocol_list *const baseProtocols;
4493 // const struct _ivar_list_t *const ivars;
4494 // const uint8_t * const weakIvarLayout;
4495 // const struct _prop_list_t * const properties;
4496 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004497
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004498 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner845511f2011-06-18 22:49:11 +00004499 ClassRonfABITy = llvm::StructType::get(IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004500 IntTy,
4501 IntTy,
4502 Int8PtrTy,
4503 Int8PtrTy,
4504 MethodListnfABIPtrTy,
4505 ProtocolListnfABIPtrTy,
4506 IvarListnfABIPtrTy,
4507 Int8PtrTy,
4508 PropertyListPtrTy,
4509 NULL);
4510 CGM.getModule().addTypeName("struct._class_ro_t",
4511 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004512
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004513 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
John McCall9dc0db22011-05-15 01:53:33 +00004514 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
4515 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4516 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004518 // struct _class_t {
4519 // struct _class_t *isa;
4520 // struct _class_t * const superclass;
4521 // void *cache;
4522 // IMP *vtable;
4523 // struct class_ro_t *ro;
4524 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525
Owen Andersonc36edfe2009-08-13 23:27:53 +00004526 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004527 ClassnfABITy =
Chris Lattner845511f2011-06-18 22:49:11 +00004528 llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529 llvm::PointerType::getUnqual(ClassTyHolder),
4530 CachePtrTy,
4531 llvm::PointerType::getUnqual(ImpnfABITy),
4532 llvm::PointerType::getUnqual(ClassRonfABITy),
4533 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004534 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4535
4536 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004537 ClassnfABITy);
4538
Fariborz Jahanian71394042009-01-23 23:53:38 +00004539 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004540 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004541
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004542 // struct _category_t {
4543 // const char * const name;
4544 // struct _class_t *const cls;
4545 // const struct _method_list_t * const instance_methods;
4546 // const struct _method_list_t * const class_methods;
4547 // const struct _protocol_list_t * const protocols;
4548 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004549 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004550 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004551 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004552 MethodListnfABIPtrTy,
4553 MethodListnfABIPtrTy,
4554 ProtocolListnfABIPtrTy,
4555 PropertyListPtrTy,
4556 NULL);
4557 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004558
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004559 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004560 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4561 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004562
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004563 // MessageRefTy - LLVM for:
4564 // struct _message_ref_t {
4565 // IMP messenger;
4566 // SEL name;
4567 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004568
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004569 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004570 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004571 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004572 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004573 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004574 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004575 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004576 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004577 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004578 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004579
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004580 MessageRefCTy = Ctx.getTagDeclType(RD);
4581 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4582 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004584 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004585 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004586
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004587 // SuperMessageRefTy - LLVM for:
4588 // struct _super_message_ref_t {
4589 // SUPER_IMP messenger;
4590 // SEL name;
4591 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004592 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, SelectorPtrTy, NULL);
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004593 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004594
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004595 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004596 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004597
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004598
4599 // struct objc_typeinfo {
4600 // const void** vtable; // objc_ehtype_vtable + 2
4601 // const char* name; // c++ typeinfo string
4602 // Class cls;
4603 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004604 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004605 Int8PtrTy,
4606 ClassnfABIPtrTy,
4607 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004608 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004609 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004610}
4611
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004612llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004613 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004614
Fariborz Jahanian71394042009-01-23 23:53:38 +00004615 return NULL;
4616}
4617
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618void CGObjCNonFragileABIMac::AddModuleClassList(const
4619 std::vector<llvm::GlobalValue*>
4620 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004621 const char *SymbolName,
4622 const char *SectionName) {
4623 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 if (!NumClasses)
4626 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004627
Daniel Dunbar19573e72009-05-15 21:48:48 +00004628 std::vector<llvm::Constant*> Symbols(NumClasses);
4629 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004630 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004631 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004632 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004633 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004634 NumClasses),
4635 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004636
Daniel Dunbar19573e72009-05-15 21:48:48 +00004637 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004638 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004639 llvm::GlobalValue::InternalLinkage,
4640 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004641 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004642 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004643 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004644 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004645}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004646
Fariborz Jahanian71394042009-01-23 23:53:38 +00004647void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4648 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004649
Daniel Dunbar19573e72009-05-15 21:48:48 +00004650 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004651 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004653 "\01L_OBJC_LABEL_CLASS_$",
4654 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004655
Fariborz Jahanian67260552009-11-17 21:37:35 +00004656 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4657 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4658 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4659 continue;
4660 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004661 }
4662
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004663 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4664 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4665 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4666 continue;
4667 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4668 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004669
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004670 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004671 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4672 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004673
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004674 // Build list of all implemented category addresses in array
4675 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004676 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004677 "\01L_OBJC_LABEL_CATEGORY_$",
4678 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004679 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004680 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4681 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004682
Daniel Dunbar5e639272010-04-25 20:39:01 +00004683 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004684}
4685
John McCall9e8bb002011-05-14 03:10:52 +00004686/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4687/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004688/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004689/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004690bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4691 // At various points we've experimented with using vtable-based
4692 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004693 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4694 default:
John McCall9e8bb002011-05-14 03:10:52 +00004695 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004696 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004697 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004698 case CodeGenOptions::NonLegacy:
4699 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004700 case CodeGenOptions::Mixed:
4701 break;
4702 }
4703
4704 // If so, see whether this selector is in the white-list of things which must
4705 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004706 if (VTableDispatchMethods.empty()) {
4707 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4708 VTableDispatchMethods.insert(GetNullarySelector("class"));
4709 VTableDispatchMethods.insert(GetNullarySelector("self"));
4710 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4711 VTableDispatchMethods.insert(GetNullarySelector("length"));
4712 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004713
John McCall9e8bb002011-05-14 03:10:52 +00004714 // These are vtable-based if GC is disabled.
4715 // Optimistically use vtable dispatch for hybrid compiles.
4716 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
4717 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4718 VTableDispatchMethods.insert(GetNullarySelector("release"));
4719 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4720 }
4721
4722 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4723 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4724 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4725 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4726 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4727 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4728 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4729
4730 // These are vtable-based if GC is enabled.
4731 // Optimistically use vtable dispatch for hybrid compiles.
4732 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
4733 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4734 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4735
4736 // "countByEnumeratingWithState:objects:count"
4737 IdentifierInfo *KeyIdents[] = {
4738 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4739 &CGM.getContext().Idents.get("objects"),
4740 &CGM.getContext().Idents.get("count")
4741 };
4742 VTableDispatchMethods.insert(
4743 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4744 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004745 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004746
John McCall9e8bb002011-05-14 03:10:52 +00004747 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004748}
4749
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004750// Metadata flags
4751enum MetaDataDlags {
4752 CLS = 0x0,
4753 CLS_META = 0x1,
4754 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004755 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004756 CLS_EXCEPTION = 0x20,
4757
4758 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4759 CLS_HAS_IVAR_RELEASER = 0x40,
4760 /// class was compiled with -fobjc-arr
4761 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004762};
4763/// BuildClassRoTInitializer - generate meta-data for:
4764/// struct _class_ro_t {
4765/// uint32_t const flags;
4766/// uint32_t const instanceStart;
4767/// uint32_t const instanceSize;
4768/// uint32_t const reserved; // only when building for 64bit targets
4769/// const uint8_t * const ivarLayout;
4770/// const char *const name;
4771/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004772/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004773/// const struct _ivar_list_t *const ivars;
4774/// const uint8_t * const weakIvarLayout;
4775/// const struct _prop_list_t * const properties;
4776/// }
4777///
4778llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004779 unsigned flags,
4780 unsigned InstanceStart,
4781 unsigned InstanceSize,
4782 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004783 std::string ClassName = ID->getNameAsString();
4784 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004785
4786 if (CGM.getLangOptions().ObjCAutoRefCount)
4787 flags |= CLS_COMPILED_BY_ARC;
4788
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004789 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4790 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4791 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004792 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004793 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4794 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004795 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004796 // const struct _method_list_t * const baseMethods;
4797 std::vector<llvm::Constant*> Methods;
4798 std::string MethodListName("\01l_OBJC_$_");
4799 if (flags & CLS_META) {
4800 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004801 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004802 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004803 // Class methods should always be defined.
4804 Methods.push_back(GetMethodConstant(*i));
4805 }
4806 } else {
4807 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004808 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004809 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004810 // Instance methods should always be defined.
4811 Methods.push_back(GetMethodConstant(*i));
4812 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004813 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004814 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004815 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004816
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004817 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4818 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004819
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004820 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4821 if (llvm::Constant *C = GetMethodConstant(MD))
4822 Methods.push_back(C);
4823 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4824 if (llvm::Constant *C = GetMethodConstant(MD))
4825 Methods.push_back(C);
4826 }
4827 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004828 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004829 Values[ 5] = EmitMethodList(MethodListName,
4830 "__DATA, __objc_const", Methods);
4831
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004832 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4833 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004834 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004835 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004836 OID->all_referenced_protocol_begin(),
4837 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004838
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004839 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004840 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004841 else
4842 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004843 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4844 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004845 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004846 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004847 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004848 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4849 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004850 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004851 Values);
4852 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004853 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4854 llvm::GlobalValue::InternalLinkage,
4855 Init,
4856 (flags & CLS_META) ?
4857 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4858 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004859 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004860 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004861 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004862 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004863
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004864}
4865
4866/// BuildClassMetaData - This routine defines that to-level meta-data
4867/// for the given ClassName for:
4868/// struct _class_t {
4869/// struct _class_t *isa;
4870/// struct _class_t * const superclass;
4871/// void *cache;
4872/// IMP *vtable;
4873/// struct class_ro_t *ro;
4874/// }
4875///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004876llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004877 std::string &ClassName,
4878 llvm::Constant *IsAGV,
4879 llvm::Constant *SuperClassGV,
4880 llvm::Constant *ClassRoGV,
4881 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004882 std::vector<llvm::Constant*> Values(5);
4883 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004884 Values[1] = SuperClassGV;
4885 if (!Values[1])
4886 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004887 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4888 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4889 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004890 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004891 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004892 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4893 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004894 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004895 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004896 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004897 if (HiddenVisibility)
4898 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004899 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004900}
4901
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004902bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004903CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004904 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004905}
4906
Daniel Dunbar961202372009-05-03 12:57:56 +00004907void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004908 uint32_t &InstanceStart,
4909 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004910 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004911 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004912
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004913 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004914 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004915
4916 // If there are no fields, the start is the same as the end.
4917 if (!RL.getFieldCount())
4918 InstanceStart = InstanceSize;
4919 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004920 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004921}
4922
Fariborz Jahanian71394042009-01-23 23:53:38 +00004923void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4924 std::string ClassName = ID->getNameAsString();
4925 if (!ObjCEmptyCacheVar) {
4926 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004927 CGM.getModule(),
4928 ObjCTypes.CacheTy,
4929 false,
4930 llvm::GlobalValue::ExternalLinkage,
4931 0,
4932 "_objc_empty_cache");
4933
Fariborz Jahanian71394042009-01-23 23:53:38 +00004934 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004935 CGM.getModule(),
4936 ObjCTypes.ImpnfABITy,
4937 false,
4938 llvm::GlobalValue::ExternalLinkage,
4939 0,
4940 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004941 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004942 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004943 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004944 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004945 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004946 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004947 uint32_t InstanceSize = InstanceStart;
4948 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004949 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4950 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004951
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004952 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004953
4954 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004955 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004956 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004957 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004958 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004959 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004960 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004961 // class is root
4962 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004963 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004964 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +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 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4968 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4969 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004970 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004971 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004972 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004973 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004974 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004975 ObjCMetaClassName +
4976 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004977 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004978 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004979 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004980 }
4981 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4982 InstanceStart,
4983 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004984 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004985 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004986 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4987 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004988 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004989
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004990 // Metadata for the class
4991 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004992 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004993 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004994 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004995 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004996
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004997 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004998 flags |= CLS_EXCEPTION;
4999
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005000 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005001 flags |= CLS_ROOT;
5002 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005003 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005004 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005005 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005006 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005007 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005008 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005009 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005010 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005011 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005012 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005013 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005014 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005015 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005017 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005018 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005019 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5020 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005021 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005022
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005023 // Determine if this class is also "non-lazy".
5024 if (ImplementationIsNonLazy(ID))
5025 DefinedNonLazyClasses.push_back(ClassMD);
5026
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005027 // Force the definition of the EHType if necessary.
5028 if (flags & CLS_EXCEPTION)
5029 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005030 // Make sure method definition entries are all clear for next implementation.
5031 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005032}
5033
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005034/// GenerateProtocolRef - This routine is called to generate code for
5035/// a protocol reference expression; as in:
5036/// @code
5037/// @protocol(Proto1);
5038/// @endcode
5039/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5040/// which will hold address of the protocol meta-data.
5041///
5042llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005043 const ObjCProtocolDecl *PD) {
5044
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005045 // This routine is called for @protocol only. So, we must build definition
5046 // of protocol's meta-data (not a reference to it!)
5047 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005048 llvm::Constant *Init =
5049 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5050 ObjCTypes.ExternalProtocolPtrTy);
5051
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005052 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005053 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005054
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005055 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5056 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005057 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005058 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005059 CGM.getModule(),
5060 Init->getType(), false,
5061 llvm::GlobalValue::WeakAnyLinkage,
5062 Init,
5063 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005064 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5065 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005066 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005067 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005068}
5069
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005070/// GenerateCategory - Build metadata for a category implementation.
5071/// struct _category_t {
5072/// const char * const name;
5073/// struct _class_t *const cls;
5074/// const struct _method_list_t * const instance_methods;
5075/// const struct _method_list_t * const class_methods;
5076/// const struct _protocol_list_t * const protocols;
5077/// const struct _prop_list_t * const properties;
5078/// }
5079///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005080void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005081 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005082 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005083 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5084 "_$_" + OCD->getNameAsString());
5085 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005086 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005088 std::vector<llvm::Constant*> Values(6);
5089 Values[0] = GetClassName(OCD->getIdentifier());
5090 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005091 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005092 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005093 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5094
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005095 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005096 std::vector<llvm::Constant*> Methods;
5097 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005098 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005099 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005100
5101 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005102 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005103 // Instance methods should always be defined.
5104 Methods.push_back(GetMethodConstant(*i));
5105 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005106
5107 Values[2] = EmitMethodList(MethodListName,
5108 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005109 Methods);
5110
5111 MethodListName = Prefix;
5112 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5113 OCD->getNameAsString();
5114 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005115 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005116 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005117 // Class methods should always be defined.
5118 Methods.push_back(GetMethodConstant(*i));
5119 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005120
5121 Values[3] = EmitMethodList(MethodListName,
5122 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005123 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005124 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005125 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005126 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005127 llvm::SmallString<256> ExtName;
5128 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5129 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005130 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005131 + Interface->getName() + "_$_"
5132 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005133 Category->protocol_begin(),
5134 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005135 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5136 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005137 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005138 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5139 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005140 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005141
5142 llvm::Constant *Init =
5143 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005144 Values);
5145 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005146 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005147 false,
5148 llvm::GlobalValue::InternalLinkage,
5149 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005150 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005151 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005152 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005153 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005154 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005155 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005156
5157 // Determine if this category is also "non-lazy".
5158 if (ImplementationIsNonLazy(OCD))
5159 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005160 // method definition entries must be clear for next implementation.
5161 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005162}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005163
5164/// GetMethodConstant - Return a struct objc_method constant for the
5165/// given method if it has been defined. The result is null if the
5166/// method has not been defined. The return value has type MethodPtrTy.
5167llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005169 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005170 if (!Fn)
5171 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005172
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005173 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005174 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005175 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005176 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005177 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005178 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005179 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005180}
5181
5182/// EmitMethodList - Build meta-data for method declarations
5183/// struct _method_list_t {
5184/// uint32_t entsize; // sizeof(struct _objc_method)
5185/// uint32_t method_count;
5186/// struct _objc_method method_list[method_count];
5187/// }
5188///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005189llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5190 const char *Section,
5191 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005192 // Return null for empty list.
5193 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005194 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005195
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005196 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005197 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005198 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005199 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005200 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005201 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005202 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005203 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005204 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005205 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005206
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005207 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005208 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005209 llvm::GlobalValue::InternalLinkage, Init, Name);
5210 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005211 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005212 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005213 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005214}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005215
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005216/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5217/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005218llvm::GlobalVariable *
5219CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5220 const ObjCIvarDecl *Ivar) {
5221 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005222 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005223 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005224 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005225 CGM.getModule().getGlobalVariable(Name);
5226 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005227 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005228 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005229 false,
5230 llvm::GlobalValue::ExternalLinkage,
5231 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005232 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005233 return IvarOffsetGV;
5234}
5235
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005236llvm::Constant *
5237CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5238 const ObjCIvarDecl *Ivar,
5239 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005240 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005241 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005242 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005243 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005244 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005245
Mike Stump18bb9282009-05-16 07:57:57 +00005246 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5247 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005248 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5249 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005250 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005251 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005252 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005253 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005254 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005255 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005256}
5257
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005258/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005259/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005260/// IvarListnfABIPtrTy.
5261/// struct _ivar_t {
5262/// unsigned long int *offset; // pointer to ivar offset location
5263/// char *name;
5264/// char *type;
5265/// uint32_t alignment;
5266/// uint32_t size;
5267/// }
5268/// struct _ivar_list_t {
5269/// uint32 entsize; // sizeof(struct _ivar_t)
5270/// uint32 count;
5271/// struct _iver_t list[count];
5272/// }
5273///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005274
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005275llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005276 const ObjCImplementationDecl *ID) {
5277
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005278 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005279
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005280 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5281 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005282
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005283 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005284
Daniel Dunbarae032262009-04-20 00:33:43 +00005285 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005286 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005287 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005289 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5290 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005291 // Ignore unnamed bit-fields.
5292 if (!IVD->getDeclName())
5293 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005294 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005295 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005296 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5297 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005298 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005299 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005300 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005301 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005302 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005303 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005304 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005305 // NOTE. Size of a bitfield does not match gcc's, because of the
5306 // way bitfields are treated special in each. But I am told that
5307 // 'size' for bitfield ivars is ignored by the runtime so it does
5308 // not matter. If it matters, there is enough info to get the
5309 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005310 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005311 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005312 }
5313 // Return null for empty list.
5314 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005315 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005316
5317 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005318 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005319 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5320 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005321 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005322 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005323 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005324 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005325 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5326 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005327 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005328 llvm::GlobalValue::InternalLinkage,
5329 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005330 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005331 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005332 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005333 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005334
Chris Lattnerf56501c2009-07-17 23:57:13 +00005335 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005336 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005337}
5338
5339llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005340 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005341 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005342
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005343 if (!Entry) {
5344 // We use the initializer as a marker of whether this is a forward
5345 // reference or not. At module finalization we add the empty
5346 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005347 Entry =
5348 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5349 llvm::GlobalValue::ExternalLinkage,
5350 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005351 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005352 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005353 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005354
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005355 return Entry;
5356}
5357
5358/// GetOrEmitProtocol - Generate the protocol meta-data:
5359/// @code
5360/// struct _protocol_t {
5361/// id isa; // NULL
5362/// const char * const protocol_name;
5363/// const struct _protocol_list_t * protocol_list; // super protocols
5364/// const struct method_list_t * const instance_methods;
5365/// const struct method_list_t * const class_methods;
5366/// const struct method_list_t *optionalInstanceMethods;
5367/// const struct method_list_t *optionalClassMethods;
5368/// const struct _prop_list_t * properties;
5369/// const uint32_t size; // sizeof(struct _protocol_t)
5370/// const uint32_t flags; // = 0
5371/// }
5372/// @endcode
5373///
5374
5375llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005376 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005377 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005379 // Early exit if a defining object has already been generated.
5380 if (Entry && Entry->hasInitializer())
5381 return Entry;
5382
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 // Construct method lists.
5384 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5385 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005386 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005387 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005388 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005389 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005390 if (!C)
5391 return GetOrEmitProtocolRef(PD);
5392
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005393 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5394 OptInstanceMethods.push_back(C);
5395 } else {
5396 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005397 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005398 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005399
5400 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005401 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005403 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005404 if (!C)
5405 return GetOrEmitProtocolRef(PD);
5406
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005407 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5408 OptClassMethods.push_back(C);
5409 } else {
5410 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005412 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 std::vector<llvm::Constant*> Values(10);
5415 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005416 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005417 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005418 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5419 PD->protocol_begin(),
5420 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005421
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005422 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005423 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005424 "__DATA, __objc_const",
5425 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005426 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005427 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005428 "__DATA, __objc_const",
5429 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005430 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005431 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005432 "__DATA, __objc_const",
5433 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005435 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005436 "__DATA, __objc_const",
5437 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005438 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005439 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005440 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005441 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005442 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005443 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005444 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005445 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005446
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005447 if (Entry) {
5448 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005449 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005450 Entry->setInitializer(Init);
5451 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005453 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5454 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5455 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005456 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005457 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005458 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005460 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005461 CGM.AddUsedGlobal(Entry);
5462
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005463 // Use this protocol meta-data to build protocol list table in section
5464 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005465 llvm::GlobalVariable *PTGV =
5466 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5467 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5468 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005469 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005470 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005471 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005472 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005473 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005474 return Entry;
5475}
5476
5477/// EmitProtocolList - Generate protocol list meta-data:
5478/// @code
5479/// struct _protocol_list_t {
5480/// long protocol_count; // Note, this is 32/64 bit
5481/// struct _protocol_t[protocol_count];
5482/// }
5483/// @endcode
5484///
5485llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005486CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5487 ObjCProtocolDecl::protocol_iterator begin,
5488 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005490
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005491 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005492 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005493 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005494
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005495 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005496 llvm::SmallString<256> TmpName;
5497 Name.toVector(TmpName);
5498 llvm::GlobalVariable *GV =
5499 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005500 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005501 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005502
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005503 for (; begin != end; ++begin)
5504 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5505
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005506 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005507 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005508 ObjCTypes.ProtocolnfABIPtrTy));
5509
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005510 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005511 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005512 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005513 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005514 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005515 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005516 ProtocolRefs.size()),
5517 ProtocolRefs);
5518
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005519 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005520 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005521 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005522 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005523 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005524 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005525 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005526 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005527 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005528 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005529}
5530
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005531/// GetMethodDescriptionConstant - This routine build following meta-data:
5532/// struct _objc_method {
5533/// SEL _cmd;
5534/// char *method_type;
5535/// char *_imp;
5536/// }
5537
5538llvm::Constant *
5539CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5540 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005541 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005542 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5543 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005544 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005545 if (!Desc[1])
5546 return 0;
5547
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005548 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005549 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005550 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005551}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005552
5553/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5554/// This code gen. amounts to generating code for:
5555/// @code
5556/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5557/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005558///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005559LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005560 CodeGen::CodeGenFunction &CGF,
5561 QualType ObjectTy,
5562 llvm::Value *BaseValue,
5563 const ObjCIvarDecl *Ivar,
5564 unsigned CVRQualifiers) {
5565 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005566 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5567 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005568}
5569
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005570llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005571 CodeGen::CodeGenFunction &CGF,
5572 const ObjCInterfaceDecl *Interface,
5573 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005574 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005575}
5576
John McCall234eac82011-05-13 23:16:18 +00005577static void appendSelectorForMessageRefTable(std::string &buffer,
5578 Selector selector) {
5579 if (selector.isUnarySelector()) {
5580 buffer += selector.getNameForSlot(0);
5581 return;
5582 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005583
John McCall234eac82011-05-13 23:16:18 +00005584 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5585 buffer += selector.getNameForSlot(i);
5586 buffer += '_';
5587 }
5588}
5589
John McCall9e8bb002011-05-14 03:10:52 +00005590/// Emit a "v-table" message send. We emit a weak hidden-visibility
5591/// struct, initially containing the selector pointer and a pointer to
5592/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5593/// load and call the function pointer, passing the address of the
5594/// struct as the second parameter. The runtime determines whether
5595/// the selector is currently emitted using vtable dispatch; if so, it
5596/// substitutes a stub function which simply tail-calls through the
5597/// appropriate vtable slot, and if not, it substitues a stub function
5598/// which tail-calls objc_msgSend. Both stubs adjust the selector
5599/// argument to correctly point to the selector.
5600RValue
5601CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5602 ReturnValueSlot returnSlot,
5603 QualType resultType,
5604 Selector selector,
5605 llvm::Value *arg0,
5606 QualType arg0Type,
5607 bool isSuper,
5608 const CallArgList &formalArgs,
5609 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005610 // Compute the actual arguments.
5611 CallArgList args;
5612
John McCall9e8bb002011-05-14 03:10:52 +00005613 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005614 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005615 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5616 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005617
John McCall9e8bb002011-05-14 03:10:52 +00005618 // Second argument: a pointer to the message ref structure. Leave
5619 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005620 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5621
5622 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5623
5624 const CGFunctionInfo &fnInfo =
5625 CGM.getTypes().getFunctionInfo(resultType, args,
5626 FunctionType::ExtInfo());
5627
John McCall5880fb82011-05-14 21:12:11 +00005628 NullReturnState nullReturn;
5629
John McCall9e8bb002011-05-14 03:10:52 +00005630 // Find the function to call and the mangled name for the message
5631 // ref structure. Using a different mangled name wouldn't actually
5632 // be a problem; it would just be a waste.
5633 //
5634 // The runtime currently never uses vtable dispatch for anything
5635 // except normal, non-super message-sends.
5636 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005637 llvm::Constant *fn = 0;
5638 std::string messageRefName("\01l_");
5639 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005640 if (isSuper) {
5641 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5642 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005643 } else {
John McCall5880fb82011-05-14 21:12:11 +00005644 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005645 fn = ObjCTypes.getMessageSendStretFixupFn();
5646 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005647 }
John McCall234eac82011-05-13 23:16:18 +00005648 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5649 fn = ObjCTypes.getMessageSendFpretFixupFn();
5650 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005651 } else {
John McCall234eac82011-05-13 23:16:18 +00005652 if (isSuper) {
5653 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5654 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005655 } else {
John McCall234eac82011-05-13 23:16:18 +00005656 fn = ObjCTypes.getMessageSendFixupFn();
5657 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005658 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005659 }
John McCall234eac82011-05-13 23:16:18 +00005660 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5661 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005662
John McCall234eac82011-05-13 23:16:18 +00005663 // Append the selector name, except use underscores anywhere we
5664 // would have used colons.
5665 appendSelectorForMessageRefTable(messageRefName, selector);
5666
5667 llvm::GlobalVariable *messageRef
5668 = CGM.getModule().getGlobalVariable(messageRefName);
5669 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005670 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005671 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005672 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005673 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5674 init->getType(),
5675 /*constant*/ false,
5676 llvm::GlobalValue::WeakAnyLinkage,
5677 init,
5678 messageRefName);
5679 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5680 messageRef->setAlignment(16);
5681 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5682 }
5683 llvm::Value *mref =
5684 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5685
John McCall9e8bb002011-05-14 03:10:52 +00005686 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005687 args[1].RV = RValue::get(mref);
5688
5689 // Load the function to call from the message ref table.
5690 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5691 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5692
5693 bool variadic = method ? method->isVariadic() : false;
5694 const llvm::FunctionType *fnType =
5695 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5696 callee = CGF.Builder.CreateBitCast(callee,
5697 llvm::PointerType::getUnqual(fnType));
5698
John McCall5880fb82011-05-14 21:12:11 +00005699 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5700 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005701}
5702
5703/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005704CodeGen::RValue
5705CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005706 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005707 QualType ResultType,
5708 Selector Sel,
5709 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005710 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005711 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005712 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005713 return isVTableDispatchedSelector(Sel)
5714 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005716 false, CallArgs, Method)
5717 : EmitMessageSend(CGF, Return, ResultType,
5718 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005719 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005720 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005721}
5722
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005723llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005724CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005725 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5726
Daniel Dunbara6468342009-03-02 05:18:14 +00005727 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005728 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005729 false, llvm::GlobalValue::ExternalLinkage,
5730 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005731 }
5732
5733 return GV;
5734}
5735
John McCall31168b02011-06-15 23:02:42 +00005736llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5737 IdentifierInfo *II) {
5738 llvm::GlobalVariable *&Entry = ClassReferences[II];
5739
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005740 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005741 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005742 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005743 Entry =
John McCall31168b02011-06-15 23:02:42 +00005744 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5745 false, llvm::GlobalValue::InternalLinkage,
5746 ClassGV,
5747 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005748 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005749 CGM.getTargetData().getABITypeAlignment(
5750 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005751 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005752 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005753 }
John McCall31168b02011-06-15 23:02:42 +00005754
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005755 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005756}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005757
John McCall31168b02011-06-15 23:02:42 +00005758llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5759 const ObjCInterfaceDecl *ID) {
5760 return EmitClassRefFromId(Builder, ID->getIdentifier());
5761}
5762
5763llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5764 CGBuilderTy &Builder) {
5765 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5766 return EmitClassRefFromId(Builder, II);
5767}
5768
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005769llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005770CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005771 const ObjCInterfaceDecl *ID) {
5772 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005773
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005774 if (!Entry) {
5775 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5776 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005777 Entry =
5778 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005779 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005780 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005781 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005782 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005783 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005784 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005785 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005786 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005787 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005788
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005789 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005790}
5791
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005792/// EmitMetaClassRef - Return a Value * of the address of _class_t
5793/// meta-data
5794///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005795llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5796 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005797 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5798 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005799 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005800
Daniel Dunbar15894b72009-04-07 05:48:37 +00005801 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005802 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005803 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005804 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005805 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005806 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005807 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005808 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005809 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005810 ObjCTypes.ClassnfABIPtrTy));
5811
Daniel Dunbare60aa052009-04-15 19:03:14 +00005812 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005813 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005814
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005815 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005816}
5817
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005818/// GetClass - Return a reference to the class for the given interface
5819/// decl.
5820llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5821 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005822 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005823 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5824 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5825 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5826 }
5827
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005828 return EmitClassRef(Builder, ID);
5829}
5830
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005831/// Generates a message send where the super is the receiver. This is
5832/// a message send to self with special delivery semantics indicating
5833/// which class's method should be called.
5834CodeGen::RValue
5835CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005836 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005837 QualType ResultType,
5838 Selector Sel,
5839 const ObjCInterfaceDecl *Class,
5840 bool isCategoryImpl,
5841 llvm::Value *Receiver,
5842 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005843 const CodeGen::CallArgList &CallArgs,
5844 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005845 // ...
5846 // Create and init a super structure; this is a (receiver, class)
5847 // pair we will pass to objc_msgSendSuper.
5848 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005849 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005850
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005851 llvm::Value *ReceiverAsObject =
5852 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5853 CGF.Builder.CreateStore(ReceiverAsObject,
5854 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005855
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005856 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005857 llvm::Value *Target;
5858 if (IsClassMessage) {
5859 if (isCategoryImpl) {
5860 // Message sent to "super' in a class method defined in
5861 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005862 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005863 Target = CGF.Builder.CreateStructGEP(Target, 0);
5864 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005865 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005866 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005867 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005868 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005869
Mike Stump18bb9282009-05-16 07:57:57 +00005870 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5871 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005872 const llvm::Type *ClassTy =
5873 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5874 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5875 CGF.Builder.CreateStore(Target,
5876 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005877
John McCall9e8bb002011-05-14 03:10:52 +00005878 return (isVTableDispatchedSelector(Sel))
5879 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005880 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005881 true, CallArgs, Method)
5882 : EmitMessageSend(CGF, Return, ResultType,
5883 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005884 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005885 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005886}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005887
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005888llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005889 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005890 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005891
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005892 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005893 llvm::Constant *Casted =
5894 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5895 ObjCTypes.SelectorPtrTy);
5896 Entry =
5897 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5898 llvm::GlobalValue::InternalLinkage,
5899 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005900 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005901 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005902 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005903
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005904 if (lval)
5905 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005906 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005907}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005908/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005909/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005910///
5911void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005912 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005913 llvm::Value *dst,
5914 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005915 const llvm::Type * SrcTy = src->getType();
5916 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005917 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005918 assert(Size <= 8 && "does not support size > 8");
5919 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5920 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005921 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5922 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005923 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5924 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005925 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5926 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005927 return;
5928}
5929
5930/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5931/// objc_assign_strongCast (id src, id *dst)
5932///
5933void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005934 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005935 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005936 const llvm::Type * SrcTy = src->getType();
5937 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005938 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005939 assert(Size <= 8 && "does not support size > 8");
5940 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005941 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005942 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5943 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005944 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5945 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005946 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005947 src, dst, "weakassign");
5948 return;
5949}
5950
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005951void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005952 CodeGen::CodeGenFunction &CGF,
5953 llvm::Value *DestPtr,
5954 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005955 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005956 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5957 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005958 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005959 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005960 return;
5961}
5962
Fariborz Jahanian06292952009-02-16 22:52:32 +00005963/// EmitObjCWeakRead - Code gen for loading value of a __weak
5964/// object: objc_read_weak (id *src)
5965///
5966llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005967 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005968 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005969 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005970 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5971 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005972 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005973 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005974 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005975 return read_weak;
5976}
5977
5978/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5979/// objc_assign_weak (id src, id *dst)
5980///
5981void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005982 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005983 const llvm::Type * SrcTy = src->getType();
5984 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005985 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005986 assert(Size <= 8 && "does not support size > 8");
5987 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5988 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005989 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5990 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005991 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5992 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005993 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005994 src, dst, "weakassign");
5995 return;
5996}
5997
5998/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5999/// objc_assign_global (id src, id *dst)
6000///
6001void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006002 llvm::Value *src, llvm::Value *dst,
6003 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006004 const llvm::Type * SrcTy = src->getType();
6005 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006006 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006007 assert(Size <= 8 && "does not support size > 8");
6008 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6009 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006010 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6011 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006012 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6013 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006014 if (!threadlocal)
6015 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6016 src, dst, "globalassign");
6017 else
6018 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6019 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006020 return;
6021}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006022
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006023void
John McCallbd309292010-07-06 01:34:17 +00006024CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6025 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006026 EmitAtSynchronizedStmt(CGF, S,
6027 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6028 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006029}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006030
John McCall2ca705e2010-07-24 00:37:23 +00006031llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006032CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006033 // There's a particular fixed type info for 'id'.
6034 if (T->isObjCIdType() ||
6035 T->isObjCQualifiedIdType()) {
6036 llvm::Constant *IDEHType =
6037 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6038 if (!IDEHType)
6039 IDEHType =
6040 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6041 false,
6042 llvm::GlobalValue::ExternalLinkage,
6043 0, "OBJC_EHTYPE_id");
6044 return IDEHType;
6045 }
6046
6047 // All other types should be Objective-C interface pointer types.
6048 const ObjCObjectPointerType *PT =
6049 T->getAs<ObjCObjectPointerType>();
6050 assert(PT && "Invalid @catch type.");
6051 const ObjCInterfaceType *IT = PT->getInterfaceType();
6052 assert(IT && "Invalid @catch type.");
6053 return GetInterfaceEHType(IT->getDecl(), false);
6054}
6055
John McCallbd309292010-07-06 01:34:17 +00006056void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6057 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006058 EmitTryCatchStmt(CGF, S,
6059 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6060 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6061 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006062}
6063
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006064/// EmitThrowStmt - Generate code for a throw statement.
6065void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6066 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006067 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006068 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006069 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6070 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006071 llvm::Value *Args[] = { Exception };
6072 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6073 Args, Args+1)
6074 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006075 } else {
John McCall17afe452010-10-16 08:21:07 +00006076 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6077 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006078 }
6079
John McCall17afe452010-10-16 08:21:07 +00006080 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006081 CGF.Builder.ClearInsertionPoint();
6082}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006083
John McCall2ca705e2010-07-24 00:37:23 +00006084llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006085CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006086 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006087 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006088
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006089 // If we don't need a definition, return the entry if found or check
6090 // if we use an external reference.
6091 if (!ForDefinition) {
6092 if (Entry)
6093 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006094
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006095 // If this type (or a super class) has the __objc_exception__
6096 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006097 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006098 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006099 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006100 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006101 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006102 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006103 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006104 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006105
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006106 // Otherwise we need to either make a new entry or fill in the
6107 // initializer.
6108 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006109 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006110 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006111 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006112 CGM.getModule().getGlobalVariable(VTableName);
6113 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006114 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6115 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006116 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006117 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006118
Chris Lattner5e016ae2010-06-27 07:15:29 +00006119 llvm::Value *VTableIdx =
6120 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006121
6122 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006123 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006124 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006125 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006126 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006127 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006128
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006129 if (Entry) {
6130 Entry->setInitializer(Init);
6131 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006132 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006133 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006134 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006135 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006136 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006137 }
6138
John McCall457a04e2010-10-22 21:05:15 +00006139 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006140 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006141 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6142 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006143
6144 if (ForDefinition) {
6145 Entry->setSection("__DATA,__objc_const");
6146 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6147 } else {
6148 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6149 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006150
6151 return Entry;
6152}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006153
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006154/* *** */
6155
Daniel Dunbarb036db82008-08-13 03:21:16 +00006156CodeGen::CGObjCRuntime *
6157CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006158 if (CGM.getLangOptions().ObjCNonFragileABI)
6159 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006160 return new CGObjCMac(CGM);
6161}