blob: f98770071ecd47d7beaffae0e93b077bc8357f6a [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;
Daniel Dunbar59e476b2009-08-03 17:06:42 +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;
Daniel Dunbar59e476b2009-08-03 17:06:42 +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;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +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
John McCall2ca705e2010-07-24 00:37:23 +00001013 virtual llvm::Constant *GetEHType(QualType T);
1014
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
John McCall2ca705e2010-07-24 00:37:23 +00001274 virtual llvm::Constant *GetEHType(QualType T);
1275
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
John McCall2ca705e2010-07-24 00:37:23 +00001417llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1418 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1419 return 0;
1420}
1421
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001422/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001423/*
1424 struct __builtin_CFString {
1425 const int *isa; // point to __CFConstantStringClassReference
1426 int flags;
1427 const char *str;
1428 long length;
1429 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001430*/
1431
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001432/// or Generate a constant NSString object.
1433/*
1434 struct __builtin_NSString {
1435 const int *isa; // point to __NSConstantStringClassReference
1436 const char *str;
1437 unsigned int length;
1438 };
1439*/
1440
Fariborz Jahanian71394042009-01-23 23:53:38 +00001441llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001442 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001443 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1444 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001445 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001446}
1447
1448/// Generates a message send where the super is the receiver. This is
1449/// a message send to self with special delivery semantics indicating
1450/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001451CodeGen::RValue
1452CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001453 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001454 QualType ResultType,
1455 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001456 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001457 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001458 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001459 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001460 const CodeGen::CallArgList &CallArgs,
1461 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001462 // Create and init a super structure; this is a (receiver, class)
1463 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001464 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001465 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001466 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001467 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001468 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001469 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001470
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001471 // If this is a class message the metaclass is passed as the target.
1472 llvm::Value *Target;
1473 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001474 if (isCategoryImpl) {
1475 // Message sent to 'super' in a class method defined in a category
1476 // implementation requires an odd treatment.
1477 // If we are in a class method, we must retrieve the
1478 // _metaclass_ for the current class, pointed at by
1479 // the class's "isa" pointer. The following assumes that
1480 // isa" is the first ivar in a class (which it must be).
1481 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1482 Target = CGF.Builder.CreateStructGEP(Target, 0);
1483 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001484 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001485 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1486 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1487 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1488 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001489 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001490 }
1491 else if (isCategoryImpl)
1492 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1493 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001494 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1495 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1496 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001497 }
Mike Stump18bb9282009-05-16 07:57:57 +00001498 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1499 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001500 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001501 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001502 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001503 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001504 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001505 return EmitMessageSend(CGF, Return, ResultType,
1506 EmitSelector(CGF.Builder, Sel),
1507 ObjCSuper, ObjCTypes.SuperPtrCTy,
1508 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001509}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001510
1511/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001512CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001513 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001514 QualType ResultType,
1515 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001516 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001517 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001518 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001519 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001520 return EmitMessageSend(CGF, Return, ResultType,
1521 EmitSelector(CGF.Builder, Sel),
1522 Receiver, CGF.getContext().getObjCIdType(),
1523 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001524}
1525
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001526CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001527CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1528 ReturnValueSlot Return,
1529 QualType ResultType,
1530 llvm::Value *Sel,
1531 llvm::Value *Arg0,
1532 QualType Arg0Ty,
1533 bool IsSuper,
1534 const CallArgList &CallArgs,
1535 const ObjCMethodDecl *Method,
1536 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001537 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001538 if (!IsSuper)
1539 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Eli Friedman43dca6a2011-05-02 17:57:46 +00001540 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1541 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001542 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001543
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001544 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001545 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001546 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001547 const llvm::FunctionType *FTy =
1548 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001549
Anders Carlsson280e61f12010-06-21 20:59:55 +00001550 if (Method)
1551 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1552 CGM.getContext().getCanonicalType(ResultType) &&
1553 "Result type mismatch!");
1554
John McCall5880fb82011-05-14 21:12:11 +00001555 NullReturnState nullReturn;
1556
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001557 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001558 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001559 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001560 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001561 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001562 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1563 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1564 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001565 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001566 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001567 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001568 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001569 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001570 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1571 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001572}
1573
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001574static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1575 if (FQT.isObjCGCStrong())
1576 return Qualifiers::Strong;
1577
John McCall31168b02011-06-15 23:02:42 +00001578 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001579 return Qualifiers::Weak;
1580
1581 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1582 return Qualifiers::Strong;
1583
1584 if (const PointerType *PT = FQT->getAs<PointerType>())
1585 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1586
1587 return Qualifiers::GCNone;
1588}
1589
John McCall351762c2011-02-07 10:33:21 +00001590llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1591 const CGBlockInfo &blockInfo) {
1592 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001593 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001594
John McCall31168b02011-06-15 23:02:42 +00001595 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
1596 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001597 return nullPtr;
1598
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001599 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001600 SkipIvars.clear();
1601 IvarsInfo.clear();
1602 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1603 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1604
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001605 // __isa is the first field in block descriptor and must assume by runtime's
1606 // convention that it is GC'able.
1607 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001608
1609 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1610
1611 // Calculate the basic layout of the block structure.
1612 const llvm::StructLayout *layout =
1613 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1614
1615 // Ignore the optional 'this' capture: C++ objects are not assumed
1616 // to be GC'ed.
1617
1618 // Walk the captured variables.
1619 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1620 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1621 const VarDecl *variable = ci->getVariable();
1622 QualType type = variable->getType();
1623
1624 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1625
1626 // Ignore constant captures.
1627 if (capture.isConstant()) continue;
1628
1629 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1630
1631 // __block variables are passed by their descriptor address.
1632 if (ci->isByRef()) {
1633 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001634 continue;
John McCall351762c2011-02-07 10:33:21 +00001635 }
1636
1637 assert(!type->isArrayType() && "array variable should not be caught");
1638 if (const RecordType *record = type->getAs<RecordType>()) {
1639 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001640 continue;
1641 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001642
John McCall351762c2011-02-07 10:33:21 +00001643 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1644 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1645
1646 if (GCAttr == Qualifiers::Strong)
1647 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1648 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001649 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001650 SkipIvars.push_back(GC_IVAR(fieldOffset,
1651 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001652 }
1653
1654 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001655 return nullPtr;
1656
1657 // Sort on byte position; captures might not be allocated in order,
1658 // and unions can do funny things.
1659 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1660 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001661
1662 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001663 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001664 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1665 printf("\n block variable layout for block: ");
1666 const unsigned char *s = (unsigned char*)BitMap.c_str();
1667 for (unsigned i = 0; i < BitMap.size(); i++)
1668 if (!(s[i] & 0xf0))
1669 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1670 else
1671 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1672 printf("\n");
1673 }
1674
1675 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001676}
1677
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001678llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001679 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001680 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001681 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001682 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1683
Owen Andersonade90fd2009-07-29 18:54:39 +00001684 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001685 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001686}
1687
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001688void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001689 // FIXME: We shouldn't need this, the protocol decl should contain enough
1690 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001691 DefinedProtocols.insert(PD->getIdentifier());
1692
1693 // If we have generated a forward reference to this protocol, emit
1694 // it now. Otherwise do nothing, the protocol objects are lazily
1695 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001696 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001697 GetOrEmitProtocol(PD);
1698}
1699
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001700llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001701 if (DefinedProtocols.count(PD->getIdentifier()))
1702 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001703
Daniel Dunbarc475d422008-10-29 22:36:39 +00001704 return GetOrEmitProtocolRef(PD);
1705}
1706
Daniel Dunbarb036db82008-08-13 03:21:16 +00001707/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001708// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1709struct _objc_protocol {
1710struct _objc_protocol_extension *isa;
1711char *protocol_name;
1712struct _objc_protocol_list *protocol_list;
1713struct _objc__method_prototype_list *instance_methods;
1714struct _objc__method_prototype_list *class_methods
1715};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001716
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001717See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001718*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001719llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1720 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1721
1722 // Early exit if a defining object has already been generated.
1723 if (Entry && Entry->hasInitializer())
1724 return Entry;
1725
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001726 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001727 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001728 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1729
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001730 // Construct method lists.
1731 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1732 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001733 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001734 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001735 ObjCMethodDecl *MD = *i;
1736 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001737 if (!C)
1738 return GetOrEmitProtocolRef(PD);
1739
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001740 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1741 OptInstanceMethods.push_back(C);
1742 } else {
1743 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001744 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001745 }
1746
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001747 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001748 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001749 ObjCMethodDecl *MD = *i;
1750 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001751 if (!C)
1752 return GetOrEmitProtocolRef(PD);
1753
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001754 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1755 OptClassMethods.push_back(C);
1756 } else {
1757 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001758 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001759 }
1760
Daniel Dunbarb036db82008-08-13 03:21:16 +00001761 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001762 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001763 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001764 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001765 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001766 PD->protocol_begin(),
1767 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001768 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001769 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001770 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1771 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001772 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001773 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001774 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1775 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001776 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001777 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001778
Daniel Dunbarb036db82008-08-13 03:21:16 +00001779 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001780 // Already created, fix the linkage and update the initializer.
1781 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001782 Entry->setInitializer(Init);
1783 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001784 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001785 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001786 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001787 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001788 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001789 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001790 // FIXME: Is this necessary? Why only for protocol?
1791 Entry->setAlignment(4);
1792 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001793 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001794
1795 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001796}
1797
Daniel Dunbarc475d422008-10-29 22:36:39 +00001798llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001799 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1800
1801 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001802 // We use the initializer as a marker of whether this is a forward
1803 // reference or not. At module finalization we add the empty
1804 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001805 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001806 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001807 llvm::GlobalValue::ExternalLinkage,
1808 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001809 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001810 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001811 // FIXME: Is this necessary? Why only for protocol?
1812 Entry->setAlignment(4);
1813 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001814
Daniel Dunbarb036db82008-08-13 03:21:16 +00001815 return Entry;
1816}
1817
1818/*
1819 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001820 uint32_t size;
1821 struct objc_method_description_list *optional_instance_methods;
1822 struct objc_method_description_list *optional_class_methods;
1823 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001824 };
1825*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001826llvm::Constant *
1827CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1828 const ConstantVector &OptInstanceMethods,
1829 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001831 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001832 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001833 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001834 Values[1] =
1835 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001836 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001837 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1838 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001839 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001840 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001841 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1842 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001843 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001844 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001845
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001846 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001847 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001848 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001849 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001850
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001852 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001853
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001854 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001855 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001856 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001857 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858}
1859
1860/*
1861 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001862 struct objc_protocol_list *next;
1863 long count;
1864 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001865 };
1866*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001867llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001868CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001869 ObjCProtocolDecl::protocol_iterator begin,
1870 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871 std::vector<llvm::Constant*> ProtocolRefs;
1872
Daniel Dunbardec75f82008-08-21 21:57:41 +00001873 for (; begin != end; ++begin)
1874 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001875
1876 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001877 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001878 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001879
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001880 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001881 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001882
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001883 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001884 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001885 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001886 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887 ProtocolRefs.size() - 1);
1888 Values[2] =
1889 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1890 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001891 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001892
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001893 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001894 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001895 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001896 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001897 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001898}
1899
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001900void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1901 std::vector<llvm::Constant*> &Properties,
1902 const Decl *Container,
1903 const ObjCProtocolDecl *PROTO,
1904 const ObjCCommonTypesHelper &ObjCTypes) {
1905 std::vector<llvm::Constant*> Prop(2);
1906 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1907 E = PROTO->protocol_end(); P != E; ++P)
1908 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1909 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1910 E = PROTO->prop_end(); I != E; ++I) {
1911 const ObjCPropertyDecl *PD = *I;
1912 if (!PropertySet.insert(PD->getIdentifier()))
1913 continue;
1914 Prop[0] = GetPropertyName(PD->getIdentifier());
1915 Prop[1] = GetPropertyTypeString(PD, Container);
1916 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1917 }
1918}
1919
Daniel Dunbarb036db82008-08-13 03:21:16 +00001920/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001921 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001922 const char * const name;
1923 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001924 };
1925
1926 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001927 uint32_t entsize; // sizeof (struct _objc_property)
1928 uint32_t prop_count;
1929 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001930 };
1931*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001932llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001933 const Decl *Container,
1934 const ObjCContainerDecl *OCD,
1935 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001936 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001937 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001938 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1939 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001940 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001941 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001942 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001943 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001944 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001945 Prop));
1946 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001947 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001948 for (ObjCInterfaceDecl::all_protocol_iterator
1949 P = OID->all_referenced_protocol_begin(),
1950 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001951 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1952 ObjCTypes);
1953 }
1954 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1955 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1956 E = CD->protocol_end(); P != E; ++P)
1957 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1958 ObjCTypes);
1959 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001960
1961 // Return null for empty list.
1962 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001963 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001964
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001965 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001966 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001967 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001968 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1969 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001970 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001971 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001972 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001973 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001974
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001975 llvm::GlobalVariable *GV =
1976 CreateMetadataVar(Name, Init,
1977 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001978 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001979 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001980 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001981 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001982}
1983
1984/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001985 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986 int count;
1987 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001988 };
1989*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001990llvm::Constant *
1991CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1992 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001993 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1995 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001996 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001997 if (!Desc[1])
1998 return 0;
1999
Owen Anderson0e0189d2009-07-27 22:29:56 +00002000 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002001 Desc);
2002}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002003
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002004llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002005 const char *Section,
2006 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002007 // Return null for empty list.
2008 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002009 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002010
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002011 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002012 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002013 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002014 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002015 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002016 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002017
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002018 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002019 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002020 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002021}
2022
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002023/*
2024 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 char *category_name;
2026 char *class_name;
2027 struct _objc_method_list *instance_methods;
2028 struct _objc_method_list *class_methods;
2029 struct _objc_protocol_list *protocols;
2030 uint32_t size; // <rdar://4585769>
2031 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002032 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002033*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002034void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002035 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002036
Mike Stump18bb9282009-05-16 07:57:57 +00002037 // FIXME: This is poor design, the OCD should have a pointer to the category
2038 // decl. Additionally, note that Category can be null for the @implementation
2039 // w/o an @interface case. Sema should just create one for us as it does for
2040 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002041 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002043 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002044
2045 llvm::SmallString<256> ExtName;
2046 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2047 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002048
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002049 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002050 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002051 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002052 // Instance methods should always be defined.
2053 InstanceMethods.push_back(GetMethodConstant(*i));
2054 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002055 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002056 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002057 // Class methods should always be defined.
2058 ClassMethods.push_back(GetMethodConstant(*i));
2059 }
2060
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002061 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002062 Values[0] = GetClassName(OCD->getIdentifier());
2063 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002064 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002065 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002066 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002067 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002068 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002070 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002071 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002072 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002073 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002074 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002075 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002076 Category->protocol_begin(),
2077 Category->protocol_end());
2078 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002079 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002080 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002081 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002082
2083 // If there is no category @interface then there can be no properties.
2084 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002085 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002086 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002087 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002088 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002089 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002090
Owen Anderson0e0189d2009-07-27 22:29:56 +00002091 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002092 Values);
2093
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002094 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002095 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002096 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002097 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002098 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002099 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002100 // method definition entries must be clear for next implementation.
2101 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002102}
2103
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002104// FIXME: Get from somewhere?
2105enum ClassFlags {
2106 eClassFlags_Factory = 0x00001,
2107 eClassFlags_Meta = 0x00002,
2108 // <rdr://5142207>
2109 eClassFlags_HasCXXStructors = 0x02000,
2110 eClassFlags_Hidden = 0x20000,
2111 eClassFlags_ABI2_Hidden = 0x00010,
2112 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2113};
2114
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002115/*
2116 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002117 Class isa;
2118 Class super_class;
2119 const char *name;
2120 long version;
2121 long info;
2122 long instance_size;
2123 struct _objc_ivar_list *ivars;
2124 struct _objc_method_list *methods;
2125 struct _objc_cache *cache;
2126 struct _objc_protocol_list *protocols;
2127 // Objective-C 1.0 extensions (<rdr://4585769>)
2128 const char *ivar_layout;
2129 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002130 };
2131
2132 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002133*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002134void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002135 DefinedSymbols.insert(ID->getIdentifier());
2136
Chris Lattner86d7d912008-11-24 03:54:41 +00002137 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002138 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002140 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002141 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002142 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002143 Interface->all_referenced_protocol_begin(),
2144 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002145 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002146 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002147 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002148 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002149 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002150
2151 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002152 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002153 Flags |= eClassFlags_Hidden;
2154
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002155 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002156 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002157 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002158 // Instance methods should always be defined.
2159 InstanceMethods.push_back(GetMethodConstant(*i));
2160 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002161 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002162 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002163 // Class methods should always be defined.
2164 ClassMethods.push_back(GetMethodConstant(*i));
2165 }
2166
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002167 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002168 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002169 ObjCPropertyImplDecl *PID = *i;
2170
2171 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2172 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2173
2174 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2175 if (llvm::Constant *C = GetMethodConstant(MD))
2176 InstanceMethods.push_back(C);
2177 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2178 if (llvm::Constant *C = GetMethodConstant(MD))
2179 InstanceMethods.push_back(C);
2180 }
2181 }
2182
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002183 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002184 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002185 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002186 // Record a reference to the super class.
2187 LazySymbols.insert(Super->getIdentifier());
2188
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002189 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002190 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002191 ObjCTypes.ClassPtrTy);
2192 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002193 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002194 }
2195 Values[ 2] = GetClassName(ID->getIdentifier());
2196 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002197 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2198 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2199 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002200 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002201 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002202 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002203 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002204 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002205 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002206 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002207 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002208 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002209 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002210 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002211 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002212 std::string Name("\01L_OBJC_CLASS_");
2213 Name += ClassName;
2214 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2215 // Check for a forward reference.
2216 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2217 if (GV) {
2218 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2219 "Forward metaclass reference has incorrect type.");
2220 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2221 GV->setInitializer(Init);
2222 GV->setSection(Section);
2223 GV->setAlignment(4);
2224 CGM.AddUsedGlobal(GV);
2225 }
2226 else
2227 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002228 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002229 // method definition entries must be clear for next implementation.
2230 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002231}
2232
2233llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2234 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002235 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002236 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002237 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238
John McCall457a04e2010-10-22 21:05:15 +00002239 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002240 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002241
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002242 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002243 // The isa for the metaclass is the root of the hierarchy.
2244 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2245 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2246 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002247 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002248 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002249 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002250 // The super class for the metaclass is emitted as the name of the
2251 // super class. The runtime fixes this up to point to the
2252 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002254 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002255 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002256 ObjCTypes.ClassPtrTy);
2257 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002258 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002259 }
2260 Values[ 2] = GetClassName(ID->getIdentifier());
2261 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002262 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2263 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2264 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002265 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002266 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002267 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002268 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002269 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002270 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002271 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002272 Values[ 9] = Protocols;
2273 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002274 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002275 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002276 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002277 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002278 Values);
2279
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002280 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002281 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002282
2283 // Check for a forward reference.
2284 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2285 if (GV) {
2286 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2287 "Forward metaclass reference has incorrect type.");
2288 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2289 GV->setInitializer(Init);
2290 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002291 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002292 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002293 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002294 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002295 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002296 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002297 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002298
2299 return GV;
2300}
2301
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002302llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002303 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002304
Mike Stump18bb9282009-05-16 07:57:57 +00002305 // FIXME: Should we look these up somewhere other than the module. Its a bit
2306 // silly since we only generate these while processing an implementation, so
2307 // exactly one pointer would work if know when we entered/exitted an
2308 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002309
2310 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002311 // Previously, metaclass with internal linkage may have been defined.
2312 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002313 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2314 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002315 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2316 "Forward metaclass reference has incorrect type.");
2317 return GV;
2318 } else {
2319 // Generate as an external reference to keep a consistent
2320 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002321 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002322 llvm::GlobalValue::ExternalLinkage,
2323 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002324 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002325 }
2326}
2327
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002328llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2329 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2330
2331 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2332 true)) {
2333 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2334 "Forward class metadata reference has incorrect type.");
2335 return GV;
2336 } else {
2337 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2338 llvm::GlobalValue::ExternalLinkage,
2339 0,
2340 Name);
2341 }
2342}
2343
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002344/*
2345 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002346 uint32_t size;
2347 const char *weak_ivar_layout;
2348 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002349 };
2350*/
2351llvm::Constant *
2352CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002353 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002354 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002355
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002356 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002357 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002358 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002359 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002360 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002361
2362 // Return null if no extension bits are used.
2363 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002364 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002365
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002366 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002367 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002368 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002369 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002370 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002371}
2372
2373/*
2374 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002375 char *ivar_name;
2376 char *ivar_type;
2377 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002378 };
2379
2380 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002381 int ivar_count;
2382 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002383 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002384*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002385llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002386 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002387 std::vector<llvm::Constant*> Ivars, Ivar(3);
2388
2389 // When emitting the root class GCC emits ivar entries for the
2390 // actual class structure. It is not clear if we need to follow this
2391 // behavior; for now lets try and get away with not doing it. If so,
2392 // the cleanest solution would be to make up an ObjCInterfaceDecl
2393 // for the class.
2394 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002395 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002396
2397 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002398 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002399
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002400 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002401 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002402
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002403 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2404 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002405 // Ignore unnamed bit-fields.
2406 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002407 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002408 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2409 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002410 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002411 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002412 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002413 }
2414
2415 // Return null for empty list.
2416 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002417 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002418
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002419 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002420 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002421 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002422 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002423 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002424 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002425
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002426 llvm::GlobalVariable *GV;
2427 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002428 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002429 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002430 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002431 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002432 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002433 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002434 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002435 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002436}
2437
2438/*
2439 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002440 SEL method_name;
2441 char *method_types;
2442 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002444
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002445 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002446 struct objc_method_list *obsolete;
2447 int count;
2448 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 };
2450*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002451
2452/// GetMethodConstant - Return a struct objc_method constant for the
2453/// given method if it has been defined. The result is null if the
2454/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002455llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002456 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002457 if (!Fn)
2458 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002459
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002460 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002461 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002462 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002463 ObjCTypes.SelectorPtrTy);
2464 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002465 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002466 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002467}
2468
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002469llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002470 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002471 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002472 // Return null for empty list.
2473 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002474 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002475
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002476 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002477 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002478 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002479 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002480 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002481 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002482 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002483
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002484 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002485 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002486}
2487
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002488llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002489 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002490 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002491 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002492
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002493 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002494 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002495 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002496 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002497 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002498 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002499 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002500 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002501 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002502
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002503 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002504}
2505
Daniel Dunbar30c65362009-03-09 20:09:19 +00002506llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002507CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002508 llvm::Constant *Init,
2509 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002510 unsigned Align,
2511 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002512 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002513 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002514 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002515 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002516 if (Section)
2517 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002518 if (Align)
2519 GV->setAlignment(Align);
2520 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002521 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002522 return GV;
2523}
2524
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002525llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002526 // Abuse this interface function as a place to finalize.
2527 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002528 return NULL;
2529}
2530
Chris Lattnerd4808922009-03-22 21:03:39 +00002531llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002532 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002533}
2534
Chris Lattnerd4808922009-03-22 21:03:39 +00002535llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002536 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002537}
2538
David Chisnall168b80f2010-12-26 22:13:16 +00002539llvm::Constant *CGObjCMac::GetGetStructFunction() {
2540 return ObjCTypes.getCopyStructFn();
2541}
2542llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002543 return ObjCTypes.getCopyStructFn();
2544}
2545
Chris Lattnerd4808922009-03-22 21:03:39 +00002546llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002547 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002548}
2549
John McCallbd309292010-07-06 01:34:17 +00002550void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2551 return EmitTryOrSynchronizedStmt(CGF, S);
2552}
2553
2554void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2555 const ObjCAtSynchronizedStmt &S) {
2556 return EmitTryOrSynchronizedStmt(CGF, S);
2557}
2558
John McCall65bea082010-07-21 06:59:36 +00002559namespace {
John McCallcda666c2010-07-21 07:22:38 +00002560 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002561 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002562 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002563 llvm::Value *CallTryExitVar;
2564 llvm::Value *ExceptionData;
2565 ObjCTypesHelper &ObjCTypes;
2566 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002567 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002568 llvm::Value *CallTryExitVar,
2569 llvm::Value *ExceptionData,
2570 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002571 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002572 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2573
2574 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2575 // Check whether we need to call objc_exception_try_exit.
2576 // In optimized code, this branch will always be folded.
2577 llvm::BasicBlock *FinallyCallExit =
2578 CGF.createBasicBlock("finally.call_exit");
2579 llvm::BasicBlock *FinallyNoCallExit =
2580 CGF.createBasicBlock("finally.no_call_exit");
2581 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2582 FinallyCallExit, FinallyNoCallExit);
2583
2584 CGF.EmitBlock(FinallyCallExit);
2585 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2586 ->setDoesNotThrow();
2587
2588 CGF.EmitBlock(FinallyNoCallExit);
2589
2590 if (isa<ObjCAtTryStmt>(S)) {
2591 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002592 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2593 // Save the current cleanup destination in case there's
2594 // control flow inside the finally statement.
2595 llvm::Value *CurCleanupDest =
2596 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2597
John McCall65bea082010-07-21 06:59:36 +00002598 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2599
John McCallcebe0ca2010-08-11 00:16:14 +00002600 if (CGF.HaveInsertPoint()) {
2601 CGF.Builder.CreateStore(CurCleanupDest,
2602 CGF.getNormalCleanupDestSlot());
2603 } else {
2604 // Currently, the end of the cleanup must always exist.
2605 CGF.EnsureInsertPoint();
2606 }
2607 }
John McCall65bea082010-07-21 06:59:36 +00002608 } else {
2609 // Emit objc_sync_exit(expr); as finally's sole statement for
2610 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002611 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002612 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2613 ->setDoesNotThrow();
2614 }
2615 }
2616 };
John McCall42227ed2010-07-31 23:20:56 +00002617
2618 class FragileHazards {
2619 CodeGenFunction &CGF;
2620 llvm::SmallVector<llvm::Value*, 20> Locals;
2621 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2622
2623 llvm::InlineAsm *ReadHazard;
2624 llvm::InlineAsm *WriteHazard;
2625
2626 llvm::FunctionType *GetAsmFnType();
2627
2628 void collectLocals();
2629 void emitReadHazard(CGBuilderTy &Builder);
2630
2631 public:
2632 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002633
John McCall42227ed2010-07-31 23:20:56 +00002634 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002635 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002636 };
2637}
2638
2639/// Create the fragile-ABI read and write hazards based on the current
2640/// state of the function, which is presumed to be immediately prior
2641/// to a @try block. These hazards are used to maintain correct
2642/// semantics in the face of optimization and the fragile ABI's
2643/// cavalier use of setjmp/longjmp.
2644FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2645 collectLocals();
2646
2647 if (Locals.empty()) return;
2648
2649 // Collect all the blocks in the function.
2650 for (llvm::Function::iterator
2651 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2652 BlocksBeforeTry.insert(&*I);
2653
2654 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2655
2656 // Create a read hazard for the allocas. This inhibits dead-store
2657 // optimizations and forces the values to memory. This hazard is
2658 // inserted before any 'throwing' calls in the protected scope to
2659 // reflect the possibility that the variables might be read from the
2660 // catch block if the call throws.
2661 {
2662 std::string Constraint;
2663 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2664 if (I) Constraint += ',';
2665 Constraint += "*m";
2666 }
2667
2668 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2669 }
2670
2671 // Create a write hazard for the allocas. This inhibits folding
2672 // loads across the hazard. This hazard is inserted at the
2673 // beginning of the catch path to reflect the possibility that the
2674 // variables might have been written within the protected scope.
2675 {
2676 std::string Constraint;
2677 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2678 if (I) Constraint += ',';
2679 Constraint += "=*m";
2680 }
2681
2682 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2683 }
2684}
2685
2686/// Emit a write hazard at the current location.
2687void FragileHazards::emitWriteHazard() {
2688 if (Locals.empty()) return;
2689
2690 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2691 ->setDoesNotThrow();
2692}
2693
John McCall42227ed2010-07-31 23:20:56 +00002694void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2695 assert(!Locals.empty());
2696 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2697 ->setDoesNotThrow();
2698}
2699
2700/// Emit read hazards in all the protected blocks, i.e. all the blocks
2701/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002702void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002703 if (Locals.empty()) return;
2704
2705 CGBuilderTy Builder(CGF.getLLVMContext());
2706
2707 // Iterate through all blocks, skipping those prior to the try.
2708 for (llvm::Function::iterator
2709 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2710 llvm::BasicBlock &BB = *FI;
2711 if (BlocksBeforeTry.count(&BB)) continue;
2712
2713 // Walk through all the calls in the block.
2714 for (llvm::BasicBlock::iterator
2715 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2716 llvm::Instruction &I = *BI;
2717
2718 // Ignore instructions that aren't non-intrinsic calls.
2719 // These are the only calls that can possibly call longjmp.
2720 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2721 if (isa<llvm::IntrinsicInst>(I))
2722 continue;
2723
2724 // Ignore call sites marked nounwind. This may be questionable,
2725 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2726 llvm::CallSite CS(&I);
2727 if (CS.doesNotThrow()) continue;
2728
John McCall2dd7d442010-08-04 05:59:32 +00002729 // Insert a read hazard before the call. This will ensure that
2730 // any writes to the locals are performed before making the
2731 // call. If the call throws, then this is sufficient to
2732 // guarantee correctness as long as it doesn't also write to any
2733 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002734 Builder.SetInsertPoint(&BB, BI);
2735 emitReadHazard(Builder);
2736 }
2737 }
2738}
2739
2740static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2741 if (V) S.insert(V);
2742}
2743
2744void FragileHazards::collectLocals() {
2745 // Compute a set of allocas to ignore.
2746 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2747 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2748 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2749 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2750
2751 // Collect all the allocas currently in the function. This is
2752 // probably way too aggressive.
2753 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2754 for (llvm::BasicBlock::iterator
2755 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2756 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2757 Locals.push_back(&*I);
2758}
2759
2760llvm::FunctionType *FragileHazards::GetAsmFnType() {
John McCall9dc0db22011-05-15 01:53:33 +00002761 llvm::SmallVector<const llvm::Type *, 16> tys(Locals.size());
2762 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2763 tys[i] = Locals[i]->getType();
2764 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002765}
2766
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002767/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002768
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002769 Objective-C setjmp-longjmp (sjlj) Exception Handling
2770 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002771
John McCallbd309292010-07-06 01:34:17 +00002772 A catch buffer is a setjmp buffer plus:
2773 - a pointer to the exception that was caught
2774 - a pointer to the previous exception data buffer
2775 - two pointers of reserved storage
2776 Therefore catch buffers form a stack, with a pointer to the top
2777 of the stack kept in thread-local storage.
2778
2779 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2780 objc_exception_try_exit pops the given catch buffer, which is
2781 required to be the top of the EH stack.
2782 objc_exception_throw pops the top of the EH stack, writes the
2783 thrown exception into the appropriate field, and longjmps
2784 to the setjmp buffer. It crashes the process (with a printf
2785 and an abort()) if there are no catch buffers on the stack.
2786 objc_exception_extract just reads the exception pointer out of the
2787 catch buffer.
2788
2789 There's no reason an implementation couldn't use a light-weight
2790 setjmp here --- something like __builtin_setjmp, but API-compatible
2791 with the heavyweight setjmp. This will be more important if we ever
2792 want to implement correct ObjC/C++ exception interactions for the
2793 fragile ABI.
2794
2795 Note that for this use of setjmp/longjmp to be correct, we may need
2796 to mark some local variables volatile: if a non-volatile local
2797 variable is modified between the setjmp and the longjmp, it has
2798 indeterminate value. For the purposes of LLVM IR, it may be
2799 sufficient to make loads and stores within the @try (to variables
2800 declared outside the @try) volatile. This is necessary for
2801 optimized correctness, but is not currently being done; this is
2802 being tracked as rdar://problem/8160285
2803
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002804 The basic framework for a @try-catch-finally is as follows:
2805 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002806 objc_exception_data d;
2807 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002808 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002809
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002810 objc_exception_try_enter(&d);
2811 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002812 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002813 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002814 // exception path
2815 id _caught = objc_exception_extract(&d);
2816
2817 // enter new try scope for handlers
2818 if (!setjmp(d.jmp_buf)) {
2819 ... match exception and execute catch blocks ...
2820
2821 // fell off end, rethrow.
2822 _rethrow = _caught;
2823 ... jump-through-finally to finally_rethrow ...
2824 } else {
2825 // exception in catch block
2826 _rethrow = objc_exception_extract(&d);
2827 _call_try_exit = false;
2828 ... jump-through-finally to finally_rethrow ...
2829 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002830 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002831 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002832
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002833 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002834 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002835 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002836
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002837 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002838 ... dispatch to finally destination ...
2839
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002840 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002841 objc_exception_throw(_rethrow);
2842
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002843 finally_end:
2844 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002845
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002846 This framework differs slightly from the one gcc uses, in that gcc
2847 uses _rethrow to determine if objc_exception_try_exit should be called
2848 and if the object should be rethrown. This breaks in the face of
2849 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002850
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002851 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002852
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002853 - If there are no catch blocks, then we avoid emitting the second
2854 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002855
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002856 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2857 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002858
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002859 - FIXME: If there is no @finally block we can do a few more
2860 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002861
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002862 Rethrows and Jumps-Through-Finally
2863 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002864
John McCallbd309292010-07-06 01:34:17 +00002865 '@throw;' is supported by pushing the currently-caught exception
2866 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002867
John McCallbd309292010-07-06 01:34:17 +00002868 Branches through the @finally block are handled with an ordinary
2869 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2870 exceptions are not compatible with C++ exceptions, and this is
2871 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002872
John McCallbd309292010-07-06 01:34:17 +00002873 @synchronized(expr) { stmt; } is emitted as if it were:
2874 id synch_value = expr;
2875 objc_sync_enter(synch_value);
2876 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002877*/
2878
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002879void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2880 const Stmt &S) {
2881 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002882
2883 // A destination for the fall-through edges of the catch handlers to
2884 // jump to.
2885 CodeGenFunction::JumpDest FinallyEnd =
2886 CGF.getJumpDestInCurrentScope("finally.end");
2887
2888 // A destination for the rethrow edge of the catch handlers to jump
2889 // to.
2890 CodeGenFunction::JumpDest FinallyRethrow =
2891 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002892
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002893 // For @synchronized, call objc_sync_enter(sync.expr). The
2894 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002895 // @synchronized. We can't avoid a temp here because we need the
2896 // value to be preserved. If the backend ever does liveness
2897 // correctly after setjmp, this will be unnecessary.
2898 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002899 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002900 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002901 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2902 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002903 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2904 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002905
2906 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2907 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002908 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002909
John McCall2dd7d442010-08-04 05:59:32 +00002910 // Allocate memory for the setjmp buffer. This needs to be kept
2911 // live throughout the try and catch blocks.
2912 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2913 "exceptiondata.ptr");
2914
John McCall42227ed2010-07-31 23:20:56 +00002915 // Create the fragile hazards. Note that this will not capture any
2916 // of the allocas required for exception processing, but will
2917 // capture the current basic block (which extends all the way to the
2918 // setjmp call) as "before the @try".
2919 FragileHazards Hazards(CGF);
2920
John McCallbd309292010-07-06 01:34:17 +00002921 // Create a flag indicating whether the cleanup needs to call
2922 // objc_exception_try_exit. This is true except when
2923 // - no catches match and we're branching through the cleanup
2924 // just to rethrow the exception, or
2925 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002926 // The setjmp-safety rule here is that we should always store to this
2927 // variable in a place that dominates the branch through the cleanup
2928 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002929 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002930 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931
John McCall9916e3f2010-10-04 23:42:51 +00002932 // A slot containing the exception to rethrow. Only needed when we
2933 // have both a @catch and a @finally.
2934 llvm::Value *PropagatingExnVar = 0;
2935
John McCallbd309292010-07-06 01:34:17 +00002936 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002937 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002938 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002939 CallTryExitVar,
2940 ExceptionData,
2941 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002942
2943 // Enter a try block:
2944 // - Call objc_exception_try_enter to push ExceptionData on top of
2945 // the EH stack.
2946 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2947 ->setDoesNotThrow();
2948
2949 // - Call setjmp on the exception data buffer.
2950 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2951 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2952 llvm::Value *SetJmpBuffer =
2953 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2954 llvm::CallInst *SetJmpResult =
2955 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2956 SetJmpResult->setDoesNotThrow();
2957
2958 // If setjmp returned 0, enter the protected block; otherwise,
2959 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002960 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2961 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002962 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002963 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2964 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002965
John McCallbd309292010-07-06 01:34:17 +00002966 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002967 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002968 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002969 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002970 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002971
2972 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002973
John McCallbd309292010-07-06 01:34:17 +00002974 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002975 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002976
John McCall42227ed2010-07-31 23:20:56 +00002977 // Don't optimize loads of the in-scope locals across this point.
2978 Hazards.emitWriteHazard();
2979
John McCallbd309292010-07-06 01:34:17 +00002980 // For a @synchronized (or a @try with no catches), just branch
2981 // through the cleanup to the rethrow block.
2982 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2983 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002984 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002985 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002986
2987 // Otherwise, we have to match against the caught exceptions.
2988 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002989 // Retrieve the exception object. We may emit multiple blocks but
2990 // nothing can cross this so the value is already in SSA form.
2991 llvm::CallInst *Caught =
2992 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2993 ExceptionData, "caught");
2994 Caught->setDoesNotThrow();
2995
John McCallbd309292010-07-06 01:34:17 +00002996 // Push the exception to rethrow onto the EH value stack for the
2997 // benefit of any @throws in the handlers.
2998 CGF.ObjCEHValueStack.push_back(Caught);
2999
Douglas Gregor96c79492010-04-23 22:50:49 +00003000 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003001
John McCall2dd7d442010-08-04 05:59:32 +00003002 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003003
John McCall2dd7d442010-08-04 05:59:32 +00003004 llvm::BasicBlock *CatchBlock = 0;
3005 llvm::BasicBlock *CatchHandler = 0;
3006 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003007 // Save the currently-propagating exception before
3008 // objc_exception_try_enter clears the exception slot.
3009 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3010 "propagating_exception");
3011 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3012
John McCall2dd7d442010-08-04 05:59:32 +00003013 // Enter a new exception try block (in case a @catch block
3014 // throws an exception).
3015 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3016 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003017
John McCall2dd7d442010-08-04 05:59:32 +00003018 llvm::CallInst *SetJmpResult =
3019 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3020 "setjmp.result");
3021 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003022
John McCall2dd7d442010-08-04 05:59:32 +00003023 llvm::Value *Threw =
3024 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3025
3026 CatchBlock = CGF.createBasicBlock("catch");
3027 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3028 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3029
3030 CGF.EmitBlock(CatchBlock);
3031 }
3032
3033 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003034
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003035 // Handle catch list. As a special case we check if everything is
3036 // matched and avoid generating code for falling off the end if
3037 // so.
3038 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003039 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3040 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003041
Douglas Gregor46a572b2010-04-26 16:46:50 +00003042 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003043 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003044
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003045 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003046 if (!CatchParam) {
3047 AllMatched = true;
3048 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003049 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003050
John McCallbd309292010-07-06 01:34:17 +00003051 // catch(id e) always matches under this ABI, since only
3052 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003053 // FIXME: For the time being we also match id<X>; this should
3054 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003055 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003056 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003057 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003058
John McCallbd309292010-07-06 01:34:17 +00003059 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003060 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003061 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3062
Anders Carlsson9396a892008-09-11 09:15:33 +00003063 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003064 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003065 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003066
3067 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003068 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003069 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003070
Anders Carlsson9396a892008-09-11 09:15:33 +00003071 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003072
3073 // The scope of the catch variable ends right here.
3074 CatchVarCleanups.ForceCleanup();
3075
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003076 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003077 break;
3078 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003079
Steve Naroff7cae42b2009-07-10 23:34:53 +00003080 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003081 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003082
3083 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003084 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3085 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003086
3087 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003088 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003089
John McCallbd309292010-07-06 01:34:17 +00003090 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003091 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3092 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003093 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003094
John McCallbd309292010-07-06 01:34:17 +00003095 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3096 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003097
3098 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003099 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003101 // Emit the @catch block.
3102 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003103
3104 // Collect any cleanups for the catch variable. The scope lasts until
3105 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003106 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003107
John McCall1c9c3fd2010-10-15 04:57:14 +00003108 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003109 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003110
John McCallbd309292010-07-06 01:34:17 +00003111 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003112 llvm::Value *Tmp =
3113 CGF.Builder.CreateBitCast(Caught,
3114 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003115 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003116 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003117
Anders Carlsson9396a892008-09-11 09:15:33 +00003118 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003119
3120 // We're done with the catch variable.
3121 CatchVarCleanups.ForceCleanup();
3122
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003123 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003124
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003125 CGF.EmitBlock(NextCatchBlock);
3126 }
3127
John McCallbd309292010-07-06 01:34:17 +00003128 CGF.ObjCEHValueStack.pop_back();
3129
John McCall2dd7d442010-08-04 05:59:32 +00003130 // If nothing wanted anything to do with the caught exception,
3131 // kill the extract call.
3132 if (Caught->use_empty())
3133 Caught->eraseFromParent();
3134
3135 if (!AllMatched)
3136 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3137
3138 if (HasFinally) {
3139 // Emit the exception handler for the @catch blocks.
3140 CGF.EmitBlock(CatchHandler);
3141
3142 // In theory we might now need a write hazard, but actually it's
3143 // unnecessary because there's no local-accessing code between
3144 // the try's write hazard and here.
3145 //Hazards.emitWriteHazard();
3146
John McCall9916e3f2010-10-04 23:42:51 +00003147 // Extract the new exception and save it to the
3148 // propagating-exception slot.
3149 assert(PropagatingExnVar);
3150 llvm::CallInst *NewCaught =
3151 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3152 ExceptionData, "caught");
3153 NewCaught->setDoesNotThrow();
3154 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3155
John McCall2dd7d442010-08-04 05:59:32 +00003156 // Don't pop the catch handler; the throw already did.
3157 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003158 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003159 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003160 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003161
John McCall42227ed2010-07-31 23:20:56 +00003162 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003163 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003164
John McCallbd309292010-07-06 01:34:17 +00003165 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003166 CGF.Builder.restoreIP(TryFallthroughIP);
3167 if (CGF.HaveInsertPoint())
3168 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003169 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003170 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003171
John McCallbd309292010-07-06 01:34:17 +00003172 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003173 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003174 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003175 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003176 // If we have a propagating-exception variable, check it.
3177 llvm::Value *PropagatingExn;
3178 if (PropagatingExnVar) {
3179 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003180
John McCall9916e3f2010-10-04 23:42:51 +00003181 // Otherwise, just look in the buffer for the exception to throw.
3182 } else {
3183 llvm::CallInst *Caught =
3184 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3185 ExceptionData);
3186 Caught->setDoesNotThrow();
3187 PropagatingExn = Caught;
3188 }
3189
3190 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003191 ->setDoesNotThrow();
3192 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003193 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003194
John McCall42227ed2010-07-31 23:20:56 +00003195 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003196}
3197
3198void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003199 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003200 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003201
Anders Carlssone005aa12008-09-09 16:16:55 +00003202 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3203 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003204 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003205 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3206 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003207 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003208 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003209 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003210 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003211
John McCallbd309292010-07-06 01:34:17 +00003212 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3213 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003214 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003215
3216 // Clear the insertion point to indicate we are in unreachable code.
3217 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003218}
3219
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003220/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003221/// object: objc_read_weak (id *src)
3222///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003223llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003224 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003225 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003226 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3227 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3228 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003229 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003230 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003231 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003232 return read_weak;
3233}
3234
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003235/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3236/// objc_assign_weak (id src, id *dst)
3237///
3238void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003239 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003240 const llvm::Type * SrcTy = src->getType();
3241 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003242 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003243 assert(Size <= 8 && "does not support size > 8");
3244 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003245 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003246 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3247 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003248 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3249 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003250 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003251 src, dst, "weakassign");
3252 return;
3253}
3254
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003255/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3256/// objc_assign_global (id src, id *dst)
3257///
3258void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003259 llvm::Value *src, llvm::Value *dst,
3260 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003261 const llvm::Type * SrcTy = src->getType();
3262 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003263 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003264 assert(Size <= 8 && "does not support size > 8");
3265 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003266 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003267 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3268 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003269 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3270 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003271 if (!threadlocal)
3272 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3273 src, dst, "globalassign");
3274 else
3275 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3276 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003277 return;
3278}
3279
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003280/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003281/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003282///
3283void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003284 llvm::Value *src, llvm::Value *dst,
3285 llvm::Value *ivarOffset) {
3286 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003287 const llvm::Type * SrcTy = src->getType();
3288 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003289 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003290 assert(Size <= 8 && "does not support size > 8");
3291 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003292 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003293 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3294 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003295 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3296 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003297 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3298 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003299 return;
3300}
3301
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003302/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3303/// objc_assign_strongCast (id src, id *dst)
3304///
3305void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003306 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003307 const llvm::Type * SrcTy = src->getType();
3308 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003309 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003310 assert(Size <= 8 && "does not support size > 8");
3311 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003312 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003313 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3314 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003315 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3316 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003317 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003318 src, dst, "weakassign");
3319 return;
3320}
3321
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003322void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003323 llvm::Value *DestPtr,
3324 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003325 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003326 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3327 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003328 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003329 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003330 return;
3331}
3332
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003333/// EmitObjCValueForIvar - Code Gen for ivar reference.
3334///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003335LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3336 QualType ObjectTy,
3337 llvm::Value *BaseValue,
3338 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003339 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003340 const ObjCInterfaceDecl *ID =
3341 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003342 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3343 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003344}
3345
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003346llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003347 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003348 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003349 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003350 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003351 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3352 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003353}
3354
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003355/* *** Private Interface *** */
3356
3357/// EmitImageInfo - Emit the image info marker used to encode some module
3358/// level information.
3359///
3360/// See: <rdr://4810609&4810587&4810587>
3361/// struct IMAGE_INFO {
3362/// unsigned version;
3363/// unsigned flags;
3364/// };
3365enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003366 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003367 eImageInfo_GarbageCollected = (1 << 1),
3368 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003369 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3370
Daniel Dunbar5e639272010-04-25 20:39:01 +00003371 // A flag indicating that the module has no instances of a @synthesize of a
3372 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003373 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003374};
3375
Daniel Dunbar5e639272010-04-25 20:39:01 +00003376void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003377 unsigned version = 0; // Version is unused?
3378 unsigned flags = 0;
3379
3380 // FIXME: Fix and continue?
3381 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3382 flags |= eImageInfo_GarbageCollected;
3383 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3384 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003385
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003386 // We never allow @synthesize of a superclass property.
3387 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003388
Chris Lattner5e016ae2010-06-27 07:15:29 +00003389 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3390
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003391 // Emitted as int[2];
3392 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003393 llvm::ConstantInt::get(Int32Ty, version),
3394 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003395 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003396 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003397
3398 const char *Section;
3399 if (ObjCABI == 1)
3400 Section = "__OBJC, __image_info,regular";
3401 else
3402 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003403 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003404 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003405 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003406 Section,
3407 0,
3408 true);
3409 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003410}
3411
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003412
3413// struct objc_module {
3414// unsigned long version;
3415// unsigned long size;
3416// const char *name;
3417// Symtab symtab;
3418// };
3419
3420// FIXME: Get from somewhere
3421static const int ModuleVersion = 7;
3422
3423void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003424 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003425
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003426 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003427 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3428 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003429 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003430 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003431 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003432 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003433 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003434 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003435 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003436}
3437
3438llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003439 unsigned NumClasses = DefinedClasses.size();
3440 unsigned NumCategories = DefinedCategories.size();
3441
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003442 // Return null if no symbols were defined.
3443 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003444 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003445
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003446 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003447 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003448 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003449 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3450 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003451
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003452 // The runtime expects exactly the list of defined classes followed
3453 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003454 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003455 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003456 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003457 ObjCTypes.Int8PtrTy);
3458 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003459 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003460 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003461 ObjCTypes.Int8PtrTy);
3462
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003463 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003464 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003465 NumClasses + NumCategories),
3466 Symbols);
3467
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003468 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003469
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003470 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003471 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3472 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003473 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003474 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003475}
3476
John McCall31168b02011-06-15 23:02:42 +00003477llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3478 IdentifierInfo *II) {
3479 LazySymbols.insert(II);
3480
3481 llvm::GlobalVariable *&Entry = ClassReferences[II];
3482
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003483 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003484 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003485 llvm::ConstantExpr::getBitCast(GetClassName(II),
3486 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003487 Entry =
John McCall31168b02011-06-15 23:02:42 +00003488 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3489 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3490 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003491 }
John McCall31168b02011-06-15 23:02:42 +00003492
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003493 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003494}
3495
John McCall31168b02011-06-15 23:02:42 +00003496llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3497 const ObjCInterfaceDecl *ID) {
3498 return EmitClassRefFromId(Builder, ID->getIdentifier());
3499}
3500
3501llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3502 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3503 return EmitClassRefFromId(Builder, II);
3504}
3505
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003506llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3507 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003508 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003509
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003510 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003511 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003512 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003513 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003514 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003515 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3516 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003517 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003518 }
3519
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003520 if (lvalue)
3521 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003522 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003523}
3524
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003525llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003526 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003527
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003528 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003529 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003530 llvm::ConstantArray::get(VMContext,
3531 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003532 ((ObjCABI == 2) ?
3533 "__TEXT,__objc_classname,cstring_literals" :
3534 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003535 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003536
Owen Anderson170229f2009-07-14 23:10:40 +00003537 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003538}
3539
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003540llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3541 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3542 I = MethodDefinitions.find(MD);
3543 if (I != MethodDefinitions.end())
3544 return I->second;
3545
3546 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3547 // MD isn't emitted yet because it comes from PCH.
3548 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3549 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3550 return MethodDefinitions[MD];
3551 }
3552
3553 return NULL;
3554}
3555
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003556/// GetIvarLayoutName - Returns a unique constant for the given
3557/// ivar layout bitmap.
3558llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003560 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003561}
3562
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003563void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003565 bool ForStrongLayout,
3566 bool &HasUnion) {
3567 const RecordDecl *RD = RT->getDecl();
3568 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003569 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003570 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003571 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003572 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003573
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003574 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3575 ForStrongLayout, HasUnion);
3576}
3577
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003578void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003579 const llvm::StructLayout *Layout,
3580 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003581 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 unsigned int BytePos, bool ForStrongLayout,
3583 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003584 bool IsUnion = (RD && RD->isUnion());
3585 uint64_t MaxUnionIvarSize = 0;
3586 uint64_t MaxSkippedUnionIvarSize = 0;
3587 FieldDecl *MaxField = 0;
3588 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003589 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003590 uint64_t MaxFieldOffset = 0;
3591 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003592 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003593 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003594
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003595 if (RecFields.empty())
3596 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003597 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3598 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003599 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
3600 FieldDecl *FirstField = RecFields[0];
3601 FirstFieldDelta =
3602 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3603 }
3604
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003605 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003606 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003607 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003608 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003609 // Note that 'i' here is actually the field index inside RD of Field,
3610 // although this dependency is hidden.
3611 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003612 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003613 } else
John McCall31168b02011-06-15 23:02:42 +00003614 FieldOffset =
3615 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003616
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003617 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003618 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003619 LastFieldBitfieldOrUnnamed = Field;
3620 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003621 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003622 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003623
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003624 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003625 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003626 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003627 if (FQT->isUnionType())
3628 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003629
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003630 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003631 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003632 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003633 continue;
3634 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003635
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003636 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003638 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003639 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003640 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003641 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003642 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3643 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003644 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003645 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003646 FQT = CArray->getElementType();
3647 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003648
3649 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003650 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003651 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003652 int OldIndex = IvarsInfo.size() - 1;
3653 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003654
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003655 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003656 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003657 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003658
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003659 // Replicate layout information for each array element. Note that
3660 // one element is already done.
3661 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662 for (int FirstIndex = IvarsInfo.size() - 1,
3663 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003664 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003665 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3666 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3667 IvarsInfo[i].ivar_size));
3668 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3669 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3670 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003671 }
3672 continue;
3673 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003674 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003675 // At this point, we are done with Record/Union and array there of.
3676 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003677 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003678
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003679 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003680 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3681 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003682 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003683 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003684 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003685 MaxUnionIvarSize = UnionIvarSize;
3686 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003687 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003688 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003689 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003690 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003691 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003692 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003693 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003694 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3695 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003696 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003697 // FIXME: Why the asymmetry? We divide by word size in bits on other
3698 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003699 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003700 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003701 MaxSkippedUnionIvarSize = UnionIvarSize;
3702 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003703 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003704 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003705 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003706 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003707 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003708 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003709 }
3710 }
3711 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003712
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003713 if (LastFieldBitfieldOrUnnamed) {
3714 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3715 // Last field was a bitfield. Must update skip info.
3716 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3717 uint64_t BitFieldSize =
3718 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3719 GC_IVAR skivar;
3720 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3721 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3722 + ((BitFieldSize % ByteSizeInBits) != 0);
3723 SkipIvars.push_back(skivar);
3724 } else {
3725 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3726 // Last field was unnamed. Must update skip info.
3727 unsigned FieldSize
3728 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3729 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3730 FieldSize / ByteSizeInBits));
3731 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003732 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003733
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003734 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003735 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003736 MaxUnionIvarSize));
3737 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003738 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003739 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003740}
3741
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003742/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3743/// the computations and returning the layout bitmap (for ivar or blocks) in
3744/// the given argument BitMap string container. Routine reads
3745/// two containers, IvarsInfo and SkipIvars which are assumed to be
3746/// filled already by the caller.
3747llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003748 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003749 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003750
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003751 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003752 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003753 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003754 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003755 if (IvarsInfo[0].ivar_bytepos == 0) {
3756 WordsToSkip = 0;
3757 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003758 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003759 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3760 WordsToScan = IvarsInfo[0].ivar_size;
3761 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003762 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003763 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003764 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003765 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003766 // consecutive 'scanned' object pointers.
3767 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003768 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003769 // Skip over 'gc'able object pointer which lay over each other.
3770 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3771 continue;
3772 // Must skip over 1 or more words. We save current skip/scan values
3773 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003774 SKIP_SCAN SkScan;
3775 SkScan.skip = WordsToSkip;
3776 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003777 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003778
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003779 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003780 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3781 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003782 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003783 WordsToSkip = 0;
3784 WordsToScan = IvarsInfo[i].ivar_size;
3785 }
3786 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003787 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003788 SKIP_SCAN SkScan;
3789 SkScan.skip = WordsToSkip;
3790 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003791 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003792 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003793
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003794 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003795 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003796 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003797 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003798 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003799 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003800 IvarsInfo[LastIndex].ivar_bytepos +
3801 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003802 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003803 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003804 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003805 SKIP_SCAN SkScan;
3806 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3807 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003808 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003809 }
3810 }
3811 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3812 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003813 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003814 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003815 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3816 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3817 // 0xM0 followed by 0x0N detected.
3818 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3819 for (int j = i+1; j < SkipScan; j++)
3820 SkipScanIvars[j] = SkipScanIvars[j+1];
3821 --SkipScan;
3822 }
3823 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003824
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003825 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003826 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003827 unsigned char byte;
3828 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3829 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3830 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3831 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003832
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003833 // first skip big.
3834 for (unsigned int ix = 0; ix < skip_big; ix++)
3835 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003836
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003837 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003838 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003839 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003840 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003841 byte |= 0xf;
3842 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003843 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003844 byte |= scan_small;
3845 scan_small = 0;
3846 }
3847 BitMap += byte;
3848 }
3849 // next scan big
3850 for (unsigned int ix = 0; ix < scan_big; ix++)
3851 BitMap += (unsigned char)(0x0f);
3852 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003853 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003854 byte = scan_small;
3855 BitMap += byte;
3856 }
3857 }
3858 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003859 unsigned char zero = 0;
3860 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003861
3862 llvm::GlobalVariable * Entry =
3863 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3864 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003865 ((ObjCABI == 2) ?
3866 "__TEXT,__objc_classname,cstring_literals" :
3867 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003868 1, true);
3869 return getConstantGEP(VMContext, Entry, 0, 0);
3870}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003871
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003872/// BuildIvarLayout - Builds ivar layout bitmap for the class
3873/// implementation for the __strong or __weak case.
3874/// The layout map displays which words in ivar list must be skipped
3875/// and which must be scanned by GC (see below). String is built of bytes.
3876/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3877/// of words to skip and right nibble is count of words to scan. So, each
3878/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3879/// represented by a 0x00 byte which also ends the string.
3880/// 1. when ForStrongLayout is true, following ivars are scanned:
3881/// - id, Class
3882/// - object *
3883/// - __strong anything
3884///
3885/// 2. When ForStrongLayout is false, following ivars are scanned:
3886/// - __weak anything
3887///
3888llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3889 const ObjCImplementationDecl *OMD,
3890 bool ForStrongLayout) {
3891 bool hasUnion = false;
3892
3893 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
John McCall31168b02011-06-15 23:02:42 +00003894 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
3895 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003896 return llvm::Constant::getNullValue(PtrTy);
3897
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003898 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003899 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
John McCall31168b02011-06-15 23:02:42 +00003900 if (CGM.getLangOptions().ObjCAutoRefCount)
3901 CGM.getContext().ShallowCollectObjCIvars(OI, Ivars);
3902 else
3903 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003904
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003905 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003906 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3907 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3908
3909 if (RecFields.empty())
3910 return llvm::Constant::getNullValue(PtrTy);
3911
3912 SkipIvars.clear();
3913 IvarsInfo.clear();
3914
3915 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3916 if (IvarsInfo.empty())
3917 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003918 // Sort on byte position in case we encounterred a union nested in
3919 // the ivar list.
3920 if (hasUnion && !IvarsInfo.empty())
3921 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3922 if (hasUnion && !SkipIvars.empty())
3923 std::sort(SkipIvars.begin(), SkipIvars.end());
3924
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003925 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003926 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003927
3928 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003929 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003930 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003931 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003932 const unsigned char *s = (unsigned char*)BitMap.c_str();
3933 for (unsigned i = 0; i < BitMap.size(); i++)
3934 if (!(s[i] & 0xf0))
3935 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3936 else
3937 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3938 printf("\n");
3939 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003940 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003941}
3942
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003943llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003944 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3945
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003946 // FIXME: Avoid std::string copying.
3947 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003948 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003949 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003950 ((ObjCABI == 2) ?
3951 "__TEXT,__objc_methname,cstring_literals" :
3952 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003953 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003954
Owen Anderson170229f2009-07-14 23:10:40 +00003955 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003956}
3957
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003958// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003959llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003960 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3961}
3962
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003963llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003964 std::string TypeStr;
3965 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3966
3967 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003968
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003969 if (!Entry)
3970 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003971 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003972 ((ObjCABI == 2) ?
3973 "__TEXT,__objc_methtype,cstring_literals" :
3974 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003975 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003976
Owen Anderson170229f2009-07-14 23:10:40 +00003977 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003978}
3979
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003980llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003981 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00003982 if (CGM.getContext().getObjCEncodingForMethodDecl(
3983 const_cast<ObjCMethodDecl*>(D),
3984 TypeStr))
3985 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003986
3987 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3988
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003989 if (!Entry)
3990 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003991 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003992 ((ObjCABI == 2) ?
3993 "__TEXT,__objc_methtype,cstring_literals" :
3994 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003995 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003996
Owen Anderson170229f2009-07-14 23:10:40 +00003997 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003998}
3999
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004000// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004001llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004002 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004003
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004004 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004005 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004006 llvm::ConstantArray::get(VMContext,
4007 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004008 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004009 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004010
Owen Anderson170229f2009-07-14 23:10:40 +00004011 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004012}
4013
4014// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004015// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004016llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004017CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4018 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004019 std::string TypeStr;
4020 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004021 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4022}
4023
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004024void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004025 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00004026 llvm::SmallVectorImpl<char> &Name) {
4027 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004028 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004029 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4030 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004031 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004032 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004033 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004034 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004035}
4036
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004037void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004038 EmitModuleInfo();
4039
Daniel Dunbarc475d422008-10-29 22:36:39 +00004040 // Emit the dummy bodies for any protocols which were referenced but
4041 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004042 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004043 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4044 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004045 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004046
Daniel Dunbarc475d422008-10-29 22:36:39 +00004047 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00004048 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004049 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004050 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004051 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004052 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004053 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004054 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004055 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004056 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004057 }
4058
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004059 // Add assembler directives to add lazy undefined symbol references
4060 // for classes which are referenced but not defined. This is
4061 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004062 //
4063 // FIXME: It would be nice if we had an LLVM construct for this.
4064 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4065 llvm::SmallString<256> Asm;
4066 Asm += CGM.getModule().getModuleInlineAsm();
4067 if (!Asm.empty() && Asm.back() != '\n')
4068 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004069
Daniel Dunbard027a922009-09-07 00:20:42 +00004070 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004071 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4072 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004073 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4074 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004075 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004076 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004077 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004078 }
4079
4080 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4081 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4082 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4083 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004084
Daniel Dunbard027a922009-09-07 00:20:42 +00004085 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004086 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004087}
4088
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004089CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004090 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004091 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004092 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004093 ObjCABI = 2;
4094}
4095
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004096/* *** */
4097
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004098ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004099 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004100 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4101 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004102
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004103 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004104 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004105 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004106 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004107 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004108
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004109 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004110 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004111 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004112
Mike Stump18bb9282009-05-16 07:57:57 +00004113 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4114 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004115 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004116 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004117
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004118 // I'm not sure I like this. The implicit coordination is a bit
4119 // gross. We should solve this in a reasonable fashion because this
4120 // is a pretty common task (match some runtime data structure with
4121 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004123 // FIXME: This is leaked.
4124 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004126 // struct _objc_super {
4127 // id self;
4128 // Class cls;
4129 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004130 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004131 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004132 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004133 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004134 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004135 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004136 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004137 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004138 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004139
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004140 SuperCTy = Ctx.getTagDeclType(RD);
4141 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004142
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004143 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004144 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4145
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004146 // struct _prop_t {
4147 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004149 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004150 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004151 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004152 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004153
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004154 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004155 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004156 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004157 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004158 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004159 PropertyListTy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004160 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004161 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004162 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004163 PropertyListTy);
4164 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004165 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004166
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004167 // struct _objc_method {
4168 // SEL _cmd;
4169 // char *method_type;
4170 // char *_imp;
4171 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004172 MethodTy = llvm::StructType::get(SelectorPtrTy, Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004173 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004174
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004175 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004176 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004177 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004178 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004179}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004180
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004181ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004182 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004183 // struct _objc_method_description {
4184 // SEL name;
4185 // char *types;
4186 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004187 MethodDescriptionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004188 llvm::StructType::get(SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004189 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004190 MethodDescriptionTy);
4191
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004192 // struct _objc_method_description_list {
4193 // int count;
4194 // struct _objc_method_description[1];
4195 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196 MethodDescriptionListTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004197 llvm::StructType::get(IntTy, llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004198 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004199 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004200 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004201
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004202 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004203 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004204 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004205
Daniel Dunbarb036db82008-08-13 03:21:16 +00004206 // Protocol description structures
4207
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004208 // struct _objc_protocol_extension {
4209 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4210 // struct _objc_method_description_list *optional_instance_methods;
4211 // struct _objc_method_description_list *optional_class_methods;
4212 // struct _objc_property_list *instance_properties;
4213 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004214 ProtocolExtensionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004215 llvm::StructType::get(IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004216 MethodDescriptionListPtrTy,
4217 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004218 PropertyListPtrTy,
4219 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004220 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004221 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004222
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004223 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004224 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004225
Daniel Dunbarc475d422008-10-29 22:36:39 +00004226 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004227
Owen Andersonc36edfe2009-08-13 23:27:53 +00004228 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4229 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004230
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004231 const llvm::Type *T =
Chris Lattner845511f2011-06-18 22:49:11 +00004232 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004233 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004234 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004235 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004236 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4237
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004238 // struct _objc_protocol {
4239 // struct _objc_protocol_extension *isa;
4240 // char *protocol_name;
4241 // struct _objc_protocol **_objc_protocol_list;
4242 // struct _objc_method_description_list *instance_methods;
4243 // struct _objc_method_description_list *class_methods;
4244 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004245 T = llvm::StructType::get(ProtocolExtensionPtrTy, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004246 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004247 MethodDescriptionListPtrTy,
4248 MethodDescriptionListPtrTy,
4249 NULL);
4250 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4251
4252 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004253 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004254 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004255 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004256 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004257
4258 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004259 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004260 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004261
4262 // Class description structures
4263
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004264 // struct _objc_ivar {
4265 // char *ivar_name;
4266 // char *ivar_type;
4267 // int ivar_offset;
4268 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004269 IvarTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004270 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4271
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004272 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004273 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004274 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004275 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004276
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004277 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004278 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004279 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004280 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004281
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004282 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004283 ClassExtensionTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004284 llvm::StructType::get(IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004285 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004286 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004287
Owen Andersonc36edfe2009-08-13 23:27:53 +00004288 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004289
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004290 // struct _objc_class {
4291 // Class isa;
4292 // Class super_class;
4293 // char *name;
4294 // long version;
4295 // long info;
4296 // long instance_size;
4297 // struct _objc_ivar_list *ivars;
4298 // struct _objc_method_list *methods;
4299 // struct _objc_cache *cache;
4300 // struct _objc_protocol_list *protocols;
4301 // char *ivar_layout;
4302 // struct _objc_class_ext *ext;
4303 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004304 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004305 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004306 Int8PtrTy,
4307 LongTy,
4308 LongTy,
4309 LongTy,
4310 IvarListPtrTy,
4311 MethodListPtrTy,
4312 CachePtrTy,
4313 ProtocolListPtrTy,
4314 Int8PtrTy,
4315 ClassExtensionPtrTy,
4316 NULL);
4317 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004318
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004319 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4320 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004321 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004322
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004323 // struct _objc_category {
4324 // char *category_name;
4325 // char *class_name;
4326 // struct _objc_method_list *instance_method;
4327 // struct _objc_method_list *class_method;
4328 // uint32_t size; // sizeof(struct _objc_category)
4329 // struct _objc_property_list *instance_properties;// category's @property
4330 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004331 CategoryTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4332 MethodListPtrTy, ProtocolListPtrTy,
4333 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004334 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4335
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004336 // Global metadata structures
4337
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004338 // struct _objc_symtab {
4339 // long sel_ref_cnt;
4340 // SEL *refs;
4341 // short cls_def_cnt;
4342 // short cat_def_cnt;
4343 // char *defs[cls_def_cnt + cat_def_cnt];
4344 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004345 SymtabTy = llvm::StructType::get(LongTy, SelectorPtrTy, ShortTy, ShortTy,
4346 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004347 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004348 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004349
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004350 // struct _objc_module {
4351 // long version;
4352 // long size; // sizeof(struct _objc_module)
4353 // char *name;
4354 // struct _objc_symtab* symtab;
4355 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004356 ModuleTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004357 llvm::StructType::get(LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004358 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004359
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004360
Mike Stump18bb9282009-05-16 07:57:57 +00004361 // FIXME: This is the size of the setjmp buffer and should be target
4362 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004363 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004364
Anders Carlsson9ff22482008-09-09 10:10:21 +00004365 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004366 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004367 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004368
4369 ExceptionDataTy =
Chris Lattner845511f2011-06-18 22:49:11 +00004370 llvm::StructType::get(
Chris Lattner5e016ae2010-06-27 07:15:29 +00004371 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4372 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004373 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004374 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004375 ExceptionDataTy);
4376
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004377}
4378
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004380 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004381 // struct _method_list_t {
4382 // uint32_t entsize; // sizeof(struct _objc_method)
4383 // uint32_t method_count;
4384 // struct _objc_method method_list[method_count];
4385 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004386 MethodListnfABITy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004387 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004388 NULL);
4389 CGM.getModule().addTypeName("struct.__method_list_t",
4390 MethodListnfABITy);
4391 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004392 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004393
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004394 // struct _protocol_t {
4395 // id isa; // NULL
4396 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004397 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004398 // const struct method_list_t * const instance_methods;
4399 // const struct method_list_t * const class_methods;
4400 // const struct method_list_t *optionalInstanceMethods;
4401 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004402 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004403 // const uint32_t size; // sizeof(struct _protocol_t)
4404 // const uint32_t flags; // = 0
4405 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004406
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004407 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004408 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004409
Chris Lattner845511f2011-06-18 22:49:11 +00004410 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, Int8PtrTy,
4411 ProtocolListTyHolder->getPointerTo(),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004412 MethodListnfABIPtrTy,
4413 MethodListnfABIPtrTy,
4414 MethodListnfABIPtrTy,
4415 MethodListnfABIPtrTy,
4416 PropertyListPtrTy,
4417 IntTy,
4418 IntTy,
4419 NULL);
4420 CGM.getModule().addTypeName("struct._protocol_t",
4421 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004422
4423 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004424 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004425
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004426 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004427 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004428 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004429 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004430 ProtocolListnfABITy = llvm::StructType::get(LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004431 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004432 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004433 NULL);
4434 CGM.getModule().addTypeName("struct._objc_protocol_list",
4435 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004436 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004437 ProtocolListnfABITy);
4438
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004439 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004440 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004441
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004442 // struct _ivar_t {
4443 // unsigned long int *offset; // pointer to ivar offset location
4444 // char *name;
4445 // char *type;
4446 // uint32_t alignment;
4447 // uint32_t size;
4448 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004449 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004450 Int8PtrTy,
4451 Int8PtrTy,
4452 IntTy,
4453 IntTy,
4454 NULL);
4455 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004456
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004457 // struct _ivar_list_t {
4458 // uint32 entsize; // sizeof(struct _ivar_t)
4459 // uint32 count;
4460 // struct _iver_t list[count];
4461 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004462 IvarListnfABITy = llvm::StructType::get(IntTy, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004463 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004464 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004465 NULL);
4466 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004467
Owen Anderson9793f0e2009-07-29 22:16:19 +00004468 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004470 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004471 // uint32_t const flags;
4472 // uint32_t const instanceStart;
4473 // uint32_t const instanceSize;
4474 // uint32_t const reserved; // only when building for 64bit targets
4475 // const uint8_t * const ivarLayout;
4476 // const char *const name;
4477 // const struct _method_list_t * const baseMethods;
4478 // const struct _objc_protocol_list *const baseProtocols;
4479 // const struct _ivar_list_t *const ivars;
4480 // const uint8_t * const weakIvarLayout;
4481 // const struct _prop_list_t * const properties;
4482 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004483
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004484 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner845511f2011-06-18 22:49:11 +00004485 ClassRonfABITy = llvm::StructType::get(IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 IntTy,
4487 IntTy,
4488 Int8PtrTy,
4489 Int8PtrTy,
4490 MethodListnfABIPtrTy,
4491 ProtocolListnfABIPtrTy,
4492 IvarListnfABIPtrTy,
4493 Int8PtrTy,
4494 PropertyListPtrTy,
4495 NULL);
4496 CGM.getModule().addTypeName("struct._class_ro_t",
4497 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004498
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004499 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
John McCall9dc0db22011-05-15 01:53:33 +00004500 const llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
4501 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4502 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004503
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004504 // struct _class_t {
4505 // struct _class_t *isa;
4506 // struct _class_t * const superclass;
4507 // void *cache;
4508 // IMP *vtable;
4509 // struct class_ro_t *ro;
4510 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004511
Owen Andersonc36edfe2009-08-13 23:27:53 +00004512 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004513 ClassnfABITy =
Chris Lattner845511f2011-06-18 22:49:11 +00004514 llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004515 llvm::PointerType::getUnqual(ClassTyHolder),
4516 CachePtrTy,
4517 llvm::PointerType::getUnqual(ImpnfABITy),
4518 llvm::PointerType::getUnqual(ClassRonfABITy),
4519 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004520 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4521
4522 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523 ClassnfABITy);
4524
Fariborz Jahanian71394042009-01-23 23:53:38 +00004525 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004526 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004527
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004528 // struct _category_t {
4529 // const char * const name;
4530 // struct _class_t *const cls;
4531 // const struct _method_list_t * const instance_methods;
4532 // const struct _method_list_t * const class_methods;
4533 // const struct _protocol_list_t * const protocols;
4534 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004535 // }
Chris Lattner845511f2011-06-18 22:49:11 +00004536 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004537 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004538 MethodListnfABIPtrTy,
4539 MethodListnfABIPtrTy,
4540 ProtocolListnfABIPtrTy,
4541 PropertyListPtrTy,
4542 NULL);
4543 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004544
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004545 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004546 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4547 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004549 // MessageRefTy - LLVM for:
4550 // struct _message_ref_t {
4551 // IMP messenger;
4552 // SEL name;
4553 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004554
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004555 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004556 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004557 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004558 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004559 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004560 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004561 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004562 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004563 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004564 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004565
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004566 MessageRefCTy = Ctx.getTagDeclType(RD);
4567 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4568 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004569
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004570 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004571 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004573 // SuperMessageRefTy - LLVM for:
4574 // struct _super_message_ref_t {
4575 // SUPER_IMP messenger;
4576 // SEL name;
4577 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004578 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, SelectorPtrTy, NULL);
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004579 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004580
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004581 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004582 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4583
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004584
4585 // struct objc_typeinfo {
4586 // const void** vtable; // objc_ehtype_vtable + 2
4587 // const char* name; // c++ typeinfo string
4588 // Class cls;
4589 // };
Chris Lattner845511f2011-06-18 22:49:11 +00004590 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004591 Int8PtrTy,
4592 ClassnfABIPtrTy,
4593 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004594 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004595 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004596}
4597
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004598llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004599 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004600
Fariborz Jahanian71394042009-01-23 23:53:38 +00004601 return NULL;
4602}
4603
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004604void CGObjCNonFragileABIMac::AddModuleClassList(const
4605 std::vector<llvm::GlobalValue*>
4606 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004607 const char *SymbolName,
4608 const char *SectionName) {
4609 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004610
Daniel Dunbar19573e72009-05-15 21:48:48 +00004611 if (!NumClasses)
4612 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004613
Daniel Dunbar19573e72009-05-15 21:48:48 +00004614 std::vector<llvm::Constant*> Symbols(NumClasses);
4615 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004616 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004617 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004619 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004620 NumClasses),
4621 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004622
Daniel Dunbar19573e72009-05-15 21:48:48 +00004623 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004624 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 llvm::GlobalValue::InternalLinkage,
4626 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004627 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004628 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004629 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004630 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004631}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004632
Fariborz Jahanian71394042009-01-23 23:53:38 +00004633void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4634 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004635
Daniel Dunbar19573e72009-05-15 21:48:48 +00004636 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004637 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004638 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004639 "\01L_OBJC_LABEL_CLASS_$",
4640 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004641
Fariborz Jahanian67260552009-11-17 21:37:35 +00004642 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4643 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4644 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4645 continue;
4646 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004647 }
4648
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004649 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4650 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4651 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4652 continue;
4653 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4654 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004655
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004656 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004657 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4658 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004659
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004660 // Build list of all implemented category addresses in array
4661 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004662 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004663 "\01L_OBJC_LABEL_CATEGORY_$",
4664 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004665 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004666 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4667 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004668
Daniel Dunbar5e639272010-04-25 20:39:01 +00004669 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004670}
4671
John McCall9e8bb002011-05-14 03:10:52 +00004672/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4673/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004674/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004675/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004676bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4677 // At various points we've experimented with using vtable-based
4678 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004679 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4680 default:
John McCall9e8bb002011-05-14 03:10:52 +00004681 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004682 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004683 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004684 case CodeGenOptions::NonLegacy:
4685 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004686 case CodeGenOptions::Mixed:
4687 break;
4688 }
4689
4690 // If so, see whether this selector is in the white-list of things which must
4691 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004692 if (VTableDispatchMethods.empty()) {
4693 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4694 VTableDispatchMethods.insert(GetNullarySelector("class"));
4695 VTableDispatchMethods.insert(GetNullarySelector("self"));
4696 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4697 VTableDispatchMethods.insert(GetNullarySelector("length"));
4698 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004699
John McCall9e8bb002011-05-14 03:10:52 +00004700 // These are vtable-based if GC is disabled.
4701 // Optimistically use vtable dispatch for hybrid compiles.
4702 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
4703 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4704 VTableDispatchMethods.insert(GetNullarySelector("release"));
4705 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4706 }
4707
4708 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4709 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4710 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4711 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4712 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4713 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4714 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4715
4716 // These are vtable-based if GC is enabled.
4717 // Optimistically use vtable dispatch for hybrid compiles.
4718 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
4719 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4720 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4721
4722 // "countByEnumeratingWithState:objects:count"
4723 IdentifierInfo *KeyIdents[] = {
4724 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4725 &CGM.getContext().Idents.get("objects"),
4726 &CGM.getContext().Idents.get("count")
4727 };
4728 VTableDispatchMethods.insert(
4729 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4730 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004731 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004732
John McCall9e8bb002011-05-14 03:10:52 +00004733 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004734}
4735
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004736// Metadata flags
4737enum MetaDataDlags {
4738 CLS = 0x0,
4739 CLS_META = 0x1,
4740 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004741 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004742 CLS_EXCEPTION = 0x20,
4743
4744 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4745 CLS_HAS_IVAR_RELEASER = 0x40,
4746 /// class was compiled with -fobjc-arr
4747 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004748};
4749/// BuildClassRoTInitializer - generate meta-data for:
4750/// struct _class_ro_t {
4751/// uint32_t const flags;
4752/// uint32_t const instanceStart;
4753/// uint32_t const instanceSize;
4754/// uint32_t const reserved; // only when building for 64bit targets
4755/// const uint8_t * const ivarLayout;
4756/// const char *const name;
4757/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004758/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004759/// const struct _ivar_list_t *const ivars;
4760/// const uint8_t * const weakIvarLayout;
4761/// const struct _prop_list_t * const properties;
4762/// }
4763///
4764llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004765 unsigned flags,
4766 unsigned InstanceStart,
4767 unsigned InstanceSize,
4768 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004769 std::string ClassName = ID->getNameAsString();
4770 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004771
4772 if (CGM.getLangOptions().ObjCAutoRefCount)
4773 flags |= CLS_COMPILED_BY_ARC;
4774
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004775 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4776 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4777 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004778 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004779 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4780 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004781 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004782 // const struct _method_list_t * const baseMethods;
4783 std::vector<llvm::Constant*> Methods;
4784 std::string MethodListName("\01l_OBJC_$_");
4785 if (flags & CLS_META) {
4786 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004787 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004788 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004789 // Class methods should always be defined.
4790 Methods.push_back(GetMethodConstant(*i));
4791 }
4792 } else {
4793 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004795 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004796 // Instance methods should always be defined.
4797 Methods.push_back(GetMethodConstant(*i));
4798 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004800 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004801 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004803 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4804 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004805
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004806 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4807 if (llvm::Constant *C = GetMethodConstant(MD))
4808 Methods.push_back(C);
4809 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4810 if (llvm::Constant *C = GetMethodConstant(MD))
4811 Methods.push_back(C);
4812 }
4813 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004814 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004815 Values[ 5] = EmitMethodList(MethodListName,
4816 "__DATA, __objc_const", Methods);
4817
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004818 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4819 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004820 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004821 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004822 OID->all_referenced_protocol_begin(),
4823 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004824
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004825 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004826 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004827 else
4828 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004829 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4830 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004831 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004832 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004833 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004834 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4835 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004836 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004837 Values);
4838 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004839 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4840 llvm::GlobalValue::InternalLinkage,
4841 Init,
4842 (flags & CLS_META) ?
4843 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4844 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004845 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004846 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004847 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004848 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004849
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004850}
4851
4852/// BuildClassMetaData - This routine defines that to-level meta-data
4853/// for the given ClassName for:
4854/// struct _class_t {
4855/// struct _class_t *isa;
4856/// struct _class_t * const superclass;
4857/// void *cache;
4858/// IMP *vtable;
4859/// struct class_ro_t *ro;
4860/// }
4861///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004862llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004863 std::string &ClassName,
4864 llvm::Constant *IsAGV,
4865 llvm::Constant *SuperClassGV,
4866 llvm::Constant *ClassRoGV,
4867 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004868 std::vector<llvm::Constant*> Values(5);
4869 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004870 Values[1] = SuperClassGV;
4871 if (!Values[1])
4872 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004873 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4874 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4875 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004877 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004878 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4879 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004880 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004881 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004882 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004883 if (HiddenVisibility)
4884 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004885 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004886}
4887
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004888bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004889CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004890 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004891}
4892
Daniel Dunbar961202372009-05-03 12:57:56 +00004893void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004894 uint32_t &InstanceStart,
4895 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004896 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004897 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004898
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004899 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004900 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004901
4902 // If there are no fields, the start is the same as the end.
4903 if (!RL.getFieldCount())
4904 InstanceStart = InstanceSize;
4905 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004906 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004907}
4908
Fariborz Jahanian71394042009-01-23 23:53:38 +00004909void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4910 std::string ClassName = ID->getNameAsString();
4911 if (!ObjCEmptyCacheVar) {
4912 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004913 CGM.getModule(),
4914 ObjCTypes.CacheTy,
4915 false,
4916 llvm::GlobalValue::ExternalLinkage,
4917 0,
4918 "_objc_empty_cache");
4919
Fariborz Jahanian71394042009-01-23 23:53:38 +00004920 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004921 CGM.getModule(),
4922 ObjCTypes.ImpnfABITy,
4923 false,
4924 llvm::GlobalValue::ExternalLinkage,
4925 0,
4926 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004927 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004929 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004930 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004931 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004932 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004933 uint32_t InstanceSize = InstanceStart;
4934 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004935 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4936 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004937
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004938 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004939
4940 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004941 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004942 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004943 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004944 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004945 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004946 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004947 // class is root
4948 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004949 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004950 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004951 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004952 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004953 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4954 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4955 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004956 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004957 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004958 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004959 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004960 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004961 ObjCMetaClassName +
4962 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004963 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004964 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004965 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004966 }
4967 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4968 InstanceStart,
4969 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004970 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004971 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004972 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4973 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004974 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004975
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004976 // Metadata for the class
4977 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004978 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004979 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004980 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004981 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004982
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004983 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004984 flags |= CLS_EXCEPTION;
4985
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004986 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004987 flags |= CLS_ROOT;
4988 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004989 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004990 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004991 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004992 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004993 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004994 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004995 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004996 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004997 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004998 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004999 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005000 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005001 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005002
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005003 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005004 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005005 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5006 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005007 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005008
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005009 // Determine if this class is also "non-lazy".
5010 if (ImplementationIsNonLazy(ID))
5011 DefinedNonLazyClasses.push_back(ClassMD);
5012
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005013 // Force the definition of the EHType if necessary.
5014 if (flags & CLS_EXCEPTION)
5015 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005016 // Make sure method definition entries are all clear for next implementation.
5017 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005018}
5019
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005020/// GenerateProtocolRef - This routine is called to generate code for
5021/// a protocol reference expression; as in:
5022/// @code
5023/// @protocol(Proto1);
5024/// @endcode
5025/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5026/// which will hold address of the protocol meta-data.
5027///
5028llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005029 const ObjCProtocolDecl *PD) {
5030
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005031 // This routine is called for @protocol only. So, we must build definition
5032 // of protocol's meta-data (not a reference to it!)
5033 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005034 llvm::Constant *Init =
5035 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5036 ObjCTypes.ExternalProtocolPtrTy);
5037
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005038 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005039 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005040
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005041 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5042 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005043 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005044 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005045 CGM.getModule(),
5046 Init->getType(), false,
5047 llvm::GlobalValue::WeakAnyLinkage,
5048 Init,
5049 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005050 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5051 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005052 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005053 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005054}
5055
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005056/// GenerateCategory - Build metadata for a category implementation.
5057/// struct _category_t {
5058/// const char * const name;
5059/// struct _class_t *const cls;
5060/// const struct _method_list_t * const instance_methods;
5061/// const struct _method_list_t * const class_methods;
5062/// const struct _protocol_list_t * const protocols;
5063/// const struct _prop_list_t * const properties;
5064/// }
5065///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005066void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005067 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005068 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005069 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5070 "_$_" + OCD->getNameAsString());
5071 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005072 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005074 std::vector<llvm::Constant*> Values(6);
5075 Values[0] = GetClassName(OCD->getIdentifier());
5076 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005077 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005078 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005079 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5080
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005081 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005082 std::vector<llvm::Constant*> Methods;
5083 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005084 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005085 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005086
5087 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005088 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005089 // Instance methods should always be defined.
5090 Methods.push_back(GetMethodConstant(*i));
5091 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005092
5093 Values[2] = EmitMethodList(MethodListName,
5094 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005095 Methods);
5096
5097 MethodListName = Prefix;
5098 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5099 OCD->getNameAsString();
5100 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005101 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005102 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005103 // Class methods should always be defined.
5104 Methods.push_back(GetMethodConstant(*i));
5105 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005106
5107 Values[3] = EmitMethodList(MethodListName,
5108 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005109 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005110 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005111 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005112 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005113 llvm::SmallString<256> ExtName;
5114 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5115 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005116 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005117 + Interface->getName() + "_$_"
5118 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005119 Category->protocol_begin(),
5120 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005121 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5122 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005123 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005124 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5125 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005126 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005127
5128 llvm::Constant *Init =
5129 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005130 Values);
5131 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005132 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005133 false,
5134 llvm::GlobalValue::InternalLinkage,
5135 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005136 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005137 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005138 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005139 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005140 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005141 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005142
5143 // Determine if this category is also "non-lazy".
5144 if (ImplementationIsNonLazy(OCD))
5145 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005146 // method definition entries must be clear for next implementation.
5147 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005148}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005149
5150/// GetMethodConstant - Return a struct objc_method constant for the
5151/// given method if it has been defined. The result is null if the
5152/// method has not been defined. The return value has type MethodPtrTy.
5153llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005155 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005156 if (!Fn)
5157 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005158
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005159 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005160 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005161 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005162 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005163 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005164 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005165 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005166}
5167
5168/// EmitMethodList - Build meta-data for method declarations
5169/// struct _method_list_t {
5170/// uint32_t entsize; // sizeof(struct _objc_method)
5171/// uint32_t method_count;
5172/// struct _objc_method method_list[method_count];
5173/// }
5174///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005175llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5176 const char *Section,
5177 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005178 // Return null for empty list.
5179 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005180 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005181
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005182 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005183 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005184 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005185 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005186 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005187 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005188 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005189 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005190 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005191 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005192
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005193 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005194 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005195 llvm::GlobalValue::InternalLinkage, Init, Name);
5196 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005197 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005198 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005199 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005200}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005201
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005202/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5203/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005204llvm::GlobalVariable *
5205CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5206 const ObjCIvarDecl *Ivar) {
5207 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005208 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005209 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005210 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005211 CGM.getModule().getGlobalVariable(Name);
5212 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005213 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005214 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005215 false,
5216 llvm::GlobalValue::ExternalLinkage,
5217 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005218 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005219 return IvarOffsetGV;
5220}
5221
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005222llvm::Constant *
5223CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5224 const ObjCIvarDecl *Ivar,
5225 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005226 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005227 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005228 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005229 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005230 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005231
Mike Stump18bb9282009-05-16 07:57:57 +00005232 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5233 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005234 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5235 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005236 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005237 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005238 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005239 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005240 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005241 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005242}
5243
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005244/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005245/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005246/// IvarListnfABIPtrTy.
5247/// struct _ivar_t {
5248/// unsigned long int *offset; // pointer to ivar offset location
5249/// char *name;
5250/// char *type;
5251/// uint32_t alignment;
5252/// uint32_t size;
5253/// }
5254/// struct _ivar_list_t {
5255/// uint32 entsize; // sizeof(struct _ivar_t)
5256/// uint32 count;
5257/// struct _iver_t list[count];
5258/// }
5259///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005260
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005261llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005262 const ObjCImplementationDecl *ID) {
5263
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005264 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005265
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005266 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5267 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005268
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005269 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005270
Daniel Dunbarae032262009-04-20 00:33:43 +00005271 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005272 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005273 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005274
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005275 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5276 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005277 // Ignore unnamed bit-fields.
5278 if (!IVD->getDeclName())
5279 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005280 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005281 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005282 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5283 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005284 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005285 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005286 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005287 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005289 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005290 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005291 // NOTE. Size of a bitfield does not match gcc's, because of the
5292 // way bitfields are treated special in each. But I am told that
5293 // 'size' for bitfield ivars is ignored by the runtime so it does
5294 // not matter. If it matters, there is enough info to get the
5295 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005296 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005297 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005298 }
5299 // Return null for empty list.
5300 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005301 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005302
5303 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005304 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005305 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5306 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005307 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005308 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005309 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005310 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005311 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5312 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005313 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005314 llvm::GlobalValue::InternalLinkage,
5315 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005316 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005317 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005318 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005319 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005320
Chris Lattnerf56501c2009-07-17 23:57:13 +00005321 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005322 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005323}
5324
5325llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005327 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005328
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005329 if (!Entry) {
5330 // We use the initializer as a marker of whether this is a forward
5331 // reference or not. At module finalization we add the empty
5332 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005333 Entry =
5334 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5335 llvm::GlobalValue::ExternalLinkage,
5336 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005337 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005338 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005339 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005340
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005341 return Entry;
5342}
5343
5344/// GetOrEmitProtocol - Generate the protocol meta-data:
5345/// @code
5346/// struct _protocol_t {
5347/// id isa; // NULL
5348/// const char * const protocol_name;
5349/// const struct _protocol_list_t * protocol_list; // super protocols
5350/// const struct method_list_t * const instance_methods;
5351/// const struct method_list_t * const class_methods;
5352/// const struct method_list_t *optionalInstanceMethods;
5353/// const struct method_list_t *optionalClassMethods;
5354/// const struct _prop_list_t * properties;
5355/// const uint32_t size; // sizeof(struct _protocol_t)
5356/// const uint32_t flags; // = 0
5357/// }
5358/// @endcode
5359///
5360
5361llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005362 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005363 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005364
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005365 // Early exit if a defining object has already been generated.
5366 if (Entry && Entry->hasInitializer())
5367 return Entry;
5368
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005369 // Construct method lists.
5370 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5371 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005372 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005373 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005374 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005375 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005376 if (!C)
5377 return GetOrEmitProtocolRef(PD);
5378
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005379 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5380 OptInstanceMethods.push_back(C);
5381 } else {
5382 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005383 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005385
5386 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005387 i = PD->classmeth_begin(), e = PD->classmeth_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 OptClassMethods.push_back(C);
5395 } else {
5396 ClassMethods.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
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 std::vector<llvm::Constant*> Values(10);
5401 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005402 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005403 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005404 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5405 PD->protocol_begin(),
5406 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005407
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005408 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005409 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005410 "__DATA, __objc_const",
5411 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005412 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005413 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 "__DATA, __objc_const",
5415 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005416 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005417 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005418 "__DATA, __objc_const",
5419 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005420 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005421 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005422 "__DATA, __objc_const",
5423 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005424 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005425 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005426 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005427 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005428 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005429 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005430 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005431 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005432
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 if (Entry) {
5434 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005435 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005436 Entry->setInitializer(Init);
5437 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005438 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005439 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5440 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5441 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005442 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005443 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005444 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005445 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005446 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005447 CGM.AddUsedGlobal(Entry);
5448
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005449 // Use this protocol meta-data to build protocol list table in section
5450 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005451 llvm::GlobalVariable *PTGV =
5452 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5453 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5454 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005455 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005456 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005457 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005458 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005459 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005460 return Entry;
5461}
5462
5463/// EmitProtocolList - Generate protocol list meta-data:
5464/// @code
5465/// struct _protocol_list_t {
5466/// long protocol_count; // Note, this is 32/64 bit
5467/// struct _protocol_t[protocol_count];
5468/// }
5469/// @endcode
5470///
5471llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005472CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5473 ObjCProtocolDecl::protocol_iterator begin,
5474 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005475 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005476
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005477 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005479 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005480
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005481 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005482 llvm::SmallString<256> TmpName;
5483 Name.toVector(TmpName);
5484 llvm::GlobalVariable *GV =
5485 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005486 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005487 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005488
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005489 for (; begin != end; ++begin)
5490 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5491
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005492 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005493 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005494 ObjCTypes.ProtocolnfABIPtrTy));
5495
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005496 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005497 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005498 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005499 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005500 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005501 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005502 ProtocolRefs.size()),
5503 ProtocolRefs);
5504
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005505 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005506 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005507 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005508 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005509 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005510 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005511 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005512 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005513 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005514 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005515}
5516
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005517/// GetMethodDescriptionConstant - This routine build following meta-data:
5518/// struct _objc_method {
5519/// SEL _cmd;
5520/// char *method_type;
5521/// char *_imp;
5522/// }
5523
5524llvm::Constant *
5525CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5526 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005527 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005528 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5529 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005530 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005531 if (!Desc[1])
5532 return 0;
5533
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005534 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005535 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005536 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005537}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005538
5539/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5540/// This code gen. amounts to generating code for:
5541/// @code
5542/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5543/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005545LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005546 CodeGen::CodeGenFunction &CGF,
5547 QualType ObjectTy,
5548 llvm::Value *BaseValue,
5549 const ObjCIvarDecl *Ivar,
5550 unsigned CVRQualifiers) {
5551 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005552 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5553 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005554}
5555
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005556llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005557 CodeGen::CodeGenFunction &CGF,
5558 const ObjCInterfaceDecl *Interface,
5559 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005560 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005561}
5562
John McCall234eac82011-05-13 23:16:18 +00005563static void appendSelectorForMessageRefTable(std::string &buffer,
5564 Selector selector) {
5565 if (selector.isUnarySelector()) {
5566 buffer += selector.getNameForSlot(0);
5567 return;
5568 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005569
John McCall234eac82011-05-13 23:16:18 +00005570 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5571 buffer += selector.getNameForSlot(i);
5572 buffer += '_';
5573 }
5574}
5575
John McCall9e8bb002011-05-14 03:10:52 +00005576/// Emit a "v-table" message send. We emit a weak hidden-visibility
5577/// struct, initially containing the selector pointer and a pointer to
5578/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5579/// load and call the function pointer, passing the address of the
5580/// struct as the second parameter. The runtime determines whether
5581/// the selector is currently emitted using vtable dispatch; if so, it
5582/// substitutes a stub function which simply tail-calls through the
5583/// appropriate vtable slot, and if not, it substitues a stub function
5584/// which tail-calls objc_msgSend. Both stubs adjust the selector
5585/// argument to correctly point to the selector.
5586RValue
5587CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5588 ReturnValueSlot returnSlot,
5589 QualType resultType,
5590 Selector selector,
5591 llvm::Value *arg0,
5592 QualType arg0Type,
5593 bool isSuper,
5594 const CallArgList &formalArgs,
5595 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005596 // Compute the actual arguments.
5597 CallArgList args;
5598
John McCall9e8bb002011-05-14 03:10:52 +00005599 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005600 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005601 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5602 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005603
John McCall9e8bb002011-05-14 03:10:52 +00005604 // Second argument: a pointer to the message ref structure. Leave
5605 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005606 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5607
5608 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5609
5610 const CGFunctionInfo &fnInfo =
5611 CGM.getTypes().getFunctionInfo(resultType, args,
5612 FunctionType::ExtInfo());
5613
John McCall5880fb82011-05-14 21:12:11 +00005614 NullReturnState nullReturn;
5615
John McCall9e8bb002011-05-14 03:10:52 +00005616 // Find the function to call and the mangled name for the message
5617 // ref structure. Using a different mangled name wouldn't actually
5618 // be a problem; it would just be a waste.
5619 //
5620 // The runtime currently never uses vtable dispatch for anything
5621 // except normal, non-super message-sends.
5622 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005623 llvm::Constant *fn = 0;
5624 std::string messageRefName("\01l_");
5625 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005626 if (isSuper) {
5627 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5628 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005629 } else {
John McCall5880fb82011-05-14 21:12:11 +00005630 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005631 fn = ObjCTypes.getMessageSendStretFixupFn();
5632 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005633 }
John McCall234eac82011-05-13 23:16:18 +00005634 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5635 fn = ObjCTypes.getMessageSendFpretFixupFn();
5636 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005637 } else {
John McCall234eac82011-05-13 23:16:18 +00005638 if (isSuper) {
5639 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5640 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005641 } else {
John McCall234eac82011-05-13 23:16:18 +00005642 fn = ObjCTypes.getMessageSendFixupFn();
5643 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005644 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005645 }
John McCall234eac82011-05-13 23:16:18 +00005646 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5647 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005648
John McCall234eac82011-05-13 23:16:18 +00005649 // Append the selector name, except use underscores anywhere we
5650 // would have used colons.
5651 appendSelectorForMessageRefTable(messageRefName, selector);
5652
5653 llvm::GlobalVariable *messageRef
5654 = CGM.getModule().getGlobalVariable(messageRefName);
5655 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005656 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005657 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005658 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005659 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5660 init->getType(),
5661 /*constant*/ false,
5662 llvm::GlobalValue::WeakAnyLinkage,
5663 init,
5664 messageRefName);
5665 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5666 messageRef->setAlignment(16);
5667 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5668 }
5669 llvm::Value *mref =
5670 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5671
John McCall9e8bb002011-05-14 03:10:52 +00005672 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005673 args[1].RV = RValue::get(mref);
5674
5675 // Load the function to call from the message ref table.
5676 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5677 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5678
5679 bool variadic = method ? method->isVariadic() : false;
5680 const llvm::FunctionType *fnType =
5681 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5682 callee = CGF.Builder.CreateBitCast(callee,
5683 llvm::PointerType::getUnqual(fnType));
5684
John McCall5880fb82011-05-14 21:12:11 +00005685 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5686 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005687}
5688
5689/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005690CodeGen::RValue
5691CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005692 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005693 QualType ResultType,
5694 Selector Sel,
5695 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005696 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005697 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005698 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005699 return isVTableDispatchedSelector(Sel)
5700 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005702 false, CallArgs, Method)
5703 : EmitMessageSend(CGF, Return, ResultType,
5704 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005705 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005706 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005707}
5708
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005709llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005710CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005711 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5712
Daniel Dunbara6468342009-03-02 05:18:14 +00005713 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005714 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005715 false, llvm::GlobalValue::ExternalLinkage,
5716 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005717 }
5718
5719 return GV;
5720}
5721
John McCall31168b02011-06-15 23:02:42 +00005722llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5723 IdentifierInfo *II) {
5724 llvm::GlobalVariable *&Entry = ClassReferences[II];
5725
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005726 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005727 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005728 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005729 Entry =
John McCall31168b02011-06-15 23:02:42 +00005730 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5731 false, llvm::GlobalValue::InternalLinkage,
5732 ClassGV,
5733 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005734 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005735 CGM.getTargetData().getABITypeAlignment(
5736 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005737 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005738 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005739 }
John McCall31168b02011-06-15 23:02:42 +00005740
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005741 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005742}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005743
John McCall31168b02011-06-15 23:02:42 +00005744llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5745 const ObjCInterfaceDecl *ID) {
5746 return EmitClassRefFromId(Builder, ID->getIdentifier());
5747}
5748
5749llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5750 CGBuilderTy &Builder) {
5751 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5752 return EmitClassRefFromId(Builder, II);
5753}
5754
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005755llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005756CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005757 const ObjCInterfaceDecl *ID) {
5758 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005759
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005760 if (!Entry) {
5761 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5762 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005763 Entry =
5764 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005765 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005766 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005767 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005768 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005769 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005770 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005771 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005772 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005773 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005774
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005775 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005776}
5777
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005778/// EmitMetaClassRef - Return a Value * of the address of _class_t
5779/// meta-data
5780///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005781llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5782 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005783 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5784 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005785 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005786
Daniel Dunbar15894b72009-04-07 05:48:37 +00005787 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005788 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005789 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005790 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005791 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005792 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005793 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005794 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005795 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005796 ObjCTypes.ClassnfABIPtrTy));
5797
Daniel Dunbare60aa052009-04-15 19:03:14 +00005798 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005799 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005800
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005801 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005802}
5803
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005804/// GetClass - Return a reference to the class for the given interface
5805/// decl.
5806llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5807 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005808 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005809 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5810 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5811 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5812 }
5813
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005814 return EmitClassRef(Builder, ID);
5815}
5816
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005817/// Generates a message send where the super is the receiver. This is
5818/// a message send to self with special delivery semantics indicating
5819/// which class's method should be called.
5820CodeGen::RValue
5821CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005822 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005823 QualType ResultType,
5824 Selector Sel,
5825 const ObjCInterfaceDecl *Class,
5826 bool isCategoryImpl,
5827 llvm::Value *Receiver,
5828 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005829 const CodeGen::CallArgList &CallArgs,
5830 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005831 // ...
5832 // Create and init a super structure; this is a (receiver, class)
5833 // pair we will pass to objc_msgSendSuper.
5834 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005835 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005836
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005837 llvm::Value *ReceiverAsObject =
5838 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5839 CGF.Builder.CreateStore(ReceiverAsObject,
5840 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005841
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005842 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005843 llvm::Value *Target;
5844 if (IsClassMessage) {
5845 if (isCategoryImpl) {
5846 // Message sent to "super' in a class method defined in
5847 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005848 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005849 Target = CGF.Builder.CreateStructGEP(Target, 0);
5850 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005851 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005852 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005853 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005854 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005855
Mike Stump18bb9282009-05-16 07:57:57 +00005856 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5857 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005858 const llvm::Type *ClassTy =
5859 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5860 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5861 CGF.Builder.CreateStore(Target,
5862 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005863
John McCall9e8bb002011-05-14 03:10:52 +00005864 return (isVTableDispatchedSelector(Sel))
5865 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005866 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005867 true, CallArgs, Method)
5868 : EmitMessageSend(CGF, Return, ResultType,
5869 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005870 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005871 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005872}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005873
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005874llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005875 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005876 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005877
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005878 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005879 llvm::Constant *Casted =
5880 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5881 ObjCTypes.SelectorPtrTy);
5882 Entry =
5883 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5884 llvm::GlobalValue::InternalLinkage,
5885 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005886 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005887 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005888 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005889
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005890 if (lval)
5891 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005892 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005893}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005894/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005895/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005896///
5897void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005898 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005899 llvm::Value *dst,
5900 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005901 const llvm::Type * SrcTy = src->getType();
5902 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005903 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005904 assert(Size <= 8 && "does not support size > 8");
5905 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5906 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005907 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5908 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005909 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5910 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005911 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5912 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005913 return;
5914}
5915
5916/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5917/// objc_assign_strongCast (id src, id *dst)
5918///
5919void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005920 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005921 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005922 const llvm::Type * SrcTy = src->getType();
5923 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005924 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005925 assert(Size <= 8 && "does not support size > 8");
5926 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005927 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005928 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5929 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005930 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5931 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005932 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005933 src, dst, "weakassign");
5934 return;
5935}
5936
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005937void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005938 CodeGen::CodeGenFunction &CGF,
5939 llvm::Value *DestPtr,
5940 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005941 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005942 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5943 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005944 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005945 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005946 return;
5947}
5948
Fariborz Jahanian06292952009-02-16 22:52:32 +00005949/// EmitObjCWeakRead - Code gen for loading value of a __weak
5950/// object: objc_read_weak (id *src)
5951///
5952llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005953 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005954 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005955 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005956 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5957 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005958 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005959 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005960 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005961 return read_weak;
5962}
5963
5964/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5965/// objc_assign_weak (id src, id *dst)
5966///
5967void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005968 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005969 const llvm::Type * SrcTy = src->getType();
5970 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005971 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005972 assert(Size <= 8 && "does not support size > 8");
5973 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5974 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005975 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5976 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005977 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5978 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005979 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005980 src, dst, "weakassign");
5981 return;
5982}
5983
5984/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5985/// objc_assign_global (id src, id *dst)
5986///
5987void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005988 llvm::Value *src, llvm::Value *dst,
5989 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005990 const llvm::Type * SrcTy = src->getType();
5991 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005992 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005993 assert(Size <= 8 && "does not support size > 8");
5994 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5995 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005996 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5997 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005998 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5999 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006000 if (!threadlocal)
6001 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6002 src, dst, "globalassign");
6003 else
6004 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6005 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006006 return;
6007}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006008
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006009void
John McCallbd309292010-07-06 01:34:17 +00006010CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6011 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006012 EmitAtSynchronizedStmt(CGF, S,
6013 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6014 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006015}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006016
John McCall2ca705e2010-07-24 00:37:23 +00006017llvm::Constant *
6018CGObjCNonFragileABIMac::GetEHType(QualType T) {
6019 // There's a particular fixed type info for 'id'.
6020 if (T->isObjCIdType() ||
6021 T->isObjCQualifiedIdType()) {
6022 llvm::Constant *IDEHType =
6023 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6024 if (!IDEHType)
6025 IDEHType =
6026 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6027 false,
6028 llvm::GlobalValue::ExternalLinkage,
6029 0, "OBJC_EHTYPE_id");
6030 return IDEHType;
6031 }
6032
6033 // All other types should be Objective-C interface pointer types.
6034 const ObjCObjectPointerType *PT =
6035 T->getAs<ObjCObjectPointerType>();
6036 assert(PT && "Invalid @catch type.");
6037 const ObjCInterfaceType *IT = PT->getInterfaceType();
6038 assert(IT && "Invalid @catch type.");
6039 return GetInterfaceEHType(IT->getDecl(), false);
6040}
6041
John McCallbd309292010-07-06 01:34:17 +00006042void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6043 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006044 EmitTryCatchStmt(CGF, S,
6045 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6046 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6047 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006048}
6049
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006050/// EmitThrowStmt - Generate code for a throw statement.
6051void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6052 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006053 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00006054 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00006055 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6056 "tmp");
John McCall17afe452010-10-16 08:21:07 +00006057 llvm::Value *Args[] = { Exception };
6058 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6059 Args, Args+1)
6060 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006061 } else {
John McCall17afe452010-10-16 08:21:07 +00006062 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6063 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006064 }
6065
John McCall17afe452010-10-16 08:21:07 +00006066 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006067 CGF.Builder.ClearInsertionPoint();
6068}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006069
John McCall2ca705e2010-07-24 00:37:23 +00006070llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006071CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006072 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006073 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006074
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006075 // If we don't need a definition, return the entry if found or check
6076 // if we use an external reference.
6077 if (!ForDefinition) {
6078 if (Entry)
6079 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006080
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006081 // If this type (or a super class) has the __objc_exception__
6082 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006083 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006084 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006085 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006086 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006087 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006088 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006089 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006090 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006091
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006092 // Otherwise we need to either make a new entry or fill in the
6093 // initializer.
6094 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006095 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006096 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006097 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006098 CGM.getModule().getGlobalVariable(VTableName);
6099 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006100 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6101 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006102 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006103 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006104
Chris Lattner5e016ae2010-06-27 07:15:29 +00006105 llvm::Value *VTableIdx =
6106 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006107
6108 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006109 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006110 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006111 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006112 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006113 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006114
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006115 if (Entry) {
6116 Entry->setInitializer(Init);
6117 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006118 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006119 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006120 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006121 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006122 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006123 }
6124
John McCall457a04e2010-10-22 21:05:15 +00006125 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006126 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006127 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6128 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006129
6130 if (ForDefinition) {
6131 Entry->setSection("__DATA,__objc_const");
6132 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6133 } else {
6134 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6135 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006136
6137 return Entry;
6138}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006139
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006140/* *** */
6141
Daniel Dunbarb036db82008-08-13 03:21:16 +00006142CodeGen::CGObjCRuntime *
6143CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006144 if (CGM.getLangOptions().ObjCNonFragileABI)
6145 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006146 return new CGObjCMac(CGM);
6147}