blob: 044bc18c1e7c9e0bc46835fe9e8ebc560a625699 [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"
John McCallad7c5c12011-02-08 08:22:06 +000015#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000016#include "CGCleanup.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGRecordLayout.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000026#include "clang/CodeGen/CGFunctionInfo.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/ADT/DenseSet.h"
29#include "llvm/ADT/SetVector.h"
30#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallString.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/InlineAsm.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.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"
Torok Edwindb714922009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000043
44namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000045
Daniel Dunbar59e476b2009-08-03 17:06:42 +000046// FIXME: We should find a nicer way to make the labels for metadata, string
47// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000048
Fariborz Jahanian279eda62009-01-21 22:04:16 +000049class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000050protected:
51 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000052
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000053private:
John McCall9dc0db22011-05-15 01:53:33 +000054 // The types of these functions don't really matter because we
55 // should always bitcast before calling them.
56
57 /// id objc_msgSend (id, SEL, ...)
58 ///
59 /// The default messenger, used for sends whose ABI is unchanged from
60 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000061 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000062 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
63 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000064 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Bill Wendling8594fcb2013-01-31 00:30:05 +000065 return
66 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
68 "objc_msgSend",
69 llvm::AttributeSet::get(CGM.getLLVMContext(),
70 llvm::AttributeSet::FunctionIndex,
71 llvm::Attribute::NonLazyBind));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000072 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000073
John McCall9dc0db22011-05-15 01:53:33 +000074 /// void objc_msgSend_stret (id, SEL, ...)
75 ///
76 /// The messenger used when the return value is an aggregate returned
77 /// by indirect reference in the first argument, and therefore the
78 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000079 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000080 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000081 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
82 params, true),
83 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000084
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000085 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000086
John McCall9dc0db22011-05-15 01:53:33 +000087 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
88 ///
89 /// The messenger used when the return value is returned on the x87
90 /// floating-point stack; without a special entrypoint, the nil case
91 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000092 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000093 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattnerece04092012-02-07 00:39:47 +000094 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
95 params, true),
John McCall9dc0db22011-05-15 01:53:33 +000096 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000097
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000098 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000099
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000100 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
101 ///
102 /// The messenger used when the return value is returned in two values on the
103 /// x87 floating point stack; without a special entrypoint, the nil case
104 /// would be unbalanced. Only used on 64-bit X86.
105 llvm::Constant *getMessageSendFp2retFn() const {
106 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
107 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
108 llvm::Type *resultType =
109 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
110
111 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
112 params, true),
113 "objc_msgSend_fp2ret");
114 }
115
John McCall9dc0db22011-05-15 01:53:33 +0000116 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
117 ///
118 /// The messenger used for super calls, which have different dispatch
119 /// semantics. The class passed is the superclass of the current
120 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000121 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000122 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000123 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000124 params, true),
125 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000126 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000127
John McCall9dc0db22011-05-15 01:53:33 +0000128 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
129 ///
130 /// A slightly different messenger used for super calls. The class
131 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000132 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000133 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000134 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000135 params, true),
136 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000137 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000138
John McCall9dc0db22011-05-15 01:53:33 +0000139 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
140 /// SEL op, ...)
141 ///
142 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000143 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000144 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000145 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000146 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000147 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000148 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000149
John McCall9dc0db22011-05-15 01:53:33 +0000150 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
151 /// SEL op, ...)
152 ///
153 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000154 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000155 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000156 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000157 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000158 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000159 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000161 llvm::Constant *getMessageSendSuperFpretFn() const {
162 // There is no objc_msgSendSuper_fpret? How can that work?
163 return getMessageSendSuperFn();
164 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000165
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000166 llvm::Constant *getMessageSendSuperFpretFn2() const {
167 // There is no objc_msgSendSuper_fpret? How can that work?
168 return getMessageSendSuperFn2();
169 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000170
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000171protected:
172 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000173
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000174public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000175 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000176 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000177
Daniel Dunbar5d715592008-08-12 05:28:47 +0000178 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000179 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000180
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000181 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000182 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000183
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000184 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000185 llvm::Type *SelectorPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000186
187private:
Daniel Dunbarb036db82008-08-13 03:21:16 +0000188 /// ProtocolPtrTy - LLVM type for external protocol handles
189 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000190 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000191
192public:
193 llvm::Type *getExternalProtocolPtrTy() {
194 if (!ExternalProtocolPtrTy) {
195 // FIXME: It would be nice to unify this with the opaque type, so that the
196 // IR comes out a bit cleaner.
197 CodeGen::CodeGenTypes &Types = CGM.getTypes();
198 ASTContext &Ctx = CGM.getContext();
199 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
200 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
201 }
202
203 return ExternalProtocolPtrTy;
204 }
205
Daniel Dunbarc722b852008-08-30 03:02:31 +0000206 // SuperCTy - clang type for struct objc_super.
207 QualType SuperCTy;
208 // SuperPtrCTy - clang type for struct objc_super *.
209 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000210
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000211 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000212 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000213 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000214 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000215
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000216 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
217 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000218 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000219
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000220 /// PropertyListTy - LLVM type for struct objc_property_list
221 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000222 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000223 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000224 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000225
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000226 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000227 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000228
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000229 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000230 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000231 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000232 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000233
Chris Lattnerce8754e2009-04-22 02:44:54 +0000234 llvm::Constant *getGetPropertyFn() {
235 CodeGen::CodeGenTypes &Types = CGM.getTypes();
236 ASTContext &Ctx = CGM.getContext();
237 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000238 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000239 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
240 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000241 Params.push_back(IdType);
242 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000243 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000244 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000245 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000246 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, false, Params,
247 FunctionType::ExtInfo(),
John McCall8dda7b22012-07-07 06:41:13 +0000248 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000249 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
250 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000251
Chris Lattnerce8754e2009-04-22 02:44:54 +0000252 llvm::Constant *getSetPropertyFn() {
253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
254 ASTContext &Ctx = CGM.getContext();
255 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000256 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000257 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
258 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000259 Params.push_back(IdType);
260 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000261 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000262 Params.push_back(IdType);
263 Params.push_back(Ctx.BoolTy);
264 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000265 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000266 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false,
267 Params,
268 FunctionType::ExtInfo(),
John McCall8dda7b22012-07-07 06:41:13 +0000269 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
271 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Ted Kremeneke65b0862012-03-06 20:05:56 +0000273 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
274 CodeGen::CodeGenTypes &Types = CGM.getTypes();
275 ASTContext &Ctx = CGM.getContext();
276 // void objc_setProperty_atomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_nonatomic(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
283 // id newValue, ptrdiff_t offset);
284
285 SmallVector<CanQualType,4> Params;
286 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
287 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
288 Params.push_back(IdType);
289 Params.push_back(SelType);
290 Params.push_back(IdType);
291 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
292 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000293 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false,
294 Params,
John McCall8dda7b22012-07-07 06:41:13 +0000295 FunctionType::ExtInfo(),
296 RequiredArgs::All));
Ted Kremeneke65b0862012-03-06 20:05:56 +0000297 const char *name;
298 if (atomic && copy)
299 name = "objc_setProperty_atomic_copy";
300 else if (atomic && !copy)
301 name = "objc_setProperty_atomic";
302 else if (!atomic && copy)
303 name = "objc_setProperty_nonatomic_copy";
304 else
305 name = "objc_setProperty_nonatomic";
306
307 return CGM.CreateRuntimeFunction(FTy, name);
308 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000309
310 llvm::Constant *getCopyStructFn() {
311 CodeGen::CodeGenTypes &Types = CGM.getTypes();
312 ASTContext &Ctx = CGM.getContext();
313 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000314 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000315 Params.push_back(Ctx.VoidPtrTy);
316 Params.push_back(Ctx.VoidPtrTy);
317 Params.push_back(Ctx.LongTy);
318 Params.push_back(Ctx.BoolTy);
319 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000320 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000321 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false,
322 Params,
323 FunctionType::ExtInfo(),
John McCall8dda7b22012-07-07 06:41:13 +0000324 RequiredArgs::All));
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000325 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
326 }
327
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000328 /// This routine declares and returns address of:
329 /// void objc_copyCppObjectAtomic(
330 /// void *dest, const void *src,
331 /// void (*copyHelper) (void *dest, const void *source));
332 llvm::Constant *getCppAtomicObjectFunction() {
333 CodeGen::CodeGenTypes &Types = CGM.getTypes();
334 ASTContext &Ctx = CGM.getContext();
335 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
336 SmallVector<CanQualType,3> Params;
337 Params.push_back(Ctx.VoidPtrTy);
338 Params.push_back(Ctx.VoidPtrTy);
339 Params.push_back(Ctx.VoidPtrTy);
340 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000341 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false,
342 Params,
343 FunctionType::ExtInfo(),
John McCall8dda7b22012-07-07 06:41:13 +0000344 RequiredArgs::All));
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000345 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
346 }
347
Chris Lattnerce8754e2009-04-22 02:44:54 +0000348 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000349 CodeGen::CodeGenTypes &Types = CGM.getTypes();
350 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000351 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000352 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000353 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000354 llvm::FunctionType *FTy =
Reid Kleckner4982b822014-01-31 22:54:50 +0000355 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, false,
356 Params,
357 FunctionType::ExtInfo(),
John McCalla729c622012-02-17 03:33:10 +0000358 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
360 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000361
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000362 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000363 llvm::Constant *getGcReadWeakFn() {
364 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000365 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000366 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000367 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000369 }
370
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000371 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000372 llvm::Constant *getGcAssignWeakFn() {
373 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000374 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000375 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000376 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000379
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000380 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000381 llvm::Constant *getGcAssignGlobalFn() {
382 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000383 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000384 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000385 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000386 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
387 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000388
Fariborz Jahanian217af242010-07-20 20:30:03 +0000389 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
390 llvm::Constant *getGcAssignThreadLocalFn() {
391 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000392 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000393 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000394 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000395 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
396 }
397
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000398 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000399 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000400 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000401 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
402 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000403 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000404 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000405 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
406 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000407
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000408 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
409 llvm::Constant *GcMemmoveCollectableFn() {
410 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000411 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000412 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000413 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
414 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000415
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000416 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000417 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000418 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000419 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000420 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000421 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000422 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
423 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000424
425 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000426 llvm::Constant *getExceptionThrowFn() {
427 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000428 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000429 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000430 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000431 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
432 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000433
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000434 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
435 llvm::Constant *getExceptionRethrowFn() {
436 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000437 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000438 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
439 }
440
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000441 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000442 llvm::Constant *getSyncEnterFn() {
Aaron Ballman9c004462012-09-06 16:44:16 +0000443 // int objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000444 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000445 llvm::FunctionType *FTy =
Aaron Ballman9c004462012-09-06 16:44:16 +0000446 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000447 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
448 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000449
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000450 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000451 llvm::Constant *getSyncExitFn() {
Aaron Ballman9c004462012-09-06 16:44:16 +0000452 // int objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000453 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000454 llvm::FunctionType *FTy =
Aaron Ballman9c004462012-09-06 16:44:16 +0000455 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000456 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
457 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000458
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000459 llvm::Constant *getSendFn(bool IsSuper) const {
460 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
461 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000462
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000463 llvm::Constant *getSendFn2(bool IsSuper) const {
464 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
465 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000466
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000467 llvm::Constant *getSendStretFn(bool IsSuper) const {
468 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
469 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000470
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000471 llvm::Constant *getSendStretFn2(bool IsSuper) const {
472 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
473 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000474
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000475 llvm::Constant *getSendFpretFn(bool IsSuper) const {
476 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
477 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000478
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000479 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
480 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
481 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000482
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000483 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
485 }
486
487 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
489 }
490
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000491 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
492 ~ObjCCommonTypesHelper(){}
493};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000494
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000495/// ObjCTypesHelper - Helper class that encapsulates lazy
496/// construction of varies types used during ObjC generation.
497class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000498public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000499 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000500 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000501 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000502 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000503 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000504 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000505
Daniel Dunbarb036db82008-08-13 03:21:16 +0000506 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000507 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000508 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000510 /// ProtocolExtensionTy - LLVM type for struct
511 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000512 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000513 /// ProtocolExtensionTy - LLVM type for struct
514 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000515 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000516 /// MethodDescriptionTy - LLVM type for struct
517 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000518 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000519 /// MethodDescriptionListTy - LLVM type for struct
520 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000521 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000522 /// MethodDescriptionListPtrTy - LLVM type for struct
523 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000524 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000525 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000526 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000527 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000528 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000529 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000530 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000531 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000532 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000533 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000534 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000535 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000536 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000537 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000538 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000539 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000540 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000541 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000542 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000543 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000544 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000545 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000546 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000547 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000548 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000549
Anders Carlsson9ff22482008-09-09 10:10:21 +0000550 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000551 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000552
Anders Carlsson9ff22482008-09-09 10:10:21 +0000553 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000554 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000555 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000556 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000557 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000558 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000559 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000560
561 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000562 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000563 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000564 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000565 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000566 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000567 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000568
569 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000571 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000572 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000573 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000574 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000575 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000576
Anders Carlsson9ff22482008-09-09 10:10:21 +0000577 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000578 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000579 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000580 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000581 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000582 "objc_exception_match");
583
Chris Lattnerc6406db2009-04-22 02:26:14 +0000584 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585
Anders Carlsson9ff22482008-09-09 10:10:21 +0000586 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000587 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000588 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000589 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
Bill Wendling8594fcb2013-01-31 00:30:05 +0000590 return
591 CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
592 params, false),
593 "_setjmp",
594 llvm::AttributeSet::get(CGM.getLLVMContext(),
595 llvm::AttributeSet::FunctionIndex,
596 llvm::Attribute::NonLazyBind));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000597 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000599public:
600 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000601 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000602};
603
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000604/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000605/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000606class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000607public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000608
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000609 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000610 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000612 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000613 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000614
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000615 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000616 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000617
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000618 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000619 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000620
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000621 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000622 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000623
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000624 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000625 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000627 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000628 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000629
Fariborz Jahanian71394042009-01-23 23:53:38 +0000630 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000631 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000632
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000633 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000634 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000635
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000636 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000637 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000639 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000640 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000641
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000642 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000643 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000645 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000646 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000647
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000648 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000649 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000650
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000651 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000653 // MessageRefTy - LLVM for:
654 // struct _message_ref_t {
655 // IMP messenger;
656 // SEL name;
657 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000658 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000659 // MessageRefCTy - clang type for struct _message_ref_t
660 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000662 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000663 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000664 // MessageRefCPtrTy - clang type for struct _message_ref_t*
665 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000666
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000667 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000668 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000669
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000670 // SuperMessageRefTy - LLVM for:
671 // struct _super_message_ref_t {
672 // SUPER_IMP messenger;
673 // SEL name;
674 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000675 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000676
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000677 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000678 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000679
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000680 llvm::Constant *getMessageSendFixupFn() {
681 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000682 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000684 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000685 "objc_msgSend_fixup");
686 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000688 llvm::Constant *getMessageSendFpretFixupFn() {
689 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000690 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000692 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000693 "objc_msgSend_fpret_fixup");
694 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000696 llvm::Constant *getMessageSendStretFixupFn() {
697 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000698 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000699 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000700 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000701 "objc_msgSend_stret_fixup");
702 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000703
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000704 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000705 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000706 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000707 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000708 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000709 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000710 "objc_msgSendSuper2_fixup");
711 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000713 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000714 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000715 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000716 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000718 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000719 "objc_msgSendSuper2_stret_fixup");
720 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000721
Chris Lattnera7c00b42009-04-22 02:15:23 +0000722 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000723 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000724 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Chris Lattnera7c00b42009-04-22 02:15:23 +0000726 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000727
Chris Lattnera7c00b42009-04-22 02:15:23 +0000728 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000729 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000730 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000731 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000732 "objc_begin_catch");
733 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000734
Chris Lattnera5f58b02011-07-09 17:41:47 +0000735 llvm::StructType *EHTypeTy;
736 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000737
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000738 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
739 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000740};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000741
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000742class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000743public:
744 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000745 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000746 public:
Eli Friedman8cbca202012-11-06 22:15:52 +0000747 unsigned ivar_bytepos;
748 unsigned ivar_size;
749 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000751
752 // Allow sorting based on byte pos.
753 bool operator<(const GC_IVAR &b) const {
754 return ivar_bytepos < b.ivar_bytepos;
755 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000756 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000758 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000759 public:
760 unsigned skip;
761 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000762 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000763 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000764 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000765
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000766 /// opcode for captured block variables layout 'instructions'.
767 /// In the following descriptions, 'I' is the value of the immediate field.
768 /// (field following the opcode).
769 ///
770 enum BLOCK_LAYOUT_OPCODE {
771 /// An operator which affects how the following layout should be
772 /// interpreted.
773 /// I == 0: Halt interpretation and treat everything else as
774 /// a non-pointer. Note that this instruction is equal
775 /// to '\0'.
776 /// I != 0: Currently unused.
777 BLOCK_LAYOUT_OPERATOR = 0,
778
779 /// The next I+1 bytes do not contain a value of object pointer type.
780 /// Note that this can leave the stream unaligned, meaning that
781 /// subsequent word-size instructions do not begin at a multiple of
782 /// the pointer size.
783 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
784
785 /// The next I+1 words do not contain a value of object pointer type.
786 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
787 /// when the required skip quantity is a multiple of the pointer size.
788 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
789
790 /// The next I+1 words are __strong pointers to Objective-C
791 /// objects or blocks.
792 BLOCK_LAYOUT_STRONG = 3,
793
794 /// The next I+1 words are pointers to __block variables.
795 BLOCK_LAYOUT_BYREF = 4,
796
797 /// The next I+1 words are __weak pointers to Objective-C
798 /// objects or blocks.
799 BLOCK_LAYOUT_WEAK = 5,
800
801 /// The next I+1 words are __unsafe_unretained pointers to
802 /// Objective-C objects or blocks.
803 BLOCK_LAYOUT_UNRETAINED = 6
804
805 /// The next I+1 words are block or object pointers with some
806 /// as-yet-unspecified ownership semantics. If we add more
807 /// flavors of ownership semantics, values will be taken from
808 /// this range.
809 ///
810 /// This is included so that older tools can at least continue
811 /// processing the layout past such things.
812 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
813
814 /// All other opcodes are reserved. Halt interpretation and
815 /// treat everything else as opaque.
816 };
817
818 class RUN_SKIP {
819 public:
820 enum BLOCK_LAYOUT_OPCODE opcode;
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000821 CharUnits block_var_bytepos;
822 CharUnits block_var_size;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000823 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000824 CharUnits BytePos = CharUnits::Zero(),
825 CharUnits Size = CharUnits::Zero())
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +0000826 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000827
828 // Allow sorting based on byte pos.
829 bool operator<(const RUN_SKIP &b) const {
830 return block_var_bytepos < b.block_var_bytepos;
831 }
832 };
833
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000834protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000835 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000836 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000837 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000838
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000839 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000840 SmallVector<GC_IVAR, 16> SkipIvars;
841 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000842
843 // arc/mrr layout of captured block literal variables.
844 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000845
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000846 /// LazySymbols - Symbols to generate a lazy reference for. See
847 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000848 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000850 /// DefinedSymbols - External symbols which are defined by this
851 /// module. The symbols in this list and LazySymbols are used to add
852 /// special linker symbols which ensure that Objective-C modules are
853 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000854 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000856 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000857 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000858
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000859 /// MethodVarNames - uniqued method variable names.
860 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000861
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000862 /// DefinedCategoryNames - list of category names in form Class_Category.
863 llvm::SetVector<std::string> DefinedCategoryNames;
864
Daniel Dunbarb036db82008-08-13 03:21:16 +0000865 /// MethodVarTypes - uniqued method type signatures. We have to use
866 /// a StringMap here because have no other unique reference.
867 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000868
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000869 /// MethodDefinitions - map of methods which have been defined in
870 /// this translation unit.
871 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000873 /// PropertyNames - uniqued method variable names.
874 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000875
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000876 /// ClassReferences - uniqued class references.
877 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000878
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000879 /// SelectorReferences - uniqued selector references.
880 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881
Daniel Dunbarb036db82008-08-13 03:21:16 +0000882 /// Protocols - Protocols for which an objc_protocol structure has
883 /// been emitted. Forward declarations are handled by creating an
884 /// empty structure whose initializer is filled in when/if defined.
885 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
Daniel Dunbarc475d422008-10-29 22:36:39 +0000887 /// DefinedProtocols - Protocols which have actually been
888 /// defined. We should not need this, see FIXME in GenerateProtocol.
889 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000891 /// DefinedClasses - List of defined classes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000892 SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000893
894 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000895 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000897 /// DefinedCategories - List of defined categories.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000898 SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000899
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000900 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000901 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000903 /// GetNameForMethod - Return a name for the given method.
904 /// \param[out] NameOut - The return value.
905 void GetNameForMethod(const ObjCMethodDecl *OMD,
906 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000907 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000909 /// GetMethodVarName - Return a unique constant for the given
910 /// selector's name. The return value has type char *.
911 llvm::Constant *GetMethodVarName(Selector Sel);
912 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000914 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000915 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000916
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000917 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000918 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
919 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000920 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000921
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000922 /// GetPropertyName - Return a unique constant for the given
923 /// name. The return value has type char *.
924 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000925
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000926 // FIXME: This can be dropped once string functions are unified.
927 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
928 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000929
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000930 /// GetClassName - Return a unique constant for the given selector's
931 /// name. The return value has type char *.
932 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000933
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000934 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
935
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000936 /// BuildIvarLayout - Builds ivar layout bitmap for the class
937 /// implementation for the __strong or __weak case.
938 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000939 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
940 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000941
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000942 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000943
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000944 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman8cbca202012-11-06 22:15:52 +0000945 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000946 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000947 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000948 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000949 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000950 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cbca202012-11-06 22:15:52 +0000951 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000952 bool &HasUnion);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000953
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000954 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout);
Fariborz Jahanian2dd78192012-11-02 22:51:18 +0000955
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000956 void UpdateRunSkipBlockVars(bool IsByref,
957 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000958 CharUnits FieldOffset,
959 CharUnits FieldSize);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000960
961 void BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000962 CharUnits BytePos, bool &HasUnion,
963 bool ByrefLayout=false);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000964
965 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
966 const RecordDecl *RD,
967 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000968 CharUnits BytePos, bool &HasUnion,
969 bool ByrefLayout);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000970
Fariborz Jahanian23290b02012-11-01 18:32:55 +0000971 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
972
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000973 llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout);
974
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000975
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000976 /// GetIvarLayoutName - Returns a unique constant for the given
977 /// ivar layout bitmap.
978 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
979 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000980
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000981 /// EmitPropertyList - Emit the given property list. The return
982 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000983 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000984 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000985 const ObjCContainerDecl *OCD,
986 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000987
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000988 /// EmitProtocolMethodTypes - Generate the array of extended method type
989 /// strings. The return value has type Int8PtrPtrTy.
990 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000991 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000992 const ObjCCommonTypesHelper &ObjCTypes);
993
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000994 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000995 void PushProtocolProperties(
996 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000997 SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlinga515b582012-02-09 22:16:49 +0000998 const Decl *Container,
999 const ObjCProtocolDecl *PROTO,
1000 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001001
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001002 /// GetProtocolRef - Return a reference to the internal protocol
1003 /// description, creating an empty one if it has not been
1004 /// defined. The return value has type ProtocolPtrTy.
1005 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +00001006
Daniel Dunbar30c65362009-03-09 20:09:19 +00001007 /// CreateMetadataVar - Create a global variable with internal
1008 /// linkage for use by the Objective-C runtime.
1009 ///
1010 /// This is a convenience wrapper which not only creates the
1011 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +00001012 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001013 ///
1014 /// \param Name - The variable name.
1015 /// \param Init - The variable initializer; this is also used to
1016 /// define the type of the variable.
1017 /// \param Section - The section the variable should go into, or 0.
1018 /// \param Align - The alignment for the variable, or 0.
1019 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +00001020 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001021 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00001022 llvm::Constant *Init,
1023 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001024 unsigned Align,
1025 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +00001026
John McCall9e8bb002011-05-14 03:10:52 +00001027 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1028 ReturnValueSlot Return,
1029 QualType ResultType,
1030 llvm::Value *Sel,
1031 llvm::Value *Arg0,
1032 QualType Arg0Ty,
1033 bool IsSuper,
1034 const CallArgList &CallArgs,
1035 const ObjCMethodDecl *OMD,
1036 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001037
Daniel Dunbar5e639272010-04-25 20:39:01 +00001038 /// EmitImageInfo - Emit the image info marker used to encode some module
1039 /// level information.
1040 void EmitImageInfo();
1041
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001042public:
Owen Andersonae86c192009-07-13 04:10:07 +00001043 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +00001044 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001045
David Chisnall481e3a82010-01-23 02:40:42 +00001046 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001047
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001048 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1049 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001050
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001051 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001052
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001053 /// GetOrEmitProtocol - Get the protocol object for the given
1054 /// declaration, emitting it if necessary. The return value has type
1055 /// ProtocolPtrTy.
1056 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001057
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001058 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1059 /// object for the given declaration, emitting it if needed. These
1060 /// forward references will be filled in with empty bodies if no
1061 /// definition is seen. The return value has type ProtocolPtrTy.
1062 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001063 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1064 const CGBlockInfo &blockInfo);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001065 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1066 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001067
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00001068 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
1069 QualType T);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001070};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001071
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001072class CGObjCMac : public CGObjCCommonMac {
1073private:
1074 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001075
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001076 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001077 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001078 void EmitModuleInfo();
1079
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001080 /// EmitModuleSymols - Emit module symbols, the list of defined
1081 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001082 llvm::Constant *EmitModuleSymbols();
1083
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001084 /// FinishModule - Write out global data structures at the end of
1085 /// processing a translation unit.
1086 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001087
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001088 /// EmitClassExtension - Generate the class extension structure used
1089 /// to store the weak ivar layout and properties. The return value
1090 /// has type ClassExtensionPtrTy.
1091 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1092
1093 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1094 /// for the given class.
John McCall882987f2013-02-28 19:01:20 +00001095 llvm::Value *EmitClassRef(CodeGenFunction &CGF,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001096 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001097
John McCall882987f2013-02-28 19:01:20 +00001098 llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF,
John McCall31168b02011-06-15 23:02:42 +00001099 IdentifierInfo *II);
1100
John McCall882987f2013-02-28 19:01:20 +00001101 llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF);
John McCall31168b02011-06-15 23:02:42 +00001102
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001103 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1104 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001105
1106 /// EmitIvarList - Emit the ivar list for the given
1107 /// implementation. If ForClass is true the list of class ivars
1108 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1109 /// interface ivars will be emitted. The return value has type
1110 /// IvarListPtrTy.
1111 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001112 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001113
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001114 /// EmitMetaClass - Emit a forward reference to the class structure
1115 /// for the metaclass of the given interface. The return value has
1116 /// type ClassPtrTy.
1117 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1118
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001119 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001120 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001121 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1122 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001123 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001124
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001125 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001126
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001127 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001128
1129 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001130 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001131 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001132 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001133 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001134
1135 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001136 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001137 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001138 /// - IsProtocol: True iff these methods are for a protocol.
1139 /// - ClassMethds: True iff these are class methods.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001140 /// - Required: When true, only "required" methods are
1141 /// listed. Similarly, when false only "optional" methods are
1142 /// listed. For classes this should always be true.
1143 /// - begin, end: The method list to output.
1144 ///
1145 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001146 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001147 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001148 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001149
Daniel Dunbarc475d422008-10-29 22:36:39 +00001150 /// GetOrEmitProtocol - Get the protocol object for the given
1151 /// declaration, emitting it if necessary. The return value has type
1152 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001153 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001154
1155 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1156 /// object for the given declaration, emitting it if needed. These
1157 /// forward references will be filled in with empty bodies if no
1158 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001159 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001160
Daniel Dunbarb036db82008-08-13 03:21:16 +00001161 /// EmitProtocolExtension - Generate the protocol extension
1162 /// structure used to store optional instance and class methods, and
1163 /// protocol properties. The return value has type
1164 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001165 llvm::Constant *
1166 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001167 ArrayRef<llvm::Constant*> OptInstanceMethods,
1168 ArrayRef<llvm::Constant*> OptClassMethods,
1169 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001170
1171 /// EmitProtocolList - Generate the list of referenced
1172 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001174 ObjCProtocolDecl::protocol_iterator begin,
1175 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001176
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001177 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1178 /// for the given selector.
John McCall882987f2013-02-28 19:01:20 +00001179 llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001180 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181
1182public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001183 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001184
Fariborz Jahanian71394042009-01-23 23:53:38 +00001185 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001186
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001187 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001188 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001189 QualType ResultType,
1190 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001191 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001192 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001193 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001194 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001195
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001197 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001198 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001199 QualType ResultType,
1200 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001201 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001202 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001203 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001204 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001205 const CallArgList &CallArgs,
1206 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001207
John McCall882987f2013-02-28 19:01:20 +00001208 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001209 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001210
John McCall882987f2013-02-28 19:01:20 +00001211 virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001212 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001213
1214 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1215 /// untyped one.
John McCall882987f2013-02-28 19:01:20 +00001216 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001217 const ObjCMethodDecl *Method);
1218
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001219 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001220
Daniel Dunbar92992502008-08-15 22:20:32 +00001221 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001222
Daniel Dunbar92992502008-08-15 22:20:32 +00001223 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001224
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001225 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001226
John McCall882987f2013-02-28 19:01:20 +00001227 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001228 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001229
Chris Lattnerd4808922009-03-22 21:03:39 +00001230 virtual llvm::Constant *GetPropertyGetFunction();
1231 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001232 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1233 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001234 virtual llvm::Constant *GetGetStructFunction();
1235 virtual llvm::Constant *GetSetStructFunction();
David Chisnall0d75e062012-12-17 18:54:24 +00001236 virtual llvm::Constant *GetCppAtomicObjectGetFunction();
1237 virtual llvm::Constant *GetCppAtomicObjectSetFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001238 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001239
John McCallbd309292010-07-06 01:34:17 +00001240 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1241 const ObjCAtTryStmt &S);
1242 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1243 const ObjCAtSynchronizedStmt &S);
1244 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001245 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00001246 const ObjCAtThrowStmt &S,
1247 bool ClearInsertionPoint=true);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001248 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001249 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001250 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001251 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001252 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001253 llvm::Value *src, llvm::Value *dest,
1254 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001255 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001256 llvm::Value *src, llvm::Value *dest,
1257 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001258 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1259 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001260 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1261 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001262 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001263
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001264 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1265 QualType ObjectTy,
1266 llvm::Value *BaseValue,
1267 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001268 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001269 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001270 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001271 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001272
1273 /// GetClassGlobal - Return the global variable for the Objective-C
1274 /// class of the given name.
Rafael Espindola554256c2014-02-26 22:25:45 +00001275 llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
Craig Toppera798a9d2014-03-02 09:32:10 +00001276 bool Weak = false) override {
David Blaikie83d382b2011-09-23 05:06:16 +00001277 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001278 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001279};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001280
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001281class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001282private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001283 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001284 llvm::GlobalVariable* ObjCEmptyCacheVar;
1285 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001287 /// SuperClassReferences - uniqued super class references.
1288 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001290 /// MetaClassReferences - uniqued meta class references.
1291 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001292
1293 /// EHTypeReferences - uniqued class ehtype references.
1294 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001295
John McCall9e8bb002011-05-14 03:10:52 +00001296 /// VTableDispatchMethods - List of methods for which we generate
1297 /// vtable-based message dispatch.
1298 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299
Fariborz Jahanian67260552009-11-17 21:37:35 +00001300 /// DefinedMetaClasses - List of defined meta-classes.
1301 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1302
John McCall9e8bb002011-05-14 03:10:52 +00001303 /// isVTableDispatchedSelector - Returns true if SEL is a
1304 /// vtable-based selector.
1305 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306
Fariborz Jahanian71394042009-01-23 23:53:38 +00001307 /// FinishNonFragileABIModule - Write out global data structures at the end of
1308 /// processing a translation unit.
1309 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001310
Daniel Dunbar19573e72009-05-15 21:48:48 +00001311 /// AddModuleClassList - Add the given list of class pointers to the
1312 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001313 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001314 const char *SymbolName,
1315 const char *SectionName);
1316
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001317 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1318 unsigned InstanceStart,
1319 unsigned InstanceSize,
1320 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001321 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001323 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001324 llvm::Constant *ClassRoGV,
Rafael Espindola554256c2014-02-26 22:25:45 +00001325 bool HiddenVisibility,
1326 bool Weak = false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001327
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001328 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001329
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001330 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001331
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001332 /// EmitMethodList - Emit the method list for the given
1333 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001335 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001336 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001337 /// EmitIvarList - Emit the ivar list for the given
1338 /// implementation. If ForClass is true the list of class ivars
1339 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1340 /// interface ivars will be emitted. The return value has type
1341 /// IvarListnfABIPtrTy.
1342 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001343
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001344 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001345 const ObjCIvarDecl *Ivar,
Eli Friedman8cbca202012-11-06 22:15:52 +00001346 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001347
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001348 /// GetOrEmitProtocol - Get the protocol object for the given
1349 /// declaration, emitting it if necessary. The return value has type
1350 /// ProtocolPtrTy.
1351 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001352
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001353 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1354 /// object for the given declaration, emitting it if needed. These
1355 /// forward references will be filled in with empty bodies if no
1356 /// definition is seen. The return value has type ProtocolPtrTy.
1357 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001358
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001359 /// EmitProtocolList - Generate the list of referenced
1360 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001361 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001362 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001363 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001364
John McCall9e8bb002011-05-14 03:10:52 +00001365 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1366 ReturnValueSlot Return,
1367 QualType ResultType,
1368 Selector Sel,
1369 llvm::Value *Receiver,
1370 QualType Arg0Ty,
1371 bool IsSuper,
1372 const CallArgList &CallArgs,
1373 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001374
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001375 /// GetClassGlobal - Return the global variable for the Objective-C
1376 /// class of the given name.
Rafael Espindola554256c2014-02-26 22:25:45 +00001377 llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
Craig Toppera798a9d2014-03-02 09:32:10 +00001378 bool Weak = false) override;
Rafael Espindola554256c2014-02-26 22:25:45 +00001379
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001380 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001381 /// for the given class reference.
John McCall882987f2013-02-28 19:01:20 +00001382 llvm::Value *EmitClassRef(CodeGenFunction &CGF,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001383 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001384
John McCall882987f2013-02-28 19:01:20 +00001385 llvm::Value *EmitClassRefFromId(CodeGenFunction &CGF,
Rafael Espindola554256c2014-02-26 22:25:45 +00001386 IdentifierInfo *II, bool Weak);
John McCall31168b02011-06-15 23:02:42 +00001387
John McCall882987f2013-02-28 19:01:20 +00001388 llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001389
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001390 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1391 /// for the given super class reference.
John McCall882987f2013-02-28 19:01:20 +00001392 llvm::Value *EmitSuperClassRef(CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393 const ObjCInterfaceDecl *ID);
1394
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001395 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1396 /// meta-data
John McCall882987f2013-02-28 19:01:20 +00001397 llvm::Value *EmitMetaClassRef(CodeGenFunction &CGF,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001398 const ObjCInterfaceDecl *ID);
1399
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001400 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1401 /// the given ivar.
1402 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001403 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001404 const ObjCInterfaceDecl *ID,
1405 const ObjCIvarDecl *Ivar);
1406
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001407 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1408 /// for the given selector.
John McCall882987f2013-02-28 19:01:20 +00001409 llvm::Value *EmitSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001410 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001411
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001412 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001413 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001414 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001415 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001416
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001417 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001418 return "OBJC_METACLASS_$_";
1419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001420
Daniel Dunbar15894b72009-04-07 05:48:37 +00001421 const char *getClassSymbolPrefix() const {
1422 return "OBJC_CLASS_$_";
1423 }
1424
Daniel Dunbar961202372009-05-03 12:57:56 +00001425 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001426 uint32_t &InstanceStart,
1427 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001428
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001429 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001430 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001431 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1432 return CGM.getContext().Selectors.getSelector(0, &II);
1433 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001434
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001435 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001436 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1437 return CGM.getContext().Selectors.getSelector(1, &II);
1438 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001439
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001440 /// ImplementationIsNonLazy - Check whether the given category or
1441 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001442 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001443
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00001444 bool IsIvarOffsetKnownIdempotent(const CodeGen::CodeGenFunction &CGF,
1445 const ObjCInterfaceDecl *ID,
1446 const ObjCIvarDecl *IV) {
1447 // Annotate the load as an invariant load iff the object type is the type,
1448 // or a derived type, of the class containing the ivar within an ObjC
1449 // method. This check is needed because the ivar offset is a lazily
1450 // initialised value that may depend on objc_msgSend to perform a fixup on
1451 // the first message dispatch.
1452 //
1453 // An additional opportunity to mark the load as invariant arises when the
1454 // base of the ivar access is a parameter to an Objective C method.
1455 // However, because the parameters are not available in the current
1456 // interface, we cannot perform this check.
Douglas Gregor1c15cd62013-02-18 15:59:24 +00001457 if (CGF.CurFuncDecl && isa<ObjCMethodDecl>(CGF.CurFuncDecl))
1458 if (IV->getContainingInterface()->isSuperClassOf(ID))
1459 return true;
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00001460 return false;
1461 }
1462
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001463public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001464 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001465 // FIXME. All stubs for now!
1466 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001467
Fariborz Jahanian71394042009-01-23 23:53:38 +00001468 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001469 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001470 QualType ResultType,
1471 Selector Sel,
1472 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001473 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001474 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001475 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001476
1477 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001478 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001479 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001480 QualType ResultType,
1481 Selector Sel,
1482 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001483 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001484 llvm::Value *Receiver,
1485 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001486 const CallArgList &CallArgs,
1487 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001488
John McCall882987f2013-02-28 19:01:20 +00001489 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001490 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001491
John McCall882987f2013-02-28 19:01:20 +00001492 virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001493 bool lvalue = false)
John McCall882987f2013-02-28 19:01:20 +00001494 { return EmitSelector(CGF, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001495
1496 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1497 /// untyped one.
John McCall882987f2013-02-28 19:01:20 +00001498 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001499 const ObjCMethodDecl *Method)
John McCall882987f2013-02-28 19:01:20 +00001500 { return EmitSelector(CGF, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001501
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001502 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001503
Fariborz Jahanian71394042009-01-23 23:53:38 +00001504 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001505
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001506 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001507
John McCall882987f2013-02-28 19:01:20 +00001508 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001509 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001510
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001511 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001512
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001513 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001514 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001515 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001516 virtual llvm::Constant *GetPropertySetFunction() {
1517 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001518 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001519
Ted Kremeneke65b0862012-03-06 20:05:56 +00001520 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1521 bool copy) {
1522 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1523 }
1524
David Chisnall168b80f2010-12-26 22:13:16 +00001525 virtual llvm::Constant *GetSetStructFunction() {
1526 return ObjCTypes.getCopyStructFn();
1527 }
1528 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001529 return ObjCTypes.getCopyStructFn();
1530 }
David Chisnall0d75e062012-12-17 18:54:24 +00001531 virtual llvm::Constant *GetCppAtomicObjectSetFunction() {
1532 return ObjCTypes.getCppAtomicObjectFunction();
1533 }
1534 virtual llvm::Constant *GetCppAtomicObjectGetFunction() {
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001535 return ObjCTypes.getCppAtomicObjectFunction();
1536 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001537
Chris Lattnerd4808922009-03-22 21:03:39 +00001538 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001539 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001540 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001541
John McCallbd309292010-07-06 01:34:17 +00001542 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1543 const ObjCAtTryStmt &S);
1544 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1545 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001546 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00001547 const ObjCAtThrowStmt &S,
1548 bool ClearInsertionPoint=true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001549 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001550 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001551 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001552 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001553 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001554 llvm::Value *src, llvm::Value *dest,
1555 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001556 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001557 llvm::Value *src, llvm::Value *dest,
1558 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001559 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001560 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001561 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1562 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001563 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001564 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1565 QualType ObjectTy,
1566 llvm::Value *BaseValue,
1567 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001568 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001569 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001570 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001571 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001572};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001573
1574/// A helper class for performing the null-initialization of a return
1575/// value.
1576struct NullReturnState {
1577 llvm::BasicBlock *NullBB;
John McCall3d1e2c92013-02-12 05:53:35 +00001578 NullReturnState() : NullBB(0) {}
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001579
John McCall3d1e2c92013-02-12 05:53:35 +00001580 /// Perform a null-check of the given receiver.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001581 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
John McCall3d1e2c92013-02-12 05:53:35 +00001582 // Make blocks for the null-receiver and call edges.
1583 NullBB = CGF.createBasicBlock("msgSend.null-receiver");
1584 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001585
1586 // Check for a null receiver and, if there is one, jump to the
John McCall3d1e2c92013-02-12 05:53:35 +00001587 // null-receiver block. There's no point in trying to avoid it:
1588 // we're always going to put *something* there, because otherwise
1589 // we shouldn't have done this null-check in the first place.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001590 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1591 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1592
1593 // Otherwise, start performing the call.
1594 CGF.EmitBlock(callBB);
1595 }
1596
John McCall3d1e2c92013-02-12 05:53:35 +00001597 /// Complete the null-return operation. It is valid to call this
1598 /// regardless of whether 'init' has been called.
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001599 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1600 const CallArgList &CallArgs,
1601 const ObjCMethodDecl *Method) {
John McCall3d1e2c92013-02-12 05:53:35 +00001602 // If we never had to do a null-check, just use the raw result.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001603 if (!NullBB) return result;
John McCall3d1e2c92013-02-12 05:53:35 +00001604
1605 // The continuation block. This will be left null if we don't have an
1606 // IP, which can happen if the method we're calling is marked noreturn.
1607 llvm::BasicBlock *contBB = 0;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001608
John McCall3d1e2c92013-02-12 05:53:35 +00001609 // Finish the call path.
1610 llvm::BasicBlock *callBB = CGF.Builder.GetInsertBlock();
1611 if (callBB) {
1612 contBB = CGF.createBasicBlock("msgSend.cont");
1613 CGF.Builder.CreateBr(contBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001614 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001615
John McCall3d1e2c92013-02-12 05:53:35 +00001616 // Okay, start emitting the null-receiver block.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001617 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001618
John McCall3d1e2c92013-02-12 05:53:35 +00001619 // Release any consumed arguments we've got.
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001620 if (Method) {
1621 CallArgList::const_iterator I = CallArgs.begin();
1622 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1623 e = Method->param_end(); i != e; ++i, ++I) {
1624 const ParmVarDecl *ParamDecl = (*i);
1625 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1626 RValue RV = I->RV;
1627 assert(RV.isScalar() &&
1628 "NullReturnState::complete - arg not on object");
John McCallcdda29c2013-03-13 03:10:54 +00001629 CGF.EmitARCRelease(RV.getScalarVal(), ARCImpreciseLifetime);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001630 }
1631 }
1632 }
John McCall3d1e2c92013-02-12 05:53:35 +00001633
1634 // The phi code below assumes that we haven't needed any control flow yet.
1635 assert(CGF.Builder.GetInsertBlock() == NullBB);
1636
1637 // If we've got a void return, just jump to the continuation block.
1638 if (result.isScalar() && resultType->isVoidType()) {
1639 // No jumps required if the message-send was noreturn.
1640 if (contBB) CGF.EmitBlock(contBB);
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001641 return result;
1642 }
1643
John McCall3d1e2c92013-02-12 05:53:35 +00001644 // If we've got a scalar return, build a phi.
1645 if (result.isScalar()) {
1646 // Derive the null-initialization value.
1647 llvm::Constant *null = CGF.CGM.EmitNullConstant(resultType);
1648
1649 // If no join is necessary, just flow out.
1650 if (!contBB) return RValue::get(null);
1651
1652 // Otherwise, build a phi.
1653 CGF.EmitBlock(contBB);
1654 llvm::PHINode *phi = CGF.Builder.CreatePHI(null->getType(), 2);
1655 phi->addIncoming(result.getScalarVal(), callBB);
1656 phi->addIncoming(null, NullBB);
1657 return RValue::get(phi);
1658 }
1659
1660 // If we've got an aggregate return, null the buffer out.
1661 // FIXME: maybe we should be doing things differently for all the
1662 // cases where the ABI has us returning (1) non-agg values in
1663 // memory or (2) agg values in registers.
1664 if (result.isAggregate()) {
1665 assert(result.isAggregate() && "null init of non-aggregate result?");
1666 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1667 if (contBB) CGF.EmitBlock(contBB);
1668 return result;
1669 }
1670
1671 // Complex types.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001672 CGF.EmitBlock(contBB);
John McCall3d1e2c92013-02-12 05:53:35 +00001673 CodeGenFunction::ComplexPairTy callResult = result.getComplexVal();
1674
1675 // Find the scalar type and its zero value.
1676 llvm::Type *scalarTy = callResult.first->getType();
1677 llvm::Constant *scalarZero = llvm::Constant::getNullValue(scalarTy);
1678
1679 // Build phis for both coordinates.
1680 llvm::PHINode *real = CGF.Builder.CreatePHI(scalarTy, 2);
1681 real->addIncoming(callResult.first, callBB);
1682 real->addIncoming(scalarZero, NullBB);
1683 llvm::PHINode *imag = CGF.Builder.CreatePHI(scalarTy, 2);
1684 imag->addIncoming(callResult.second, callBB);
1685 imag->addIncoming(scalarZero, NullBB);
1686 return RValue::getComplex(real, imag);
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001687 }
1688};
1689
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001690} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001691
1692/* *** Helper Functions *** */
1693
1694/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001695static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001696 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001697 unsigned idx0,
1698 unsigned idx1) {
1699 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001700 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1701 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001702 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001703 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001704}
1705
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001706/// hasObjCExceptionAttribute - Return true if this class or any super
1707/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001708static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001709 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001710 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001711 return true;
1712 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001713 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001714 return false;
1715}
1716
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001717/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001718
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001719CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001720 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001721 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001722 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001723}
1724
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001725/// GetClass - Return a reference to the class for the given interface
1726/// decl.
John McCall882987f2013-02-28 19:01:20 +00001727llvm::Value *CGObjCMac::GetClass(CodeGenFunction &CGF,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001728 const ObjCInterfaceDecl *ID) {
John McCall882987f2013-02-28 19:01:20 +00001729 return EmitClassRef(CGF, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001730}
1731
1732/// GetSelector - Return the pointer to the unique'd string for this selector.
John McCall882987f2013-02-28 19:01:20 +00001733llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001734 bool lval) {
John McCall882987f2013-02-28 19:01:20 +00001735 return EmitSelector(CGF, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001736}
John McCall882987f2013-02-28 19:01:20 +00001737llvm::Value *CGObjCMac::GetSelector(CodeGenFunction &CGF, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001738 *Method) {
John McCall882987f2013-02-28 19:01:20 +00001739 return EmitSelector(CGF, Method->getSelector());
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001740}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001741
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001742llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001743 if (T->isObjCIdType() ||
1744 T->isObjCQualifiedIdType()) {
1745 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001746 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001747 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001748 if (T->isObjCClassType() ||
1749 T->isObjCQualifiedClassType()) {
1750 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001751 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001752 }
1753 if (T->isObjCObjectPointerType())
1754 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1755
John McCall2ca705e2010-07-24 00:37:23 +00001756 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001757}
1758
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001759/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001760/*
1761 struct __builtin_CFString {
1762 const int *isa; // point to __CFConstantStringClassReference
1763 int flags;
1764 const char *str;
1765 long length;
1766 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001767*/
1768
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001769/// or Generate a constant NSString object.
1770/*
1771 struct __builtin_NSString {
1772 const int *isa; // point to __NSConstantStringClassReference
1773 const char *str;
1774 unsigned int length;
1775 };
1776*/
1777
Fariborz Jahanian71394042009-01-23 23:53:38 +00001778llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001779 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001780 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001781 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001782 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001783}
1784
Ted Kremeneke65b0862012-03-06 20:05:56 +00001785enum {
1786 kCFTaggedObjectID_Integer = (1 << 1) + 1
1787};
1788
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001789/// Generates a message send where the super is the receiver. This is
1790/// a message send to self with special delivery semantics indicating
1791/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001792CodeGen::RValue
1793CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001794 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001795 QualType ResultType,
1796 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001797 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001798 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001799 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001800 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001801 const CodeGen::CallArgList &CallArgs,
1802 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001803 // Create and init a super structure; this is a (receiver, class)
1804 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001805 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001806 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001807 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001808 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001809 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001810 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001811
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001812 // If this is a class message the metaclass is passed as the target.
1813 llvm::Value *Target;
1814 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001815 if (isCategoryImpl) {
1816 // Message sent to 'super' in a class method defined in a category
1817 // implementation requires an odd treatment.
1818 // If we are in a class method, we must retrieve the
1819 // _metaclass_ for the current class, pointed at by
1820 // the class's "isa" pointer. The following assumes that
1821 // isa" is the first ivar in a class (which it must be).
John McCall882987f2013-02-28 19:01:20 +00001822 Target = EmitClassRef(CGF, Class->getSuperClass());
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001823 Target = CGF.Builder.CreateStructGEP(Target, 0);
1824 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001825 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001826 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1827 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1828 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1829 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001831 }
1832 else if (isCategoryImpl)
John McCall882987f2013-02-28 19:01:20 +00001833 Target = EmitClassRef(CGF, Class->getSuperClass());
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001834 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001835 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1836 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1837 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001838 }
Mike Stump18bb9282009-05-16 07:57:57 +00001839 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1840 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001841 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001842 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001843 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001844 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001845 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001846 return EmitMessageSend(CGF, Return, ResultType,
John McCall882987f2013-02-28 19:01:20 +00001847 EmitSelector(CGF, Sel),
John McCall9e8bb002011-05-14 03:10:52 +00001848 ObjCSuper, ObjCTypes.SuperPtrCTy,
1849 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001850}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851
1852/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001853CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001854 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001855 QualType ResultType,
1856 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001857 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001858 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001859 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001860 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001861 return EmitMessageSend(CGF, Return, ResultType,
John McCall882987f2013-02-28 19:01:20 +00001862 EmitSelector(CGF, Sel),
John McCall9e8bb002011-05-14 03:10:52 +00001863 Receiver, CGF.getContext().getObjCIdType(),
1864 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001865}
1866
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001867CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001868CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1869 ReturnValueSlot Return,
1870 QualType ResultType,
1871 llvm::Value *Sel,
1872 llvm::Value *Arg0,
1873 QualType Arg0Ty,
1874 bool IsSuper,
1875 const CallArgList &CallArgs,
1876 const ObjCMethodDecl *Method,
1877 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001878 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001879 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001880 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001881 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1882 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001883 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001884
John McCalla729c622012-02-17 03:33:10 +00001885 // If we're calling a method, use the formal signature.
1886 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887
Anders Carlsson280e61f12010-06-21 20:59:55 +00001888 if (Method)
Alp Toker314cc812014-01-25 16:55:45 +00001889 assert(CGM.getContext().getCanonicalType(Method->getReturnType()) ==
1890 CGM.getContext().getCanonicalType(ResultType) &&
Anders Carlsson280e61f12010-06-21 20:59:55 +00001891 "Result type mismatch!");
1892
John McCall5880fb82011-05-14 21:12:11 +00001893 NullReturnState nullReturn;
1894
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001895 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001896 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001897 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001898 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001899 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001900 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1901 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1902 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001903 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1904 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1905 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001906 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001907 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001908 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001909 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001910
1911 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001912 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001913 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1914 e = Method->param_end(); i != e; ++i) {
1915 const ParmVarDecl *ParamDecl = (*i);
1916 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1917 if (!nullReturn.NullBB)
1918 nullReturn.init(CGF, Arg0);
1919 requiresnullCheck = true;
1920 break;
1921 }
1922 }
1923
John McCalla729c622012-02-17 03:33:10 +00001924 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1925 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001926 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1927 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001928}
1929
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001930static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1931 if (FQT.isObjCGCStrong())
1932 return Qualifiers::Strong;
1933
John McCall31168b02011-06-15 23:02:42 +00001934 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001935 return Qualifiers::Weak;
1936
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001937 // check for __unsafe_unretained
1938 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1939 return Qualifiers::GCNone;
1940
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001941 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1942 return Qualifiers::Strong;
1943
1944 if (const PointerType *PT = FQT->getAs<PointerType>())
1945 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1946
1947 return Qualifiers::GCNone;
1948}
1949
John McCall351762c2011-02-07 10:33:21 +00001950llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1951 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001952
Chris Lattnerece04092012-02-07 00:39:47 +00001953 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001954 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1955 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001956 return nullPtr;
1957
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001958 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001959 SkipIvars.clear();
1960 IvarsInfo.clear();
John McCallc8e01702013-04-16 22:48:15 +00001961 unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0);
1962 unsigned ByteSizeInBits = CGM.getTarget().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001963
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001964 // __isa is the first field in block descriptor and must assume by runtime's
1965 // convention that it is GC'able.
Eli Friedman8cbca202012-11-06 22:15:52 +00001966 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001967
1968 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1969
1970 // Calculate the basic layout of the block structure.
1971 const llvm::StructLayout *layout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001972 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall351762c2011-02-07 10:33:21 +00001973
1974 // Ignore the optional 'this' capture: C++ objects are not assumed
1975 // to be GC'ed.
1976
1977 // Walk the captured variables.
1978 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1979 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1980 const VarDecl *variable = ci->getVariable();
1981 QualType type = variable->getType();
1982
1983 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1984
1985 // Ignore constant captures.
1986 if (capture.isConstant()) continue;
1987
Eli Friedman8cbca202012-11-06 22:15:52 +00001988 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
John McCall351762c2011-02-07 10:33:21 +00001989
1990 // __block variables are passed by their descriptor address.
1991 if (ci->isByRef()) {
Eli Friedman8cbca202012-11-06 22:15:52 +00001992 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001993 continue;
John McCall351762c2011-02-07 10:33:21 +00001994 }
1995
1996 assert(!type->isArrayType() && "array variable should not be caught");
1997 if (const RecordType *record = type->getAs<RecordType>()) {
1998 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001999 continue;
2000 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00002001
John McCall351762c2011-02-07 10:33:21 +00002002 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
Eli Friedman8cbca202012-11-06 22:15:52 +00002003 unsigned fieldSize = CGM.getContext().getTypeSize(type);
John McCall351762c2011-02-07 10:33:21 +00002004
2005 if (GCAttr == Qualifiers::Strong)
Eli Friedman8cbca202012-11-06 22:15:52 +00002006 IvarsInfo.push_back(GC_IVAR(fieldOffset,
2007 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002008 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
Eli Friedman8cbca202012-11-06 22:15:52 +00002009 SkipIvars.push_back(GC_IVAR(fieldOffset,
2010 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002011 }
2012
2013 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00002014 return nullPtr;
2015
2016 // Sort on byte position; captures might not be allocated in order,
2017 // and unions can do funny things.
2018 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
2019 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002020
2021 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00002022 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002023 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002024 printf("\n block variable layout for block: ");
Roman Divackye6377112012-09-06 15:59:27 +00002025 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00002026 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002027 if (!(s[i] & 0xf0))
2028 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
2029 else
2030 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
2031 printf("\n");
2032 }
2033
2034 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00002035}
2036
Fariborz Jahanian2c96d302012-11-04 18:19:40 +00002037/// getBlockCaptureLifetime - This routine returns life time of the captured
2038/// block variable for the purpose of block layout meta-data generation. FQT is
2039/// the type of the variable captured in the block.
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002040Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT,
2041 bool ByrefLayout) {
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002042 if (CGM.getLangOpts().ObjCAutoRefCount)
2043 return FQT.getObjCLifetime();
2044
Fariborz Jahanian2c96d302012-11-04 18:19:40 +00002045 // MRR.
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002046 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002047 return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong;
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002048
2049 return Qualifiers::OCL_None;
2050}
2051
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002052void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
2053 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002054 CharUnits FieldOffset,
2055 CharUnits FieldSize) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002056 // __block variables are passed by their descriptor address.
2057 if (IsByref)
2058 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002059 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002060 else if (LifeTime == Qualifiers::OCL_Strong)
2061 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002062 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002063 else if (LifeTime == Qualifiers::OCL_Weak)
2064 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002065 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002066 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2067 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002068 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002069 else
2070 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2071 FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002072 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002073}
2074
2075void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2076 const RecordDecl *RD,
2077 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002078 CharUnits BytePos, bool &HasUnion,
2079 bool ByrefLayout) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002080 bool IsUnion = (RD && RD->isUnion());
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002081 CharUnits MaxUnionSize = CharUnits::Zero();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002082 const FieldDecl *MaxField = 0;
2083 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002084 CharUnits MaxFieldOffset = CharUnits::Zero();
2085 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002086
2087 if (RecFields.empty())
2088 return;
John McCallc8e01702013-04-16 22:48:15 +00002089 unsigned ByteSizeInBits = CGM.getTarget().getCharWidth();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002090
2091 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2092 const FieldDecl *Field = RecFields[i];
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002093 // Note that 'i' here is actually the field index inside RD of Field,
2094 // although this dependency is hidden.
2095 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002096 CharUnits FieldOffset =
2097 CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002098
2099 // Skip over unnamed or bitfields
2100 if (!Field->getIdentifier() || Field->isBitField()) {
2101 LastFieldBitfieldOrUnnamed = Field;
2102 LastBitfieldOrUnnamedOffset = FieldOffset;
2103 continue;
2104 }
2105
2106 LastFieldBitfieldOrUnnamed = 0;
2107 QualType FQT = Field->getType();
2108 if (FQT->isRecordType() || FQT->isUnionType()) {
2109 if (FQT->isUnionType())
2110 HasUnion = true;
2111
2112 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2113 BytePos + FieldOffset, HasUnion);
2114 continue;
2115 }
2116
2117 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2118 const ConstantArrayType *CArray =
2119 dyn_cast_or_null<ConstantArrayType>(Array);
2120 uint64_t ElCount = CArray->getSize().getZExtValue();
2121 assert(CArray && "only array with known element size is supported");
2122 FQT = CArray->getElementType();
2123 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2124 const ConstantArrayType *CArray =
2125 dyn_cast_or_null<ConstantArrayType>(Array);
2126 ElCount *= CArray->getSize().getZExtValue();
2127 FQT = CArray->getElementType();
2128 }
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002129 if (FQT->isRecordType() && ElCount) {
2130 int OldIndex = RunSkipBlockVars.size() - 1;
2131 const RecordType *RT = FQT->getAs<RecordType>();
2132 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2133 HasUnion);
2134
2135 // Replicate layout information for each array element. Note that
2136 // one element is already done.
2137 uint64_t ElIx = 1;
2138 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002139 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002140 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2141 RunSkipBlockVars.push_back(
2142 RUN_SKIP(RunSkipBlockVars[i].opcode,
2143 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2144 RunSkipBlockVars[i].block_var_size));
2145 }
2146 continue;
2147 }
2148 }
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002149 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType());
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002150 if (IsUnion) {
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002151 CharUnits UnionIvarSize = FieldSize;
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002152 if (UnionIvarSize > MaxUnionSize) {
2153 MaxUnionSize = UnionIvarSize;
2154 MaxField = Field;
2155 MaxFieldOffset = FieldOffset;
2156 }
2157 } else {
2158 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002159 getBlockCaptureLifetime(FQT, ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002160 BytePos + FieldOffset,
2161 FieldSize);
2162 }
2163 }
2164
2165 if (LastFieldBitfieldOrUnnamed) {
2166 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2167 // Last field was a bitfield. Must update the info.
2168 uint64_t BitFieldSize
2169 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002170 unsigned UnsSize = (BitFieldSize / ByteSizeInBits) +
Eli Friedman8cbca202012-11-06 22:15:52 +00002171 ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002172 CharUnits Size = CharUnits::fromQuantity(UnsSize);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002173 Size += LastBitfieldOrUnnamedOffset;
2174 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002175 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2176 ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002177 BytePos + LastBitfieldOrUnnamedOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002178 Size);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002179 } else {
2180 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2181 // Last field was unnamed. Must update skip info.
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002182 CharUnits FieldSize
2183 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002184 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002185 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2186 ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002187 BytePos + LastBitfieldOrUnnamedOffset,
2188 FieldSize);
2189 }
2190 }
2191
2192 if (MaxField)
2193 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002194 getBlockCaptureLifetime(MaxField->getType(), ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002195 BytePos + MaxFieldOffset,
2196 MaxUnionSize);
2197}
2198
2199void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002200 CharUnits BytePos,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002201 bool &HasUnion,
2202 bool ByrefLayout) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002203 const RecordDecl *RD = RT->getDecl();
2204 SmallVector<const FieldDecl*, 16> Fields;
2205 for (RecordDecl::field_iterator i = RD->field_begin(),
2206 e = RD->field_end(); i != e; ++i)
2207 Fields.push_back(*i);
2208 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2209 const llvm::StructLayout *RecLayout =
2210 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2211
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002212 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002213}
2214
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002215/// InlineLayoutInstruction - This routine produce an inline instruction for the
2216/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2217/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2218/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2219/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2220/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2221/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2222/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2223/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2224uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2225 SmallVectorImpl<unsigned char> &Layout) {
2226 uint64_t Result = 0;
2227 if (Layout.size() <= 3) {
2228 unsigned size = Layout.size();
2229 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2230 unsigned char inst;
2231 enum BLOCK_LAYOUT_OPCODE opcode ;
2232 switch (size) {
2233 case 3:
2234 inst = Layout[0];
2235 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2236 if (opcode == BLOCK_LAYOUT_STRONG)
2237 strong_word_count = (inst & 0xF)+1;
2238 else
2239 return 0;
2240 inst = Layout[1];
2241 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2242 if (opcode == BLOCK_LAYOUT_BYREF)
2243 byref_word_count = (inst & 0xF)+1;
2244 else
2245 return 0;
2246 inst = Layout[2];
2247 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2248 if (opcode == BLOCK_LAYOUT_WEAK)
2249 weak_word_count = (inst & 0xF)+1;
2250 else
2251 return 0;
2252 break;
2253
2254 case 2:
2255 inst = Layout[0];
2256 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2257 if (opcode == BLOCK_LAYOUT_STRONG) {
2258 strong_word_count = (inst & 0xF)+1;
2259 inst = Layout[1];
2260 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2261 if (opcode == BLOCK_LAYOUT_BYREF)
2262 byref_word_count = (inst & 0xF)+1;
2263 else if (opcode == BLOCK_LAYOUT_WEAK)
2264 weak_word_count = (inst & 0xF)+1;
2265 else
2266 return 0;
2267 }
2268 else if (opcode == BLOCK_LAYOUT_BYREF) {
2269 byref_word_count = (inst & 0xF)+1;
2270 inst = Layout[1];
2271 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2272 if (opcode == BLOCK_LAYOUT_WEAK)
2273 weak_word_count = (inst & 0xF)+1;
2274 else
2275 return 0;
2276 }
2277 else
2278 return 0;
2279 break;
2280
2281 case 1:
2282 inst = Layout[0];
2283 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2284 if (opcode == BLOCK_LAYOUT_STRONG)
2285 strong_word_count = (inst & 0xF)+1;
2286 else if (opcode == BLOCK_LAYOUT_BYREF)
2287 byref_word_count = (inst & 0xF)+1;
2288 else if (opcode == BLOCK_LAYOUT_WEAK)
2289 weak_word_count = (inst & 0xF)+1;
2290 else
2291 return 0;
2292 break;
2293
2294 default:
2295 return 0;
2296 }
2297
2298 // Cannot inline when any of the word counts is 15. Because this is one less
2299 // than the actual work count (so 15 means 16 actual word counts),
2300 // and we can only display 0 thru 15 word counts.
2301 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2302 return 0;
2303
2304 unsigned count =
2305 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2306
2307 if (size == count) {
2308 if (strong_word_count)
2309 Result = strong_word_count;
2310 Result <<= 4;
2311 if (byref_word_count)
2312 Result += byref_word_count;
2313 Result <<= 4;
2314 if (weak_word_count)
2315 Result += weak_word_count;
2316 }
2317 }
2318 return Result;
2319}
2320
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002321llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) {
2322 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2323 if (RunSkipBlockVars.empty())
2324 return nullPtr;
John McCallc8e01702013-04-16 22:48:15 +00002325 unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0);
2326 unsigned ByteSizeInBits = CGM.getTarget().getCharWidth();
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002327 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2328
2329 // Sort on byte position; captures might not be allocated in order,
2330 // and unions can do funny things.
2331 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
2332 SmallVector<unsigned char, 16> Layout;
2333
2334 unsigned size = RunSkipBlockVars.size();
2335 for (unsigned i = 0; i < size; i++) {
2336 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2337 CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2338 CharUnits end_byte_pos = start_byte_pos;
2339 unsigned j = i+1;
2340 while (j < size) {
2341 if (opcode == RunSkipBlockVars[j].opcode) {
2342 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2343 i++;
2344 }
2345 else
2346 break;
2347 }
2348 CharUnits size_in_bytes =
2349 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
2350 if (j < size) {
2351 CharUnits gap =
2352 RunSkipBlockVars[j].block_var_bytepos -
2353 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2354 size_in_bytes += gap;
2355 }
2356 CharUnits residue_in_bytes = CharUnits::Zero();
2357 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2358 residue_in_bytes = size_in_bytes % WordSizeInBytes;
2359 size_in_bytes -= residue_in_bytes;
2360 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2361 }
2362
2363 unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes;
2364 while (size_in_words >= 16) {
2365 // Note that value in imm. is one less that the actual
2366 // value. So, 0xf means 16 words follow!
2367 unsigned char inst = (opcode << 4) | 0xf;
2368 Layout.push_back(inst);
2369 size_in_words -= 16;
2370 }
2371 if (size_in_words > 0) {
2372 // Note that value in imm. is one less that the actual
2373 // value. So, we subtract 1 away!
2374 unsigned char inst = (opcode << 4) | (size_in_words-1);
2375 Layout.push_back(inst);
2376 }
2377 if (residue_in_bytes > CharUnits::Zero()) {
2378 unsigned char inst =
2379 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1);
2380 Layout.push_back(inst);
2381 }
2382 }
2383
2384 int e = Layout.size()-1;
2385 while (e >= 0) {
2386 unsigned char inst = Layout[e--];
2387 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2388 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2389 Layout.pop_back();
2390 else
2391 break;
2392 }
2393
2394 uint64_t Result = InlineLayoutInstruction(Layout);
2395 if (Result != 0) {
2396 // Block variable layout instruction has been inlined.
2397 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2398 if (ComputeByrefLayout)
2399 printf("\n Inline instruction for BYREF variable layout: ");
2400 else
2401 printf("\n Inline instruction for block variable layout: ");
Benjamin Kramerc2727942013-04-22 16:10:38 +00002402 printf("0x0%" PRIx64 "\n", Result);
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002403 }
2404 if (WordSizeInBytes == 8) {
2405 const llvm::APInt Instruction(64, Result);
2406 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2407 }
2408 else {
2409 const llvm::APInt Instruction(32, Result);
2410 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2411 }
2412 }
2413
2414 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2415 Layout.push_back(inst);
2416 std::string BitMap;
2417 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2418 BitMap += Layout[i];
2419
2420 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2421 if (ComputeByrefLayout)
2422 printf("\n BYREF variable layout: ");
2423 else
2424 printf("\n block variable layout: ");
2425 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2426 unsigned char inst = BitMap[i];
2427 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2428 unsigned delta = 1;
2429 switch (opcode) {
2430 case BLOCK_LAYOUT_OPERATOR:
2431 printf("BL_OPERATOR:");
2432 delta = 0;
2433 break;
2434 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2435 printf("BL_NON_OBJECT_BYTES:");
2436 break;
2437 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2438 printf("BL_NON_OBJECT_WORD:");
2439 break;
2440 case BLOCK_LAYOUT_STRONG:
2441 printf("BL_STRONG:");
2442 break;
2443 case BLOCK_LAYOUT_BYREF:
2444 printf("BL_BYREF:");
2445 break;
2446 case BLOCK_LAYOUT_WEAK:
2447 printf("BL_WEAK:");
2448 break;
2449 case BLOCK_LAYOUT_UNRETAINED:
2450 printf("BL_UNRETAINED:");
2451 break;
2452 }
2453 // Actual value of word count is one more that what is in the imm.
2454 // field of the instruction
2455 printf("%d", (inst & 0xf) + delta);
2456 if (i < e-1)
2457 printf(", ");
2458 else
2459 printf("\n");
2460 }
2461 }
2462
2463 llvm::GlobalVariable * Entry =
2464 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2465 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2466 "__TEXT,__objc_classname,cstring_literals", 1, true);
2467 return getConstantGEP(VMContext, Entry, 0, 0);
2468}
2469
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002470llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2471 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002472 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2473
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002474 RunSkipBlockVars.clear();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002475 bool hasUnion = false;
2476
John McCallc8e01702013-04-16 22:48:15 +00002477 unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0);
2478 unsigned ByteSizeInBits = CGM.getTarget().getCharWidth();
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002479 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2480
2481 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2482
2483 // Calculate the basic layout of the block structure.
2484 const llvm::StructLayout *layout =
2485 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2486
2487 // Ignore the optional 'this' capture: C++ objects are not assumed
2488 // to be GC'ed.
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00002489 if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero())
2490 UpdateRunSkipBlockVars(false, Qualifiers::OCL_None,
2491 blockInfo.BlockHeaderForcedGapOffset,
2492 blockInfo.BlockHeaderForcedGapSize);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002493 // Walk the captured variables.
2494 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2495 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2496 const VarDecl *variable = ci->getVariable();
2497 QualType type = variable->getType();
2498
2499 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2500
2501 // Ignore constant captures.
2502 if (capture.isConstant()) continue;
2503
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002504 CharUnits fieldOffset =
2505 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002506
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002507 assert(!type->isArrayType() && "array variable should not be caught");
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002508 if (!ci->isByRef())
2509 if (const RecordType *record = type->getAs<RecordType>()) {
2510 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
2511 continue;
2512 }
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002513 CharUnits fieldSize;
2514 if (ci->isByRef())
2515 fieldSize = CharUnits::fromQuantity(WordSizeInBytes);
2516 else
2517 fieldSize = CGM.getContext().getTypeSizeInChars(type);
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002518 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002519 fieldOffset, fieldSize);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002520 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002521 return getBitmapBlockLayout(false);
2522}
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002523
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002524
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002525llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM,
2526 QualType T) {
2527 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2528 assert(!T->isArrayType() && "__block array variable should not be caught");
2529 CharUnits fieldOffset;
2530 RunSkipBlockVars.clear();
2531 bool hasUnion = false;
2532 if (const RecordType *record = T->getAs<RecordType>()) {
2533 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */);
2534 llvm::Constant *Result = getBitmapBlockLayout(true);
2535 return Result;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002536 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002537 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2538 return nullPtr;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002539}
2540
John McCall882987f2013-02-28 19:01:20 +00002541llvm::Value *CGObjCMac::GenerateProtocolRef(CodeGenFunction &CGF,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00002542 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00002543 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002544 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00002545 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2546
Owen Andersonade90fd2009-07-29 18:54:39 +00002547 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00002548 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002549}
2550
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002551void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00002552 // FIXME: We shouldn't need this, the protocol decl should contain enough
2553 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00002554 DefinedProtocols.insert(PD->getIdentifier());
2555
2556 // If we have generated a forward reference to this protocol, emit
2557 // it now. Otherwise do nothing, the protocol objects are lazily
2558 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002559 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00002560 GetOrEmitProtocol(PD);
2561}
2562
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002563llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002564 if (DefinedProtocols.count(PD->getIdentifier()))
2565 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002566
Daniel Dunbarc475d422008-10-29 22:36:39 +00002567 return GetOrEmitProtocolRef(PD);
2568}
2569
Rafael Espindola21039aa2014-02-27 16:26:32 +00002570static void assertPrivateName(const llvm::GlobalValue *GV) {
2571 StringRef NameRef = GV->getName();
Rui Ueyamaad33e602014-02-27 20:50:04 +00002572 (void)NameRef;
Rafael Espindola21039aa2014-02-27 16:26:32 +00002573 assert(NameRef[0] == '\01' && (NameRef[1] == 'L' || NameRef[1] == 'l'));
Rafael Espindola5179a4e2014-02-27 19:01:11 +00002574 assert(GV->getVisibility() == llvm::GlobalValue::DefaultVisibility);
2575 assert(GV->getLinkage() == llvm::GlobalValue::PrivateLinkage);
Rafael Espindola21039aa2014-02-27 16:26:32 +00002576}
2577
Daniel Dunbarb036db82008-08-13 03:21:16 +00002578/*
Rafael Espindolaf9e1e5e2014-02-20 14:09:04 +00002579// Objective-C 1.0 extensions
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002580struct _objc_protocol {
2581struct _objc_protocol_extension *isa;
2582char *protocol_name;
2583struct _objc_protocol_list *protocol_list;
2584struct _objc__method_prototype_list *instance_methods;
2585struct _objc__method_prototype_list *class_methods
2586};
Daniel Dunbarb036db82008-08-13 03:21:16 +00002587
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002588See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00002589*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00002590llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00002591 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00002592
2593 // Early exit if a defining object has already been generated.
2594 if (Entry && Entry->hasInitializer())
2595 return Entry;
2596
Douglas Gregora715bff2012-01-01 19:51:50 +00002597 // Use the protocol definition, if there is one.
2598 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2599 PD = Def;
2600
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002601 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002602 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002603 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2604
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002605 // Construct method lists.
2606 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2607 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002608 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002609 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002610 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002611 ObjCMethodDecl *MD = *i;
2612 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002613 if (!C)
2614 return GetOrEmitProtocolRef(PD);
2615
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002616 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2617 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002618 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002619 } else {
2620 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002621 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002622 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002623 }
2624
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002625 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002626 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002627 ObjCMethodDecl *MD = *i;
2628 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002629 if (!C)
2630 return GetOrEmitProtocolRef(PD);
2631
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002632 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2633 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002634 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002635 } else {
2636 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002637 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002638 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002639 }
2640
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002641 MethodTypesExt.insert(MethodTypesExt.end(),
2642 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2643
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002644 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002645 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2646 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002647 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002648 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002649 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002650 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002651 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002652 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002653 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002654 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002655 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002656 ClassMethods)
2657 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002658 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002659 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002660
Daniel Dunbarb036db82008-08-13 03:21:16 +00002661 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002662 // Already created, fix the linkage and update the initializer.
Daniel Dunbarb036db82008-08-13 03:21:16 +00002663 Entry->setInitializer(Init);
2664 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002665 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002666 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00002667 llvm::GlobalValue::PrivateLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002668 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002669 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002670 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002671 // FIXME: Is this necessary? Why only for protocol?
2672 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002673
2674 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002675 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00002676 assertPrivateName(Entry);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002677 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002678
2679 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002680}
2681
Daniel Dunbarc475d422008-10-29 22:36:39 +00002682llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002683 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2684
2685 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002686 // We use the initializer as a marker of whether this is a forward
2687 // reference or not. At module finalization we add the empty
2688 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002689 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002690 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00002691 llvm::GlobalValue::PrivateLinkage,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002692 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002693 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002694 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002695 // FIXME: Is this necessary? Why only for protocol?
2696 Entry->setAlignment(4);
2697 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00002698 assertPrivateName(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002699
Daniel Dunbarb036db82008-08-13 03:21:16 +00002700 return Entry;
2701}
2702
2703/*
2704 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002705 uint32_t size;
2706 struct objc_method_description_list *optional_instance_methods;
2707 struct objc_method_description_list *optional_class_methods;
2708 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002709 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002710 };
2711*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002712llvm::Constant *
2713CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002714 ArrayRef<llvm::Constant*> OptInstanceMethods,
2715 ArrayRef<llvm::Constant*> OptClassMethods,
2716 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002717 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002718 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002719 llvm::Constant *Values[] = {
2720 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002721 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002722 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002723 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002724 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002725 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002726 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002727 OptClassMethods),
2728 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002729 ObjCTypes),
2730 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2731 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002732 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002733
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002734 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002735 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002736 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002737 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002738
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002739 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002740 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002741
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002742 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002743 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002744 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002745 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002746}
2747
2748/*
2749 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002750 struct objc_protocol_list *next;
2751 long count;
2752 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002753 };
2754*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002755llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002756CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002757 ObjCProtocolDecl::protocol_iterator begin,
2758 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002759 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002760
Daniel Dunbardec75f82008-08-21 21:57:41 +00002761 for (; begin != end; ++begin)
2762 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002763
2764 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002765 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002766 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002767
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002768 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002769 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002770
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002771 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002772 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002773 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002774 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002775 ProtocolRefs.size() - 1);
2776 Values[2] =
2777 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2778 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002779 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002780
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002781 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002782 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002783 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002784 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002785 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002786}
2787
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002788void CGObjCCommonMac::
2789PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002790 SmallVectorImpl<llvm::Constant *> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002791 const Decl *Container,
2792 const ObjCProtocolDecl *PROTO,
2793 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002794 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2795 E = PROTO->protocol_end(); P != E; ++P)
2796 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2797 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2798 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002799 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002800 if (!PropertySet.insert(PD->getIdentifier()))
2801 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002802 llvm::Constant *Prop[] = {
2803 GetPropertyName(PD->getIdentifier()),
2804 GetPropertyTypeString(PD, Container)
2805 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002806 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2807 }
2808}
2809
Daniel Dunbarb036db82008-08-13 03:21:16 +00002810/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002811 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002812 const char * const name;
2813 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002814 };
2815
2816 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002817 uint32_t entsize; // sizeof (struct _objc_property)
2818 uint32_t prop_count;
2819 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002820 };
2821*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002822llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002823 const Decl *Container,
2824 const ObjCContainerDecl *OCD,
2825 const ObjCCommonTypesHelper &ObjCTypes) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002826 SmallVector<llvm::Constant *, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002827 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002828 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2829 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002830 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002831 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002832 llvm::Constant *Prop[] = {
2833 GetPropertyName(PD->getIdentifier()),
2834 GetPropertyTypeString(PD, Container)
2835 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002836 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002837 Prop));
2838 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002839 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002840 for (ObjCInterfaceDecl::all_protocol_iterator
2841 P = OID->all_referenced_protocol_begin(),
2842 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002843 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2844 ObjCTypes);
2845 }
2846 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2847 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2848 E = CD->protocol_end(); P != E; ++P)
2849 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2850 ObjCTypes);
2851 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002852
2853 // Return null for empty list.
2854 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002855 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002856
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002857 unsigned PropertySize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002858 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002859 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002860 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2861 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002862 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002863 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002864 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002865 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002866
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002867 llvm::GlobalVariable *GV =
2868 CreateMetadataVar(Name, Init,
2869 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002870 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002871 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002872 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002873 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002874}
2875
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002876llvm::Constant *
2877CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2878 ArrayRef<llvm::Constant*> MethodTypes,
2879 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002880 // Return null for empty list.
2881 if (MethodTypes.empty())
2882 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2883
2884 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2885 MethodTypes.size());
2886 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2887
2888 llvm::GlobalVariable *GV =
2889 CreateMetadataVar(Name, Init,
2890 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2891 (ObjCABI == 2) ? 8 : 4,
2892 true);
2893 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2894}
2895
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002896/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002897 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002898 int count;
2899 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002900 };
2901*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002902llvm::Constant *
2903CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002904 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002905 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002906 ObjCTypes.SelectorPtrTy),
2907 GetMethodVarType(MD)
2908 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002909 if (!Desc[1])
2910 return 0;
2911
Owen Anderson0e0189d2009-07-27 22:29:56 +00002912 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002913 Desc);
2914}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002915
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002916llvm::Constant *
2917CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2918 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002919 // Return null for empty list.
2920 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002921 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002922
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002923 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002924 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002925 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002926 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002927 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002928 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002929
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002930 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002932 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002933}
2934
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002935/*
2936 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002937 char *category_name;
2938 char *class_name;
2939 struct _objc_method_list *instance_methods;
2940 struct _objc_method_list *class_methods;
2941 struct _objc_protocol_list *protocols;
2942 uint32_t size; // <rdar://4585769>
2943 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002944 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002945*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002946void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00002947 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002948
Mike Stump18bb9282009-05-16 07:57:57 +00002949 // FIXME: This is poor design, the OCD should have a pointer to the category
2950 // decl. Additionally, note that Category can be null for the @implementation
2951 // w/o an @interface case. Sema should just create one for us as it does for
2952 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002953 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002954 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002955 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002956
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002957 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002958 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2959 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002960
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002961 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002962 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002963 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002964 // Instance methods should always be defined.
2965 InstanceMethods.push_back(GetMethodConstant(*i));
2966 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002967 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002968 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002969 // Class methods should always be defined.
2970 ClassMethods.push_back(GetMethodConstant(*i));
2971 }
2972
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002973 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002974 Values[0] = GetClassName(OCD->getIdentifier());
2975 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002976 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002977 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002978 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002979 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002980 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002981 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002982 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002983 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002984 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002985 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002986 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002987 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002988 Category->protocol_begin(),
2989 Category->protocol_end());
2990 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002991 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002992 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002993 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002994
2995 // If there is no category @interface then there can be no properties.
2996 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002997 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002998 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002999 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003000 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00003001 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003002
Owen Anderson0e0189d2009-07-27 22:29:56 +00003003 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003004 Values);
3005
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003006 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003007 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003008 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003009 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003010 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003011 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00003012 // method definition entries must be clear for next implementation.
3013 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003014}
3015
John McCallef19dbb2012-10-17 04:53:23 +00003016enum FragileClassFlags {
3017 FragileABI_Class_Factory = 0x00001,
3018 FragileABI_Class_Meta = 0x00002,
3019 FragileABI_Class_HasCXXStructors = 0x02000,
3020 FragileABI_Class_Hidden = 0x20000
3021};
3022
3023enum NonFragileClassFlags {
3024 /// Is a meta-class.
3025 NonFragileABI_Class_Meta = 0x00001,
3026
3027 /// Is a root class.
3028 NonFragileABI_Class_Root = 0x00002,
3029
3030 /// Has a C++ constructor and destructor.
3031 NonFragileABI_Class_HasCXXStructors = 0x00004,
3032
3033 /// Has hidden visibility.
3034 NonFragileABI_Class_Hidden = 0x00010,
3035
3036 /// Has the exception attribute.
3037 NonFragileABI_Class_Exception = 0x00020,
3038
3039 /// (Obsolete) ARC-specific: this class has a .release_ivars method
3040 NonFragileABI_Class_HasIvarReleaser = 0x00040,
3041
3042 /// Class implementation was compiled under ARC.
John McCall0d54a172012-10-17 04:53:31 +00003043 NonFragileABI_Class_CompiledByARC = 0x00080,
3044
3045 /// Class has non-trivial destructors, but zero-initialization is okay.
3046 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003047};
3048
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003049/*
3050 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003051 Class isa;
3052 Class super_class;
3053 const char *name;
3054 long version;
3055 long info;
3056 long instance_size;
3057 struct _objc_ivar_list *ivars;
3058 struct _objc_method_list *methods;
3059 struct _objc_cache *cache;
3060 struct _objc_protocol_list *protocols;
3061 // Objective-C 1.0 extensions (<rdr://4585769>)
3062 const char *ivar_layout;
3063 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003064 };
3065
3066 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003067*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003068void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003069 DefinedSymbols.insert(ID->getIdentifier());
3070
Chris Lattner86d7d912008-11-24 03:54:41 +00003071 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003072 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003073 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003074 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003075 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003076 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003077 Interface->all_referenced_protocol_begin(),
3078 Interface->all_referenced_protocol_end());
John McCallef19dbb2012-10-17 04:53:23 +00003079 unsigned Flags = FragileABI_Class_Factory;
John McCall0d54a172012-10-17 04:53:31 +00003080 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCallef19dbb2012-10-17 04:53:23 +00003081 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003082 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00003083 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003084
3085 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00003086 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00003087 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003088
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003089 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003090 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003091 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003092 // Instance methods should always be defined.
3093 InstanceMethods.push_back(GetMethodConstant(*i));
3094 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003095 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003096 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003097 // Class methods should always be defined.
3098 ClassMethods.push_back(GetMethodConstant(*i));
3099 }
3100
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003101 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003102 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00003103 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003104
3105 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
3106 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3107
3108 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3109 if (llvm::Constant *C = GetMethodConstant(MD))
3110 InstanceMethods.push_back(C);
3111 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3112 if (llvm::Constant *C = GetMethodConstant(MD))
3113 InstanceMethods.push_back(C);
3114 }
3115 }
3116
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003117 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00003118 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003119 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003120 // Record a reference to the super class.
3121 LazySymbols.insert(Super->getIdentifier());
3122
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003123 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003124 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003125 ObjCTypes.ClassPtrTy);
3126 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003127 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003128 }
3129 Values[ 2] = GetClassName(ID->getIdentifier());
3130 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003131 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3132 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3133 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003134 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003135 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003136 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003137 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003138 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003139 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003140 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003141 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003142 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003143 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003144 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003145 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003146 std::string Name("\01L_OBJC_CLASS_");
3147 Name += ClassName;
3148 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3149 // Check for a forward reference.
Rafael Espindola554256c2014-02-26 22:25:45 +00003150 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003151 if (GV) {
3152 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3153 "Forward metaclass reference has incorrect type.");
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003154 GV->setInitializer(Init);
3155 GV->setSection(Section);
3156 GV->setAlignment(4);
3157 CGM.AddUsedGlobal(GV);
Rafael Espindola21039aa2014-02-27 16:26:32 +00003158 } else
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003159 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Rafael Espindola21039aa2014-02-27 16:26:32 +00003160 assertPrivateName(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003161 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00003162 // method definition entries must be clear for next implementation.
3163 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003164}
3165
3166llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3167 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003168 ArrayRef<llvm::Constant*> Methods) {
John McCallef19dbb2012-10-17 04:53:23 +00003169 unsigned Flags = FragileABI_Class_Meta;
Micah Villmowdd31ca12012-10-08 16:25:52 +00003170 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003171
John McCall457a04e2010-10-22 21:05:15 +00003172 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00003173 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003175 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003176 // The isa for the metaclass is the root of the hierarchy.
3177 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3178 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3179 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003180 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003181 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003182 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003183 // The super class for the metaclass is emitted as the name of the
3184 // super class. The runtime fixes this up to point to the
3185 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003186 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003187 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003188 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003189 ObjCTypes.ClassPtrTy);
3190 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003191 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003192 }
3193 Values[ 2] = GetClassName(ID->getIdentifier());
3194 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003195 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3196 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3197 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003198 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003199 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003200 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003201 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003202 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003203 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003204 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003205 Values[ 9] = Protocols;
3206 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003207 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003208 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00003209 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003210 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003211 Values);
3212
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003213 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer1bbcbd02012-07-31 11:45:39 +00003214 Name += ID->getName();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003215
3216 // Check for a forward reference.
Rafael Espindola554256c2014-02-26 22:25:45 +00003217 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003218 if (GV) {
3219 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3220 "Forward metaclass reference has incorrect type.");
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003221 GV->setInitializer(Init);
3222 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00003223 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00003224 llvm::GlobalValue::PrivateLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00003225 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003226 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00003227 assertPrivateName(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003228 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00003229 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003230 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003231
3232 return GV;
3233}
3234
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003235llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003236 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003237
Mike Stump18bb9282009-05-16 07:57:57 +00003238 // FIXME: Should we look these up somewhere other than the module. Its a bit
3239 // silly since we only generate these while processing an implementation, so
3240 // exactly one pointer would work if know when we entered/exitted an
3241 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003242
3243 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00003244 // Previously, metaclass with internal linkage may have been defined.
3245 // pass 'true' as 2nd argument so it is returned.
Rafael Espindola21039aa2014-02-27 16:26:32 +00003246 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
3247 if (!GV)
3248 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00003249 llvm::GlobalValue::PrivateLinkage, 0, Name);
Rafael Espindola21039aa2014-02-27 16:26:32 +00003250
3251 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3252 "Forward metaclass reference has incorrect type.");
3253 assertPrivateName(GV);
3254 return GV;
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003255}
3256
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003257llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3258 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
Rafael Espindola21039aa2014-02-27 16:26:32 +00003259 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
3260
3261 if (!GV)
3262 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00003263 llvm::GlobalValue::PrivateLinkage, 0, Name);
Rafael Espindola21039aa2014-02-27 16:26:32 +00003264
3265 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3266 "Forward class metadata reference has incorrect type.");
3267 assertPrivateName(GV);
3268 return GV;
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003269}
3270
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003271/*
3272 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003273 uint32_t size;
3274 const char *weak_ivar_layout;
3275 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003276 };
3277*/
3278llvm::Constant *
3279CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003280 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00003281 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003282
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003283 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003284 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00003285 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003286 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00003287 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003288
3289 // Return null if no extension bits are used.
3290 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00003291 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003292
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003293 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00003294 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003295 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003296 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003297 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003298}
3299
3300/*
3301 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003302 char *ivar_name;
3303 char *ivar_type;
3304 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003305 };
3306
3307 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003308 int ivar_count;
3309 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003310 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003311*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003312llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003313 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003314 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003315
3316 // When emitting the root class GCC emits ivar entries for the
3317 // actual class structure. It is not clear if we need to follow this
3318 // behavior; for now lets try and get away with not doing it. If so,
3319 // the cleanest solution would be to make up an ObjCInterfaceDecl
3320 // for the class.
3321 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00003322 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003323
Jordy Rosea91768e2011-07-22 02:08:32 +00003324 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003325
Jordy Rosea91768e2011-07-22 02:08:32 +00003326 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003327 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00003328 // Ignore unnamed bit-fields.
3329 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003330 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003331 llvm::Constant *Ivar[] = {
3332 GetMethodVarName(IVD->getIdentifier()),
3333 GetMethodVarType(IVD),
3334 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedman8cbca202012-11-06 22:15:52 +00003335 ComputeIvarBaseOffset(CGM, OID, IVD))
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003336 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003337 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003338 }
3339
3340 // Return null for empty list.
3341 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003342 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003343
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003344 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003345 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003346 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003347 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003348 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003349 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003350
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003351 llvm::GlobalVariable *GV;
3352 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003353 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003354 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003355 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003356 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003357 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003358 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003359 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003360 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003361}
3362
3363/*
3364 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003365 SEL method_name;
3366 char *method_types;
3367 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003368 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003369
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003370 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003371 struct objc_method_list *obsolete;
3372 int count;
3373 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003374 };
3375*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003376
3377/// GetMethodConstant - Return a struct objc_method constant for the
3378/// given method if it has been defined. The result is null if the
3379/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00003380llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003381 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003382 if (!Fn)
3383 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003384
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003385 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00003386 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003387 ObjCTypes.SelectorPtrTy),
3388 GetMethodVarType(MD),
3389 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3390 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003391 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003392}
3393
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003394llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003395 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003396 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003397 // Return null for empty list.
3398 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003399 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003400
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003401 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00003402 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003403 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003404 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003405 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003406 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003407 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003408
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003409 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003410 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003411}
3412
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00003413llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003414 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003415 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003416 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003417
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00003418 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00003419 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00003420 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003421 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00003422 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003423 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00003424 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003425 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003426 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003427
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003428 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003429}
3430
Daniel Dunbar30c65362009-03-09 20:09:19 +00003431llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003432CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00003433 llvm::Constant *Init,
3434 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003435 unsigned Align,
3436 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003437 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003438 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00003439 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00003440 llvm::GlobalValue::PrivateLinkage, Init, Name);
Rafael Espindola21039aa2014-02-27 16:26:32 +00003441 assertPrivateName(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003442 if (Section)
3443 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003444 if (Align)
3445 GV->setAlignment(Align);
3446 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00003447 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003448 return GV;
3449}
3450
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003451llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003452 // Abuse this interface function as a place to finalize.
3453 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003454 return NULL;
3455}
3456
Chris Lattnerd4808922009-03-22 21:03:39 +00003457llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003458 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003459}
3460
Chris Lattnerd4808922009-03-22 21:03:39 +00003461llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003462 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003463}
3464
Ted Kremeneke65b0862012-03-06 20:05:56 +00003465llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3466 bool copy) {
3467 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3468}
3469
David Chisnall168b80f2010-12-26 22:13:16 +00003470llvm::Constant *CGObjCMac::GetGetStructFunction() {
3471 return ObjCTypes.getCopyStructFn();
3472}
3473llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00003474 return ObjCTypes.getCopyStructFn();
3475}
3476
David Chisnall0d75e062012-12-17 18:54:24 +00003477llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() {
3478 return ObjCTypes.getCppAtomicObjectFunction();
3479}
3480llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() {
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00003481 return ObjCTypes.getCppAtomicObjectFunction();
3482}
3483
Chris Lattnerd4808922009-03-22 21:03:39 +00003484llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003485 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00003486}
3487
John McCallbd309292010-07-06 01:34:17 +00003488void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3489 return EmitTryOrSynchronizedStmt(CGF, S);
3490}
3491
3492void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3493 const ObjCAtSynchronizedStmt &S) {
3494 return EmitTryOrSynchronizedStmt(CGF, S);
3495}
3496
John McCall65bea082010-07-21 06:59:36 +00003497namespace {
John McCallcda666c2010-07-21 07:22:38 +00003498 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00003499 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00003500 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00003501 llvm::Value *CallTryExitVar;
3502 llvm::Value *ExceptionData;
3503 ObjCTypesHelper &ObjCTypes;
3504 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00003505 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00003506 llvm::Value *CallTryExitVar,
3507 llvm::Value *ExceptionData,
3508 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00003509 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00003510 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3511
John McCall30317fd2011-07-12 20:27:29 +00003512 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00003513 // Check whether we need to call objc_exception_try_exit.
3514 // In optimized code, this branch will always be folded.
3515 llvm::BasicBlock *FinallyCallExit =
3516 CGF.createBasicBlock("finally.call_exit");
3517 llvm::BasicBlock *FinallyNoCallExit =
3518 CGF.createBasicBlock("finally.no_call_exit");
3519 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3520 FinallyCallExit, FinallyNoCallExit);
3521
3522 CGF.EmitBlock(FinallyCallExit);
John McCall882987f2013-02-28 19:01:20 +00003523 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryExitFn(),
3524 ExceptionData);
John McCall65bea082010-07-21 06:59:36 +00003525
3526 CGF.EmitBlock(FinallyNoCallExit);
3527
3528 if (isa<ObjCAtTryStmt>(S)) {
3529 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00003530 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
John McCall638d4f52013-04-03 00:56:07 +00003531 // Don't try to do the @finally if this is an EH cleanup.
3532 if (flags.isForEHCleanup()) return;
3533
John McCallcebe0ca2010-08-11 00:16:14 +00003534 // Save the current cleanup destination in case there's
3535 // control flow inside the finally statement.
3536 llvm::Value *CurCleanupDest =
3537 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3538
John McCall65bea082010-07-21 06:59:36 +00003539 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3540
John McCallcebe0ca2010-08-11 00:16:14 +00003541 if (CGF.HaveInsertPoint()) {
3542 CGF.Builder.CreateStore(CurCleanupDest,
3543 CGF.getNormalCleanupDestSlot());
3544 } else {
3545 // Currently, the end of the cleanup must always exist.
3546 CGF.EnsureInsertPoint();
3547 }
3548 }
John McCall65bea082010-07-21 06:59:36 +00003549 } else {
3550 // Emit objc_sync_exit(expr); as finally's sole statement for
3551 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00003552 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall882987f2013-02-28 19:01:20 +00003553 CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncExitFn(), SyncArg);
John McCall65bea082010-07-21 06:59:36 +00003554 }
3555 }
3556 };
John McCall42227ed2010-07-31 23:20:56 +00003557
3558 class FragileHazards {
3559 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003560 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00003561 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3562
3563 llvm::InlineAsm *ReadHazard;
3564 llvm::InlineAsm *WriteHazard;
3565
3566 llvm::FunctionType *GetAsmFnType();
3567
3568 void collectLocals();
3569 void emitReadHazard(CGBuilderTy &Builder);
3570
3571 public:
3572 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00003573
John McCall42227ed2010-07-31 23:20:56 +00003574 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00003575 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003576 };
3577}
3578
3579/// Create the fragile-ABI read and write hazards based on the current
3580/// state of the function, which is presumed to be immediately prior
3581/// to a @try block. These hazards are used to maintain correct
3582/// semantics in the face of optimization and the fragile ABI's
3583/// cavalier use of setjmp/longjmp.
3584FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3585 collectLocals();
3586
3587 if (Locals.empty()) return;
3588
3589 // Collect all the blocks in the function.
3590 for (llvm::Function::iterator
3591 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3592 BlocksBeforeTry.insert(&*I);
3593
3594 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3595
3596 // Create a read hazard for the allocas. This inhibits dead-store
3597 // optimizations and forces the values to memory. This hazard is
3598 // inserted before any 'throwing' calls in the protected scope to
3599 // reflect the possibility that the variables might be read from the
3600 // catch block if the call throws.
3601 {
3602 std::string Constraint;
3603 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3604 if (I) Constraint += ',';
3605 Constraint += "*m";
3606 }
3607
3608 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3609 }
3610
3611 // Create a write hazard for the allocas. This inhibits folding
3612 // loads across the hazard. This hazard is inserted at the
3613 // beginning of the catch path to reflect the possibility that the
3614 // variables might have been written within the protected scope.
3615 {
3616 std::string Constraint;
3617 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3618 if (I) Constraint += ',';
3619 Constraint += "=*m";
3620 }
3621
3622 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3623 }
3624}
3625
3626/// Emit a write hazard at the current location.
3627void FragileHazards::emitWriteHazard() {
3628 if (Locals.empty()) return;
3629
John McCall882987f2013-02-28 19:01:20 +00003630 CGF.EmitNounwindRuntimeCall(WriteHazard, Locals);
John McCall42227ed2010-07-31 23:20:56 +00003631}
3632
John McCall42227ed2010-07-31 23:20:56 +00003633void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3634 assert(!Locals.empty());
John McCall882987f2013-02-28 19:01:20 +00003635 llvm::CallInst *call = Builder.CreateCall(ReadHazard, Locals);
3636 call->setDoesNotThrow();
3637 call->setCallingConv(CGF.getRuntimeCC());
John McCall42227ed2010-07-31 23:20:56 +00003638}
3639
3640/// Emit read hazards in all the protected blocks, i.e. all the blocks
3641/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00003642void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00003643 if (Locals.empty()) return;
3644
3645 CGBuilderTy Builder(CGF.getLLVMContext());
3646
3647 // Iterate through all blocks, skipping those prior to the try.
3648 for (llvm::Function::iterator
3649 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3650 llvm::BasicBlock &BB = *FI;
3651 if (BlocksBeforeTry.count(&BB)) continue;
3652
3653 // Walk through all the calls in the block.
3654 for (llvm::BasicBlock::iterator
3655 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3656 llvm::Instruction &I = *BI;
3657
3658 // Ignore instructions that aren't non-intrinsic calls.
3659 // These are the only calls that can possibly call longjmp.
3660 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3661 if (isa<llvm::IntrinsicInst>(I))
3662 continue;
3663
3664 // Ignore call sites marked nounwind. This may be questionable,
3665 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3666 llvm::CallSite CS(&I);
3667 if (CS.doesNotThrow()) continue;
3668
John McCall2dd7d442010-08-04 05:59:32 +00003669 // Insert a read hazard before the call. This will ensure that
3670 // any writes to the locals are performed before making the
3671 // call. If the call throws, then this is sufficient to
3672 // guarantee correctness as long as it doesn't also write to any
3673 // locals.
John McCall42227ed2010-07-31 23:20:56 +00003674 Builder.SetInsertPoint(&BB, BI);
3675 emitReadHazard(Builder);
3676 }
3677 }
3678}
3679
3680static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3681 if (V) S.insert(V);
3682}
3683
3684void FragileHazards::collectLocals() {
3685 // Compute a set of allocas to ignore.
3686 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3687 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3688 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00003689
3690 // Collect all the allocas currently in the function. This is
3691 // probably way too aggressive.
3692 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3693 for (llvm::BasicBlock::iterator
3694 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3695 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3696 Locals.push_back(&*I);
3697}
3698
3699llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003700 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003701 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3702 tys[i] = Locals[i]->getType();
3703 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003704}
3705
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003707
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003708 Objective-C setjmp-longjmp (sjlj) Exception Handling
3709 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003710
John McCallbd309292010-07-06 01:34:17 +00003711 A catch buffer is a setjmp buffer plus:
3712 - a pointer to the exception that was caught
3713 - a pointer to the previous exception data buffer
3714 - two pointers of reserved storage
3715 Therefore catch buffers form a stack, with a pointer to the top
3716 of the stack kept in thread-local storage.
3717
3718 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3719 objc_exception_try_exit pops the given catch buffer, which is
3720 required to be the top of the EH stack.
3721 objc_exception_throw pops the top of the EH stack, writes the
3722 thrown exception into the appropriate field, and longjmps
3723 to the setjmp buffer. It crashes the process (with a printf
3724 and an abort()) if there are no catch buffers on the stack.
3725 objc_exception_extract just reads the exception pointer out of the
3726 catch buffer.
3727
3728 There's no reason an implementation couldn't use a light-weight
3729 setjmp here --- something like __builtin_setjmp, but API-compatible
3730 with the heavyweight setjmp. This will be more important if we ever
3731 want to implement correct ObjC/C++ exception interactions for the
3732 fragile ABI.
3733
3734 Note that for this use of setjmp/longjmp to be correct, we may need
3735 to mark some local variables volatile: if a non-volatile local
3736 variable is modified between the setjmp and the longjmp, it has
3737 indeterminate value. For the purposes of LLVM IR, it may be
3738 sufficient to make loads and stores within the @try (to variables
3739 declared outside the @try) volatile. This is necessary for
3740 optimized correctness, but is not currently being done; this is
3741 being tracked as rdar://problem/8160285
3742
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003743 The basic framework for a @try-catch-finally is as follows:
3744 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003745 objc_exception_data d;
3746 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003747 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003748
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003749 objc_exception_try_enter(&d);
3750 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003751 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003752 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003753 // exception path
3754 id _caught = objc_exception_extract(&d);
3755
3756 // enter new try scope for handlers
3757 if (!setjmp(d.jmp_buf)) {
3758 ... match exception and execute catch blocks ...
3759
3760 // fell off end, rethrow.
3761 _rethrow = _caught;
3762 ... jump-through-finally to finally_rethrow ...
3763 } else {
3764 // exception in catch block
3765 _rethrow = objc_exception_extract(&d);
3766 _call_try_exit = false;
3767 ... jump-through-finally to finally_rethrow ...
3768 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003769 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003770 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003771
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003772 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003773 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003774 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003775
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003776 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003777 ... dispatch to finally destination ...
3778
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003779 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003780 objc_exception_throw(_rethrow);
3781
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003782 finally_end:
3783 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003784
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003785 This framework differs slightly from the one gcc uses, in that gcc
3786 uses _rethrow to determine if objc_exception_try_exit should be called
3787 and if the object should be rethrown. This breaks in the face of
3788 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003789
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003790 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003791
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003792 - If there are no catch blocks, then we avoid emitting the second
3793 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003794
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003795 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3796 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003797
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003798 - FIXME: If there is no @finally block we can do a few more
3799 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003800
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003801 Rethrows and Jumps-Through-Finally
3802 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003803
John McCallbd309292010-07-06 01:34:17 +00003804 '@throw;' is supported by pushing the currently-caught exception
3805 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003806
John McCallbd309292010-07-06 01:34:17 +00003807 Branches through the @finally block are handled with an ordinary
3808 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3809 exceptions are not compatible with C++ exceptions, and this is
3810 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003811
John McCallbd309292010-07-06 01:34:17 +00003812 @synchronized(expr) { stmt; } is emitted as if it were:
3813 id synch_value = expr;
3814 objc_sync_enter(synch_value);
3815 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003816*/
3817
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003818void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3819 const Stmt &S) {
3820 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003821
3822 // A destination for the fall-through edges of the catch handlers to
3823 // jump to.
3824 CodeGenFunction::JumpDest FinallyEnd =
3825 CGF.getJumpDestInCurrentScope("finally.end");
3826
3827 // A destination for the rethrow edge of the catch handlers to jump
3828 // to.
3829 CodeGenFunction::JumpDest FinallyRethrow =
3830 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003831
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003832 // For @synchronized, call objc_sync_enter(sync.expr). The
3833 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003834 // @synchronized. We can't avoid a temp here because we need the
3835 // value to be preserved. If the backend ever does liveness
3836 // correctly after setjmp, this will be unnecessary.
3837 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003838 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003839 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003840 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3841 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00003842 CGF.EmitNounwindRuntimeCall(ObjCTypes.getSyncEnterFn(), SyncArg);
John McCall2dd7d442010-08-04 05:59:32 +00003843
3844 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3845 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003846 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003847
John McCall2dd7d442010-08-04 05:59:32 +00003848 // Allocate memory for the setjmp buffer. This needs to be kept
3849 // live throughout the try and catch blocks.
3850 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3851 "exceptiondata.ptr");
3852
John McCall42227ed2010-07-31 23:20:56 +00003853 // Create the fragile hazards. Note that this will not capture any
3854 // of the allocas required for exception processing, but will
3855 // capture the current basic block (which extends all the way to the
3856 // setjmp call) as "before the @try".
3857 FragileHazards Hazards(CGF);
3858
John McCallbd309292010-07-06 01:34:17 +00003859 // Create a flag indicating whether the cleanup needs to call
3860 // objc_exception_try_exit. This is true except when
3861 // - no catches match and we're branching through the cleanup
3862 // just to rethrow the exception, or
3863 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003864 // The setjmp-safety rule here is that we should always store to this
3865 // variable in a place that dominates the branch through the cleanup
3866 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003867 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003868 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003869
John McCall9916e3f2010-10-04 23:42:51 +00003870 // A slot containing the exception to rethrow. Only needed when we
3871 // have both a @catch and a @finally.
3872 llvm::Value *PropagatingExnVar = 0;
3873
John McCallbd309292010-07-06 01:34:17 +00003874 // Push a normal cleanup to leave the try scope.
John McCall638d4f52013-04-03 00:56:07 +00003875 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalAndEHCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003876 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003877 CallTryExitVar,
3878 ExceptionData,
3879 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003880
3881 // Enter a try block:
3882 // - Call objc_exception_try_enter to push ExceptionData on top of
3883 // the EH stack.
John McCall882987f2013-02-28 19:01:20 +00003884 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
John McCallbd309292010-07-06 01:34:17 +00003885
3886 // - Call setjmp on the exception data buffer.
3887 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3888 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3889 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003890 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003891 llvm::CallInst *SetJmpResult =
John McCall882987f2013-02-28 19:01:20 +00003892 CGF.EmitNounwindRuntimeCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003893 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003894
3895 // If setjmp returned 0, enter the protected block; otherwise,
3896 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003897 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3898 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003899 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003900 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3901 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003902
John McCallbd309292010-07-06 01:34:17 +00003903 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003904 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003905 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003906 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003907 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003908
3909 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003910
John McCallbd309292010-07-06 01:34:17 +00003911 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003912 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003913
John McCall42227ed2010-07-31 23:20:56 +00003914 // Don't optimize loads of the in-scope locals across this point.
3915 Hazards.emitWriteHazard();
3916
John McCallbd309292010-07-06 01:34:17 +00003917 // For a @synchronized (or a @try with no catches), just branch
3918 // through the cleanup to the rethrow block.
3919 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3920 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003921 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003922 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003923
3924 // Otherwise, we have to match against the caught exceptions.
3925 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003926 // Retrieve the exception object. We may emit multiple blocks but
3927 // nothing can cross this so the value is already in SSA form.
3928 llvm::CallInst *Caught =
John McCall882987f2013-02-28 19:01:20 +00003929 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(),
3930 ExceptionData, "caught");
John McCall2dd7d442010-08-04 05:59:32 +00003931
John McCallbd309292010-07-06 01:34:17 +00003932 // Push the exception to rethrow onto the EH value stack for the
3933 // benefit of any @throws in the handlers.
3934 CGF.ObjCEHValueStack.push_back(Caught);
3935
Douglas Gregor96c79492010-04-23 22:50:49 +00003936 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003937
John McCall2dd7d442010-08-04 05:59:32 +00003938 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003939
John McCall2dd7d442010-08-04 05:59:32 +00003940 llvm::BasicBlock *CatchBlock = 0;
3941 llvm::BasicBlock *CatchHandler = 0;
3942 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003943 // Save the currently-propagating exception before
3944 // objc_exception_try_enter clears the exception slot.
3945 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3946 "propagating_exception");
3947 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3948
John McCall2dd7d442010-08-04 05:59:32 +00003949 // Enter a new exception try block (in case a @catch block
3950 // throws an exception).
John McCall882987f2013-02-28 19:01:20 +00003951 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionTryEnterFn(),
3952 ExceptionData);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003953
John McCall2dd7d442010-08-04 05:59:32 +00003954 llvm::CallInst *SetJmpResult =
John McCall882987f2013-02-28 19:01:20 +00003955 CGF.EmitNounwindRuntimeCall(ObjCTypes.getSetJmpFn(),
3956 SetJmpBuffer, "setjmp.result");
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003957 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003958
John McCall2dd7d442010-08-04 05:59:32 +00003959 llvm::Value *Threw =
3960 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3961
3962 CatchBlock = CGF.createBasicBlock("catch");
3963 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3964 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3965
3966 CGF.EmitBlock(CatchBlock);
3967 }
3968
3969 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003970
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003971 // Handle catch list. As a special case we check if everything is
3972 // matched and avoid generating code for falling off the end if
3973 // so.
3974 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003975 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3976 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003977
Douglas Gregor46a572b2010-04-26 16:46:50 +00003978 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003979 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003980
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003981 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003982 if (!CatchParam) {
3983 AllMatched = true;
3984 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003985 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003986
John McCallbd309292010-07-06 01:34:17 +00003987 // catch(id e) always matches under this ABI, since only
3988 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003989 // FIXME: For the time being we also match id<X>; this should
3990 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003991 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003992 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003993 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003994
John McCallbd309292010-07-06 01:34:17 +00003995 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003996 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003997 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3998
Anders Carlsson9396a892008-09-11 09:15:33 +00003999 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00004000 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004001 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00004002
4003 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00004004 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00004005 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004006
Anders Carlsson9396a892008-09-11 09:15:33 +00004007 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00004008
4009 // The scope of the catch variable ends right here.
4010 CatchVarCleanups.ForceCleanup();
4011
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004012 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004013 break;
4014 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004015
Steve Naroff7cae42b2009-07-10 23:34:53 +00004016 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00004017 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00004018
4019 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00004020 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
4021 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004022
4023 // Check if the @catch block matches the exception object.
John McCall882987f2013-02-28 19:01:20 +00004024 llvm::Value *Class = EmitClassRef(CGF, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004025
John McCall882987f2013-02-28 19:01:20 +00004026 llvm::Value *matchArgs[] = { Class, Caught };
John McCallbd309292010-07-06 01:34:17 +00004027 llvm::CallInst *Match =
John McCall882987f2013-02-28 19:01:20 +00004028 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionMatchFn(),
4029 matchArgs, "match");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004030
John McCallbd309292010-07-06 01:34:17 +00004031 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
4032 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004033
4034 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00004035 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004036
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004037 // Emit the @catch block.
4038 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00004039
4040 // Collect any cleanups for the catch variable. The scope lasts until
4041 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00004042 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00004043
John McCall1c9c3fd2010-10-15 04:57:14 +00004044 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004045 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00004046
John McCallbd309292010-07-06 01:34:17 +00004047 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004048 llvm::Value *Tmp =
4049 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004050 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00004051 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004052
Anders Carlsson9396a892008-09-11 09:15:33 +00004053 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00004054
4055 // We're done with the catch variable.
4056 CatchVarCleanups.ForceCleanup();
4057
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004058 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004059
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004060 CGF.EmitBlock(NextCatchBlock);
4061 }
4062
John McCallbd309292010-07-06 01:34:17 +00004063 CGF.ObjCEHValueStack.pop_back();
4064
John McCall2dd7d442010-08-04 05:59:32 +00004065 // If nothing wanted anything to do with the caught exception,
4066 // kill the extract call.
4067 if (Caught->use_empty())
4068 Caught->eraseFromParent();
4069
4070 if (!AllMatched)
4071 CGF.EmitBranchThroughCleanup(FinallyRethrow);
4072
4073 if (HasFinally) {
4074 // Emit the exception handler for the @catch blocks.
4075 CGF.EmitBlock(CatchHandler);
4076
4077 // In theory we might now need a write hazard, but actually it's
4078 // unnecessary because there's no local-accessing code between
4079 // the try's write hazard and here.
4080 //Hazards.emitWriteHazard();
4081
John McCall9916e3f2010-10-04 23:42:51 +00004082 // Extract the new exception and save it to the
4083 // propagating-exception slot.
4084 assert(PropagatingExnVar);
4085 llvm::CallInst *NewCaught =
John McCall882987f2013-02-28 19:01:20 +00004086 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(),
4087 ExceptionData, "caught");
John McCall9916e3f2010-10-04 23:42:51 +00004088 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
4089
John McCall2dd7d442010-08-04 05:59:32 +00004090 // Don't pop the catch handler; the throw already did.
4091 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004092 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00004093 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004094 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004095
John McCall42227ed2010-07-31 23:20:56 +00004096 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00004097 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00004098
John McCallbd309292010-07-06 01:34:17 +00004099 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00004100 CGF.Builder.restoreIP(TryFallthroughIP);
4101 if (CGF.HaveInsertPoint())
4102 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00004103 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00004104 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004105
John McCallbd309292010-07-06 01:34:17 +00004106 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00004107 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00004108 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00004109 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00004110 // If we have a propagating-exception variable, check it.
4111 llvm::Value *PropagatingExn;
4112 if (PropagatingExnVar) {
4113 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00004114
John McCall9916e3f2010-10-04 23:42:51 +00004115 // Otherwise, just look in the buffer for the exception to throw.
4116 } else {
4117 llvm::CallInst *Caught =
John McCall882987f2013-02-28 19:01:20 +00004118 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionExtractFn(),
4119 ExceptionData);
John McCall9916e3f2010-10-04 23:42:51 +00004120 PropagatingExn = Caught;
4121 }
4122
John McCall882987f2013-02-28 19:01:20 +00004123 CGF.EmitNounwindRuntimeCall(ObjCTypes.getExceptionThrowFn(),
4124 PropagatingExn);
John McCallbd309292010-07-06 01:34:17 +00004125 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00004126 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004127
John McCall42227ed2010-07-31 23:20:56 +00004128 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004129}
4130
4131void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00004132 const ObjCAtThrowStmt &S,
4133 bool ClearInsertionPoint) {
Anders Carlssone005aa12008-09-09 16:16:55 +00004134 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135
Anders Carlssone005aa12008-09-09 16:16:55 +00004136 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00004137 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004139 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00004140 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004141 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00004142 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00004143 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00004144 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004145
John McCall882987f2013-02-28 19:01:20 +00004146 CGF.EmitRuntimeCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
John McCallbd309292010-07-06 01:34:17 +00004147 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004148 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004149
4150 // Clear the insertion point to indicate we are in unreachable code.
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00004151 if (ClearInsertionPoint)
4152 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004153}
4154
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004155/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004156/// object: objc_read_weak (id *src)
4157///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004158llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004159 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004160 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4162 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4163 ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00004164 llvm::Value *read_weak =
4165 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(),
4166 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00004167 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004168 return read_weak;
4169}
4170
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004171/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4172/// objc_assign_weak (id src, id *dst)
4173///
4174void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004175 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004176 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004177 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004178 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004179 assert(Size <= 8 && "does not support size > 8");
4180 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004181 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004182 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4183 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004184 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4185 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00004186 llvm::Value *args[] = { src, dst };
4187 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(),
4188 args, "weakassign");
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004189 return;
4190}
4191
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004192/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4193/// objc_assign_global (id src, id *dst)
4194///
4195void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00004196 llvm::Value *src, llvm::Value *dst,
4197 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004198 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004199 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004200 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004201 assert(Size <= 8 && "does not support size > 8");
4202 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004203 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004204 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4205 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004206 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4207 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00004208 llvm::Value *args[] = { src, dst };
Fariborz Jahanian217af242010-07-20 20:30:03 +00004209 if (!threadlocal)
John McCall882987f2013-02-28 19:01:20 +00004210 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(),
4211 args, "globalassign");
Fariborz Jahanian217af242010-07-20 20:30:03 +00004212 else
John McCall882987f2013-02-28 19:01:20 +00004213 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(),
4214 args, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004215 return;
4216}
4217
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004218/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004219/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004220///
4221void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004222 llvm::Value *src, llvm::Value *dst,
4223 llvm::Value *ivarOffset) {
4224 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00004225 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004226 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004227 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004228 assert(Size <= 8 && "does not support size > 8");
4229 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004230 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004231 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4232 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004233 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4234 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00004235 llvm::Value *args[] = { src, dst, ivarOffset };
4236 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004237 return;
4238}
4239
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004240/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4241/// objc_assign_strongCast (id src, id *dst)
4242///
4243void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004244 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004245 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004246 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004247 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004248 assert(Size <= 8 && "does not support size > 8");
4249 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004250 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004251 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4252 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004253 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4254 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00004255 llvm::Value *args[] = { src, dst };
4256 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(),
4257 args, "weakassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004258 return;
4259}
4260
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004261void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004262 llvm::Value *DestPtr,
4263 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00004264 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004265 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4266 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
John McCall882987f2013-02-28 19:01:20 +00004267 llvm::Value *args[] = { DestPtr, SrcPtr, size };
4268 CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004269}
4270
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004271/// EmitObjCValueForIvar - Code Gen for ivar reference.
4272///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004273LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4274 QualType ObjectTy,
4275 llvm::Value *BaseValue,
4276 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004277 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00004278 const ObjCInterfaceDecl *ID =
4279 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00004280 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4281 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004282}
4283
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004284llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00004285 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004286 const ObjCIvarDecl *Ivar) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004287 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4288 return llvm::ConstantInt::get(
4289 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4290 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004291}
4292
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004293/* *** Private Interface *** */
4294
4295/// EmitImageInfo - Emit the image info marker used to encode some module
4296/// level information.
4297///
4298/// See: <rdr://4810609&4810587&4810587>
4299/// struct IMAGE_INFO {
4300/// unsigned version;
4301/// unsigned flags;
4302/// };
4303enum ImageInfoFlags {
Fariborz Jahanian39c17a82014-01-14 22:01:08 +00004304 eImageInfo_FixAndContinue = (1 << 0), // This flag is no longer set by clang.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004305 eImageInfo_GarbageCollected = (1 << 1),
4306 eImageInfo_GCOnly = (1 << 2),
Fariborz Jahanian39c17a82014-01-14 22:01:08 +00004307 eImageInfo_OptimizedByDyld = (1 << 3), // This flag is set by the dyld shared cache.
Daniel Dunbar75e909f2009-04-20 07:11:47 +00004308
Daniel Dunbar5e639272010-04-25 20:39:01 +00004309 // A flag indicating that the module has no instances of a @synthesize of a
4310 // superclass variable. <rdar://problem/6803242>
Fariborz Jahanian39c17a82014-01-14 22:01:08 +00004311 eImageInfo_CorrectedSynthesize = (1 << 4), // This flag is no longer set by clang.
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004312 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004313};
4314
Daniel Dunbar5e639272010-04-25 20:39:01 +00004315void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004316 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004317 const char *Section = (ObjCABI == 1) ?
4318 "__OBJC, __image_info,regular" :
4319 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004320
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004321 // Generate module-level named metadata to convey this information to the
4322 // linker and code-gen.
4323 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004324
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004325 // Add the ObjC ABI version to the module flags.
4326 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4327 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4328 version);
4329 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4330 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004331
David Blaikiebbafb8a2012-03-11 07:00:24 +00004332 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004333 // Non-GC overrides those files which specify GC.
4334 Mod.addModuleFlag(llvm::Module::Override,
4335 "Objective-C Garbage Collection", (uint32_t)0);
4336 } else {
4337 // Add the ObjC garbage collection value.
4338 Mod.addModuleFlag(llvm::Module::Error,
4339 "Objective-C Garbage Collection",
4340 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004341
David Blaikiebbafb8a2012-03-11 07:00:24 +00004342 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004343 // Add the ObjC GC Only value.
4344 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4345 eImageInfo_GCOnly);
4346
4347 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4348 llvm::Value *Ops[2] = {
4349 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4350 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4351 eImageInfo_GarbageCollected)
4352 };
4353 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4354 llvm::MDNode::get(VMContext, Ops));
4355 }
4356 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004357
4358 // Indicate whether we're compiling this to run on a simulator.
4359 const llvm::Triple &Triple = CGM.getTarget().getTriple();
Cameron Esfahani556d91e2013-09-14 01:09:11 +00004360 if (Triple.isiOS() &&
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004361 (Triple.getArch() == llvm::Triple::x86 ||
4362 Triple.getArch() == llvm::Triple::x86_64))
4363 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4364 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004365}
4366
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004367// struct objc_module {
4368// unsigned long version;
4369// unsigned long size;
4370// const char *name;
4371// Symtab symtab;
4372// };
4373
4374// FIXME: Get from somewhere
4375static const int ModuleVersion = 7;
4376
4377void CGObjCMac::EmitModuleInfo() {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004378 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004380 llvm::Constant *Values[] = {
4381 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4382 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4383 // This used to be the filename, now it is unused. <rdr://4327263>
4384 GetClassName(&CGM.getContext().Idents.get("")),
4385 EmitModuleSymbols()
4386 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004387 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00004388 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004389 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00004390 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004391}
4392
4393llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004394 unsigned NumClasses = DefinedClasses.size();
4395 unsigned NumCategories = DefinedCategories.size();
4396
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004397 // Return null if no symbols were defined.
4398 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00004399 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004400
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004401 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004402 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00004403 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004404 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4405 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004406
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004407 // The runtime expects exactly the list of defined classes followed
4408 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00004409 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004410 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004411 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004412 ObjCTypes.Int8PtrTy);
4413 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004414 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00004415 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004416 ObjCTypes.Int8PtrTy);
4417
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004418 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004419 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004420 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004421 Symbols);
4422
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004423 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004424
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004425 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004426 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4427 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004428 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00004429 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004430}
4431
John McCall882987f2013-02-28 19:01:20 +00004432llvm::Value *CGObjCMac::EmitClassRefFromId(CodeGenFunction &CGF,
4433 IdentifierInfo *II) {
John McCall31168b02011-06-15 23:02:42 +00004434 LazySymbols.insert(II);
4435
4436 llvm::GlobalVariable *&Entry = ClassReferences[II];
4437
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004438 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00004440 llvm::ConstantExpr::getBitCast(GetClassName(II),
4441 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442 Entry =
John McCall31168b02011-06-15 23:02:42 +00004443 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4444 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4445 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004446 }
John McCall31168b02011-06-15 23:02:42 +00004447
John McCall882987f2013-02-28 19:01:20 +00004448 return CGF.Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004449}
4450
John McCall882987f2013-02-28 19:01:20 +00004451llvm::Value *CGObjCMac::EmitClassRef(CodeGenFunction &CGF,
John McCall31168b02011-06-15 23:02:42 +00004452 const ObjCInterfaceDecl *ID) {
John McCall882987f2013-02-28 19:01:20 +00004453 return EmitClassRefFromId(CGF, ID->getIdentifier());
John McCall31168b02011-06-15 23:02:42 +00004454}
4455
John McCall882987f2013-02-28 19:01:20 +00004456llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
John McCall31168b02011-06-15 23:02:42 +00004457 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
John McCall882987f2013-02-28 19:01:20 +00004458 return EmitClassRefFromId(CGF, II);
John McCall31168b02011-06-15 23:02:42 +00004459}
4460
John McCall882987f2013-02-28 19:01:20 +00004461llvm::Value *CGObjCMac::EmitSelector(CodeGenFunction &CGF, Selector Sel,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004462 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004463 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004464
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004465 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004466 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00004467 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004468 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004470 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4471 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004472 4, true);
Michael Gottesman5c205962013-02-05 23:08:45 +00004473 Entry->setExternallyInitialized(true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004474 }
4475
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004476 if (lvalue)
4477 return Entry;
John McCall882987f2013-02-28 19:01:20 +00004478 return CGF.Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004479}
4480
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004481llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00004482 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004483
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004484 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004485 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004486 llvm::ConstantDataArray::getString(VMContext,
4487 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004488 ((ObjCABI == 2) ?
4489 "__TEXT,__objc_classname,cstring_literals" :
4490 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004491 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004492
Owen Anderson170229f2009-07-14 23:10:40 +00004493 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004494}
4495
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004496llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4497 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4498 I = MethodDefinitions.find(MD);
4499 if (I != MethodDefinitions.end())
4500 return I->second;
4501
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004502 return NULL;
4503}
4504
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004505/// GetIvarLayoutName - Returns a unique constant for the given
4506/// ivar layout bitmap.
4507llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004508 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00004509 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004510}
4511
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004512void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman8cbca202012-11-06 22:15:52 +00004513 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004514 bool ForStrongLayout,
4515 bool &HasUnion) {
4516 const RecordDecl *RD = RT->getDecl();
4517 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00004518 SmallVector<const FieldDecl*, 16> Fields;
4519 for (RecordDecl::field_iterator i = RD->field_begin(),
4520 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00004521 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00004522 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523 const llvm::StructLayout *RecLayout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004524 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004526 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4527 ForStrongLayout, HasUnion);
4528}
4529
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00004530void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004531 const llvm::StructLayout *Layout,
4532 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00004533 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cbca202012-11-06 22:15:52 +00004534 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004535 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004536 bool IsUnion = (RD && RD->isUnion());
Eli Friedman8cbca202012-11-06 22:15:52 +00004537 uint64_t MaxUnionIvarSize = 0;
4538 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00004539 const FieldDecl *MaxField = 0;
4540 const FieldDecl *MaxSkippedField = 0;
4541 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedman8cbca202012-11-06 22:15:52 +00004542 uint64_t MaxFieldOffset = 0;
4543 uint64_t MaxSkippedFieldOffset = 0;
4544 uint64_t LastBitfieldOrUnnamedOffset = 0;
4545 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004546
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004547 if (RecFields.empty())
4548 return;
John McCallc8e01702013-04-16 22:48:15 +00004549 unsigned WordSizeInBits = CGM.getTarget().getPointerWidth(0);
4550 unsigned ByteSizeInBits = CGM.getTarget().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004551 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004552 const FieldDecl *FirstField = RecFields[0];
4553 FirstFieldDelta =
4554 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
John McCall31168b02011-06-15 23:02:42 +00004555 }
4556
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004557 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004558 const FieldDecl *Field = RecFields[i];
Eli Friedman8cbca202012-11-06 22:15:52 +00004559 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004560 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00004561 // Note that 'i' here is actually the field index inside RD of Field,
4562 // although this dependency is hidden.
4563 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedman8cbca202012-11-06 22:15:52 +00004564 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004565 } else
John McCall31168b02011-06-15 23:02:42 +00004566 FieldOffset =
4567 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004568
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004569 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004570 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004571 LastFieldBitfieldOrUnnamed = Field;
4572 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004573 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004574 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004575
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004576 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004577 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004578 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004579 if (FQT->isUnionType())
4580 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004581
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004582 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004583 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004584 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004585 continue;
4586 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004587
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004588 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004589 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004590 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004591 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004592 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004593 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004594 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4595 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004596 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004597 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004598 FQT = CArray->getElementType();
4599 }
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00004600 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004601 int OldIndex = IvarsInfo.size() - 1;
4602 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004604 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004605 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004606 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004607
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004608 // Replicate layout information for each array element. Note that
4609 // one element is already done.
4610 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004611 for (int FirstIndex = IvarsInfo.size() - 1,
4612 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004613 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004614 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4615 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4616 IvarsInfo[i].ivar_size));
4617 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4618 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4619 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004620 }
4621 continue;
4622 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004623 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004624 // At this point, we are done with Record/Union and array there of.
4625 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00004626 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004627
Eli Friedman8cbca202012-11-06 22:15:52 +00004628 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00004629 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4630 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004631 if (IsUnion) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004632 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004633 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004634 MaxUnionIvarSize = UnionIvarSize;
4635 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004636 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004637 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004638 } else {
Eli Friedman8cbca202012-11-06 22:15:52 +00004639 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
4640 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004641 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004642 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00004643 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4644 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004645 if (IsUnion) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004646 // FIXME: Why the asymmetry? We divide by word size in bits on other
4647 // side.
4648 uint64_t UnionIvarSize = FieldSize / ByteSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004649 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004650 MaxSkippedUnionIvarSize = UnionIvarSize;
4651 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004652 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004653 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004654 } else {
Eli Friedman8cbca202012-11-06 22:15:52 +00004655 // FIXME: Why the asymmetry, we divide by byte size in bits here?
4656 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
4657 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004658 }
4659 }
4660 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004661
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004662 if (LastFieldBitfieldOrUnnamed) {
4663 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4664 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00004665 uint64_t BitFieldSize
4666 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004667 GC_IVAR skivar;
4668 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
Eli Friedman8cbca202012-11-06 22:15:52 +00004669 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4670 + ((BitFieldSize % ByteSizeInBits) != 0);
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004671 SkipIvars.push_back(skivar);
4672 } else {
4673 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4674 // Last field was unnamed. Must update skip info.
Eli Friedman8cbca202012-11-06 22:15:52 +00004675 unsigned FieldSize
4676 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004677 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
Eli Friedman8cbca202012-11-06 22:15:52 +00004678 FieldSize / ByteSizeInBits));
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004679 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004680 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004681
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004682 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004684 MaxUnionIvarSize));
4685 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00004686 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004687 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004688}
4689
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004690/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4691/// the computations and returning the layout bitmap (for ivar or blocks) in
4692/// the given argument BitMap string container. Routine reads
4693/// two containers, IvarsInfo and SkipIvars which are assumed to be
4694/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004695llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004696 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004697 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004698
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004699 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004700 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedman8cbca202012-11-06 22:15:52 +00004701 unsigned int WordSize =
4702 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
4703 if (IvarsInfo[0].ivar_bytepos == 0) {
4704 WordsToSkip = 0;
4705 WordsToScan = IvarsInfo[0].ivar_size;
4706 } else {
4707 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4708 WordsToScan = IvarsInfo[0].ivar_size;
4709 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004710 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004711 unsigned int TailPrevGCObjC =
4712 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004713 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004714 // consecutive 'scanned' object pointers.
Eli Friedman8cbca202012-11-06 22:15:52 +00004715 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004716 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004717 // Skip over 'gc'able object pointer which lay over each other.
4718 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4719 continue;
4720 // Must skip over 1 or more words. We save current skip/scan values
4721 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004722 SKIP_SCAN SkScan;
4723 SkScan.skip = WordsToSkip;
4724 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004725 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004726
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004727 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004728 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4729 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004730 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004731 WordsToSkip = 0;
Eli Friedman8cbca202012-11-06 22:15:52 +00004732 WordsToScan = IvarsInfo[i].ivar_size;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004733 }
4734 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004735 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004736 SKIP_SCAN SkScan;
4737 SkScan.skip = WordsToSkip;
4738 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004739 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004740 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004741
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004742 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004743 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedman8cbca202012-11-06 22:15:52 +00004744 int LastByteSkipped =
4745 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004746 LastIndex = IvarsInfo.size()-1;
Eli Friedman8cbca202012-11-06 22:15:52 +00004747 int LastByteScanned =
4748 IvarsInfo[LastIndex].ivar_bytepos +
4749 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004750 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004751 if (LastByteSkipped > LastByteScanned) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004752 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004753 SKIP_SCAN SkScan;
Eli Friedman8cbca202012-11-06 22:15:52 +00004754 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004755 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004756 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004757 }
4758 }
4759 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4760 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004761 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004762 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004763 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4764 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4765 // 0xM0 followed by 0x0N detected.
4766 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4767 for (int j = i+1; j < SkipScan; j++)
4768 SkipScanIvars[j] = SkipScanIvars[j+1];
4769 --SkipScan;
4770 }
4771 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004772
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004773 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004774 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004775 unsigned char byte;
4776 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4777 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4778 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4779 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004780
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004781 // first skip big.
4782 for (unsigned int ix = 0; ix < skip_big; ix++)
4783 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004784
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004785 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004786 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004787 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004788 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004789 byte |= 0xf;
4790 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004791 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004792 byte |= scan_small;
4793 scan_small = 0;
4794 }
4795 BitMap += byte;
4796 }
4797 // next scan big
4798 for (unsigned int ix = 0; ix < scan_big; ix++)
4799 BitMap += (unsigned char)(0x0f);
4800 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004801 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004802 byte = scan_small;
4803 BitMap += byte;
4804 }
4805 }
4806 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004807 unsigned char zero = 0;
4808 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004809
4810 llvm::GlobalVariable * Entry =
4811 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004812 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004813 ((ObjCABI == 2) ?
4814 "__TEXT,__objc_classname,cstring_literals" :
4815 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004816 1, true);
4817 return getConstantGEP(VMContext, Entry, 0, 0);
4818}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004819
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004820/// BuildIvarLayout - Builds ivar layout bitmap for the class
4821/// implementation for the __strong or __weak case.
4822/// The layout map displays which words in ivar list must be skipped
4823/// and which must be scanned by GC (see below). String is built of bytes.
4824/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4825/// of words to skip and right nibble is count of words to scan. So, each
4826/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4827/// represented by a 0x00 byte which also ends the string.
4828/// 1. when ForStrongLayout is true, following ivars are scanned:
4829/// - id, Class
4830/// - object *
4831/// - __strong anything
4832///
4833/// 2. When ForStrongLayout is false, following ivars are scanned:
4834/// - __weak anything
4835///
4836llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4837 const ObjCImplementationDecl *OMD,
4838 bool ForStrongLayout) {
4839 bool hasUnion = false;
4840
Chris Lattnerece04092012-02-07 00:39:47 +00004841 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004842 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4843 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004844 return llvm::Constant::getNullValue(PtrTy);
4845
Jordy Rosea91768e2011-07-22 02:08:32 +00004846 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4847 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004848 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004849 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004850 IVD; IVD = IVD->getNextIvar())
4851 RecFields.push_back(cast<FieldDecl>(IVD));
4852 }
4853 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004854 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004855 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004856
Jordy Rosea91768e2011-07-22 02:08:32 +00004857 // FIXME: This is not ideal; we shouldn't have to do this copy.
4858 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004859 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004860
4861 if (RecFields.empty())
4862 return llvm::Constant::getNullValue(PtrTy);
4863
4864 SkipIvars.clear();
4865 IvarsInfo.clear();
4866
Eli Friedman8cbca202012-11-06 22:15:52 +00004867 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004868 if (IvarsInfo.empty())
4869 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004870 // Sort on byte position in case we encounterred a union nested in
4871 // the ivar list.
4872 if (hasUnion && !IvarsInfo.empty())
4873 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4874 if (hasUnion && !SkipIvars.empty())
4875 std::sort(SkipIvars.begin(), SkipIvars.end());
4876
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004877 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004878 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004879
David Blaikiebbafb8a2012-03-11 07:00:24 +00004880 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004881 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004882 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004883 OMD->getClassInterface()->getName().data());
Roman Divackye6377112012-09-06 15:59:27 +00004884 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004885 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004886 if (!(s[i] & 0xf0))
4887 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4888 else
4889 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4890 printf("\n");
4891 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004892 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004893}
4894
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004895llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004896 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4897
Chris Lattner3def9ae2012-02-06 22:16:34 +00004898 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004899 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004900 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004901 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004902 ((ObjCABI == 2) ?
4903 "__TEXT,__objc_methname,cstring_literals" :
4904 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004905 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004906
Owen Anderson170229f2009-07-14 23:10:40 +00004907 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004908}
4909
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004910// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004911llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004912 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4913}
4914
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004915llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004916 std::string TypeStr;
4917 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4918
4919 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004920
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004921 if (!Entry)
4922 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004923 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004924 ((ObjCABI == 2) ?
4925 "__TEXT,__objc_methtype,cstring_literals" :
4926 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004927 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928
Owen Anderson170229f2009-07-14 23:10:40 +00004929 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004930}
4931
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004932llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4933 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004934 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004935 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004936 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004937
4938 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4939
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004940 if (!Entry)
4941 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004942 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004943 ((ObjCABI == 2) ?
4944 "__TEXT,__objc_methtype,cstring_literals" :
4945 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004946 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004947
Owen Anderson170229f2009-07-14 23:10:40 +00004948 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004949}
4950
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004951// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004952llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004953 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004954
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004955 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004956 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004957 llvm::ConstantDataArray::getString(VMContext,
4958 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004959 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004960 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004961
Owen Anderson170229f2009-07-14 23:10:40 +00004962 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004963}
4964
4965// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004966// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004967llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004968CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4969 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004970 std::string TypeStr;
4971 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004972 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4973}
4974
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004975void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004976 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004977 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004978 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004979 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004980 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4981 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004982 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004983 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004984 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004985 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004986}
4987
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004988void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004989 EmitModuleInfo();
4990
Daniel Dunbarc475d422008-10-29 22:36:39 +00004991 // Emit the dummy bodies for any protocols which were referenced but
4992 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004993 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004994 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4995 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004996 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004997
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004998 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004999 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005000 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00005001 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00005002 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00005003 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Rafael Espindola21039aa2014-02-27 16:26:32 +00005004 assertPrivateName(I->second);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005005 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00005006 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005007 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00005008 }
5009
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005010 // Add assembler directives to add lazy undefined symbol references
5011 // for classes which are referenced but not defined. This is
5012 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00005013 //
5014 // FIXME: It would be nice if we had an LLVM construct for this.
5015 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005016 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00005017 Asm += CGM.getModule().getModuleInlineAsm();
5018 if (!Asm.empty() && Asm.back() != '\n')
5019 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005020
Daniel Dunbard027a922009-09-07 00:20:42 +00005021 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00005022 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
5023 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00005024 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
5025 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00005026 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005027 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00005028 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005029 }
5030
Bill Wendling53136852012-02-07 09:06:01 +00005031 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005032 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
5033 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
5034 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00005035
Daniel Dunbard027a922009-09-07 00:20:42 +00005036 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005037 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00005038}
5039
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005040CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005041 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00005042 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005043 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005044 ObjCABI = 2;
5045}
5046
Daniel Dunbar3ad53482008-08-11 21:35:06 +00005047/* *** */
5048
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005049ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00005050 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
5051{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005052 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5053 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005054
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005055 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005056 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005057 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005058 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00005059 Int8PtrTy = CGM.Int8PtrTy;
5060 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005061
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005062 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005063 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005064 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005065
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005066 // I'm not sure I like this. The implicit coordination is a bit
5067 // gross. We should solve this in a reasonable fashion because this
5068 // is a pretty common task (match some runtime data structure with
5069 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005070
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005071 // FIXME: This is leaked.
5072 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005074 // struct _objc_super {
5075 // id self;
5076 // Class cls;
5077 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00005078 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00005079 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00005080 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005081 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005082 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005083 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005084 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005085 Ctx.getObjCClassType(), 0, 0, false,
5086 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00005087 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005089 SuperCTy = Ctx.getTagDeclType(RD);
5090 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005092 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
5094
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005095 // struct _prop_t {
5096 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005097 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005098 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005099 PropertyTy = llvm::StructType::create("struct._prop_t",
5100 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005101
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005102 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005103 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005104 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005105 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005106 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005107 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005108 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
5109 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005110 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005111 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005112
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005113 // struct _objc_method {
5114 // SEL _cmd;
5115 // char *method_type;
5116 // char *_imp;
5117 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005118 MethodTy = llvm::StructType::create("struct._objc_method",
5119 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5120 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005121
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005122 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00005123 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005124 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00005125
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005126}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005127
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005128ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005129 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005130 // struct _objc_method_description {
5131 // SEL name;
5132 // char *types;
5133 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005135 llvm::StructType::create("struct._objc_method_description",
5136 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005137
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005138 // struct _objc_method_description_list {
5139 // int count;
5140 // struct _objc_method_description[1];
5141 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005143 llvm::StructType::create("struct._objc_method_description_list",
5144 IntTy,
5145 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005146
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005147 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005148 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005149 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005150
Daniel Dunbarb036db82008-08-13 03:21:16 +00005151 // Protocol description structures
5152
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005153 // struct _objc_protocol_extension {
5154 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5155 // struct _objc_method_description_list *optional_instance_methods;
5156 // struct _objc_method_description_list *optional_class_methods;
5157 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005158 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005159 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005160 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005161 llvm::StructType::create("struct._objc_protocol_extension",
5162 IntTy, MethodDescriptionListPtrTy,
5163 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005164 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005165
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005166 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005167 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005168
Daniel Dunbarc475d422008-10-29 22:36:39 +00005169 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00005170
Chris Lattnera5f58b02011-07-09 17:41:47 +00005171 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005172 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00005173
Chris Lattnera5f58b02011-07-09 17:41:47 +00005174 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005175 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005176 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005177 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00005178 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005179 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005180
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005181 // struct _objc_protocol {
5182 // struct _objc_protocol_extension *isa;
5183 // char *protocol_name;
5184 // struct _objc_protocol **_objc_protocol_list;
5185 // struct _objc_method_description_list *instance_methods;
5186 // struct _objc_method_description_list *class_methods;
5187 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005188 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5189 llvm::PointerType::getUnqual(ProtocolListTy),
5190 MethodDescriptionListPtrTy,
5191 MethodDescriptionListPtrTy,
5192 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005193
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005194 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005195 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005196
Owen Anderson9793f0e2009-07-29 22:16:19 +00005197 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005198
5199 // Class description structures
5200
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005201 // struct _objc_ivar {
5202 // char *ivar_name;
5203 // char *ivar_type;
5204 // int ivar_offset;
5205 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005206 IvarTy = llvm::StructType::create("struct._objc_ivar",
5207 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005208
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005209 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005210 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005211 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005212 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005213
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005214 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005215 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005216 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005217 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005218
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005219 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005220 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005221 llvm::StructType::create("struct._objc_class_extension",
5222 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005223 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005224
Chris Lattner5ec04a52011-08-12 17:43:31 +00005225 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005226
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005227 // struct _objc_class {
5228 // Class isa;
5229 // Class super_class;
5230 // char *name;
5231 // long version;
5232 // long info;
5233 // long instance_size;
5234 // struct _objc_ivar_list *ivars;
5235 // struct _objc_method_list *methods;
5236 // struct _objc_cache *cache;
5237 // struct _objc_protocol_list *protocols;
5238 // char *ivar_layout;
5239 // struct _objc_class_ext *ext;
5240 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005241 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5242 llvm::PointerType::getUnqual(ClassTy),
5243 Int8PtrTy,
5244 LongTy,
5245 LongTy,
5246 LongTy,
5247 IvarListPtrTy,
5248 MethodListPtrTy,
5249 CachePtrTy,
5250 ProtocolListPtrTy,
5251 Int8PtrTy,
5252 ClassExtensionPtrTy,
5253 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254
Owen Anderson9793f0e2009-07-29 22:16:19 +00005255 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005256
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005257 // struct _objc_category {
5258 // char *category_name;
5259 // char *class_name;
5260 // struct _objc_method_list *instance_method;
5261 // struct _objc_method_list *class_method;
5262 // uint32_t size; // sizeof(struct _objc_category)
5263 // struct _objc_property_list *instance_properties;// category's @property
5264 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005265 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005266 llvm::StructType::create("struct._objc_category",
5267 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5268 MethodListPtrTy, ProtocolListPtrTy,
5269 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00005270
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005271 // Global metadata structures
5272
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005273 // struct _objc_symtab {
5274 // long sel_ref_cnt;
5275 // SEL *refs;
5276 // short cls_def_cnt;
5277 // short cat_def_cnt;
5278 // char *defs[cls_def_cnt + cat_def_cnt];
5279 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005280 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005281 llvm::StructType::create("struct._objc_symtab",
5282 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5283 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005284 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005285
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00005286 // struct _objc_module {
5287 // long version;
5288 // long size; // sizeof(struct _objc_module)
5289 // char *name;
5290 // struct _objc_symtab* symtab;
5291 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005292 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005293 llvm::StructType::create("struct._objc_module",
5294 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00005295
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005296
Mike Stump18bb9282009-05-16 07:57:57 +00005297 // FIXME: This is the size of the setjmp buffer and should be target
5298 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00005299 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005300
Anders Carlsson9ff22482008-09-09 10:10:21 +00005301 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00005302 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303
5304 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005305 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00005306 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5307 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00005308
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005309}
5310
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005311ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005312 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005313 // struct _method_list_t {
5314 // uint32_t entsize; // sizeof(struct _objc_method)
5315 // uint32_t method_count;
5316 // struct _objc_method method_list[method_count];
5317 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005318 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005319 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5320 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005321 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005322 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005324 // struct _protocol_t {
5325 // id isa; // NULL
5326 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005327 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005328 // const struct method_list_t * const instance_methods;
5329 // const struct method_list_t * const class_methods;
5330 // const struct method_list_t *optionalInstanceMethods;
5331 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005332 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005333 // const uint32_t size; // sizeof(struct _protocol_t)
5334 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005335 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005336 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005337
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005338 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005339 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005340 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005341
Chris Lattnera5f58b02011-07-09 17:41:47 +00005342 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005343 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5344 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5345 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5346 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005347 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5348 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005349
5350 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005351 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005352
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005353 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005354 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005355 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005356 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005357 ProtocolListnfABITy->setBody(LongTy,
5358 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5359 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005360
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005361 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005362 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005363
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005364 // struct _ivar_t {
5365 // unsigned long int *offset; // pointer to ivar offset location
5366 // char *name;
5367 // char *type;
5368 // uint32_t alignment;
5369 // uint32_t size;
5370 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005371 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005372 llvm::StructType::create("struct._ivar_t",
5373 llvm::PointerType::getUnqual(LongTy),
5374 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005375
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005376 // struct _ivar_list_t {
5377 // uint32 entsize; // sizeof(struct _ivar_t)
5378 // uint32 count;
5379 // struct _iver_t list[count];
5380 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005381 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005382 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5383 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005384
Owen Anderson9793f0e2009-07-29 22:16:19 +00005385 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005386
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005387 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005388 // uint32_t const flags;
5389 // uint32_t const instanceStart;
5390 // uint32_t const instanceSize;
5391 // uint32_t const reserved; // only when building for 64bit targets
5392 // const uint8_t * const ivarLayout;
5393 // const char *const name;
5394 // const struct _method_list_t * const baseMethods;
5395 // const struct _objc_protocol_list *const baseProtocols;
5396 // const struct _ivar_list_t *const ivars;
5397 // const uint8_t * const weakIvarLayout;
5398 // const struct _prop_list_t * const properties;
5399 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005400
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005401 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00005402 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5403 IntTy, IntTy, IntTy, Int8PtrTy,
5404 Int8PtrTy, MethodListnfABIPtrTy,
5405 ProtocolListnfABIPtrTy,
5406 IvarListnfABIPtrTy,
5407 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005408
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005409 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00005410 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00005411 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5412 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005414 // struct _class_t {
5415 // struct _class_t *isa;
5416 // struct _class_t * const superclass;
5417 // void *cache;
5418 // IMP *vtable;
5419 // struct class_ro_t *ro;
5420 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005421
Chris Lattner5ec04a52011-08-12 17:43:31 +00005422 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005423 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5424 llvm::PointerType::getUnqual(ClassnfABITy),
5425 CachePtrTy,
5426 llvm::PointerType::getUnqual(ImpnfABITy),
5427 llvm::PointerType::getUnqual(ClassRonfABITy),
5428 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005429
Fariborz Jahanian71394042009-01-23 23:53:38 +00005430 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005431 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005432
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005433 // struct _category_t {
5434 // const char * const name;
5435 // struct _class_t *const cls;
5436 // const struct _method_list_t * const instance_methods;
5437 // const struct _method_list_t * const class_methods;
5438 // const struct _protocol_list_t * const protocols;
5439 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00005440 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005441 CategorynfABITy = llvm::StructType::create("struct._category_t",
5442 Int8PtrTy, ClassnfABIPtrTy,
5443 MethodListnfABIPtrTy,
5444 MethodListnfABIPtrTy,
5445 ProtocolListnfABIPtrTy,
5446 PropertyListPtrTy,
5447 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005448
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005449 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005450 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5451 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005453 // MessageRefTy - LLVM for:
5454 // struct _message_ref_t {
5455 // IMP messenger;
5456 // SEL name;
5457 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005458
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005459 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00005460 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00005461 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00005462 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005463 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005464 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005465 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005466 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005467 Ctx.getObjCSelType(), 0, 0, false,
5468 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00005469 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005470
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005471 MessageRefCTy = Ctx.getTagDeclType(RD);
5472 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5473 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005474
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005475 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005476 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005477
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005478 // SuperMessageRefTy - LLVM for:
5479 // struct _super_message_ref_t {
5480 // SUPER_IMP messenger;
5481 // SEL name;
5482 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005483 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005484 llvm::StructType::create("struct._super_message_ref_t",
5485 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005486
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005487 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005488 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00005489
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005490
5491 // struct objc_typeinfo {
5492 // const void** vtable; // objc_ehtype_vtable + 2
5493 // const char* name; // c++ typeinfo string
5494 // Class cls;
5495 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005496 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005497 llvm::StructType::create("struct._objc_typeinfo",
5498 llvm::PointerType::getUnqual(Int8PtrTy),
5499 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005500 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005501}
5502
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005503llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005504 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005505
Fariborz Jahanian71394042009-01-23 23:53:38 +00005506 return NULL;
5507}
5508
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005509void CGObjCNonFragileABIMac::
5510AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5511 const char *SymbolName,
5512 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00005513 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005514
Daniel Dunbar19573e72009-05-15 21:48:48 +00005515 if (!NumClasses)
5516 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005517
Chris Lattner3def9ae2012-02-06 22:16:34 +00005518 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005519 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00005520 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00005521 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00005522 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005523 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00005524 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00005525 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005526
Daniel Dunbar19573e72009-05-15 21:48:48 +00005527 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005528 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00005529 llvm::GlobalValue::PrivateLinkage,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005530 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005531 SymbolName);
Rafael Espindola21039aa2014-02-27 16:26:32 +00005532 assertPrivateName(GV);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005533 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00005534 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005535 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005536}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005537
Fariborz Jahanian71394042009-01-23 23:53:38 +00005538void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5539 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005540
Daniel Dunbar19573e72009-05-15 21:48:48 +00005541 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005542 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005543 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005544 "\01L_OBJC_LABEL_CLASS_$",
5545 "__DATA, __objc_classlist, regular, no_dead_strip");
Rafael Espindola554256c2014-02-26 22:25:45 +00005546
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005547 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005548 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5549 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005550
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005551 // Build list of all implemented category addresses in array
5552 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005553 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005554 "\01L_OBJC_LABEL_CATEGORY_$",
5555 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005556 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005557 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5558 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005559
Daniel Dunbar5e639272010-04-25 20:39:01 +00005560 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005561}
5562
John McCall9e8bb002011-05-14 03:10:52 +00005563/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5564/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005565/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005566/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00005567bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5568 // At various points we've experimented with using vtable-based
5569 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005570 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005571 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00005572 return false;
John McCall9e8bb002011-05-14 03:10:52 +00005573 case CodeGenOptions::NonLegacy:
5574 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005575 case CodeGenOptions::Mixed:
5576 break;
5577 }
5578
5579 // If so, see whether this selector is in the white-list of things which must
5580 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00005581 if (VTableDispatchMethods.empty()) {
5582 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5583 VTableDispatchMethods.insert(GetNullarySelector("class"));
5584 VTableDispatchMethods.insert(GetNullarySelector("self"));
5585 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5586 VTableDispatchMethods.insert(GetNullarySelector("length"));
5587 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005588
John McCall9e8bb002011-05-14 03:10:52 +00005589 // These are vtable-based if GC is disabled.
5590 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005591 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00005592 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5593 VTableDispatchMethods.insert(GetNullarySelector("release"));
5594 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5595 }
5596
5597 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5598 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5599 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5600 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5601 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5602 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5603 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5604
5605 // These are vtable-based if GC is enabled.
5606 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005607 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00005608 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5609 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5610
5611 // "countByEnumeratingWithState:objects:count"
5612 IdentifierInfo *KeyIdents[] = {
5613 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5614 &CGM.getContext().Idents.get("objects"),
5615 &CGM.getContext().Idents.get("count")
5616 };
5617 VTableDispatchMethods.insert(
5618 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5619 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005620 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005621
John McCall9e8bb002011-05-14 03:10:52 +00005622 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005623}
5624
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005625/// BuildClassRoTInitializer - generate meta-data for:
5626/// struct _class_ro_t {
5627/// uint32_t const flags;
5628/// uint32_t const instanceStart;
5629/// uint32_t const instanceSize;
5630/// uint32_t const reserved; // only when building for 64bit targets
5631/// const uint8_t * const ivarLayout;
5632/// const char *const name;
5633/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005634/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005635/// const struct _ivar_list_t *const ivars;
5636/// const uint8_t * const weakIvarLayout;
5637/// const struct _prop_list_t * const properties;
5638/// }
5639///
5640llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005641 unsigned flags,
5642 unsigned InstanceStart,
5643 unsigned InstanceSize,
5644 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005645 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005646 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00005647
David Blaikiebbafb8a2012-03-11 07:00:24 +00005648 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallef19dbb2012-10-17 04:53:23 +00005649 flags |= NonFragileABI_Class_CompiledByARC;
John McCall31168b02011-06-15 23:02:42 +00005650
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005651 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5652 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5653 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005654 // FIXME. For 64bit targets add 0 here.
John McCallef19dbb2012-10-17 04:53:23 +00005655 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5656 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005657 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005658 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005659 // const struct _method_list_t * const baseMethods;
5660 std::vector<llvm::Constant*> Methods;
5661 std::string MethodListName("\01l_OBJC_$_");
John McCallef19dbb2012-10-17 04:53:23 +00005662 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005663 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005664 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005665 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005666 // Class methods should always be defined.
5667 Methods.push_back(GetMethodConstant(*i));
5668 }
5669 } else {
5670 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005671 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005672 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005673 // Instance methods should always be defined.
5674 Methods.push_back(GetMethodConstant(*i));
5675 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005676 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005677 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005678 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005679
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005680 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5681 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005682
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005683 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5684 if (llvm::Constant *C = GetMethodConstant(MD))
5685 Methods.push_back(C);
5686 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5687 if (llvm::Constant *C = GetMethodConstant(MD))
5688 Methods.push_back(C);
5689 }
5690 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005691 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005692 Values[ 5] = EmitMethodList(MethodListName,
5693 "__DATA, __objc_const", Methods);
5694
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005695 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5696 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005697 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005698 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005699 OID->all_referenced_protocol_begin(),
5700 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701
John McCallef19dbb2012-10-17 04:53:23 +00005702 if (flags & NonFragileABI_Class_Meta) {
Owen Anderson0b75f232009-07-31 20:28:54 +00005703 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005704 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Anderson0b75f232009-07-31 20:28:54 +00005705 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005706 } else {
5707 Values[ 7] = EmitIvarList(ID);
5708 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005709 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5710 ID, ID->getClassInterface(), ObjCTypes);
John McCallef19dbb2012-10-17 04:53:23 +00005711 }
Owen Anderson0e0189d2009-07-27 22:29:56 +00005712 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005713 Values);
5714 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00005716 llvm::GlobalValue::PrivateLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005717 Init,
John McCallef19dbb2012-10-17 04:53:23 +00005718 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005719 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5720 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Rafael Espindola21039aa2014-02-27 16:26:32 +00005721 assertPrivateName(CLASS_RO_GV);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005722 CLASS_RO_GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005723 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005724 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005725 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005726
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005727}
5728
5729/// BuildClassMetaData - This routine defines that to-level meta-data
5730/// for the given ClassName for:
5731/// struct _class_t {
5732/// struct _class_t *isa;
5733/// struct _class_t * const superclass;
5734/// void *cache;
5735/// IMP *vtable;
5736/// struct class_ro_t *ro;
5737/// }
5738///
Rafael Espindola554256c2014-02-26 22:25:45 +00005739llvm::GlobalVariable *CGObjCNonFragileABIMac::BuildClassMetaData(
5740 std::string &ClassName, llvm::Constant *IsAGV, llvm::Constant *SuperClassGV,
5741 llvm::Constant *ClassRoGV, bool HiddenVisibility, bool Weak) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005742 llvm::Constant *Values[] = {
5743 IsAGV,
5744 SuperClassGV,
5745 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5746 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5747 ClassRoGV // &CLASS_RO_GV
5748 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005749 if (!Values[1])
5750 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian42d49552013-10-24 17:40:28 +00005751 if (!Values[3])
5752 Values[3] = llvm::Constant::getNullValue(
5753 llvm::PointerType::getUnqual(ObjCTypes.ImpnfABITy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005754 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005755 Values);
Rafael Espindola554256c2014-02-26 22:25:45 +00005756 llvm::GlobalVariable *GV = GetClassGlobal(ClassName, Weak);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005757 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005758 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005759 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005760 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005761 if (HiddenVisibility)
5762 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005763 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005764}
5765
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005766bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005767CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005768 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005769}
5770
Daniel Dunbar961202372009-05-03 12:57:56 +00005771void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005772 uint32_t &InstanceStart,
5773 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005774 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005775 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005776
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005777 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005778 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005779
5780 // If there are no fields, the start is the same as the end.
5781 if (!RL.getFieldCount())
5782 InstanceStart = InstanceSize;
5783 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005784 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005785}
5786
Fariborz Jahanian71394042009-01-23 23:53:38 +00005787void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5788 std::string ClassName = ID->getNameAsString();
5789 if (!ObjCEmptyCacheVar) {
5790 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005791 CGM.getModule(),
5792 ObjCTypes.CacheTy,
5793 false,
5794 llvm::GlobalValue::ExternalLinkage,
5795 0,
5796 "_objc_empty_cache");
Fariborz Jahanian42d49552013-10-24 17:40:28 +00005797
5798 // Make this entry NULL for any iOS device target, any iOS simulator target,
5799 // OS X with deployment target 10.9 or later.
5800 const llvm::Triple &Triple = CGM.getTarget().getTriple();
5801 if (Triple.isiOS() || (Triple.isMacOSX() && !Triple.isMacOSXVersionLT(10, 9)))
5802 // This entry will be null.
5803 ObjCEmptyVtableVar = 0;
5804 else
5805 ObjCEmptyVtableVar = new llvm::GlobalVariable(
5806 CGM.getModule(),
5807 ObjCTypes.ImpnfABITy,
5808 false,
5809 llvm::GlobalValue::ExternalLinkage,
5810 0,
5811 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005812 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005813 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005814 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005815 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005816 uint32_t InstanceStart =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005817 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005818 uint32_t InstanceSize = InstanceStart;
John McCallef19dbb2012-10-17 04:53:23 +00005819 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005820 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5821 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005822
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005823 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005824
John McCall0d54a172012-10-17 04:53:31 +00005825 // Build the flags for the metaclass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005826 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005827 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005828 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005829 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005830
5831 // FIXME: why is this flag set on the metaclass?
5832 // ObjC metaclasses have no fields and don't really get constructed.
5833 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005834 flags |= NonFragileABI_Class_HasCXXStructors;
John McCall0d54a172012-10-17 04:53:31 +00005835 if (!ID->hasNonZeroConstructors())
5836 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5837 }
5838
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005839 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005840 // class is root
John McCallef19dbb2012-10-17 04:53:23 +00005841 flags |= NonFragileABI_Class_Root;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005842 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Rafael Espindola554256c2014-02-26 22:25:45 +00005843 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName,
5844 ID->getClassInterface()->isWeakImported());
5845
5846 // We are implementing a weak imported interface. Give it external
5847 // linkage.
5848 if (!ID->isWeakImported() && ID->getClassInterface()->isWeakImported())
5849 IsAGV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005850 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005851 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005852 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5853 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5854 Root = Super;
Rafael Espindola554256c2014-02-26 22:25:45 +00005855 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString(),
5856 Root->isWeakImported());
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005857 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005858 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005859 ObjCMetaClassName +
5860 ID->getClassInterface()->getSuperClass()->getNameAsString();
Rafael Espindola554256c2014-02-26 22:25:45 +00005861 SuperClassGV = GetClassGlobal(
5862 SuperClassName,
5863 ID->getClassInterface()->getSuperClass()->isWeakImported());
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005864 }
5865 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5866 InstanceStart,
5867 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005868 std::string TClassName = ObjCMetaClassName + ClassName;
Rafael Espindola554256c2014-02-26 22:25:45 +00005869 llvm::GlobalVariable *MetaTClass = BuildClassMetaData(
5870 TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, classIsHidden,
5871 ID->isWeakImported());
Fariborz Jahanian67260552009-11-17 21:37:35 +00005872 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005873
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005874 // Metadata for the class
John McCallef19dbb2012-10-17 04:53:23 +00005875 flags = 0;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005876 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005877 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005878
5879 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005880 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005881
John McCall0d54a172012-10-17 04:53:31 +00005882 // Set a flag to enable a runtime optimization when a class has
5883 // fields that require destruction but which don't require
5884 // anything except zero-initialization during construction. This
5885 // is most notably true of __strong and __weak types, but you can
5886 // also imagine there being C++ types with non-trivial default
5887 // constructors that merely set all fields to null.
5888 if (!ID->hasNonZeroConstructors())
5889 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5890 }
5891
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005892 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCallef19dbb2012-10-17 04:53:23 +00005893 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005894
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005895 if (!ID->getClassInterface()->getSuperClass()) {
John McCallef19dbb2012-10-17 04:53:23 +00005896 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005897 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005898 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005899 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005900 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005901 ID->getClassInterface()->getSuperClass()->getNameAsString();
Rafael Espindola554256c2014-02-26 22:25:45 +00005902 SuperClassGV = GetClassGlobal(
5903 ObjCClassName + RootClassName,
5904 ID->getClassInterface()->getSuperClass()->isWeakImported());
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005905 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005906 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005907 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005908 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005909 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005910 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005911
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005912 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005913 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005914 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5915 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005916 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005917
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005918 // Determine if this class is also "non-lazy".
5919 if (ImplementationIsNonLazy(ID))
5920 DefinedNonLazyClasses.push_back(ClassMD);
5921
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005922 // Force the definition of the EHType if necessary.
John McCallef19dbb2012-10-17 04:53:23 +00005923 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005924 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005925 // Make sure method definition entries are all clear for next implementation.
5926 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005927}
5928
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005929/// GenerateProtocolRef - This routine is called to generate code for
5930/// a protocol reference expression; as in:
5931/// @code
5932/// @protocol(Proto1);
5933/// @endcode
5934/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5935/// which will hold address of the protocol meta-data.
5936///
John McCall882987f2013-02-28 19:01:20 +00005937llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005938 const ObjCProtocolDecl *PD) {
5939
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005940 // This routine is called for @protocol only. So, we must build definition
5941 // of protocol's meta-data (not a reference to it!)
5942 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943 llvm::Constant *Init =
5944 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005945 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005946
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005947 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005948 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005949
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005950 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5951 if (PTGV)
John McCall882987f2013-02-28 19:01:20 +00005952 return CGF.Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005953 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005954 CGM.getModule(),
5955 Init->getType(), false,
5956 llvm::GlobalValue::WeakAnyLinkage,
5957 Init,
5958 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005959 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5960 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005961 CGM.AddUsedGlobal(PTGV);
John McCall882987f2013-02-28 19:01:20 +00005962 return CGF.Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005963}
5964
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005965/// GenerateCategory - Build metadata for a category implementation.
5966/// struct _category_t {
5967/// const char * const name;
5968/// struct _class_t *const cls;
5969/// const struct _method_list_t * const instance_methods;
5970/// const struct _method_list_t * const class_methods;
5971/// const struct _protocol_list_t * const protocols;
5972/// const struct _prop_list_t * const properties;
5973/// }
5974///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005975void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005976 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005977 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005978 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5979 "_$_" + OCD->getNameAsString());
5980 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005981 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005982
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005983 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005984 Values[0] = GetClassName(OCD->getIdentifier());
5985 // meta-class entry symbol
Rafael Espindola554256c2014-02-26 22:25:45 +00005986 llvm::GlobalVariable *ClassGV =
5987 GetClassGlobal(ExtClassName, Interface->isWeakImported());
5988
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005989 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005990 std::vector<llvm::Constant*> Methods;
5991 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005992 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005993 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005994
5995 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005996 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005997 // Instance methods should always be defined.
5998 Methods.push_back(GetMethodConstant(*i));
5999 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006000
6001 Values[2] = EmitMethodList(MethodListName,
6002 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00006003 Methods);
6004
6005 MethodListName = Prefix;
6006 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
6007 OCD->getNameAsString();
6008 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006009 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006010 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00006011 // Class methods should always be defined.
6012 Methods.push_back(GetMethodConstant(*i));
6013 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006014
6015 Values[3] = EmitMethodList(MethodListName,
6016 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00006017 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006018 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00006019 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006020 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00006021 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006022 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
6023 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006024 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006025 + Interface->getName() + "_$_"
6026 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006027 Category->protocol_begin(),
6028 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006029 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
6030 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00006031 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00006032 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
6033 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006034 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006035
6036 llvm::Constant *Init =
6037 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006038 Values);
6039 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006040 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006041 false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006042 llvm::GlobalValue::PrivateLinkage,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006043 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006044 ExtCatName);
Rafael Espindola21039aa2014-02-27 16:26:32 +00006045 assertPrivateName(GCATV);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006046 GCATV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006047 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006048 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006049 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006050 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00006051
6052 // Determine if this category is also "non-lazy".
6053 if (ImplementationIsNonLazy(OCD))
6054 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00006055 // method definition entries must be clear for next implementation.
6056 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006057}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006058
6059/// GetMethodConstant - Return a struct objc_method constant for the
6060/// given method if it has been defined. The result is null if the
6061/// method has not been defined. The return value has type MethodPtrTy.
6062llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006063 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00006064 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006065 if (!Fn)
6066 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006067
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006068 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00006069 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006070 ObjCTypes.SelectorPtrTy),
6071 GetMethodVarType(MD),
6072 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
6073 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00006074 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006075}
6076
6077/// EmitMethodList - Build meta-data for method declarations
6078/// struct _method_list_t {
6079/// uint32_t entsize; // sizeof(struct _objc_method)
6080/// uint32_t method_count;
6081/// struct _objc_method method_list[method_count];
6082/// }
6083///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00006084llvm::Constant *
6085CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
6086 const char *Section,
6087 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006088 // Return null for empty list.
6089 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00006090 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006091
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006092 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006093 // sizeof(struct _objc_method)
Micah Villmowdd31ca12012-10-08 16:25:52 +00006094 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006095 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006096 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006097 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00006098 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006099 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00006100 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006101 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006102
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006103 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006104 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006105 llvm::GlobalValue::PrivateLinkage, Init, Name);
Rafael Espindola21039aa2014-02-27 16:26:32 +00006106 assertPrivateName(GV);
Micah Villmowdd31ca12012-10-08 16:25:52 +00006107 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006108 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006109 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006110 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006111}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006112
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006113/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6114/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00006115llvm::GlobalVariable *
6116CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6117 const ObjCIvarDecl *Ivar) {
6118 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00006119 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00006120 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006121 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006122 CGM.getModule().getGlobalVariable(Name);
6123 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006124 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006125 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006126 false,
6127 llvm::GlobalValue::ExternalLinkage,
6128 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006129 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006130 return IvarOffsetGV;
6131}
6132
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00006133llvm::Constant *
6134CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6135 const ObjCIvarDecl *Ivar,
Eli Friedman8cbca202012-11-06 22:15:52 +00006136 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006137 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006138 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedman8cbca202012-11-06 22:15:52 +00006139 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006140 IvarOffsetGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006141 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006142
Mike Stump18bb9282009-05-16 07:57:57 +00006143 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6144 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006145 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6146 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00006147 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00006148 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006149 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00006150 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00006151 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006152 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006153}
6154
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006155/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006156/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006157/// IvarListnfABIPtrTy.
6158/// struct _ivar_t {
6159/// unsigned long int *offset; // pointer to ivar offset location
6160/// char *name;
6161/// char *type;
6162/// uint32_t alignment;
6163/// uint32_t size;
6164/// }
6165/// struct _ivar_list_t {
6166/// uint32 entsize; // sizeof(struct _ivar_t)
6167/// uint32 count;
6168/// struct _iver_t list[count];
6169/// }
6170///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00006171
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006172llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006173 const ObjCImplementationDecl *ID) {
6174
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006175 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006176
Jordy Rosea91768e2011-07-22 02:08:32 +00006177 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006178 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006179
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006180 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006181
Jordy Rosea91768e2011-07-22 02:08:32 +00006182 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00006183 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00006184 // Ignore unnamed bit-fields.
6185 if (!IVD->getDeclName())
6186 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006187 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006188 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00006189 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006190 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6191 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00006192 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006193 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00006194 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006195 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006196 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006197 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006198 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00006199 // NOTE. Size of a bitfield does not match gcc's, because of the
6200 // way bitfields are treated special in each. But I am told that
6201 // 'size' for bitfield ivars is ignored by the runtime so it does
6202 // not matter. If it matters, there is enough info to get the
6203 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006204 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006205 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006206 }
6207 // Return null for empty list.
6208 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00006209 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006210
6211 llvm::Constant *Values[3];
Micah Villmowdd31ca12012-10-08 16:25:52 +00006212 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006213 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6214 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00006215 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006216 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00006217 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006218 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006219 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6220 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006221 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006222 llvm::GlobalValue::PrivateLinkage,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006223 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006224 Prefix + OID->getName());
Rafael Espindola21039aa2014-02-27 16:26:32 +00006225 assertPrivateName(GV);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006226 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006227 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006228 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006229
Chris Lattnerf56501c2009-07-17 23:57:13 +00006230 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00006231 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006232}
6233
6234llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006235 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006236 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006237
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006238 if (!Entry) {
6239 // We use the initializer as a marker of whether this is a forward
6240 // reference or not. At module finalization we add the empty
6241 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006242 Entry =
6243 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6244 llvm::GlobalValue::ExternalLinkage,
6245 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006246 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006247 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006248 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006249
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006250 return Entry;
6251}
6252
6253/// GetOrEmitProtocol - Generate the protocol meta-data:
6254/// @code
6255/// struct _protocol_t {
6256/// id isa; // NULL
6257/// const char * const protocol_name;
6258/// const struct _protocol_list_t * protocol_list; // super protocols
6259/// const struct method_list_t * const instance_methods;
6260/// const struct method_list_t * const class_methods;
6261/// const struct method_list_t *optionalInstanceMethods;
6262/// const struct method_list_t *optionalClassMethods;
6263/// const struct _prop_list_t * properties;
6264/// const uint32_t size; // sizeof(struct _protocol_t)
6265/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006266/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006267/// }
6268/// @endcode
6269///
6270
6271llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006272 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00006273 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006274
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006275 // Early exit if a defining object has already been generated.
6276 if (Entry && Entry->hasInitializer())
6277 return Entry;
6278
Douglas Gregora715bff2012-01-01 19:51:50 +00006279 // Use the protocol definition, if there is one.
6280 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6281 PD = Def;
6282
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006283 // Construct method lists.
6284 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6285 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006286 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006287 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006288 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006289 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006290 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006291 if (!C)
6292 return GetOrEmitProtocolRef(PD);
6293
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006294 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6295 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006296 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006297 } else {
6298 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006299 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006300 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006301 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006302
6303 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006304 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006305 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006306 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006307 if (!C)
6308 return GetOrEmitProtocolRef(PD);
6309
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006310 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6311 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006312 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006313 } else {
6314 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006315 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006316 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006317 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006318
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006319 MethodTypesExt.insert(MethodTypesExt.end(),
6320 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6321
6322 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006323 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00006324 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006325 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006326 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6327 PD->protocol_begin(),
6328 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006329
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006330 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006331 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006332 "__DATA, __objc_const",
6333 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006334 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006335 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006336 "__DATA, __objc_const",
6337 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006338 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006339 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006340 "__DATA, __objc_const",
6341 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006342 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006343 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006344 "__DATA, __objc_const",
6345 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006346 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006347 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006348 uint32_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00006349 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006350 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00006351 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006352 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6353 + PD->getName(),
6354 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006355 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006356 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006357
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006358 if (Entry) {
6359 // Already created, fix the linkage and update the initializer.
Rafael Espindola554256c2014-02-26 22:25:45 +00006360 assert(Entry->getLinkage() == llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006361 Entry->setInitializer(Init);
6362 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006363 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006364 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6365 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6366 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006367 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006368 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006369 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00006370
6371 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006372 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006373 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006374 CGM.AddUsedGlobal(Entry);
6375
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006376 // Use this protocol meta-data to build protocol list table in section
6377 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006378 llvm::GlobalVariable *PTGV =
6379 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6380 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6381 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006382 PTGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006383 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00006384 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006385 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006386 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006387 return Entry;
6388}
6389
6390/// EmitProtocolList - Generate protocol list meta-data:
6391/// @code
6392/// struct _protocol_list_t {
6393/// long protocol_count; // Note, this is 32/64 bit
6394/// struct _protocol_t[protocol_count];
6395/// }
6396/// @endcode
6397///
6398llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006399CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006400 ObjCProtocolDecl::protocol_iterator begin,
6401 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006402 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006403
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006404 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006405 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00006406 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006407
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006408 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00006409 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006410 Name.toVector(TmpName);
6411 llvm::GlobalVariable *GV =
6412 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006413 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006414 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006415
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006416 for (; begin != end; ++begin)
6417 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6418
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006419 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00006420 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006421 ObjCTypes.ProtocolnfABIPtrTy));
6422
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006423 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00006424 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006425 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006426 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00006427 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6428 ProtocolRefs.size()),
6429 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006430
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006431 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00006432 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006433 llvm::GlobalValue::PrivateLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006434 Init, Name);
Rafael Espindola21039aa2014-02-27 16:26:32 +00006435 assertPrivateName(GV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006436 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006437 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006438 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00006439 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006440 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006441 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006442}
6443
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006444/// GetMethodDescriptionConstant - This routine build following meta-data:
6445/// struct _objc_method {
6446/// SEL _cmd;
6447/// char *method_type;
6448/// char *_imp;
6449/// }
6450
6451llvm::Constant *
6452CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006453 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00006454 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006455 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6456 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006457 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006458 if (!Desc[1])
6459 return 0;
6460
Fariborz Jahanian097feda2009-01-30 18:58:59 +00006461 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00006462 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006463 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006464}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006465
6466/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6467/// This code gen. amounts to generating code for:
6468/// @code
6469/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6470/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006471///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00006472LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00006473 CodeGen::CodeGenFunction &CGF,
6474 QualType ObjectTy,
6475 llvm::Value *BaseValue,
6476 const ObjCIvarDecl *Ivar,
6477 unsigned CVRQualifiers) {
6478 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006479 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00006480
6481 if (IsIvarOffsetKnownIdempotent(CGF, ID, Ivar))
6482 if (llvm::LoadInst *LI = cast<llvm::LoadInst>(Offset))
6483 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6484 llvm::MDNode::get(VMContext, ArrayRef<llvm::Value*>()));
6485
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00006486 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006487 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006488}
6489
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006490llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006491 CodeGen::CodeGenFunction &CGF,
6492 const ObjCInterfaceDecl *Interface,
6493 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00006494 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006495}
6496
John McCall234eac82011-05-13 23:16:18 +00006497static void appendSelectorForMessageRefTable(std::string &buffer,
6498 Selector selector) {
6499 if (selector.isUnarySelector()) {
6500 buffer += selector.getNameForSlot(0);
6501 return;
6502 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006503
John McCall234eac82011-05-13 23:16:18 +00006504 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6505 buffer += selector.getNameForSlot(i);
6506 buffer += '_';
6507 }
6508}
6509
John McCall9e8bb002011-05-14 03:10:52 +00006510/// Emit a "v-table" message send. We emit a weak hidden-visibility
6511/// struct, initially containing the selector pointer and a pointer to
6512/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6513/// load and call the function pointer, passing the address of the
6514/// struct as the second parameter. The runtime determines whether
6515/// the selector is currently emitted using vtable dispatch; if so, it
6516/// substitutes a stub function which simply tail-calls through the
6517/// appropriate vtable slot, and if not, it substitues a stub function
6518/// which tail-calls objc_msgSend. Both stubs adjust the selector
6519/// argument to correctly point to the selector.
6520RValue
6521CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6522 ReturnValueSlot returnSlot,
6523 QualType resultType,
6524 Selector selector,
6525 llvm::Value *arg0,
6526 QualType arg0Type,
6527 bool isSuper,
6528 const CallArgList &formalArgs,
6529 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00006530 // Compute the actual arguments.
6531 CallArgList args;
6532
John McCall9e8bb002011-05-14 03:10:52 +00006533 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00006534 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00006535 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6536 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00006537
John McCall9e8bb002011-05-14 03:10:52 +00006538 // Second argument: a pointer to the message ref structure. Leave
6539 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00006540 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6541
6542 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6543
John McCalla729c622012-02-17 03:33:10 +00006544 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00006545
John McCall5880fb82011-05-14 21:12:11 +00006546 NullReturnState nullReturn;
6547
John McCall9e8bb002011-05-14 03:10:52 +00006548 // Find the function to call and the mangled name for the message
6549 // ref structure. Using a different mangled name wouldn't actually
6550 // be a problem; it would just be a waste.
6551 //
6552 // The runtime currently never uses vtable dispatch for anything
6553 // except normal, non-super message-sends.
6554 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00006555 llvm::Constant *fn = 0;
6556 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00006557 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00006558 if (isSuper) {
6559 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6560 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006561 } else {
John McCall5880fb82011-05-14 21:12:11 +00006562 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00006563 fn = ObjCTypes.getMessageSendStretFixupFn();
6564 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006565 }
John McCall234eac82011-05-13 23:16:18 +00006566 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6567 fn = ObjCTypes.getMessageSendFpretFixupFn();
6568 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00006569 } else {
John McCall234eac82011-05-13 23:16:18 +00006570 if (isSuper) {
6571 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6572 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006573 } else {
John McCall234eac82011-05-13 23:16:18 +00006574 fn = ObjCTypes.getMessageSendFixupFn();
6575 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006576 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00006577 }
John McCall234eac82011-05-13 23:16:18 +00006578 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6579 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006580
John McCall234eac82011-05-13 23:16:18 +00006581 // Append the selector name, except use underscores anywhere we
6582 // would have used colons.
6583 appendSelectorForMessageRefTable(messageRefName, selector);
6584
6585 llvm::GlobalVariable *messageRef
6586 = CGM.getModule().getGlobalVariable(messageRefName);
6587 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00006588 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00006589 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006590 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00006591 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6592 init->getType(),
6593 /*constant*/ false,
6594 llvm::GlobalValue::WeakAnyLinkage,
6595 init,
6596 messageRefName);
6597 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6598 messageRef->setAlignment(16);
6599 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6600 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006601
6602 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00006603 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006604 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6605 e = method->param_end(); i != e; ++i) {
6606 const ParmVarDecl *ParamDecl = (*i);
6607 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6608 if (!nullReturn.NullBB)
6609 nullReturn.init(CGF, arg0);
6610 requiresnullCheck = true;
6611 break;
6612 }
6613 }
6614
John McCall234eac82011-05-13 23:16:18 +00006615 llvm::Value *mref =
6616 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6617
John McCall9e8bb002011-05-14 03:10:52 +00006618 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00006619 args[1].RV = RValue::get(mref);
6620
6621 // Load the function to call from the message ref table.
6622 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6623 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6624
John McCalla729c622012-02-17 03:33:10 +00006625 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00006626
John McCalla729c622012-02-17 03:33:10 +00006627 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006628 return nullReturn.complete(CGF, result, resultType, formalArgs,
6629 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006630}
6631
6632/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006633CodeGen::RValue
6634CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006635 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006636 QualType ResultType,
6637 Selector Sel,
6638 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006639 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00006640 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006641 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00006642 return isVTableDispatchedSelector(Sel)
6643 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006644 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006645 false, CallArgs, Method)
6646 : EmitMessageSend(CGF, Return, ResultType,
John McCall882987f2013-02-28 19:01:20 +00006647 EmitSelector(CGF, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006648 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006649 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006650}
6651
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006652llvm::GlobalVariable *
Rafael Espindola554256c2014-02-26 22:25:45 +00006653CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name, bool Weak) {
6654 llvm::GlobalValue::LinkageTypes L =
6655 Weak ? llvm::GlobalValue::ExternalWeakLinkage
6656 : llvm::GlobalValue::ExternalLinkage;
6657
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006658 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6659
Rafael Espindola554256c2014-02-26 22:25:45 +00006660 if (!GV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006661 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Rafael Espindola554256c2014-02-26 22:25:45 +00006662 false, L, 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006663
Rafael Espindola554256c2014-02-26 22:25:45 +00006664 assert(GV->getLinkage() == L);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006665 return GV;
6666}
6667
John McCall882987f2013-02-28 19:01:20 +00006668llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CodeGenFunction &CGF,
Rafael Espindola554256c2014-02-26 22:25:45 +00006669 IdentifierInfo *II,
6670 bool Weak) {
John McCall31168b02011-06-15 23:02:42 +00006671 llvm::GlobalVariable *&Entry = ClassReferences[II];
6672
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006673 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00006674 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Rafael Espindola554256c2014-02-26 22:25:45 +00006675 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, Weak);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006676 Entry =
John McCall31168b02011-06-15 23:02:42 +00006677 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006678 false, llvm::GlobalValue::PrivateLinkage,
John McCall31168b02011-06-15 23:02:42 +00006679 ClassGV,
6680 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006681 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006682 CGM.getDataLayout().getABITypeAlignment(
John McCall31168b02011-06-15 23:02:42 +00006683 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006684 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006685 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006686 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00006687 assertPrivateName(Entry);
John McCall882987f2013-02-28 19:01:20 +00006688 return CGF.Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006689}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006690
John McCall882987f2013-02-28 19:01:20 +00006691llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CodeGenFunction &CGF,
John McCall31168b02011-06-15 23:02:42 +00006692 const ObjCInterfaceDecl *ID) {
Rafael Espindola554256c2014-02-26 22:25:45 +00006693 return EmitClassRefFromId(CGF, ID->getIdentifier(), ID->isWeakImported());
John McCall31168b02011-06-15 23:02:42 +00006694}
6695
6696llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
John McCall882987f2013-02-28 19:01:20 +00006697 CodeGenFunction &CGF) {
John McCall31168b02011-06-15 23:02:42 +00006698 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
Rafael Espindola554256c2014-02-26 22:25:45 +00006699 return EmitClassRefFromId(CGF, II, false);
John McCall31168b02011-06-15 23:02:42 +00006700}
6701
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006702llvm::Value *
John McCall882987f2013-02-28 19:01:20 +00006703CGObjCNonFragileABIMac::EmitSuperClassRef(CodeGenFunction &CGF,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006704 const ObjCInterfaceDecl *ID) {
6705 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006706
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006707 if (!Entry) {
6708 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6709 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006710 Entry =
6711 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006712 false, llvm::GlobalValue::PrivateLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006713 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006714 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006715 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006716 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006717 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006718 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006719 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006720 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00006721 assertPrivateName(Entry);
John McCall882987f2013-02-28 19:01:20 +00006722 return CGF.Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006723}
6724
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006725/// EmitMetaClassRef - Return a Value * of the address of _class_t
6726/// meta-data
6727///
John McCall882987f2013-02-28 19:01:20 +00006728llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006729 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006730 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
Rafael Espindola21039aa2014-02-27 16:26:32 +00006731 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006732
Rafael Espindola21039aa2014-02-27 16:26:32 +00006733 std::string MetaClassName(getMetaclassSymbolPrefix() +
6734 ID->getNameAsString());
6735 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
6736 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006737 false, llvm::GlobalValue::PrivateLinkage,
Rafael Espindola21039aa2014-02-27 16:26:32 +00006738 MetaClassGV,
6739 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
6740 Entry->setAlignment(
6741 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006742
Rafael Espindola21039aa2014-02-27 16:26:32 +00006743 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
6744 CGM.AddUsedGlobal(Entry);
6745 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006746
Rafael Espindola21039aa2014-02-27 16:26:32 +00006747 assertPrivateName(Entry);
John McCall882987f2013-02-28 19:01:20 +00006748 return CGF.Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006749}
6750
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006751/// GetClass - Return a reference to the class for the given interface
6752/// decl.
John McCall882987f2013-02-28 19:01:20 +00006753llvm::Value *CGObjCNonFragileABIMac::GetClass(CodeGenFunction &CGF,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006754 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006755 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006756 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Rafael Espindola554256c2014-02-26 22:25:45 +00006757 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName, true);
Nick Lewycky9a441642014-02-27 00:36:00 +00006758 (void)ClassGV;
Rafael Espindola554256c2014-02-26 22:25:45 +00006759 assert(ClassGV->getLinkage() == llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006760 }
6761
John McCall882987f2013-02-28 19:01:20 +00006762 return EmitClassRef(CGF, ID);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006763}
6764
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006765/// Generates a message send where the super is the receiver. This is
6766/// a message send to self with special delivery semantics indicating
6767/// which class's method should be called.
6768CodeGen::RValue
6769CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006770 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006771 QualType ResultType,
6772 Selector Sel,
6773 const ObjCInterfaceDecl *Class,
6774 bool isCategoryImpl,
6775 llvm::Value *Receiver,
6776 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006777 const CodeGen::CallArgList &CallArgs,
6778 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006779 // ...
6780 // Create and init a super structure; this is a (receiver, class)
6781 // pair we will pass to objc_msgSendSuper.
6782 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006783 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006784
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006785 llvm::Value *ReceiverAsObject =
6786 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6787 CGF.Builder.CreateStore(ReceiverAsObject,
6788 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006789
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006790 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006791 llvm::Value *Target;
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006792 if (IsClassMessage)
John McCall882987f2013-02-28 19:01:20 +00006793 Target = EmitMetaClassRef(CGF, Class);
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006794 else
John McCall882987f2013-02-28 19:01:20 +00006795 Target = EmitSuperClassRef(CGF, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006796
Mike Stump18bb9282009-05-16 07:57:57 +00006797 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6798 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006799 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006800 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6801 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6802 CGF.Builder.CreateStore(Target,
6803 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006804
John McCall9e8bb002011-05-14 03:10:52 +00006805 return (isVTableDispatchedSelector(Sel))
6806 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006807 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006808 true, CallArgs, Method)
6809 : EmitMessageSend(CGF, Return, ResultType,
John McCall882987f2013-02-28 19:01:20 +00006810 EmitSelector(CGF, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006811 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006812 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006813}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006814
John McCall882987f2013-02-28 19:01:20 +00006815llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CodeGenFunction &CGF,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006816 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006817 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006818
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006819 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006820 llvm::Constant *Casted =
6821 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6822 ObjCTypes.SelectorPtrTy);
6823 Entry =
6824 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Rafael Espindola5179a4e2014-02-27 19:01:11 +00006825 llvm::GlobalValue::PrivateLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006826 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Michael Gottesman5c205962013-02-05 23:08:45 +00006827 Entry->setExternallyInitialized(true);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006828 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006829 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006830 }
Rafael Espindola21039aa2014-02-27 16:26:32 +00006831 assertPrivateName(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006832
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006833 if (lval)
6834 return Entry;
John McCall882987f2013-02-28 19:01:20 +00006835 llvm::LoadInst* LI = CGF.Builder.CreateLoad(Entry);
Pete Cooper9d605512011-11-10 21:45:06 +00006836
6837 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6838 llvm::MDNode::get(VMContext,
6839 ArrayRef<llvm::Value*>()));
6840 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006841}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006842/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006843/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006844///
6845void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006846 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006847 llvm::Value *dst,
6848 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006849 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006850 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006851 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006852 assert(Size <= 8 && "does not support size > 8");
6853 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6854 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006855 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6856 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006857 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6858 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00006859 llvm::Value *args[] = { src, dst, ivarOffset };
6860 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignIvarFn(), args);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006861}
6862
6863/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6864/// objc_assign_strongCast (id src, id *dst)
6865///
6866void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006867 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006868 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006869 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006870 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006871 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006872 assert(Size <= 8 && "does not support size > 8");
6873 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006874 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006875 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6876 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006877 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6878 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00006879 llvm::Value *args[] = { src, dst };
6880 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignStrongCastFn(),
6881 args, "weakassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006882}
6883
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006884void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006885 CodeGen::CodeGenFunction &CGF,
6886 llvm::Value *DestPtr,
6887 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006888 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006889 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6890 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
John McCall882987f2013-02-28 19:01:20 +00006891 llvm::Value *args[] = { DestPtr, SrcPtr, Size };
6892 CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006893}
6894
Fariborz Jahanian06292952009-02-16 22:52:32 +00006895/// EmitObjCWeakRead - Code gen for loading value of a __weak
6896/// object: objc_read_weak (id *src)
6897///
6898llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006899 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006900 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006901 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006902 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6903 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00006904 llvm::Value *read_weak =
6905 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcReadWeakFn(),
6906 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006907 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006908 return read_weak;
6909}
6910
6911/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6912/// objc_assign_weak (id src, id *dst)
6913///
6914void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006915 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006916 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006917 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006918 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006919 assert(Size <= 8 && "does not support size > 8");
6920 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6921 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006922 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6923 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006924 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6925 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00006926 llvm::Value *args[] = { src, dst };
6927 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignWeakFn(),
6928 args, "weakassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006929}
6930
6931/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6932/// objc_assign_global (id src, id *dst)
6933///
6934void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006935 llvm::Value *src, llvm::Value *dst,
6936 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006937 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006938 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006939 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006940 assert(Size <= 8 && "does not support size > 8");
6941 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6942 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006943 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6944 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006945 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6946 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00006947 llvm::Value *args[] = { src, dst };
Fariborz Jahanian217af242010-07-20 20:30:03 +00006948 if (!threadlocal)
John McCall882987f2013-02-28 19:01:20 +00006949 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignGlobalFn(),
6950 args, "globalassign");
Fariborz Jahanian217af242010-07-20 20:30:03 +00006951 else
John McCall882987f2013-02-28 19:01:20 +00006952 CGF.EmitNounwindRuntimeCall(ObjCTypes.getGcAssignThreadLocalFn(),
6953 args, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006954}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006955
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006956void
John McCallbd309292010-07-06 01:34:17 +00006957CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6958 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006959 EmitAtSynchronizedStmt(CGF, S,
6960 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6961 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006962}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006963
John McCall2ca705e2010-07-24 00:37:23 +00006964llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006965CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006966 // There's a particular fixed type info for 'id'.
6967 if (T->isObjCIdType() ||
6968 T->isObjCQualifiedIdType()) {
6969 llvm::Constant *IDEHType =
6970 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6971 if (!IDEHType)
6972 IDEHType =
6973 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6974 false,
6975 llvm::GlobalValue::ExternalLinkage,
6976 0, "OBJC_EHTYPE_id");
6977 return IDEHType;
6978 }
6979
6980 // All other types should be Objective-C interface pointer types.
6981 const ObjCObjectPointerType *PT =
6982 T->getAs<ObjCObjectPointerType>();
6983 assert(PT && "Invalid @catch type.");
6984 const ObjCInterfaceType *IT = PT->getInterfaceType();
6985 assert(IT && "Invalid @catch type.");
6986 return GetInterfaceEHType(IT->getDecl(), false);
6987}
6988
John McCallbd309292010-07-06 01:34:17 +00006989void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6990 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006991 EmitTryCatchStmt(CGF, S,
6992 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6993 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6994 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006995}
6996
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006997/// EmitThrowStmt - Generate code for a throw statement.
6998void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00006999 const ObjCAtThrowStmt &S,
7000 bool ClearInsertionPoint) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00007001 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00007002 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00007003 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
John McCall882987f2013-02-28 19:01:20 +00007004 CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00007005 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00007006 } else {
John McCall882987f2013-02-28 19:01:20 +00007007 CGF.EmitRuntimeCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00007008 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00007009 }
7010
John McCall17afe452010-10-16 08:21:07 +00007011 CGF.Builder.CreateUnreachable();
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00007012 if (ClearInsertionPoint)
7013 CGF.Builder.ClearInsertionPoint();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00007014}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007015
John McCall2ca705e2010-07-24 00:37:23 +00007016llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007017CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007018 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007019 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007020
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007021 // If we don't need a definition, return the entry if found or check
7022 // if we use an external reference.
7023 if (!ForDefinition) {
7024 if (Entry)
7025 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00007026
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007027 // If this type (or a super class) has the __objc_exception__
7028 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00007029 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007030 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00007031 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007032 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007033 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00007034 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00007035 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007036 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007037
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007038 // Otherwise we need to either make a new entry or fill in the
7039 // initializer.
7040 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00007041 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007042 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007043 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007044 CGM.getModule().getGlobalVariable(VTableName);
7045 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007046 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
7047 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007048 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00007049 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007050
Chris Lattnerece04092012-02-07 00:39:47 +00007051 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007052
Benjamin Kramer22d24c22011-10-15 12:20:02 +00007053 llvm::Constant *Values[] = {
7054 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
7055 GetClassName(ID->getIdentifier()),
7056 GetClassGlobal(ClassName)
7057 };
Owen Anderson170229f2009-07-14 23:10:40 +00007058 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00007059 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007060
Rafael Espindola554256c2014-02-26 22:25:45 +00007061 llvm::GlobalValue::LinkageTypes L = ForDefinition
7062 ? llvm::GlobalValue::ExternalLinkage
7063 : llvm::GlobalValue::WeakAnyLinkage;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007064 if (Entry) {
7065 Entry->setInitializer(Init);
7066 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00007067 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Rafael Espindola554256c2014-02-26 22:25:45 +00007068 L,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007069 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00007070 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00007071 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007072 }
Rafael Espindola554256c2014-02-26 22:25:45 +00007073 assert(Entry->getLinkage() == L);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007074
John McCalla4960982013-02-19 01:57:29 +00007075 if (ID->getVisibility() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00007076 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmowdd31ca12012-10-08 16:25:52 +00007077 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00007078 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007079
Rafael Espindola554256c2014-02-26 22:25:45 +00007080 if (ForDefinition)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007081 Entry->setSection("__DATA,__objc_const");
Rafael Espindola554256c2014-02-26 22:25:45 +00007082 else
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007083 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007084
7085 return Entry;
7086}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007087
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00007088/* *** */
7089
Daniel Dunbarb036db82008-08-13 03:21:16 +00007090CodeGen::CGObjCRuntime *
7091CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00007092 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
7093 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00007094 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00007095
7096 case ObjCRuntime::MacOSX:
7097 case ObjCRuntime::iOS:
7098 return new CGObjCNonFragileABIMac(CGM);
7099
David Chisnallb601c962012-07-03 20:49:52 +00007100 case ObjCRuntime::GNUstep:
7101 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00007102 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00007103 llvm_unreachable("these runtimes are not Mac runtimes");
7104 }
7105 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00007106}