blob: bea30a13673e78c108f40d06381ae2a8190795db [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"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/ADT/SmallString.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000031#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/InlineAsm.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Module.h"
John McCall5c08ab92010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Torok Edwindb714922009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000042
43namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000044
Daniel Dunbar59e476b2009-08-03 17:06:42 +000045// FIXME: We should find a nicer way to make the labels for metadata, string
46// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000047
Fariborz Jahanian279eda62009-01-21 22:04:16 +000048class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000049protected:
50 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000051
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000052private:
John McCall9dc0db22011-05-15 01:53:33 +000053 // The types of these functions don't really matter because we
54 // should always bitcast before calling them.
55
56 /// id objc_msgSend (id, SEL, ...)
57 ///
58 /// The default messenger, used for sends whose ABI is unchanged from
59 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000060 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000061 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
62 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000063 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Bill Wendling8594fcb2013-01-31 00:30:05 +000064 return
65 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
66 params, true),
67 "objc_msgSend",
68 llvm::AttributeSet::get(CGM.getLLVMContext(),
69 llvm::AttributeSet::FunctionIndex,
70 llvm::Attribute::NonLazyBind));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000071 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000072
John McCall9dc0db22011-05-15 01:53:33 +000073 /// void objc_msgSend_stret (id, SEL, ...)
74 ///
75 /// The messenger used when the return value is an aggregate returned
76 /// by indirect reference in the first argument, and therefore the
77 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000078 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000079 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000080 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
81 params, true),
82 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000083
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000084 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000085
John McCall9dc0db22011-05-15 01:53:33 +000086 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
87 ///
88 /// The messenger used when the return value is returned on the x87
89 /// floating-point stack; without a special entrypoint, the nil case
90 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000091 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000092 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattnerece04092012-02-07 00:39:47 +000093 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
94 params, true),
John McCall9dc0db22011-05-15 01:53:33 +000095 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000096
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000097 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000098
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000099 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
100 ///
101 /// The messenger used when the return value is returned in two values on the
102 /// x87 floating point stack; without a special entrypoint, the nil case
103 /// would be unbalanced. Only used on 64-bit X86.
104 llvm::Constant *getMessageSendFp2retFn() const {
105 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
106 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
107 llvm::Type *resultType =
108 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
109
110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
111 params, true),
112 "objc_msgSend_fp2ret");
113 }
114
John McCall9dc0db22011-05-15 01:53:33 +0000115 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
116 ///
117 /// The messenger used for super calls, which have different dispatch
118 /// semantics. The class passed is the superclass of the current
119 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000120 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000121 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000122 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000123 params, true),
124 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000125 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000126
John McCall9dc0db22011-05-15 01:53:33 +0000127 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
128 ///
129 /// A slightly different messenger used for super calls. The class
130 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000131 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000132 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000133 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000134 params, true),
135 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000136 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000137
John McCall9dc0db22011-05-15 01:53:33 +0000138 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
139 /// SEL op, ...)
140 ///
141 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000142 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000143 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000144 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000145 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000146 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000147 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000148
John McCall9dc0db22011-05-15 01:53:33 +0000149 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
150 /// SEL op, ...)
151 ///
152 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000153 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000154 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000155 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000156 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000157 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000158 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000159
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000160 llvm::Constant *getMessageSendSuperFpretFn() const {
161 // There is no objc_msgSendSuper_fpret? How can that work?
162 return getMessageSendSuperFn();
163 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000164
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000165 llvm::Constant *getMessageSendSuperFpretFn2() const {
166 // There is no objc_msgSendSuper_fpret? How can that work?
167 return getMessageSendSuperFn2();
168 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000169
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000170protected:
171 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000173public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000174 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000175 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000176
Daniel Dunbar5d715592008-08-12 05:28:47 +0000177 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000178 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000179
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000180 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000181 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000183 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000184 llvm::Type *SelectorPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000185
186private:
Daniel Dunbarb036db82008-08-13 03:21:16 +0000187 /// ProtocolPtrTy - LLVM type for external protocol handles
188 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000189 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000190
191public:
192 llvm::Type *getExternalProtocolPtrTy() {
193 if (!ExternalProtocolPtrTy) {
194 // FIXME: It would be nice to unify this with the opaque type, so that the
195 // IR comes out a bit cleaner.
196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
197 ASTContext &Ctx = CGM.getContext();
198 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
199 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
200 }
201
202 return ExternalProtocolPtrTy;
203 }
204
Daniel Dunbarc722b852008-08-30 03:02:31 +0000205 // SuperCTy - clang type for struct objc_super.
206 QualType SuperCTy;
207 // SuperPtrCTy - clang type for struct objc_super *.
208 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000209
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000210 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000211 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000212 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000213 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000214
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000215 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
216 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000217 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000218
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000219 /// PropertyListTy - LLVM type for struct objc_property_list
220 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000221 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000222 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000223 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000224
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000225 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000226 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000227
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000228 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000229 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000230 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000231 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000232
Chris Lattnerce8754e2009-04-22 02:44:54 +0000233 llvm::Constant *getGetPropertyFn() {
234 CodeGen::CodeGenTypes &Types = CGM.getTypes();
235 ASTContext &Ctx = CGM.getContext();
236 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000237 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000238 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
239 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000240 Params.push_back(IdType);
241 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000242 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000243 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000244 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000245 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
246 FunctionType::ExtInfo(),
247 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
249 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000250
Chris Lattnerce8754e2009-04-22 02:44:54 +0000251 llvm::Constant *getSetPropertyFn() {
252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
253 ASTContext &Ctx = CGM.getContext();
254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000255 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000256 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
257 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000258 Params.push_back(IdType);
259 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000260 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(Ctx.BoolTy);
263 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000264 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000265 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
266 FunctionType::ExtInfo(),
267 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000268 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
269 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000270
Ted Kremeneke65b0862012-03-06 20:05:56 +0000271 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_setProperty_atomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_nonatomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282
283 SmallVector<CanQualType,4> Params;
284 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
285 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
286 Params.push_back(IdType);
287 Params.push_back(SelType);
288 Params.push_back(IdType);
289 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
290 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000291 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
292 FunctionType::ExtInfo(),
293 RequiredArgs::All));
Ted Kremeneke65b0862012-03-06 20:05:56 +0000294 const char *name;
295 if (atomic && copy)
296 name = "objc_setProperty_atomic_copy";
297 else if (atomic && !copy)
298 name = "objc_setProperty_atomic";
299 else if (!atomic && copy)
300 name = "objc_setProperty_nonatomic_copy";
301 else
302 name = "objc_setProperty_nonatomic";
303
304 return CGM.CreateRuntimeFunction(FTy, name);
305 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000306
307 llvm::Constant *getCopyStructFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000311 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000318 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
319 FunctionType::ExtInfo(),
320 RequiredArgs::All));
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
322 }
323
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000324 /// This routine declares and returns address of:
325 /// void objc_copyCppObjectAtomic(
326 /// void *dest, const void *src,
327 /// void (*copyHelper) (void *dest, const void *source));
328 llvm::Constant *getCppAtomicObjectFunction() {
329 CodeGen::CodeGenTypes &Types = CGM.getTypes();
330 ASTContext &Ctx = CGM.getContext();
331 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
332 SmallVector<CanQualType,3> Params;
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000337 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
338 FunctionType::ExtInfo(),
339 RequiredArgs::All));
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
341 }
342
Chris Lattnerce8754e2009-04-22 02:44:54 +0000343 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000346 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000347 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000348 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000349 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000350 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCalla729c622012-02-17 03:33:10 +0000351 FunctionType::ExtInfo(),
352 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
354 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000355
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000356 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000357 llvm::Constant *getGcReadWeakFn() {
358 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000360 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000361 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000363 }
364
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000365 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000366 llvm::Constant *getGcAssignWeakFn() {
367 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000368 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000369 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000370 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
372 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000373
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000374 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignGlobalFn() {
376 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
381 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000382
Fariborz Jahanian217af242010-07-20 20:30:03 +0000383 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
384 llvm::Constant *getGcAssignThreadLocalFn() {
385 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000387 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000388 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
390 }
391
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000392 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000393 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000394 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000395 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
396 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000397 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000398 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
400 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000401
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000402 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
403 llvm::Constant *GcMemmoveCollectableFn() {
404 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000405 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000406 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
408 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000409
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000410 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000411 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000412 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000413 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000414 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000415 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000416 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
417 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000418
419 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000420 llvm::Constant *getExceptionThrowFn() {
421 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000422 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000423 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000424 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
426 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000427
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000428 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
429 llvm::Constant *getExceptionRethrowFn() {
430 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000431 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
433 }
434
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000435 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000436 llvm::Constant *getSyncEnterFn() {
Aaron Ballman9c004462012-09-06 16:44:16 +0000437 // int objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000438 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000439 llvm::FunctionType *FTy =
Aaron Ballman9c004462012-09-06 16:44:16 +0000440 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
442 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000443
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000444 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000445 llvm::Constant *getSyncExitFn() {
Aaron Ballman9c004462012-09-06 16:44:16 +0000446 // int objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000447 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000448 llvm::FunctionType *FTy =
Aaron Ballman9c004462012-09-06 16:44:16 +0000449 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
451 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000452
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000453 llvm::Constant *getSendFn(bool IsSuper) const {
454 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
455 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000456
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000457 llvm::Constant *getSendFn2(bool IsSuper) const {
458 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
459 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000460
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000461 llvm::Constant *getSendStretFn(bool IsSuper) const {
462 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
463 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000464
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000465 llvm::Constant *getSendStretFn2(bool IsSuper) const {
466 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
467 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000468
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000469 llvm::Constant *getSendFpretFn(bool IsSuper) const {
470 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
471 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000472
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000473 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
474 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
475 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000476
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000477 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
478 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
479 }
480
481 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
482 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
483 }
484
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000485 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
486 ~ObjCCommonTypesHelper(){}
487};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000488
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000489/// ObjCTypesHelper - Helper class that encapsulates lazy
490/// construction of varies types used during ObjC generation.
491class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000492public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000493 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000494 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000495 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000496 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000497 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000498 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000499
Daniel Dunbarb036db82008-08-13 03:21:16 +0000500 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000501 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000502 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000503 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000504 /// ProtocolExtensionTy - LLVM type for struct
505 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000506 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000507 /// ProtocolExtensionTy - LLVM type for struct
508 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000510 /// MethodDescriptionTy - LLVM type for struct
511 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000512 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000513 /// MethodDescriptionListTy - LLVM type for struct
514 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000515 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000516 /// MethodDescriptionListPtrTy - LLVM type for struct
517 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000518 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000519 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000520 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000521 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000522 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000523 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000524 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000525 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000526 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000527 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000528 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000529 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000530 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000531 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000532 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000533 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000534 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000535 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000536 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000537 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000538 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000539 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000540 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000541 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000542 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Anders Carlsson9ff22482008-09-09 10:10:21 +0000544 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000545 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000546
Anders Carlsson9ff22482008-09-09 10:10:21 +0000547 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000548 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000549 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000550 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000551 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000552 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000553 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000554
555 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000556 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000557 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000558 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000559 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000560 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000561 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000562
563 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000564 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000565 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000566 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000567 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000568 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000569 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000570
Anders Carlsson9ff22482008-09-09 10:10:21 +0000571 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000572 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000573 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000574 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000575 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000576 "objc_exception_match");
577
Chris Lattnerc6406db2009-04-22 02:26:14 +0000578 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000579
Anders Carlsson9ff22482008-09-09 10:10:21 +0000580 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000581 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000582 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000583 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
Bill Wendling8594fcb2013-01-31 00:30:05 +0000584 return
585 CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
586 params, false),
587 "_setjmp",
588 llvm::AttributeSet::get(CGM.getLLVMContext(),
589 llvm::AttributeSet::FunctionIndex,
590 llvm::Attribute::NonLazyBind));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000591 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000592
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000593public:
594 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000595 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000596};
597
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000598/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000599/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000600class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000601public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000603 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000604 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000605
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000606 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000607 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000608
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000609 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000610 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000612 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000613 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000614
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000615 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000616 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000617
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000618 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000619 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000620
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000621 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000622 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000623
Fariborz Jahanian71394042009-01-23 23:53:38 +0000624 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000625 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000627 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000628 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000629
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000630 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000631 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000632
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000633 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000634 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000635
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000636 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000637 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000639 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000640 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000641
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000642 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000643 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000645 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000647 // MessageRefTy - LLVM for:
648 // struct _message_ref_t {
649 // IMP messenger;
650 // SEL name;
651 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000652 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000653 // MessageRefCTy - clang type for struct _message_ref_t
654 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000656 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000657 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000658 // MessageRefCPtrTy - clang type for struct _message_ref_t*
659 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000660
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000661 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000662 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000663
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000664 // SuperMessageRefTy - LLVM for:
665 // struct _super_message_ref_t {
666 // SUPER_IMP messenger;
667 // SEL name;
668 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000669 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000671 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000672 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000673
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000674 llvm::Constant *getMessageSendFixupFn() {
675 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000676 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000677 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000678 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000679 "objc_msgSend_fixup");
680 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000681
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000682 llvm::Constant *getMessageSendFpretFixupFn() {
683 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000684 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000686 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000687 "objc_msgSend_fpret_fixup");
688 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000689
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000690 llvm::Constant *getMessageSendStretFixupFn() {
691 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000692 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000693 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000694 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000695 "objc_msgSend_stret_fixup");
696 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000697
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000698 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000699 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000700 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000701 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000702 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000703 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000704 "objc_msgSendSuper2_fixup");
705 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000706
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000707 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000708 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000709 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000710 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000711 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000712 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000713 "objc_msgSendSuper2_stret_fixup");
714 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000715
Chris Lattnera7c00b42009-04-22 02:15:23 +0000716 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000718 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000719
Chris Lattnera7c00b42009-04-22 02:15:23 +0000720 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000721
Chris Lattnera7c00b42009-04-22 02:15:23 +0000722 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000723 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000724 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000725 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000726 "objc_begin_catch");
727 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000728
Chris Lattnera5f58b02011-07-09 17:41:47 +0000729 llvm::StructType *EHTypeTy;
730 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000731
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000732 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
733 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000734};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000735
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000736class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000737public:
738 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000739 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000740 public:
Eli Friedman8cbca202012-11-06 22:15:52 +0000741 unsigned ivar_bytepos;
742 unsigned ivar_size;
743 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000744 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000745
746 // Allow sorting based on byte pos.
747 bool operator<(const GC_IVAR &b) const {
748 return ivar_bytepos < b.ivar_bytepos;
749 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000750 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000751
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000752 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000753 public:
754 unsigned skip;
755 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000756 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000757 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000758 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000759
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000760 /// opcode for captured block variables layout 'instructions'.
761 /// In the following descriptions, 'I' is the value of the immediate field.
762 /// (field following the opcode).
763 ///
764 enum BLOCK_LAYOUT_OPCODE {
765 /// An operator which affects how the following layout should be
766 /// interpreted.
767 /// I == 0: Halt interpretation and treat everything else as
768 /// a non-pointer. Note that this instruction is equal
769 /// to '\0'.
770 /// I != 0: Currently unused.
771 BLOCK_LAYOUT_OPERATOR = 0,
772
773 /// The next I+1 bytes do not contain a value of object pointer type.
774 /// Note that this can leave the stream unaligned, meaning that
775 /// subsequent word-size instructions do not begin at a multiple of
776 /// the pointer size.
777 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
778
779 /// The next I+1 words do not contain a value of object pointer type.
780 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
781 /// when the required skip quantity is a multiple of the pointer size.
782 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
783
784 /// The next I+1 words are __strong pointers to Objective-C
785 /// objects or blocks.
786 BLOCK_LAYOUT_STRONG = 3,
787
788 /// The next I+1 words are pointers to __block variables.
789 BLOCK_LAYOUT_BYREF = 4,
790
791 /// The next I+1 words are __weak pointers to Objective-C
792 /// objects or blocks.
793 BLOCK_LAYOUT_WEAK = 5,
794
795 /// The next I+1 words are __unsafe_unretained pointers to
796 /// Objective-C objects or blocks.
797 BLOCK_LAYOUT_UNRETAINED = 6
798
799 /// The next I+1 words are block or object pointers with some
800 /// as-yet-unspecified ownership semantics. If we add more
801 /// flavors of ownership semantics, values will be taken from
802 /// this range.
803 ///
804 /// This is included so that older tools can at least continue
805 /// processing the layout past such things.
806 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
807
808 /// All other opcodes are reserved. Halt interpretation and
809 /// treat everything else as opaque.
810 };
811
812 class RUN_SKIP {
813 public:
814 enum BLOCK_LAYOUT_OPCODE opcode;
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000815 CharUnits block_var_bytepos;
816 CharUnits block_var_size;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000817 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000818 CharUnits BytePos = CharUnits::Zero(),
819 CharUnits Size = CharUnits::Zero())
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +0000820 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000821
822 // Allow sorting based on byte pos.
823 bool operator<(const RUN_SKIP &b) const {
824 return block_var_bytepos < b.block_var_bytepos;
825 }
826 };
827
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000828protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000829 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000830 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000831 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000833 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000834 SmallVector<GC_IVAR, 16> SkipIvars;
835 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000836
837 // arc/mrr layout of captured block literal variables.
838 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000839
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000840 /// LazySymbols - Symbols to generate a lazy reference for. See
841 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000842 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000843
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000844 /// DefinedSymbols - External symbols which are defined by this
845 /// module. The symbols in this list and LazySymbols are used to add
846 /// special linker symbols which ensure that Objective-C modules are
847 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000848 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000850 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000851 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000853 /// MethodVarNames - uniqued method variable names.
854 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000856 /// DefinedCategoryNames - list of category names in form Class_Category.
857 llvm::SetVector<std::string> DefinedCategoryNames;
858
Daniel Dunbarb036db82008-08-13 03:21:16 +0000859 /// MethodVarTypes - uniqued method type signatures. We have to use
860 /// a StringMap here because have no other unique reference.
861 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000862
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000863 /// MethodDefinitions - map of methods which have been defined in
864 /// this translation unit.
865 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000867 /// PropertyNames - uniqued method variable names.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000869
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000870 /// ClassReferences - uniqued class references.
871 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000873 /// SelectorReferences - uniqued selector references.
874 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000875
Daniel Dunbarb036db82008-08-13 03:21:16 +0000876 /// Protocols - Protocols for which an objc_protocol structure has
877 /// been emitted. Forward declarations are handled by creating an
878 /// empty structure whose initializer is filled in when/if defined.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000880
Daniel Dunbarc475d422008-10-29 22:36:39 +0000881 /// DefinedProtocols - Protocols which have actually been
882 /// defined. We should not need this, see FIXME in GenerateProtocol.
883 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000884
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000885 /// DefinedClasses - List of defined classes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000886 SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000887
888 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000889 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000891 /// DefinedCategories - List of defined categories.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000892 SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000894 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000895 SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000897 /// GetNameForMethod - Return a name for the given method.
898 /// \param[out] NameOut - The return value.
899 void GetNameForMethod(const ObjCMethodDecl *OMD,
900 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000901 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000903 /// GetMethodVarName - Return a unique constant for the given
904 /// selector's name. The return value has type char *.
905 llvm::Constant *GetMethodVarName(Selector Sel);
906 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000908 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000909 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000910
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000911 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000912 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
913 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000914 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000915
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000916 /// GetPropertyName - Return a unique constant for the given
917 /// name. The return value has type char *.
918 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000920 // FIXME: This can be dropped once string functions are unified.
921 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
922 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000924 /// GetClassName - Return a unique constant for the given selector's
925 /// name. The return value has type char *.
926 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000927
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000928 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
929
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000930 /// BuildIvarLayout - Builds ivar layout bitmap for the class
931 /// implementation for the __strong or __weak case.
932 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000933 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
934 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000935
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000936 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000937
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000938 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman8cbca202012-11-06 22:15:52 +0000939 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000940 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000941 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000942 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000943 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000944 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cbca202012-11-06 22:15:52 +0000945 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000946 bool &HasUnion);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000947
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000948 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout);
Fariborz Jahanian2dd78192012-11-02 22:51:18 +0000949
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000950 void UpdateRunSkipBlockVars(bool IsByref,
951 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian7778d612012-11-07 20:00:32 +0000952 CharUnits FieldOffset,
953 CharUnits FieldSize);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000954
955 void BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000956 CharUnits BytePos, bool &HasUnion,
957 bool ByrefLayout=false);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000958
959 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
960 const RecordDecl *RD,
961 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000962 CharUnits BytePos, bool &HasUnion,
963 bool ByrefLayout);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000964
Fariborz Jahanian23290b02012-11-01 18:32:55 +0000965 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
966
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000967 llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout);
968
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000969
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000970 /// GetIvarLayoutName - Returns a unique constant for the given
971 /// ivar layout bitmap.
972 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
973 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000974
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000975 /// EmitPropertyList - Emit the given property list. The return
976 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000977 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000978 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000979 const ObjCContainerDecl *OCD,
980 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000981
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000982 /// EmitProtocolMethodTypes - Generate the array of extended method type
983 /// strings. The return value has type Int8PtrPtrTy.
984 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000985 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000986 const ObjCCommonTypesHelper &ObjCTypes);
987
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000988 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000989 void PushProtocolProperties(
990 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000991 SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlinga515b582012-02-09 22:16:49 +0000992 const Decl *Container,
993 const ObjCProtocolDecl *PROTO,
994 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000995
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000996 /// GetProtocolRef - Return a reference to the internal protocol
997 /// description, creating an empty one if it has not been
998 /// defined. The return value has type ProtocolPtrTy.
999 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +00001000
Daniel Dunbar30c65362009-03-09 20:09:19 +00001001 /// CreateMetadataVar - Create a global variable with internal
1002 /// linkage for use by the Objective-C runtime.
1003 ///
1004 /// This is a convenience wrapper which not only creates the
1005 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +00001006 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001007 ///
1008 /// \param Name - The variable name.
1009 /// \param Init - The variable initializer; this is also used to
1010 /// define the type of the variable.
1011 /// \param Section - The section the variable should go into, or 0.
1012 /// \param Align - The alignment for the variable, or 0.
1013 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +00001014 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001015 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00001016 llvm::Constant *Init,
1017 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001018 unsigned Align,
1019 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +00001020
John McCall9e8bb002011-05-14 03:10:52 +00001021 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1022 ReturnValueSlot Return,
1023 QualType ResultType,
1024 llvm::Value *Sel,
1025 llvm::Value *Arg0,
1026 QualType Arg0Ty,
1027 bool IsSuper,
1028 const CallArgList &CallArgs,
1029 const ObjCMethodDecl *OMD,
1030 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001031
Daniel Dunbar5e639272010-04-25 20:39:01 +00001032 /// EmitImageInfo - Emit the image info marker used to encode some module
1033 /// level information.
1034 void EmitImageInfo();
1035
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001036public:
Owen Andersonae86c192009-07-13 04:10:07 +00001037 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +00001038 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001039
David Chisnall481e3a82010-01-23 02:40:42 +00001040 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001041
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001042 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1043 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001044
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001045 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001046
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001047 /// GetOrEmitProtocol - Get the protocol object for the given
1048 /// declaration, emitting it if necessary. The return value has type
1049 /// ProtocolPtrTy.
1050 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001051
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001052 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1053 /// object for the given declaration, emitting it if needed. These
1054 /// forward references will be filled in with empty bodies if no
1055 /// definition is seen. The return value has type ProtocolPtrTy.
1056 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001057 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1058 const CGBlockInfo &blockInfo);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001059 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1060 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001061
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00001062 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
1063 QualType T);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001064};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001065
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001066class CGObjCMac : public CGObjCCommonMac {
1067private:
1068 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001069
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001070 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001071 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001072 void EmitModuleInfo();
1073
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001074 /// EmitModuleSymols - Emit module symbols, the list of defined
1075 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001076 llvm::Constant *EmitModuleSymbols();
1077
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001078 /// FinishModule - Write out global data structures at the end of
1079 /// processing a translation unit.
1080 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001081
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001082 /// EmitClassExtension - Generate the class extension structure used
1083 /// to store the weak ivar layout and properties. The return value
1084 /// has type ClassExtensionPtrTy.
1085 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1086
1087 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1088 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001090 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001091
John McCall31168b02011-06-15 23:02:42 +00001092 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1093 IdentifierInfo *II);
1094
1095 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1096
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001097 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1098 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001099
1100 /// EmitIvarList - Emit the ivar list for the given
1101 /// implementation. If ForClass is true the list of class ivars
1102 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1103 /// interface ivars will be emitted. The return value has type
1104 /// IvarListPtrTy.
1105 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001106 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001107
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001108 /// EmitMetaClass - Emit a forward reference to the class structure
1109 /// for the metaclass of the given interface. The return value has
1110 /// type ClassPtrTy.
1111 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1112
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001113 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001114 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001115 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1116 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001117 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001118
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001119 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001120
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001121 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001122
1123 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001124 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001125 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001126 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001127 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001128
1129 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001130 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001131 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001132 /// - IsProtocol: True iff these methods are for a protocol.
1133 /// - ClassMethds: True iff these are class methods.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001134 /// - Required: When true, only "required" methods are
1135 /// listed. Similarly, when false only "optional" methods are
1136 /// listed. For classes this should always be true.
1137 /// - begin, end: The method list to output.
1138 ///
1139 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001140 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001141 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001142 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001143
Daniel Dunbarc475d422008-10-29 22:36:39 +00001144 /// GetOrEmitProtocol - Get the protocol object for the given
1145 /// declaration, emitting it if necessary. The return value has type
1146 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001147 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001148
1149 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1150 /// object for the given declaration, emitting it if needed. These
1151 /// forward references will be filled in with empty bodies if no
1152 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001153 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001154
Daniel Dunbarb036db82008-08-13 03:21:16 +00001155 /// EmitProtocolExtension - Generate the protocol extension
1156 /// structure used to store optional instance and class methods, and
1157 /// protocol properties. The return value has type
1158 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001159 llvm::Constant *
1160 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001161 ArrayRef<llvm::Constant*> OptInstanceMethods,
1162 ArrayRef<llvm::Constant*> OptClassMethods,
1163 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001164
1165 /// EmitProtocolList - Generate the list of referenced
1166 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001167 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001168 ObjCProtocolDecl::protocol_iterator begin,
1169 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001170
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001171 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1172 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001173 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1174 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001175
1176public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001177 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001178
Fariborz Jahanian71394042009-01-23 23:53:38 +00001179 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001180
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001181 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001182 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001183 QualType ResultType,
1184 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001185 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001186 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001187 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001188 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001189
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001190 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001191 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001192 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001193 QualType ResultType,
1194 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001195 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001196 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001197 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001198 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001199 const CallArgList &CallArgs,
1200 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001201
Daniel Dunbarcb463852008-11-01 01:53:16 +00001202 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001203 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001204
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001205 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1206 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001207
1208 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1209 /// untyped one.
1210 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1211 const ObjCMethodDecl *Method);
1212
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001213 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001214
Daniel Dunbar92992502008-08-15 22:20:32 +00001215 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001216
Daniel Dunbar92992502008-08-15 22:20:32 +00001217 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001218
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001219 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001220
Daniel Dunbarcb463852008-11-01 01:53:16 +00001221 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001222 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001223
Chris Lattnerd4808922009-03-22 21:03:39 +00001224 virtual llvm::Constant *GetPropertyGetFunction();
1225 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001226 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1227 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001228 virtual llvm::Constant *GetGetStructFunction();
1229 virtual llvm::Constant *GetSetStructFunction();
David Chisnall0d75e062012-12-17 18:54:24 +00001230 virtual llvm::Constant *GetCppAtomicObjectGetFunction();
1231 virtual llvm::Constant *GetCppAtomicObjectSetFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001232 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001233
John McCallbd309292010-07-06 01:34:17 +00001234 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1235 const ObjCAtTryStmt &S);
1236 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1237 const ObjCAtSynchronizedStmt &S);
1238 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001239 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00001240 const ObjCAtThrowStmt &S,
1241 bool ClearInsertionPoint=true);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001242 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001244 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001245 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001246 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001247 llvm::Value *src, llvm::Value *dest,
1248 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001249 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001250 llvm::Value *src, llvm::Value *dest,
1251 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001252 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1253 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001254 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1255 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001256 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001257
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001258 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1259 QualType ObjectTy,
1260 llvm::Value *BaseValue,
1261 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001262 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001263 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001264 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001265 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001266
1267 /// GetClassGlobal - Return the global variable for the Objective-C
1268 /// class of the given name.
1269 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001270 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001271 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001272};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001274class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001275private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001276 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001277 llvm::GlobalVariable* ObjCEmptyCacheVar;
1278 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001280 /// SuperClassReferences - uniqued super class references.
1281 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001282
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001283 /// MetaClassReferences - uniqued meta class references.
1284 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001285
1286 /// EHTypeReferences - uniqued class ehtype references.
1287 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001288
John McCall9e8bb002011-05-14 03:10:52 +00001289 /// VTableDispatchMethods - List of methods for which we generate
1290 /// vtable-based message dispatch.
1291 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292
Fariborz Jahanian67260552009-11-17 21:37:35 +00001293 /// DefinedMetaClasses - List of defined meta-classes.
1294 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1295
John McCall9e8bb002011-05-14 03:10:52 +00001296 /// isVTableDispatchedSelector - Returns true if SEL is a
1297 /// vtable-based selector.
1298 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299
Fariborz Jahanian71394042009-01-23 23:53:38 +00001300 /// FinishNonFragileABIModule - Write out global data structures at the end of
1301 /// processing a translation unit.
1302 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001303
Daniel Dunbar19573e72009-05-15 21:48:48 +00001304 /// AddModuleClassList - Add the given list of class pointers to the
1305 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001306 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001307 const char *SymbolName,
1308 const char *SectionName);
1309
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1311 unsigned InstanceStart,
1312 unsigned InstanceSize,
1313 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001314 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001315 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001316 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001317 llvm::Constant *ClassRoGV,
1318 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001319
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001320 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001321
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001322 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001324 /// EmitMethodList - Emit the method list for the given
1325 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001326 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001327 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001328 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001329 /// EmitIvarList - Emit the ivar list for the given
1330 /// implementation. If ForClass is true the list of class ivars
1331 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1332 /// interface ivars will be emitted. The return value has type
1333 /// IvarListnfABIPtrTy.
1334 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001336 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001337 const ObjCIvarDecl *Ivar,
Eli Friedman8cbca202012-11-06 22:15:52 +00001338 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001339
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001340 /// GetOrEmitProtocol - Get the protocol object for the given
1341 /// declaration, emitting it if necessary. The return value has type
1342 /// ProtocolPtrTy.
1343 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001344
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001345 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1346 /// object for the given declaration, emitting it if needed. These
1347 /// forward references will be filled in with empty bodies if no
1348 /// definition is seen. The return value has type ProtocolPtrTy.
1349 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001350
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001351 /// EmitProtocolList - Generate the list of referenced
1352 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001353 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001354 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001355 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356
John McCall9e8bb002011-05-14 03:10:52 +00001357 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1358 ReturnValueSlot Return,
1359 QualType ResultType,
1360 Selector Sel,
1361 llvm::Value *Receiver,
1362 QualType Arg0Ty,
1363 bool IsSuper,
1364 const CallArgList &CallArgs,
1365 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001366
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001367 /// GetClassGlobal - Return the global variable for the Objective-C
1368 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001369 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001370
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001371 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001372 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001373 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001374 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001375
1376 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1377 IdentifierInfo *II);
1378
1379 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001380
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001381 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1382 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001383 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1384 const ObjCInterfaceDecl *ID);
1385
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001386 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1387 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001388 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001389 const ObjCInterfaceDecl *ID);
1390
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001391 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1392 /// the given ivar.
1393 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001394 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001395 const ObjCInterfaceDecl *ID,
1396 const ObjCIvarDecl *Ivar);
1397
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001398 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1399 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001400 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1401 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001402
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001403 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001404 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001405 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001406 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001407
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001408 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001409 return "OBJC_METACLASS_$_";
1410 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001411
Daniel Dunbar15894b72009-04-07 05:48:37 +00001412 const char *getClassSymbolPrefix() const {
1413 return "OBJC_CLASS_$_";
1414 }
1415
Daniel Dunbar961202372009-05-03 12:57:56 +00001416 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001417 uint32_t &InstanceStart,
1418 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001419
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001420 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001421 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001422 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1423 return CGM.getContext().Selectors.getSelector(0, &II);
1424 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001425
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001426 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001427 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1428 return CGM.getContext().Selectors.getSelector(1, &II);
1429 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001430
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001431 /// ImplementationIsNonLazy - Check whether the given category or
1432 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001433 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001434
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00001435 bool IsIvarOffsetKnownIdempotent(const CodeGen::CodeGenFunction &CGF,
1436 const ObjCInterfaceDecl *ID,
1437 const ObjCIvarDecl *IV) {
1438 // Annotate the load as an invariant load iff the object type is the type,
1439 // or a derived type, of the class containing the ivar within an ObjC
1440 // method. This check is needed because the ivar offset is a lazily
1441 // initialised value that may depend on objc_msgSend to perform a fixup on
1442 // the first message dispatch.
1443 //
1444 // An additional opportunity to mark the load as invariant arises when the
1445 // base of the ivar access is a parameter to an Objective C method.
1446 // However, because the parameters are not available in the current
1447 // interface, we cannot perform this check.
Douglas Gregor1c15cd62013-02-18 15:59:24 +00001448 if (CGF.CurFuncDecl && isa<ObjCMethodDecl>(CGF.CurFuncDecl))
1449 if (IV->getContainingInterface()->isSuperClassOf(ID))
1450 return true;
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00001451 return false;
1452 }
1453
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001454public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001455 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001456 // FIXME. All stubs for now!
1457 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001458
Fariborz Jahanian71394042009-01-23 23:53:38 +00001459 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001460 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001461 QualType ResultType,
1462 Selector Sel,
1463 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001464 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001465 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001466 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001467
1468 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001469 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001470 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001471 QualType ResultType,
1472 Selector Sel,
1473 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001474 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001475 llvm::Value *Receiver,
1476 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001477 const CallArgList &CallArgs,
1478 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001479
Fariborz Jahanian71394042009-01-23 23:53:38 +00001480 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001481 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001482
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001483 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1484 bool lvalue = false)
1485 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001486
1487 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1488 /// untyped one.
1489 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1490 const ObjCMethodDecl *Method)
1491 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001492
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001493 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001494
Fariborz Jahanian71394042009-01-23 23:53:38 +00001495 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001496
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001497 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001498
Fariborz Jahanian71394042009-01-23 23:53:38 +00001499 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001500 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001501
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001502 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001503
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001504 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001505 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001506 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001507 virtual llvm::Constant *GetPropertySetFunction() {
1508 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001509 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001510
Ted Kremeneke65b0862012-03-06 20:05:56 +00001511 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1512 bool copy) {
1513 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1514 }
1515
David Chisnall168b80f2010-12-26 22:13:16 +00001516 virtual llvm::Constant *GetSetStructFunction() {
1517 return ObjCTypes.getCopyStructFn();
1518 }
1519 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001520 return ObjCTypes.getCopyStructFn();
1521 }
David Chisnall0d75e062012-12-17 18:54:24 +00001522 virtual llvm::Constant *GetCppAtomicObjectSetFunction() {
1523 return ObjCTypes.getCppAtomicObjectFunction();
1524 }
1525 virtual llvm::Constant *GetCppAtomicObjectGetFunction() {
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001526 return ObjCTypes.getCppAtomicObjectFunction();
1527 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001528
Chris Lattnerd4808922009-03-22 21:03:39 +00001529 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001530 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001531 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001532
John McCallbd309292010-07-06 01:34:17 +00001533 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1534 const ObjCAtTryStmt &S);
1535 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1536 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001537 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00001538 const ObjCAtThrowStmt &S,
1539 bool ClearInsertionPoint=true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001540 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001541 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001542 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001543 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001544 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001545 llvm::Value *src, llvm::Value *dest,
1546 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001547 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001548 llvm::Value *src, llvm::Value *dest,
1549 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001550 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001551 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001552 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1553 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001554 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001555 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1556 QualType ObjectTy,
1557 llvm::Value *BaseValue,
1558 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001559 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001560 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001561 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001562 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001563};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001564
1565/// A helper class for performing the null-initialization of a return
1566/// value.
1567struct NullReturnState {
1568 llvm::BasicBlock *NullBB;
John McCall3d1e2c92013-02-12 05:53:35 +00001569 NullReturnState() : NullBB(0) {}
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001570
John McCall3d1e2c92013-02-12 05:53:35 +00001571 /// Perform a null-check of the given receiver.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001572 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
John McCall3d1e2c92013-02-12 05:53:35 +00001573 // Make blocks for the null-receiver and call edges.
1574 NullBB = CGF.createBasicBlock("msgSend.null-receiver");
1575 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001576
1577 // Check for a null receiver and, if there is one, jump to the
John McCall3d1e2c92013-02-12 05:53:35 +00001578 // null-receiver block. There's no point in trying to avoid it:
1579 // we're always going to put *something* there, because otherwise
1580 // we shouldn't have done this null-check in the first place.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001581 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1582 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1583
1584 // Otherwise, start performing the call.
1585 CGF.EmitBlock(callBB);
1586 }
1587
John McCall3d1e2c92013-02-12 05:53:35 +00001588 /// Complete the null-return operation. It is valid to call this
1589 /// regardless of whether 'init' has been called.
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001590 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1591 const CallArgList &CallArgs,
1592 const ObjCMethodDecl *Method) {
John McCall3d1e2c92013-02-12 05:53:35 +00001593 // If we never had to do a null-check, just use the raw result.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001594 if (!NullBB) return result;
John McCall3d1e2c92013-02-12 05:53:35 +00001595
1596 // The continuation block. This will be left null if we don't have an
1597 // IP, which can happen if the method we're calling is marked noreturn.
1598 llvm::BasicBlock *contBB = 0;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001599
John McCall3d1e2c92013-02-12 05:53:35 +00001600 // Finish the call path.
1601 llvm::BasicBlock *callBB = CGF.Builder.GetInsertBlock();
1602 if (callBB) {
1603 contBB = CGF.createBasicBlock("msgSend.cont");
1604 CGF.Builder.CreateBr(contBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001605 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001606
John McCall3d1e2c92013-02-12 05:53:35 +00001607 // Okay, start emitting the null-receiver block.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001608 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001609
John McCall3d1e2c92013-02-12 05:53:35 +00001610 // Release any consumed arguments we've got.
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001611 if (Method) {
1612 CallArgList::const_iterator I = CallArgs.begin();
1613 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1614 e = Method->param_end(); i != e; ++i, ++I) {
1615 const ParmVarDecl *ParamDecl = (*i);
1616 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1617 RValue RV = I->RV;
1618 assert(RV.isScalar() &&
1619 "NullReturnState::complete - arg not on object");
1620 CGF.EmitARCRelease(RV.getScalarVal(), true);
1621 }
1622 }
1623 }
John McCall3d1e2c92013-02-12 05:53:35 +00001624
1625 // The phi code below assumes that we haven't needed any control flow yet.
1626 assert(CGF.Builder.GetInsertBlock() == NullBB);
1627
1628 // If we've got a void return, just jump to the continuation block.
1629 if (result.isScalar() && resultType->isVoidType()) {
1630 // No jumps required if the message-send was noreturn.
1631 if (contBB) CGF.EmitBlock(contBB);
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001632 return result;
1633 }
1634
John McCall3d1e2c92013-02-12 05:53:35 +00001635 // If we've got a scalar return, build a phi.
1636 if (result.isScalar()) {
1637 // Derive the null-initialization value.
1638 llvm::Constant *null = CGF.CGM.EmitNullConstant(resultType);
1639
1640 // If no join is necessary, just flow out.
1641 if (!contBB) return RValue::get(null);
1642
1643 // Otherwise, build a phi.
1644 CGF.EmitBlock(contBB);
1645 llvm::PHINode *phi = CGF.Builder.CreatePHI(null->getType(), 2);
1646 phi->addIncoming(result.getScalarVal(), callBB);
1647 phi->addIncoming(null, NullBB);
1648 return RValue::get(phi);
1649 }
1650
1651 // If we've got an aggregate return, null the buffer out.
1652 // FIXME: maybe we should be doing things differently for all the
1653 // cases where the ABI has us returning (1) non-agg values in
1654 // memory or (2) agg values in registers.
1655 if (result.isAggregate()) {
1656 assert(result.isAggregate() && "null init of non-aggregate result?");
1657 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1658 if (contBB) CGF.EmitBlock(contBB);
1659 return result;
1660 }
1661
1662 // Complex types.
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001663 CGF.EmitBlock(contBB);
John McCall3d1e2c92013-02-12 05:53:35 +00001664 CodeGenFunction::ComplexPairTy callResult = result.getComplexVal();
1665
1666 // Find the scalar type and its zero value.
1667 llvm::Type *scalarTy = callResult.first->getType();
1668 llvm::Constant *scalarZero = llvm::Constant::getNullValue(scalarTy);
1669
1670 // Build phis for both coordinates.
1671 llvm::PHINode *real = CGF.Builder.CreatePHI(scalarTy, 2);
1672 real->addIncoming(callResult.first, callBB);
1673 real->addIncoming(scalarZero, NullBB);
1674 llvm::PHINode *imag = CGF.Builder.CreatePHI(scalarTy, 2);
1675 imag->addIncoming(callResult.second, callBB);
1676 imag->addIncoming(scalarZero, NullBB);
1677 return RValue::getComplex(real, imag);
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001678 }
1679};
1680
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001681} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001682
1683/* *** Helper Functions *** */
1684
1685/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001686static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001687 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001688 unsigned idx0,
1689 unsigned idx1) {
1690 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001691 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1692 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001693 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001694 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001695}
1696
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001697/// hasObjCExceptionAttribute - Return true if this class or any super
1698/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001699static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001700 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001701 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001702 return true;
1703 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001704 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001705 return false;
1706}
1707
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001708/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001709
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001710CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001711 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001712 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001713 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001714}
1715
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001716/// GetClass - Return a reference to the class for the given interface
1717/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001718llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001719 const ObjCInterfaceDecl *ID) {
1720 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001721}
1722
1723/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001724llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1725 bool lval) {
1726 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001727}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001728llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001729 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001730 return EmitSelector(Builder, Method->getSelector());
1731}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001732
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001733llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001734 if (T->isObjCIdType() ||
1735 T->isObjCQualifiedIdType()) {
1736 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001737 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001738 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001739 if (T->isObjCClassType() ||
1740 T->isObjCQualifiedClassType()) {
1741 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001742 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001743 }
1744 if (T->isObjCObjectPointerType())
1745 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1746
John McCall2ca705e2010-07-24 00:37:23 +00001747 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001748}
1749
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001750/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001751/*
1752 struct __builtin_CFString {
1753 const int *isa; // point to __CFConstantStringClassReference
1754 int flags;
1755 const char *str;
1756 long length;
1757 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001758*/
1759
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001760/// or Generate a constant NSString object.
1761/*
1762 struct __builtin_NSString {
1763 const int *isa; // point to __NSConstantStringClassReference
1764 const char *str;
1765 unsigned int length;
1766 };
1767*/
1768
Fariborz Jahanian71394042009-01-23 23:53:38 +00001769llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001770 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001771 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001772 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001773 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001774}
1775
Ted Kremeneke65b0862012-03-06 20:05:56 +00001776enum {
1777 kCFTaggedObjectID_Integer = (1 << 1) + 1
1778};
1779
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001780/// Generates a message send where the super is the receiver. This is
1781/// a message send to self with special delivery semantics indicating
1782/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001783CodeGen::RValue
1784CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001785 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001786 QualType ResultType,
1787 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001788 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001789 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001790 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001791 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001792 const CodeGen::CallArgList &CallArgs,
1793 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001794 // Create and init a super structure; this is a (receiver, class)
1795 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001796 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001797 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001798 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001799 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001800 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001801 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001802
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001803 // If this is a class message the metaclass is passed as the target.
1804 llvm::Value *Target;
1805 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001806 if (isCategoryImpl) {
1807 // Message sent to 'super' in a class method defined in a category
1808 // implementation requires an odd treatment.
1809 // If we are in a class method, we must retrieve the
1810 // _metaclass_ for the current class, pointed at by
1811 // the class's "isa" pointer. The following assumes that
1812 // isa" is the first ivar in a class (which it must be).
1813 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1814 Target = CGF.Builder.CreateStructGEP(Target, 0);
1815 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001816 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001817 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1818 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1819 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1820 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001821 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001822 }
1823 else if (isCategoryImpl)
1824 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1825 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001826 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1827 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1828 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001829 }
Mike Stump18bb9282009-05-16 07:57:57 +00001830 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1831 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001832 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001833 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001834 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001835 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001836 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001837 return EmitMessageSend(CGF, Return, ResultType,
1838 EmitSelector(CGF.Builder, Sel),
1839 ObjCSuper, ObjCTypes.SuperPtrCTy,
1840 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001841}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001842
1843/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001844CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001845 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001846 QualType ResultType,
1847 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001848 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001849 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001850 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001851 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001852 return EmitMessageSend(CGF, Return, ResultType,
1853 EmitSelector(CGF.Builder, Sel),
1854 Receiver, CGF.getContext().getObjCIdType(),
1855 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001856}
1857
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001858CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001859CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1860 ReturnValueSlot Return,
1861 QualType ResultType,
1862 llvm::Value *Sel,
1863 llvm::Value *Arg0,
1864 QualType Arg0Ty,
1865 bool IsSuper,
1866 const CallArgList &CallArgs,
1867 const ObjCMethodDecl *Method,
1868 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001869 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001870 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001871 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001872 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1873 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001874 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001875
John McCalla729c622012-02-17 03:33:10 +00001876 // If we're calling a method, use the formal signature.
1877 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001878
Anders Carlsson280e61f12010-06-21 20:59:55 +00001879 if (Method)
1880 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1881 CGM.getContext().getCanonicalType(ResultType) &&
1882 "Result type mismatch!");
1883
John McCall5880fb82011-05-14 21:12:11 +00001884 NullReturnState nullReturn;
1885
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001886 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001887 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001888 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001889 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001890 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001891 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1892 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1893 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001894 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1895 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1896 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001897 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001898 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001899 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001900 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001901
1902 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001903 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001904 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1905 e = Method->param_end(); i != e; ++i) {
1906 const ParmVarDecl *ParamDecl = (*i);
1907 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1908 if (!nullReturn.NullBB)
1909 nullReturn.init(CGF, Arg0);
1910 requiresnullCheck = true;
1911 break;
1912 }
1913 }
1914
John McCalla729c622012-02-17 03:33:10 +00001915 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1916 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001917 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1918 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001919}
1920
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001921static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1922 if (FQT.isObjCGCStrong())
1923 return Qualifiers::Strong;
1924
John McCall31168b02011-06-15 23:02:42 +00001925 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001926 return Qualifiers::Weak;
1927
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001928 // check for __unsafe_unretained
1929 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1930 return Qualifiers::GCNone;
1931
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001932 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1933 return Qualifiers::Strong;
1934
1935 if (const PointerType *PT = FQT->getAs<PointerType>())
1936 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1937
1938 return Qualifiers::GCNone;
1939}
1940
John McCall351762c2011-02-07 10:33:21 +00001941llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1942 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001943
Chris Lattnerece04092012-02-07 00:39:47 +00001944 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001945 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1946 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001947 return nullPtr;
1948
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001949 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001950 SkipIvars.clear();
1951 IvarsInfo.clear();
Eli Friedman8cbca202012-11-06 22:15:52 +00001952 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1953 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001954
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001955 // __isa is the first field in block descriptor and must assume by runtime's
1956 // convention that it is GC'able.
Eli Friedman8cbca202012-11-06 22:15:52 +00001957 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001958
1959 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1960
1961 // Calculate the basic layout of the block structure.
1962 const llvm::StructLayout *layout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001963 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall351762c2011-02-07 10:33:21 +00001964
1965 // Ignore the optional 'this' capture: C++ objects are not assumed
1966 // to be GC'ed.
1967
1968 // Walk the captured variables.
1969 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1970 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1971 const VarDecl *variable = ci->getVariable();
1972 QualType type = variable->getType();
1973
1974 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1975
1976 // Ignore constant captures.
1977 if (capture.isConstant()) continue;
1978
Eli Friedman8cbca202012-11-06 22:15:52 +00001979 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
John McCall351762c2011-02-07 10:33:21 +00001980
1981 // __block variables are passed by their descriptor address.
1982 if (ci->isByRef()) {
Eli Friedman8cbca202012-11-06 22:15:52 +00001983 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001984 continue;
John McCall351762c2011-02-07 10:33:21 +00001985 }
1986
1987 assert(!type->isArrayType() && "array variable should not be caught");
1988 if (const RecordType *record = type->getAs<RecordType>()) {
1989 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001990 continue;
1991 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001992
John McCall351762c2011-02-07 10:33:21 +00001993 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
Eli Friedman8cbca202012-11-06 22:15:52 +00001994 unsigned fieldSize = CGM.getContext().getTypeSize(type);
John McCall351762c2011-02-07 10:33:21 +00001995
1996 if (GCAttr == Qualifiers::Strong)
Eli Friedman8cbca202012-11-06 22:15:52 +00001997 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1998 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001999 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
Eli Friedman8cbca202012-11-06 22:15:52 +00002000 SkipIvars.push_back(GC_IVAR(fieldOffset,
2001 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002002 }
2003
2004 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00002005 return nullPtr;
2006
2007 // Sort on byte position; captures might not be allocated in order,
2008 // and unions can do funny things.
2009 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
2010 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002011
2012 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00002013 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002014 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002015 printf("\n block variable layout for block: ");
Roman Divackye6377112012-09-06 15:59:27 +00002016 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00002017 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00002018 if (!(s[i] & 0xf0))
2019 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
2020 else
2021 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
2022 printf("\n");
2023 }
2024
2025 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00002026}
2027
Fariborz Jahanian2c96d302012-11-04 18:19:40 +00002028/// getBlockCaptureLifetime - This routine returns life time of the captured
2029/// block variable for the purpose of block layout meta-data generation. FQT is
2030/// the type of the variable captured in the block.
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002031Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT,
2032 bool ByrefLayout) {
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002033 if (CGM.getLangOpts().ObjCAutoRefCount)
2034 return FQT.getObjCLifetime();
2035
Fariborz Jahanian2c96d302012-11-04 18:19:40 +00002036 // MRR.
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002037 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002038 return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong;
Fariborz Jahanian2dd78192012-11-02 22:51:18 +00002039
2040 return Qualifiers::OCL_None;
2041}
2042
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002043void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
2044 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002045 CharUnits FieldOffset,
2046 CharUnits FieldSize) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002047 // __block variables are passed by their descriptor address.
2048 if (IsByref)
2049 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002050 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002051 else if (LifeTime == Qualifiers::OCL_Strong)
2052 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002053 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002054 else if (LifeTime == Qualifiers::OCL_Weak)
2055 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002056 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002057 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2058 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002059 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002060 else
2061 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2062 FieldOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002063 FieldSize));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002064}
2065
2066void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2067 const RecordDecl *RD,
2068 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002069 CharUnits BytePos, bool &HasUnion,
2070 bool ByrefLayout) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002071 bool IsUnion = (RD && RD->isUnion());
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002072 CharUnits MaxUnionSize = CharUnits::Zero();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002073 const FieldDecl *MaxField = 0;
2074 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002075 CharUnits MaxFieldOffset = CharUnits::Zero();
2076 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002077
2078 if (RecFields.empty())
2079 return;
2080 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2081
2082 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2083 const FieldDecl *Field = RecFields[i];
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002084 // Note that 'i' here is actually the field index inside RD of Field,
2085 // although this dependency is hidden.
2086 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002087 CharUnits FieldOffset =
2088 CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i));
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002089
2090 // Skip over unnamed or bitfields
2091 if (!Field->getIdentifier() || Field->isBitField()) {
2092 LastFieldBitfieldOrUnnamed = Field;
2093 LastBitfieldOrUnnamedOffset = FieldOffset;
2094 continue;
2095 }
2096
2097 LastFieldBitfieldOrUnnamed = 0;
2098 QualType FQT = Field->getType();
2099 if (FQT->isRecordType() || FQT->isUnionType()) {
2100 if (FQT->isUnionType())
2101 HasUnion = true;
2102
2103 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2104 BytePos + FieldOffset, HasUnion);
2105 continue;
2106 }
2107
2108 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2109 const ConstantArrayType *CArray =
2110 dyn_cast_or_null<ConstantArrayType>(Array);
2111 uint64_t ElCount = CArray->getSize().getZExtValue();
2112 assert(CArray && "only array with known element size is supported");
2113 FQT = CArray->getElementType();
2114 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2115 const ConstantArrayType *CArray =
2116 dyn_cast_or_null<ConstantArrayType>(Array);
2117 ElCount *= CArray->getSize().getZExtValue();
2118 FQT = CArray->getElementType();
2119 }
2120
2121 assert(!FQT->isUnionType() &&
2122 "layout for array of unions not supported");
2123 if (FQT->isRecordType() && ElCount) {
2124 int OldIndex = RunSkipBlockVars.size() - 1;
2125 const RecordType *RT = FQT->getAs<RecordType>();
2126 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2127 HasUnion);
2128
2129 // Replicate layout information for each array element. Note that
2130 // one element is already done.
2131 uint64_t ElIx = 1;
2132 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002133 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002134 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2135 RunSkipBlockVars.push_back(
2136 RUN_SKIP(RunSkipBlockVars[i].opcode,
2137 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2138 RunSkipBlockVars[i].block_var_size));
2139 }
2140 continue;
2141 }
2142 }
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002143 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType());
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002144 if (IsUnion) {
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002145 CharUnits UnionIvarSize = FieldSize;
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002146 if (UnionIvarSize > MaxUnionSize) {
2147 MaxUnionSize = UnionIvarSize;
2148 MaxField = Field;
2149 MaxFieldOffset = FieldOffset;
2150 }
2151 } else {
2152 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002153 getBlockCaptureLifetime(FQT, ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002154 BytePos + FieldOffset,
2155 FieldSize);
2156 }
2157 }
2158
2159 if (LastFieldBitfieldOrUnnamed) {
2160 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2161 // Last field was a bitfield. Must update the info.
2162 uint64_t BitFieldSize
2163 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002164 unsigned UnsSize = (BitFieldSize / ByteSizeInBits) +
Eli Friedman8cbca202012-11-06 22:15:52 +00002165 ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002166 CharUnits Size = CharUnits::fromQuantity(UnsSize);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002167 Size += LastBitfieldOrUnnamedOffset;
2168 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002169 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2170 ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002171 BytePos + LastBitfieldOrUnnamedOffset,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002172 Size);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002173 } else {
2174 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2175 // Last field was unnamed. Must update skip info.
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002176 CharUnits FieldSize
2177 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002178 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002179 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2180 ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002181 BytePos + LastBitfieldOrUnnamedOffset,
2182 FieldSize);
2183 }
2184 }
2185
2186 if (MaxField)
2187 UpdateRunSkipBlockVars(false,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002188 getBlockCaptureLifetime(MaxField->getType(), ByrefLayout),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002189 BytePos + MaxFieldOffset,
2190 MaxUnionSize);
2191}
2192
2193void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002194 CharUnits BytePos,
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002195 bool &HasUnion,
2196 bool ByrefLayout) {
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002197 const RecordDecl *RD = RT->getDecl();
2198 SmallVector<const FieldDecl*, 16> Fields;
2199 for (RecordDecl::field_iterator i = RD->field_begin(),
2200 e = RD->field_end(); i != e; ++i)
2201 Fields.push_back(*i);
2202 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2203 const llvm::StructLayout *RecLayout =
2204 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2205
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002206 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002207}
2208
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002209/// InlineLayoutInstruction - This routine produce an inline instruction for the
2210/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2211/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2212/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2213/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2214/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2215/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2216/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2217/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2218uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2219 SmallVectorImpl<unsigned char> &Layout) {
2220 uint64_t Result = 0;
2221 if (Layout.size() <= 3) {
2222 unsigned size = Layout.size();
2223 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2224 unsigned char inst;
2225 enum BLOCK_LAYOUT_OPCODE opcode ;
2226 switch (size) {
2227 case 3:
2228 inst = Layout[0];
2229 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2230 if (opcode == BLOCK_LAYOUT_STRONG)
2231 strong_word_count = (inst & 0xF)+1;
2232 else
2233 return 0;
2234 inst = Layout[1];
2235 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2236 if (opcode == BLOCK_LAYOUT_BYREF)
2237 byref_word_count = (inst & 0xF)+1;
2238 else
2239 return 0;
2240 inst = Layout[2];
2241 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2242 if (opcode == BLOCK_LAYOUT_WEAK)
2243 weak_word_count = (inst & 0xF)+1;
2244 else
2245 return 0;
2246 break;
2247
2248 case 2:
2249 inst = Layout[0];
2250 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2251 if (opcode == BLOCK_LAYOUT_STRONG) {
2252 strong_word_count = (inst & 0xF)+1;
2253 inst = Layout[1];
2254 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2255 if (opcode == BLOCK_LAYOUT_BYREF)
2256 byref_word_count = (inst & 0xF)+1;
2257 else if (opcode == BLOCK_LAYOUT_WEAK)
2258 weak_word_count = (inst & 0xF)+1;
2259 else
2260 return 0;
2261 }
2262 else if (opcode == BLOCK_LAYOUT_BYREF) {
2263 byref_word_count = (inst & 0xF)+1;
2264 inst = Layout[1];
2265 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2266 if (opcode == BLOCK_LAYOUT_WEAK)
2267 weak_word_count = (inst & 0xF)+1;
2268 else
2269 return 0;
2270 }
2271 else
2272 return 0;
2273 break;
2274
2275 case 1:
2276 inst = Layout[0];
2277 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2278 if (opcode == BLOCK_LAYOUT_STRONG)
2279 strong_word_count = (inst & 0xF)+1;
2280 else if (opcode == BLOCK_LAYOUT_BYREF)
2281 byref_word_count = (inst & 0xF)+1;
2282 else if (opcode == BLOCK_LAYOUT_WEAK)
2283 weak_word_count = (inst & 0xF)+1;
2284 else
2285 return 0;
2286 break;
2287
2288 default:
2289 return 0;
2290 }
2291
2292 // Cannot inline when any of the word counts is 15. Because this is one less
2293 // than the actual work count (so 15 means 16 actual word counts),
2294 // and we can only display 0 thru 15 word counts.
2295 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2296 return 0;
2297
2298 unsigned count =
2299 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2300
2301 if (size == count) {
2302 if (strong_word_count)
2303 Result = strong_word_count;
2304 Result <<= 4;
2305 if (byref_word_count)
2306 Result += byref_word_count;
2307 Result <<= 4;
2308 if (weak_word_count)
2309 Result += weak_word_count;
2310 }
2311 }
2312 return Result;
2313}
2314
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002315llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) {
2316 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2317 if (RunSkipBlockVars.empty())
2318 return nullPtr;
2319 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2320 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2321 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2322
2323 // Sort on byte position; captures might not be allocated in order,
2324 // and unions can do funny things.
2325 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
2326 SmallVector<unsigned char, 16> Layout;
2327
2328 unsigned size = RunSkipBlockVars.size();
2329 for (unsigned i = 0; i < size; i++) {
2330 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2331 CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2332 CharUnits end_byte_pos = start_byte_pos;
2333 unsigned j = i+1;
2334 while (j < size) {
2335 if (opcode == RunSkipBlockVars[j].opcode) {
2336 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2337 i++;
2338 }
2339 else
2340 break;
2341 }
2342 CharUnits size_in_bytes =
2343 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
2344 if (j < size) {
2345 CharUnits gap =
2346 RunSkipBlockVars[j].block_var_bytepos -
2347 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2348 size_in_bytes += gap;
2349 }
2350 CharUnits residue_in_bytes = CharUnits::Zero();
2351 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2352 residue_in_bytes = size_in_bytes % WordSizeInBytes;
2353 size_in_bytes -= residue_in_bytes;
2354 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2355 }
2356
2357 unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes;
2358 while (size_in_words >= 16) {
2359 // Note that value in imm. is one less that the actual
2360 // value. So, 0xf means 16 words follow!
2361 unsigned char inst = (opcode << 4) | 0xf;
2362 Layout.push_back(inst);
2363 size_in_words -= 16;
2364 }
2365 if (size_in_words > 0) {
2366 // Note that value in imm. is one less that the actual
2367 // value. So, we subtract 1 away!
2368 unsigned char inst = (opcode << 4) | (size_in_words-1);
2369 Layout.push_back(inst);
2370 }
2371 if (residue_in_bytes > CharUnits::Zero()) {
2372 unsigned char inst =
2373 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1);
2374 Layout.push_back(inst);
2375 }
2376 }
2377
2378 int e = Layout.size()-1;
2379 while (e >= 0) {
2380 unsigned char inst = Layout[e--];
2381 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2382 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2383 Layout.pop_back();
2384 else
2385 break;
2386 }
2387
2388 uint64_t Result = InlineLayoutInstruction(Layout);
2389 if (Result != 0) {
2390 // Block variable layout instruction has been inlined.
2391 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2392 if (ComputeByrefLayout)
2393 printf("\n Inline instruction for BYREF variable layout: ");
2394 else
2395 printf("\n Inline instruction for block variable layout: ");
2396 printf("0x0%llx\n", (unsigned long long)Result);
2397 }
2398 if (WordSizeInBytes == 8) {
2399 const llvm::APInt Instruction(64, Result);
2400 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2401 }
2402 else {
2403 const llvm::APInt Instruction(32, Result);
2404 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2405 }
2406 }
2407
2408 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2409 Layout.push_back(inst);
2410 std::string BitMap;
2411 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2412 BitMap += Layout[i];
2413
2414 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2415 if (ComputeByrefLayout)
2416 printf("\n BYREF variable layout: ");
2417 else
2418 printf("\n block variable layout: ");
2419 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2420 unsigned char inst = BitMap[i];
2421 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2422 unsigned delta = 1;
2423 switch (opcode) {
2424 case BLOCK_LAYOUT_OPERATOR:
2425 printf("BL_OPERATOR:");
2426 delta = 0;
2427 break;
2428 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2429 printf("BL_NON_OBJECT_BYTES:");
2430 break;
2431 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2432 printf("BL_NON_OBJECT_WORD:");
2433 break;
2434 case BLOCK_LAYOUT_STRONG:
2435 printf("BL_STRONG:");
2436 break;
2437 case BLOCK_LAYOUT_BYREF:
2438 printf("BL_BYREF:");
2439 break;
2440 case BLOCK_LAYOUT_WEAK:
2441 printf("BL_WEAK:");
2442 break;
2443 case BLOCK_LAYOUT_UNRETAINED:
2444 printf("BL_UNRETAINED:");
2445 break;
2446 }
2447 // Actual value of word count is one more that what is in the imm.
2448 // field of the instruction
2449 printf("%d", (inst & 0xf) + delta);
2450 if (i < e-1)
2451 printf(", ");
2452 else
2453 printf("\n");
2454 }
2455 }
2456
2457 llvm::GlobalVariable * Entry =
2458 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2459 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2460 "__TEXT,__objc_classname,cstring_literals", 1, true);
2461 return getConstantGEP(VMContext, Entry, 0, 0);
2462}
2463
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002464llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2465 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002466 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2467
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002468 RunSkipBlockVars.clear();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002469 bool hasUnion = false;
2470
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002471 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2472 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2473 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2474
2475 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2476
2477 // Calculate the basic layout of the block structure.
2478 const llvm::StructLayout *layout =
2479 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2480
2481 // Ignore the optional 'this' capture: C++ objects are not assumed
2482 // to be GC'ed.
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00002483 if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero())
2484 UpdateRunSkipBlockVars(false, Qualifiers::OCL_None,
2485 blockInfo.BlockHeaderForcedGapOffset,
2486 blockInfo.BlockHeaderForcedGapSize);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002487 // Walk the captured variables.
2488 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2489 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2490 const VarDecl *variable = ci->getVariable();
2491 QualType type = variable->getType();
2492
2493 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2494
2495 // Ignore constant captures.
2496 if (capture.isConstant()) continue;
2497
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002498 CharUnits fieldOffset =
2499 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002500
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002501 assert(!type->isArrayType() && "array variable should not be caught");
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002502 if (!ci->isByRef())
2503 if (const RecordType *record = type->getAs<RecordType>()) {
2504 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
2505 continue;
2506 }
Fariborz Jahanian7778d612012-11-07 20:00:32 +00002507 CharUnits fieldSize;
2508 if (ci->isByRef())
2509 fieldSize = CharUnits::fromQuantity(WordSizeInBytes);
2510 else
2511 fieldSize = CGM.getContext().getTypeSizeInChars(type);
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002512 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false),
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002513 fieldOffset, fieldSize);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002514 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002515 return getBitmapBlockLayout(false);
2516}
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002517
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002518
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002519llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM,
2520 QualType T) {
2521 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2522 assert(!T->isArrayType() && "__block array variable should not be caught");
2523 CharUnits fieldOffset;
2524 RunSkipBlockVars.clear();
2525 bool hasUnion = false;
2526 if (const RecordType *record = T->getAs<RecordType>()) {
2527 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */);
2528 llvm::Constant *Result = getBitmapBlockLayout(true);
2529 return Result;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002530 }
Fariborz Jahaniana9d44642012-11-14 17:15:51 +00002531 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2532 return nullPtr;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002533}
2534
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002535llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00002536 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00002537 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002538 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00002539 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2540
Owen Andersonade90fd2009-07-29 18:54:39 +00002541 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00002542 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002543}
2544
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002545void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00002546 // FIXME: We shouldn't need this, the protocol decl should contain enough
2547 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00002548 DefinedProtocols.insert(PD->getIdentifier());
2549
2550 // If we have generated a forward reference to this protocol, emit
2551 // it now. Otherwise do nothing, the protocol objects are lazily
2552 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002553 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00002554 GetOrEmitProtocol(PD);
2555}
2556
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002557llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002558 if (DefinedProtocols.count(PD->getIdentifier()))
2559 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002560
Daniel Dunbarc475d422008-10-29 22:36:39 +00002561 return GetOrEmitProtocolRef(PD);
2562}
2563
Daniel Dunbarb036db82008-08-13 03:21:16 +00002564/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002565// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2566struct _objc_protocol {
2567struct _objc_protocol_extension *isa;
2568char *protocol_name;
2569struct _objc_protocol_list *protocol_list;
2570struct _objc__method_prototype_list *instance_methods;
2571struct _objc__method_prototype_list *class_methods
2572};
Daniel Dunbarb036db82008-08-13 03:21:16 +00002573
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002574See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00002575*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00002576llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00002577 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00002578
2579 // Early exit if a defining object has already been generated.
2580 if (Entry && Entry->hasInitializer())
2581 return Entry;
2582
Douglas Gregora715bff2012-01-01 19:51:50 +00002583 // Use the protocol definition, if there is one.
2584 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2585 PD = Def;
2586
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002587 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002588 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002589 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2590
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002591 // Construct method lists.
2592 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2593 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002594 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002595 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002596 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002597 ObjCMethodDecl *MD = *i;
2598 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002599 if (!C)
2600 return GetOrEmitProtocolRef(PD);
2601
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002602 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2603 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002604 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002605 } else {
2606 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002607 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002608 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002609 }
2610
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002611 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002612 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002613 ObjCMethodDecl *MD = *i;
2614 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002615 if (!C)
2616 return GetOrEmitProtocolRef(PD);
2617
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002618 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2619 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002620 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002621 } else {
2622 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002623 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002624 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002625 }
2626
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002627 MethodTypesExt.insert(MethodTypesExt.end(),
2628 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2629
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002630 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002631 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2632 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002633 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002634 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002635 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002636 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002637 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002638 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002639 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002640 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002641 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002642 ClassMethods)
2643 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002644 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002645 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002646
Daniel Dunbarb036db82008-08-13 03:21:16 +00002647 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002648 // Already created, fix the linkage and update the initializer.
2649 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002650 Entry->setInitializer(Init);
2651 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002652 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002653 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002654 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002655 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002656 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002657 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002658 // FIXME: Is this necessary? Why only for protocol?
2659 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002660
2661 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002662 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00002663 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002664
2665 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002666}
2667
Daniel Dunbarc475d422008-10-29 22:36:39 +00002668llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002669 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2670
2671 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002672 // We use the initializer as a marker of whether this is a forward
2673 // reference or not. At module finalization we add the empty
2674 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002675 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002676 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002677 llvm::GlobalValue::ExternalLinkage,
2678 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002679 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002680 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002681 // FIXME: Is this necessary? Why only for protocol?
2682 Entry->setAlignment(4);
2683 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002684
Daniel Dunbarb036db82008-08-13 03:21:16 +00002685 return Entry;
2686}
2687
2688/*
2689 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002690 uint32_t size;
2691 struct objc_method_description_list *optional_instance_methods;
2692 struct objc_method_description_list *optional_class_methods;
2693 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002694 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002695 };
2696*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002697llvm::Constant *
2698CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002699 ArrayRef<llvm::Constant*> OptInstanceMethods,
2700 ArrayRef<llvm::Constant*> OptClassMethods,
2701 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002702 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002703 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002704 llvm::Constant *Values[] = {
2705 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002706 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002707 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002708 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002709 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002710 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002711 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002712 OptClassMethods),
2713 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002714 ObjCTypes),
2715 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2716 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002717 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002718
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002719 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002720 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002721 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002722 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002723
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002724 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002725 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002726
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002727 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002728 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002729 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002730 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002731}
2732
2733/*
2734 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002735 struct objc_protocol_list *next;
2736 long count;
2737 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002738 };
2739*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002740llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002741CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002742 ObjCProtocolDecl::protocol_iterator begin,
2743 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002744 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002745
Daniel Dunbardec75f82008-08-21 21:57:41 +00002746 for (; begin != end; ++begin)
2747 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002748
2749 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002750 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002751 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002752
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002753 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002754 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002755
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002756 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002757 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002758 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002759 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002760 ProtocolRefs.size() - 1);
2761 Values[2] =
2762 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2763 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002764 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002765
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002766 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002767 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002768 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002769 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002770 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002771}
2772
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002773void CGObjCCommonMac::
2774PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002775 SmallVectorImpl<llvm::Constant *> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002776 const Decl *Container,
2777 const ObjCProtocolDecl *PROTO,
2778 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002779 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2780 E = PROTO->protocol_end(); P != E; ++P)
2781 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2782 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2783 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002784 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002785 if (!PropertySet.insert(PD->getIdentifier()))
2786 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002787 llvm::Constant *Prop[] = {
2788 GetPropertyName(PD->getIdentifier()),
2789 GetPropertyTypeString(PD, Container)
2790 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002791 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2792 }
2793}
2794
Daniel Dunbarb036db82008-08-13 03:21:16 +00002795/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002796 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002797 const char * const name;
2798 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002799 };
2800
2801 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002802 uint32_t entsize; // sizeof (struct _objc_property)
2803 uint32_t prop_count;
2804 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002805 };
2806*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002807llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002808 const Decl *Container,
2809 const ObjCContainerDecl *OCD,
2810 const ObjCCommonTypesHelper &ObjCTypes) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002811 SmallVector<llvm::Constant *, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002812 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002813 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2814 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002815 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002816 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002817 llvm::Constant *Prop[] = {
2818 GetPropertyName(PD->getIdentifier()),
2819 GetPropertyTypeString(PD, Container)
2820 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002821 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002822 Prop));
2823 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002824 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002825 for (ObjCInterfaceDecl::all_protocol_iterator
2826 P = OID->all_referenced_protocol_begin(),
2827 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002828 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2829 ObjCTypes);
2830 }
2831 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2832 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2833 E = CD->protocol_end(); P != E; ++P)
2834 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2835 ObjCTypes);
2836 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002837
2838 // Return null for empty list.
2839 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002840 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002841
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002842 unsigned PropertySize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002843 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002844 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002845 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2846 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002847 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002848 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002849 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002850 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002851
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002852 llvm::GlobalVariable *GV =
2853 CreateMetadataVar(Name, Init,
2854 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002855 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002856 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002857 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002858 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002859}
2860
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002861llvm::Constant *
2862CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2863 ArrayRef<llvm::Constant*> MethodTypes,
2864 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002865 // Return null for empty list.
2866 if (MethodTypes.empty())
2867 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2868
2869 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2870 MethodTypes.size());
2871 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2872
2873 llvm::GlobalVariable *GV =
2874 CreateMetadataVar(Name, Init,
2875 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2876 (ObjCABI == 2) ? 8 : 4,
2877 true);
2878 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2879}
2880
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002881/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002882 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002883 int count;
2884 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002885 };
2886*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002887llvm::Constant *
2888CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002889 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002891 ObjCTypes.SelectorPtrTy),
2892 GetMethodVarType(MD)
2893 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002894 if (!Desc[1])
2895 return 0;
2896
Owen Anderson0e0189d2009-07-27 22:29:56 +00002897 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002898 Desc);
2899}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002900
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002901llvm::Constant *
2902CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2903 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002904 // Return null for empty list.
2905 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002906 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002907
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002908 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002909 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002910 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002911 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002912 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002913 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002914
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002915 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002916 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002917 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002918}
2919
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002920/*
2921 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002922 char *category_name;
2923 char *class_name;
2924 struct _objc_method_list *instance_methods;
2925 struct _objc_method_list *class_methods;
2926 struct _objc_protocol_list *protocols;
2927 uint32_t size; // <rdar://4585769>
2928 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002929 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002930*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002931void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00002932 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002933
Mike Stump18bb9282009-05-16 07:57:57 +00002934 // FIXME: This is poor design, the OCD should have a pointer to the category
2935 // decl. Additionally, note that Category can be null for the @implementation
2936 // w/o an @interface case. Sema should just create one for us as it does for
2937 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002938 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002939 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002940 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002941
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002942 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002943 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2944 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002945
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002946 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002947 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002948 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002949 // Instance methods should always be defined.
2950 InstanceMethods.push_back(GetMethodConstant(*i));
2951 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002952 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002953 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002954 // Class methods should always be defined.
2955 ClassMethods.push_back(GetMethodConstant(*i));
2956 }
2957
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002958 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002959 Values[0] = GetClassName(OCD->getIdentifier());
2960 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002961 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002962 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002963 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002964 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002965 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002966 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002967 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002968 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002969 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002970 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002971 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002972 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002973 Category->protocol_begin(),
2974 Category->protocol_end());
2975 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002976 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002977 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002978 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002979
2980 // If there is no category @interface then there can be no properties.
2981 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002982 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002983 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002984 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002985 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002986 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002987
Owen Anderson0e0189d2009-07-27 22:29:56 +00002988 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002989 Values);
2990
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002991 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002992 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002993 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002994 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002995 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002996 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002997 // method definition entries must be clear for next implementation.
2998 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002999}
3000
John McCallef19dbb2012-10-17 04:53:23 +00003001enum FragileClassFlags {
3002 FragileABI_Class_Factory = 0x00001,
3003 FragileABI_Class_Meta = 0x00002,
3004 FragileABI_Class_HasCXXStructors = 0x02000,
3005 FragileABI_Class_Hidden = 0x20000
3006};
3007
3008enum NonFragileClassFlags {
3009 /// Is a meta-class.
3010 NonFragileABI_Class_Meta = 0x00001,
3011
3012 /// Is a root class.
3013 NonFragileABI_Class_Root = 0x00002,
3014
3015 /// Has a C++ constructor and destructor.
3016 NonFragileABI_Class_HasCXXStructors = 0x00004,
3017
3018 /// Has hidden visibility.
3019 NonFragileABI_Class_Hidden = 0x00010,
3020
3021 /// Has the exception attribute.
3022 NonFragileABI_Class_Exception = 0x00020,
3023
3024 /// (Obsolete) ARC-specific: this class has a .release_ivars method
3025 NonFragileABI_Class_HasIvarReleaser = 0x00040,
3026
3027 /// Class implementation was compiled under ARC.
John McCall0d54a172012-10-17 04:53:31 +00003028 NonFragileABI_Class_CompiledByARC = 0x00080,
3029
3030 /// Class has non-trivial destructors, but zero-initialization is okay.
3031 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003032};
3033
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003034/*
3035 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003036 Class isa;
3037 Class super_class;
3038 const char *name;
3039 long version;
3040 long info;
3041 long instance_size;
3042 struct _objc_ivar_list *ivars;
3043 struct _objc_method_list *methods;
3044 struct _objc_cache *cache;
3045 struct _objc_protocol_list *protocols;
3046 // Objective-C 1.0 extensions (<rdr://4585769>)
3047 const char *ivar_layout;
3048 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003049 };
3050
3051 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003052*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003053void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003054 DefinedSymbols.insert(ID->getIdentifier());
3055
Chris Lattner86d7d912008-11-24 03:54:41 +00003056 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003057 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003058 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003059 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003060 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003061 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003062 Interface->all_referenced_protocol_begin(),
3063 Interface->all_referenced_protocol_end());
John McCallef19dbb2012-10-17 04:53:23 +00003064 unsigned Flags = FragileABI_Class_Factory;
John McCall0d54a172012-10-17 04:53:31 +00003065 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCallef19dbb2012-10-17 04:53:23 +00003066 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003067 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00003068 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003069
3070 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00003071 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00003072 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003073
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003074 SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003075 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003076 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003077 // Instance methods should always be defined.
3078 InstanceMethods.push_back(GetMethodConstant(*i));
3079 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003080 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003081 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003082 // Class methods should always be defined.
3083 ClassMethods.push_back(GetMethodConstant(*i));
3084 }
3085
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003086 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003087 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00003088 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003089
3090 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
3091 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3092
3093 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3094 if (llvm::Constant *C = GetMethodConstant(MD))
3095 InstanceMethods.push_back(C);
3096 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3097 if (llvm::Constant *C = GetMethodConstant(MD))
3098 InstanceMethods.push_back(C);
3099 }
3100 }
3101
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003102 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00003103 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003104 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003105 // Record a reference to the super class.
3106 LazySymbols.insert(Super->getIdentifier());
3107
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003108 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003109 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003110 ObjCTypes.ClassPtrTy);
3111 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003112 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003113 }
3114 Values[ 2] = GetClassName(ID->getIdentifier());
3115 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003116 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3117 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3118 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003119 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003120 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003121 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003122 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003123 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003124 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003125 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003126 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003127 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003128 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003129 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003130 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003131 std::string Name("\01L_OBJC_CLASS_");
3132 Name += ClassName;
3133 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3134 // Check for a forward reference.
3135 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3136 if (GV) {
3137 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3138 "Forward metaclass reference has incorrect type.");
3139 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3140 GV->setInitializer(Init);
3141 GV->setSection(Section);
3142 GV->setAlignment(4);
3143 CGM.AddUsedGlobal(GV);
3144 }
3145 else
3146 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003147 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00003148 // method definition entries must be clear for next implementation.
3149 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003150}
3151
3152llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3153 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003154 ArrayRef<llvm::Constant*> Methods) {
John McCallef19dbb2012-10-17 04:53:23 +00003155 unsigned Flags = FragileABI_Class_Meta;
Micah Villmowdd31ca12012-10-08 16:25:52 +00003156 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003157
John McCall457a04e2010-10-22 21:05:15 +00003158 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00003159 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003160
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003161 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003162 // The isa for the metaclass is the root of the hierarchy.
3163 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3164 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3165 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003166 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003167 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003168 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003169 // The super class for the metaclass is emitted as the name of the
3170 // super class. The runtime fixes this up to point to the
3171 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003172 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003173 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003174 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003175 ObjCTypes.ClassPtrTy);
3176 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003177 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003178 }
3179 Values[ 2] = GetClassName(ID->getIdentifier());
3180 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003181 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3182 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3183 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003184 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003185 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003186 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003187 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003188 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003189 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003190 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003191 Values[ 9] = Protocols;
3192 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003193 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003194 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00003195 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003196 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003197 Values);
3198
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003199 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer1bbcbd02012-07-31 11:45:39 +00003200 Name += ID->getName();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003201
3202 // Check for a forward reference.
3203 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3204 if (GV) {
3205 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3206 "Forward metaclass reference has incorrect type.");
3207 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3208 GV->setInitializer(Init);
3209 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00003210 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003211 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00003212 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003213 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003214 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00003215 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003216 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003217
3218 return GV;
3219}
3220
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003221llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003222 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003223
Mike Stump18bb9282009-05-16 07:57:57 +00003224 // FIXME: Should we look these up somewhere other than the module. Its a bit
3225 // silly since we only generate these while processing an implementation, so
3226 // exactly one pointer would work if know when we entered/exitted an
3227 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003228
3229 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00003230 // Previously, metaclass with internal linkage may have been defined.
3231 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003232 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3233 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003234 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3235 "Forward metaclass reference has incorrect type.");
3236 return GV;
3237 } else {
3238 // Generate as an external reference to keep a consistent
3239 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00003240 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003241 llvm::GlobalValue::ExternalLinkage,
3242 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00003243 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003244 }
3245}
3246
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003247llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3248 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3249
3250 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3251 true)) {
3252 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3253 "Forward class metadata reference has incorrect type.");
3254 return GV;
3255 } else {
3256 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3257 llvm::GlobalValue::ExternalLinkage,
3258 0,
3259 Name);
3260 }
3261}
3262
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003263/*
3264 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003265 uint32_t size;
3266 const char *weak_ivar_layout;
3267 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003268 };
3269*/
3270llvm::Constant *
3271CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003272 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00003273 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003274
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003275 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003276 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00003277 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003278 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00003279 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003280
3281 // Return null if no extension bits are used.
3282 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00003283 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003284
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003285 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00003286 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003287 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003288 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003289 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003290}
3291
3292/*
3293 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003294 char *ivar_name;
3295 char *ivar_type;
3296 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003297 };
3298
3299 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003300 int ivar_count;
3301 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003302 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003303*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003304llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003305 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003306 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003307
3308 // When emitting the root class GCC emits ivar entries for the
3309 // actual class structure. It is not clear if we need to follow this
3310 // behavior; for now lets try and get away with not doing it. If so,
3311 // the cleanest solution would be to make up an ObjCInterfaceDecl
3312 // for the class.
3313 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00003314 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003315
Jordy Rosea91768e2011-07-22 02:08:32 +00003316 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003317
Jordy Rosea91768e2011-07-22 02:08:32 +00003318 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003319 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00003320 // Ignore unnamed bit-fields.
3321 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003322 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003323 llvm::Constant *Ivar[] = {
3324 GetMethodVarName(IVD->getIdentifier()),
3325 GetMethodVarType(IVD),
3326 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedman8cbca202012-11-06 22:15:52 +00003327 ComputeIvarBaseOffset(CGM, OID, IVD))
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003328 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003329 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003330 }
3331
3332 // Return null for empty list.
3333 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003334 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003335
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003336 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003337 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003338 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003339 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003340 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003341 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003342
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003343 llvm::GlobalVariable *GV;
3344 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003345 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003346 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003347 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003348 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003349 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003350 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003351 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003352 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003353}
3354
3355/*
3356 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003357 SEL method_name;
3358 char *method_types;
3359 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003360 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003361
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003362 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003363 struct objc_method_list *obsolete;
3364 int count;
3365 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003366 };
3367*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003368
3369/// GetMethodConstant - Return a struct objc_method constant for the
3370/// given method if it has been defined. The result is null if the
3371/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00003372llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003373 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003374 if (!Fn)
3375 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003376
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003377 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00003378 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003379 ObjCTypes.SelectorPtrTy),
3380 GetMethodVarType(MD),
3381 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3382 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003383 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003384}
3385
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003386llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003387 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003388 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003389 // Return null for empty list.
3390 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003391 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003392
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003393 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00003394 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003395 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003396 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003397 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003398 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003399 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003400
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003401 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003402 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003403}
3404
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00003405llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003406 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003407 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003408 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003409
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00003410 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00003411 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00003412 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003413 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00003414 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003415 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00003416 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003417 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003418 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003419
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003420 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003421}
3422
Daniel Dunbar30c65362009-03-09 20:09:19 +00003423llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003424CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00003425 llvm::Constant *Init,
3426 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003427 unsigned Align,
3428 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003429 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003430 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00003431 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00003432 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003433 if (Section)
3434 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003435 if (Align)
3436 GV->setAlignment(Align);
3437 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00003438 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003439 return GV;
3440}
3441
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003442llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003443 // Abuse this interface function as a place to finalize.
3444 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003445 return NULL;
3446}
3447
Chris Lattnerd4808922009-03-22 21:03:39 +00003448llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003449 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003450}
3451
Chris Lattnerd4808922009-03-22 21:03:39 +00003452llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003453 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003454}
3455
Ted Kremeneke65b0862012-03-06 20:05:56 +00003456llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3457 bool copy) {
3458 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3459}
3460
David Chisnall168b80f2010-12-26 22:13:16 +00003461llvm::Constant *CGObjCMac::GetGetStructFunction() {
3462 return ObjCTypes.getCopyStructFn();
3463}
3464llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00003465 return ObjCTypes.getCopyStructFn();
3466}
3467
David Chisnall0d75e062012-12-17 18:54:24 +00003468llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() {
3469 return ObjCTypes.getCppAtomicObjectFunction();
3470}
3471llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() {
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00003472 return ObjCTypes.getCppAtomicObjectFunction();
3473}
3474
Chris Lattnerd4808922009-03-22 21:03:39 +00003475llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003476 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00003477}
3478
John McCallbd309292010-07-06 01:34:17 +00003479void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3480 return EmitTryOrSynchronizedStmt(CGF, S);
3481}
3482
3483void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3484 const ObjCAtSynchronizedStmt &S) {
3485 return EmitTryOrSynchronizedStmt(CGF, S);
3486}
3487
John McCall65bea082010-07-21 06:59:36 +00003488namespace {
John McCallcda666c2010-07-21 07:22:38 +00003489 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00003490 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00003491 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00003492 llvm::Value *CallTryExitVar;
3493 llvm::Value *ExceptionData;
3494 ObjCTypesHelper &ObjCTypes;
3495 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00003496 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00003497 llvm::Value *CallTryExitVar,
3498 llvm::Value *ExceptionData,
3499 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00003500 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00003501 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3502
John McCall30317fd2011-07-12 20:27:29 +00003503 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00003504 // Check whether we need to call objc_exception_try_exit.
3505 // In optimized code, this branch will always be folded.
3506 llvm::BasicBlock *FinallyCallExit =
3507 CGF.createBasicBlock("finally.call_exit");
3508 llvm::BasicBlock *FinallyNoCallExit =
3509 CGF.createBasicBlock("finally.no_call_exit");
3510 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3511 FinallyCallExit, FinallyNoCallExit);
3512
3513 CGF.EmitBlock(FinallyCallExit);
3514 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3515 ->setDoesNotThrow();
3516
3517 CGF.EmitBlock(FinallyNoCallExit);
3518
3519 if (isa<ObjCAtTryStmt>(S)) {
3520 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00003521 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3522 // Save the current cleanup destination in case there's
3523 // control flow inside the finally statement.
3524 llvm::Value *CurCleanupDest =
3525 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3526
John McCall65bea082010-07-21 06:59:36 +00003527 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3528
John McCallcebe0ca2010-08-11 00:16:14 +00003529 if (CGF.HaveInsertPoint()) {
3530 CGF.Builder.CreateStore(CurCleanupDest,
3531 CGF.getNormalCleanupDestSlot());
3532 } else {
3533 // Currently, the end of the cleanup must always exist.
3534 CGF.EnsureInsertPoint();
3535 }
3536 }
John McCall65bea082010-07-21 06:59:36 +00003537 } else {
3538 // Emit objc_sync_exit(expr); as finally's sole statement for
3539 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00003540 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00003541 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3542 ->setDoesNotThrow();
3543 }
3544 }
3545 };
John McCall42227ed2010-07-31 23:20:56 +00003546
3547 class FragileHazards {
3548 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003549 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00003550 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3551
3552 llvm::InlineAsm *ReadHazard;
3553 llvm::InlineAsm *WriteHazard;
3554
3555 llvm::FunctionType *GetAsmFnType();
3556
3557 void collectLocals();
3558 void emitReadHazard(CGBuilderTy &Builder);
3559
3560 public:
3561 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00003562
John McCall42227ed2010-07-31 23:20:56 +00003563 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00003564 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003565 };
3566}
3567
3568/// Create the fragile-ABI read and write hazards based on the current
3569/// state of the function, which is presumed to be immediately prior
3570/// to a @try block. These hazards are used to maintain correct
3571/// semantics in the face of optimization and the fragile ABI's
3572/// cavalier use of setjmp/longjmp.
3573FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3574 collectLocals();
3575
3576 if (Locals.empty()) return;
3577
3578 // Collect all the blocks in the function.
3579 for (llvm::Function::iterator
3580 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3581 BlocksBeforeTry.insert(&*I);
3582
3583 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3584
3585 // Create a read hazard for the allocas. This inhibits dead-store
3586 // optimizations and forces the values to memory. This hazard is
3587 // inserted before any 'throwing' calls in the protected scope to
3588 // reflect the possibility that the variables might be read from the
3589 // catch block if the call throws.
3590 {
3591 std::string Constraint;
3592 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3593 if (I) Constraint += ',';
3594 Constraint += "*m";
3595 }
3596
3597 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3598 }
3599
3600 // Create a write hazard for the allocas. This inhibits folding
3601 // loads across the hazard. This hazard is inserted at the
3602 // beginning of the catch path to reflect the possibility that the
3603 // variables might have been written within the protected scope.
3604 {
3605 std::string Constraint;
3606 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3607 if (I) Constraint += ',';
3608 Constraint += "=*m";
3609 }
3610
3611 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3612 }
3613}
3614
3615/// Emit a write hazard at the current location.
3616void FragileHazards::emitWriteHazard() {
3617 if (Locals.empty()) return;
3618
Jay Foad5bd375a2011-07-15 08:37:34 +00003619 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003620}
3621
John McCall42227ed2010-07-31 23:20:56 +00003622void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3623 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00003624 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003625}
3626
3627/// Emit read hazards in all the protected blocks, i.e. all the blocks
3628/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00003629void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00003630 if (Locals.empty()) return;
3631
3632 CGBuilderTy Builder(CGF.getLLVMContext());
3633
3634 // Iterate through all blocks, skipping those prior to the try.
3635 for (llvm::Function::iterator
3636 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3637 llvm::BasicBlock &BB = *FI;
3638 if (BlocksBeforeTry.count(&BB)) continue;
3639
3640 // Walk through all the calls in the block.
3641 for (llvm::BasicBlock::iterator
3642 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3643 llvm::Instruction &I = *BI;
3644
3645 // Ignore instructions that aren't non-intrinsic calls.
3646 // These are the only calls that can possibly call longjmp.
3647 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3648 if (isa<llvm::IntrinsicInst>(I))
3649 continue;
3650
3651 // Ignore call sites marked nounwind. This may be questionable,
3652 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3653 llvm::CallSite CS(&I);
3654 if (CS.doesNotThrow()) continue;
3655
John McCall2dd7d442010-08-04 05:59:32 +00003656 // Insert a read hazard before the call. This will ensure that
3657 // any writes to the locals are performed before making the
3658 // call. If the call throws, then this is sufficient to
3659 // guarantee correctness as long as it doesn't also write to any
3660 // locals.
John McCall42227ed2010-07-31 23:20:56 +00003661 Builder.SetInsertPoint(&BB, BI);
3662 emitReadHazard(Builder);
3663 }
3664 }
3665}
3666
3667static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3668 if (V) S.insert(V);
3669}
3670
3671void FragileHazards::collectLocals() {
3672 // Compute a set of allocas to ignore.
3673 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3674 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3675 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00003676
3677 // Collect all the allocas currently in the function. This is
3678 // probably way too aggressive.
3679 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3680 for (llvm::BasicBlock::iterator
3681 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3682 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3683 Locals.push_back(&*I);
3684}
3685
3686llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003687 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003688 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3689 tys[i] = Locals[i]->getType();
3690 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003691}
3692
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003693/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003694
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003695 Objective-C setjmp-longjmp (sjlj) Exception Handling
3696 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003697
John McCallbd309292010-07-06 01:34:17 +00003698 A catch buffer is a setjmp buffer plus:
3699 - a pointer to the exception that was caught
3700 - a pointer to the previous exception data buffer
3701 - two pointers of reserved storage
3702 Therefore catch buffers form a stack, with a pointer to the top
3703 of the stack kept in thread-local storage.
3704
3705 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3706 objc_exception_try_exit pops the given catch buffer, which is
3707 required to be the top of the EH stack.
3708 objc_exception_throw pops the top of the EH stack, writes the
3709 thrown exception into the appropriate field, and longjmps
3710 to the setjmp buffer. It crashes the process (with a printf
3711 and an abort()) if there are no catch buffers on the stack.
3712 objc_exception_extract just reads the exception pointer out of the
3713 catch buffer.
3714
3715 There's no reason an implementation couldn't use a light-weight
3716 setjmp here --- something like __builtin_setjmp, but API-compatible
3717 with the heavyweight setjmp. This will be more important if we ever
3718 want to implement correct ObjC/C++ exception interactions for the
3719 fragile ABI.
3720
3721 Note that for this use of setjmp/longjmp to be correct, we may need
3722 to mark some local variables volatile: if a non-volatile local
3723 variable is modified between the setjmp and the longjmp, it has
3724 indeterminate value. For the purposes of LLVM IR, it may be
3725 sufficient to make loads and stores within the @try (to variables
3726 declared outside the @try) volatile. This is necessary for
3727 optimized correctness, but is not currently being done; this is
3728 being tracked as rdar://problem/8160285
3729
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003730 The basic framework for a @try-catch-finally is as follows:
3731 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003732 objc_exception_data d;
3733 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003734 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003735
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003736 objc_exception_try_enter(&d);
3737 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003738 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003739 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003740 // exception path
3741 id _caught = objc_exception_extract(&d);
3742
3743 // enter new try scope for handlers
3744 if (!setjmp(d.jmp_buf)) {
3745 ... match exception and execute catch blocks ...
3746
3747 // fell off end, rethrow.
3748 _rethrow = _caught;
3749 ... jump-through-finally to finally_rethrow ...
3750 } else {
3751 // exception in catch block
3752 _rethrow = objc_exception_extract(&d);
3753 _call_try_exit = false;
3754 ... jump-through-finally to finally_rethrow ...
3755 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003756 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003757 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003758
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003759 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003760 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003761 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003762
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003763 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003764 ... dispatch to finally destination ...
3765
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003766 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003767 objc_exception_throw(_rethrow);
3768
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003769 finally_end:
3770 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003771
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003772 This framework differs slightly from the one gcc uses, in that gcc
3773 uses _rethrow to determine if objc_exception_try_exit should be called
3774 and if the object should be rethrown. This breaks in the face of
3775 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003776
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003777 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003778
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003779 - If there are no catch blocks, then we avoid emitting the second
3780 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003781
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003782 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3783 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003784
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003785 - FIXME: If there is no @finally block we can do a few more
3786 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003787
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003788 Rethrows and Jumps-Through-Finally
3789 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003790
John McCallbd309292010-07-06 01:34:17 +00003791 '@throw;' is supported by pushing the currently-caught exception
3792 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003793
John McCallbd309292010-07-06 01:34:17 +00003794 Branches through the @finally block are handled with an ordinary
3795 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3796 exceptions are not compatible with C++ exceptions, and this is
3797 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003798
John McCallbd309292010-07-06 01:34:17 +00003799 @synchronized(expr) { stmt; } is emitted as if it were:
3800 id synch_value = expr;
3801 objc_sync_enter(synch_value);
3802 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003803*/
3804
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003805void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3806 const Stmt &S) {
3807 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003808
3809 // A destination for the fall-through edges of the catch handlers to
3810 // jump to.
3811 CodeGenFunction::JumpDest FinallyEnd =
3812 CGF.getJumpDestInCurrentScope("finally.end");
3813
3814 // A destination for the rethrow edge of the catch handlers to jump
3815 // to.
3816 CodeGenFunction::JumpDest FinallyRethrow =
3817 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003818
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003819 // For @synchronized, call objc_sync_enter(sync.expr). The
3820 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003821 // @synchronized. We can't avoid a temp here because we need the
3822 // value to be preserved. If the backend ever does liveness
3823 // correctly after setjmp, this will be unnecessary.
3824 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003825 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003826 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003827 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3828 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003829 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3830 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003831
3832 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3833 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003834 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003835
John McCall2dd7d442010-08-04 05:59:32 +00003836 // Allocate memory for the setjmp buffer. This needs to be kept
3837 // live throughout the try and catch blocks.
3838 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3839 "exceptiondata.ptr");
3840
John McCall42227ed2010-07-31 23:20:56 +00003841 // Create the fragile hazards. Note that this will not capture any
3842 // of the allocas required for exception processing, but will
3843 // capture the current basic block (which extends all the way to the
3844 // setjmp call) as "before the @try".
3845 FragileHazards Hazards(CGF);
3846
John McCallbd309292010-07-06 01:34:17 +00003847 // Create a flag indicating whether the cleanup needs to call
3848 // objc_exception_try_exit. This is true except when
3849 // - no catches match and we're branching through the cleanup
3850 // just to rethrow the exception, or
3851 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003852 // The setjmp-safety rule here is that we should always store to this
3853 // variable in a place that dominates the branch through the cleanup
3854 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003855 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003856 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003857
John McCall9916e3f2010-10-04 23:42:51 +00003858 // A slot containing the exception to rethrow. Only needed when we
3859 // have both a @catch and a @finally.
3860 llvm::Value *PropagatingExnVar = 0;
3861
John McCallbd309292010-07-06 01:34:17 +00003862 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003863 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003864 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003865 CallTryExitVar,
3866 ExceptionData,
3867 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003868
3869 // Enter a try block:
3870 // - Call objc_exception_try_enter to push ExceptionData on top of
3871 // the EH stack.
3872 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3873 ->setDoesNotThrow();
3874
3875 // - Call setjmp on the exception data buffer.
3876 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3877 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3878 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003879 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003880 llvm::CallInst *SetJmpResult =
3881 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3882 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003883 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003884
3885 // If setjmp returned 0, enter the protected block; otherwise,
3886 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003887 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3888 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003889 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003890 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3891 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003892
John McCallbd309292010-07-06 01:34:17 +00003893 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003894 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003895 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003896 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003897 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003898
3899 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003900
John McCallbd309292010-07-06 01:34:17 +00003901 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003902 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003903
John McCall42227ed2010-07-31 23:20:56 +00003904 // Don't optimize loads of the in-scope locals across this point.
3905 Hazards.emitWriteHazard();
3906
John McCallbd309292010-07-06 01:34:17 +00003907 // For a @synchronized (or a @try with no catches), just branch
3908 // through the cleanup to the rethrow block.
3909 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3910 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003911 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003912 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003913
3914 // Otherwise, we have to match against the caught exceptions.
3915 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003916 // Retrieve the exception object. We may emit multiple blocks but
3917 // nothing can cross this so the value is already in SSA form.
3918 llvm::CallInst *Caught =
3919 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3920 ExceptionData, "caught");
3921 Caught->setDoesNotThrow();
3922
John McCallbd309292010-07-06 01:34:17 +00003923 // Push the exception to rethrow onto the EH value stack for the
3924 // benefit of any @throws in the handlers.
3925 CGF.ObjCEHValueStack.push_back(Caught);
3926
Douglas Gregor96c79492010-04-23 22:50:49 +00003927 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003928
John McCall2dd7d442010-08-04 05:59:32 +00003929 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003930
John McCall2dd7d442010-08-04 05:59:32 +00003931 llvm::BasicBlock *CatchBlock = 0;
3932 llvm::BasicBlock *CatchHandler = 0;
3933 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003934 // Save the currently-propagating exception before
3935 // objc_exception_try_enter clears the exception slot.
3936 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3937 "propagating_exception");
3938 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3939
John McCall2dd7d442010-08-04 05:59:32 +00003940 // Enter a new exception try block (in case a @catch block
3941 // throws an exception).
3942 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3943 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003944
John McCall2dd7d442010-08-04 05:59:32 +00003945 llvm::CallInst *SetJmpResult =
3946 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3947 "setjmp.result");
3948 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003949 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003950
John McCall2dd7d442010-08-04 05:59:32 +00003951 llvm::Value *Threw =
3952 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3953
3954 CatchBlock = CGF.createBasicBlock("catch");
3955 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3956 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3957
3958 CGF.EmitBlock(CatchBlock);
3959 }
3960
3961 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003962
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003963 // Handle catch list. As a special case we check if everything is
3964 // matched and avoid generating code for falling off the end if
3965 // so.
3966 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003967 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3968 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003969
Douglas Gregor46a572b2010-04-26 16:46:50 +00003970 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003971 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003972
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003973 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003974 if (!CatchParam) {
3975 AllMatched = true;
3976 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003977 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003978
John McCallbd309292010-07-06 01:34:17 +00003979 // catch(id e) always matches under this ABI, since only
3980 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003981 // FIXME: For the time being we also match id<X>; this should
3982 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003983 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003984 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003985 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003986
John McCallbd309292010-07-06 01:34:17 +00003987 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003988 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003989 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3990
Anders Carlsson9396a892008-09-11 09:15:33 +00003991 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003992 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003993 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003994
3995 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003996 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003997 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003998
Anders Carlsson9396a892008-09-11 09:15:33 +00003999 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00004000
4001 // The scope of the catch variable ends right here.
4002 CatchVarCleanups.ForceCleanup();
4003
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004004 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004005 break;
4006 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004007
Steve Naroff7cae42b2009-07-10 23:34:53 +00004008 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00004009 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00004010
4011 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00004012 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
4013 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004014
4015 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00004016 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004017
John McCallbd309292010-07-06 01:34:17 +00004018 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00004019 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
4020 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00004021 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004022
John McCallbd309292010-07-06 01:34:17 +00004023 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
4024 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004025
4026 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00004027 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004028
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004029 // Emit the @catch block.
4030 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00004031
4032 // Collect any cleanups for the catch variable. The scope lasts until
4033 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00004034 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00004035
John McCall1c9c3fd2010-10-15 04:57:14 +00004036 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004037 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00004038
John McCallbd309292010-07-06 01:34:17 +00004039 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004040 llvm::Value *Tmp =
4041 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004042 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00004043 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004044
Anders Carlsson9396a892008-09-11 09:15:33 +00004045 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00004046
4047 // We're done with the catch variable.
4048 CatchVarCleanups.ForceCleanup();
4049
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004050 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004051
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004052 CGF.EmitBlock(NextCatchBlock);
4053 }
4054
John McCallbd309292010-07-06 01:34:17 +00004055 CGF.ObjCEHValueStack.pop_back();
4056
John McCall2dd7d442010-08-04 05:59:32 +00004057 // If nothing wanted anything to do with the caught exception,
4058 // kill the extract call.
4059 if (Caught->use_empty())
4060 Caught->eraseFromParent();
4061
4062 if (!AllMatched)
4063 CGF.EmitBranchThroughCleanup(FinallyRethrow);
4064
4065 if (HasFinally) {
4066 // Emit the exception handler for the @catch blocks.
4067 CGF.EmitBlock(CatchHandler);
4068
4069 // In theory we might now need a write hazard, but actually it's
4070 // unnecessary because there's no local-accessing code between
4071 // the try's write hazard and here.
4072 //Hazards.emitWriteHazard();
4073
John McCall9916e3f2010-10-04 23:42:51 +00004074 // Extract the new exception and save it to the
4075 // propagating-exception slot.
4076 assert(PropagatingExnVar);
4077 llvm::CallInst *NewCaught =
4078 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4079 ExceptionData, "caught");
4080 NewCaught->setDoesNotThrow();
4081 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
4082
John McCall2dd7d442010-08-04 05:59:32 +00004083 // Don't pop the catch handler; the throw already did.
4084 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004085 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00004086 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004087 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004088
John McCall42227ed2010-07-31 23:20:56 +00004089 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00004090 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00004091
John McCallbd309292010-07-06 01:34:17 +00004092 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00004093 CGF.Builder.restoreIP(TryFallthroughIP);
4094 if (CGF.HaveInsertPoint())
4095 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00004096 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00004097 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00004098
John McCallbd309292010-07-06 01:34:17 +00004099 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00004100 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00004101 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00004102 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00004103 // If we have a propagating-exception variable, check it.
4104 llvm::Value *PropagatingExn;
4105 if (PropagatingExnVar) {
4106 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00004107
John McCall9916e3f2010-10-04 23:42:51 +00004108 // Otherwise, just look in the buffer for the exception to throw.
4109 } else {
4110 llvm::CallInst *Caught =
4111 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4112 ExceptionData);
4113 Caught->setDoesNotThrow();
4114 PropagatingExn = Caught;
4115 }
4116
4117 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00004118 ->setDoesNotThrow();
4119 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00004120 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004121
John McCall42227ed2010-07-31 23:20:56 +00004122 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004123}
4124
4125void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00004126 const ObjCAtThrowStmt &S,
4127 bool ClearInsertionPoint) {
Anders Carlssone005aa12008-09-09 16:16:55 +00004128 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004129
Anders Carlssone005aa12008-09-09 16:16:55 +00004130 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00004131 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004132 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004133 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00004134 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00004136 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00004137 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00004138 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004139
John McCallbd309292010-07-06 01:34:17 +00004140 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4141 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004142 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004143
4144 // Clear the insertion point to indicate we are in unreachable code.
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00004145 if (ClearInsertionPoint)
4146 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004147}
4148
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004149/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004150/// object: objc_read_weak (id *src)
4151///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004152llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004153 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004154 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4156 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4157 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00004158 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004159 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00004160 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004161 return read_weak;
4162}
4163
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004164/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4165/// objc_assign_weak (id src, id *dst)
4166///
4167void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004168 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004169 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004170 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004171 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004172 assert(Size <= 8 && "does not support size > 8");
4173 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004174 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004175 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4176 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004177 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4178 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00004179 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004180 src, dst, "weakassign");
4181 return;
4182}
4183
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004184/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4185/// objc_assign_global (id src, id *dst)
4186///
4187void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00004188 llvm::Value *src, llvm::Value *dst,
4189 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004190 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004191 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004192 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004193 assert(Size <= 8 && "does not support size > 8");
4194 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004195 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004196 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4197 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004198 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4199 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00004200 if (!threadlocal)
4201 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4202 src, dst, "globalassign");
4203 else
4204 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4205 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004206 return;
4207}
4208
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004209/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004210/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004211///
4212void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004213 llvm::Value *src, llvm::Value *dst,
4214 llvm::Value *ivarOffset) {
4215 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00004216 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004217 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004218 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004219 assert(Size <= 8 && "does not support size > 8");
4220 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004221 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004222 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4223 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004224 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4225 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004226 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4227 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004228 return;
4229}
4230
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004231/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4232/// objc_assign_strongCast (id src, id *dst)
4233///
4234void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004235 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004236 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004237 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004238 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004239 assert(Size <= 8 && "does not support size > 8");
4240 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004241 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004242 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4243 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004244 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4245 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00004246 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004247 src, dst, "weakassign");
4248 return;
4249}
4250
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004251void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004252 llvm::Value *DestPtr,
4253 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00004254 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004255 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4256 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004257 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00004258 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004259 return;
4260}
4261
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004262/// EmitObjCValueForIvar - Code Gen for ivar reference.
4263///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004264LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4265 QualType ObjectTy,
4266 llvm::Value *BaseValue,
4267 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004268 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00004269 const ObjCInterfaceDecl *ID =
4270 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00004271 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4272 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004273}
4274
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004275llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00004276 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004277 const ObjCIvarDecl *Ivar) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004278 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4279 return llvm::ConstantInt::get(
4280 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4281 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004282}
4283
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004284/* *** Private Interface *** */
4285
4286/// EmitImageInfo - Emit the image info marker used to encode some module
4287/// level information.
4288///
4289/// See: <rdr://4810609&4810587&4810587>
4290/// struct IMAGE_INFO {
4291/// unsigned version;
4292/// unsigned flags;
4293/// };
4294enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00004295 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004296 eImageInfo_GarbageCollected = (1 << 1),
4297 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00004298 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4299
Daniel Dunbar5e639272010-04-25 20:39:01 +00004300 // A flag indicating that the module has no instances of a @synthesize of a
4301 // superclass variable. <rdar://problem/6803242>
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004302 eImageInfo_CorrectedSynthesize = (1 << 4),
4303 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004304};
4305
Daniel Dunbar5e639272010-04-25 20:39:01 +00004306void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004307 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004308 const char *Section = (ObjCABI == 1) ?
4309 "__OBJC, __image_info,regular" :
4310 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004311
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004312 // Generate module-level named metadata to convey this information to the
4313 // linker and code-gen.
4314 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004315
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004316 // Add the ObjC ABI version to the module flags.
4317 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4318 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4319 version);
4320 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4321 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004322
David Blaikiebbafb8a2012-03-11 07:00:24 +00004323 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004324 // Non-GC overrides those files which specify GC.
4325 Mod.addModuleFlag(llvm::Module::Override,
4326 "Objective-C Garbage Collection", (uint32_t)0);
4327 } else {
4328 // Add the ObjC garbage collection value.
4329 Mod.addModuleFlag(llvm::Module::Error,
4330 "Objective-C Garbage Collection",
4331 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004332
David Blaikiebbafb8a2012-03-11 07:00:24 +00004333 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004334 // Add the ObjC GC Only value.
4335 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4336 eImageInfo_GCOnly);
4337
4338 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4339 llvm::Value *Ops[2] = {
4340 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4341 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4342 eImageInfo_GarbageCollected)
4343 };
4344 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4345 llvm::MDNode::get(VMContext, Ops));
4346 }
4347 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004348
4349 // Indicate whether we're compiling this to run on a simulator.
4350 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4351 if (Triple.getOS() == llvm::Triple::IOS &&
4352 (Triple.getArch() == llvm::Triple::x86 ||
4353 Triple.getArch() == llvm::Triple::x86_64))
4354 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4355 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004356}
4357
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004358// struct objc_module {
4359// unsigned long version;
4360// unsigned long size;
4361// const char *name;
4362// Symtab symtab;
4363// };
4364
4365// FIXME: Get from somewhere
4366static const int ModuleVersion = 7;
4367
4368void CGObjCMac::EmitModuleInfo() {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004369 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004370
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004371 llvm::Constant *Values[] = {
4372 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4373 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4374 // This used to be the filename, now it is unused. <rdr://4327263>
4375 GetClassName(&CGM.getContext().Idents.get("")),
4376 EmitModuleSymbols()
4377 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004378 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00004379 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004380 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00004381 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004382}
4383
4384llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004385 unsigned NumClasses = DefinedClasses.size();
4386 unsigned NumCategories = DefinedCategories.size();
4387
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004388 // Return null if no symbols were defined.
4389 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00004390 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004391
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004392 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004393 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00004394 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004395 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4396 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004397
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004398 // The runtime expects exactly the list of defined classes followed
4399 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00004400 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004401 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004402 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004403 ObjCTypes.Int8PtrTy);
4404 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004405 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00004406 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004407 ObjCTypes.Int8PtrTy);
4408
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004409 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004410 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004411 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004412 Symbols);
4413
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004414 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004415
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004416 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004417 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4418 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004419 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00004420 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004421}
4422
John McCall31168b02011-06-15 23:02:42 +00004423llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4424 IdentifierInfo *II) {
4425 LazySymbols.insert(II);
4426
4427 llvm::GlobalVariable *&Entry = ClassReferences[II];
4428
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004429 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004430 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00004431 llvm::ConstantExpr::getBitCast(GetClassName(II),
4432 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004433 Entry =
John McCall31168b02011-06-15 23:02:42 +00004434 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4435 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4436 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004437 }
John McCall31168b02011-06-15 23:02:42 +00004438
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004439 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004440}
4441
John McCall31168b02011-06-15 23:02:42 +00004442llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4443 const ObjCInterfaceDecl *ID) {
4444 return EmitClassRefFromId(Builder, ID->getIdentifier());
4445}
4446
4447llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4448 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4449 return EmitClassRefFromId(Builder, II);
4450}
4451
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004452llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4453 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004454 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004455
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004456 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004457 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00004458 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004459 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004460 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004461 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4462 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004463 4, true);
Michael Gottesman5c205962013-02-05 23:08:45 +00004464 Entry->setExternallyInitialized(true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004465 }
4466
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004467 if (lvalue)
4468 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004469 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004470}
4471
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004472llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00004473 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004474
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004475 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004476 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004477 llvm::ConstantDataArray::getString(VMContext,
4478 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004479 ((ObjCABI == 2) ?
4480 "__TEXT,__objc_classname,cstring_literals" :
4481 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004482 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004483
Owen Anderson170229f2009-07-14 23:10:40 +00004484 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004485}
4486
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004487llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4488 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4489 I = MethodDefinitions.find(MD);
4490 if (I != MethodDefinitions.end())
4491 return I->second;
4492
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004493 return NULL;
4494}
4495
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004496/// GetIvarLayoutName - Returns a unique constant for the given
4497/// ivar layout bitmap.
4498llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004499 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00004500 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004501}
4502
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004503void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman8cbca202012-11-06 22:15:52 +00004504 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004505 bool ForStrongLayout,
4506 bool &HasUnion) {
4507 const RecordDecl *RD = RT->getDecl();
4508 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00004509 SmallVector<const FieldDecl*, 16> Fields;
4510 for (RecordDecl::field_iterator i = RD->field_begin(),
4511 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00004512 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00004513 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004514 const llvm::StructLayout *RecLayout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004515 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004516
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004517 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4518 ForStrongLayout, HasUnion);
4519}
4520
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00004521void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004522 const llvm::StructLayout *Layout,
4523 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00004524 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cbca202012-11-06 22:15:52 +00004525 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004526 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004527 bool IsUnion = (RD && RD->isUnion());
Eli Friedman8cbca202012-11-06 22:15:52 +00004528 uint64_t MaxUnionIvarSize = 0;
4529 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00004530 const FieldDecl *MaxField = 0;
4531 const FieldDecl *MaxSkippedField = 0;
4532 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedman8cbca202012-11-06 22:15:52 +00004533 uint64_t MaxFieldOffset = 0;
4534 uint64_t MaxSkippedFieldOffset = 0;
4535 uint64_t LastBitfieldOrUnnamedOffset = 0;
4536 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004537
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004538 if (RecFields.empty())
4539 return;
Eli Friedman8cbca202012-11-06 22:15:52 +00004540 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
Douglas Gregore8bbc122011-09-02 00:18:52 +00004541 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004542 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004543 const FieldDecl *FirstField = RecFields[0];
4544 FirstFieldDelta =
4545 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
John McCall31168b02011-06-15 23:02:42 +00004546 }
4547
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004548 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004549 const FieldDecl *Field = RecFields[i];
Eli Friedman8cbca202012-11-06 22:15:52 +00004550 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004551 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00004552 // Note that 'i' here is actually the field index inside RD of Field,
4553 // although this dependency is hidden.
4554 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedman8cbca202012-11-06 22:15:52 +00004555 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004556 } else
John McCall31168b02011-06-15 23:02:42 +00004557 FieldOffset =
4558 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004559
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004560 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004561 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004562 LastFieldBitfieldOrUnnamed = Field;
4563 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004564 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004565 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004566
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004567 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004568 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004569 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004570 if (FQT->isUnionType())
4571 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004572
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004573 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004574 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004575 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004576 continue;
4577 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004578
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004579 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004580 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004581 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004582 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004583 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004584 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004585 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4586 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004587 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004588 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004589 FQT = CArray->getElementType();
4590 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004591
4592 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004593 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00004594 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004595 int OldIndex = IvarsInfo.size() - 1;
4596 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004598 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004599 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004600 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004601
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004602 // Replicate layout information for each array element. Note that
4603 // one element is already done.
4604 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004605 for (int FirstIndex = IvarsInfo.size() - 1,
4606 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004607 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004608 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4609 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4610 IvarsInfo[i].ivar_size));
4611 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4612 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4613 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004614 }
4615 continue;
4616 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004617 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004618 // At this point, we are done with Record/Union and array there of.
4619 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00004620 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004621
Eli Friedman8cbca202012-11-06 22:15:52 +00004622 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00004623 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4624 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004625 if (IsUnion) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004626 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004627 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004628 MaxUnionIvarSize = UnionIvarSize;
4629 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004630 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004631 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004632 } else {
Eli Friedman8cbca202012-11-06 22:15:52 +00004633 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
4634 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004635 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004636 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00004637 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4638 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004639 if (IsUnion) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004640 // FIXME: Why the asymmetry? We divide by word size in bits on other
4641 // side.
4642 uint64_t UnionIvarSize = FieldSize / ByteSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004643 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004644 MaxSkippedUnionIvarSize = UnionIvarSize;
4645 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004646 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004647 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004648 } else {
Eli Friedman8cbca202012-11-06 22:15:52 +00004649 // FIXME: Why the asymmetry, we divide by byte size in bits here?
4650 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
4651 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004652 }
4653 }
4654 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004655
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004656 if (LastFieldBitfieldOrUnnamed) {
4657 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4658 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00004659 uint64_t BitFieldSize
4660 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004661 GC_IVAR skivar;
4662 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
Eli Friedman8cbca202012-11-06 22:15:52 +00004663 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4664 + ((BitFieldSize % ByteSizeInBits) != 0);
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004665 SkipIvars.push_back(skivar);
4666 } else {
4667 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4668 // Last field was unnamed. Must update skip info.
Eli Friedman8cbca202012-11-06 22:15:52 +00004669 unsigned FieldSize
4670 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004671 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
Eli Friedman8cbca202012-11-06 22:15:52 +00004672 FieldSize / ByteSizeInBits));
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004673 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004674 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004675
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004676 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004677 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004678 MaxUnionIvarSize));
4679 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00004680 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004681 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004682}
4683
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004684/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4685/// the computations and returning the layout bitmap (for ivar or blocks) in
4686/// the given argument BitMap string container. Routine reads
4687/// two containers, IvarsInfo and SkipIvars which are assumed to be
4688/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004689llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004690 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004691 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004692
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004693 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004694 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedman8cbca202012-11-06 22:15:52 +00004695 unsigned int WordSize =
4696 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
4697 if (IvarsInfo[0].ivar_bytepos == 0) {
4698 WordsToSkip = 0;
4699 WordsToScan = IvarsInfo[0].ivar_size;
4700 } else {
4701 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4702 WordsToScan = IvarsInfo[0].ivar_size;
4703 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004704 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004705 unsigned int TailPrevGCObjC =
4706 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004707 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004708 // consecutive 'scanned' object pointers.
Eli Friedman8cbca202012-11-06 22:15:52 +00004709 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004710 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004711 // Skip over 'gc'able object pointer which lay over each other.
4712 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4713 continue;
4714 // Must skip over 1 or more words. We save current skip/scan values
4715 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004716 SKIP_SCAN SkScan;
4717 SkScan.skip = WordsToSkip;
4718 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004719 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004720
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004721 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004722 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4723 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004724 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004725 WordsToSkip = 0;
Eli Friedman8cbca202012-11-06 22:15:52 +00004726 WordsToScan = IvarsInfo[i].ivar_size;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004727 }
4728 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004729 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004730 SKIP_SCAN SkScan;
4731 SkScan.skip = WordsToSkip;
4732 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004733 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004734 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004735
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004736 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004737 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedman8cbca202012-11-06 22:15:52 +00004738 int LastByteSkipped =
4739 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004740 LastIndex = IvarsInfo.size()-1;
Eli Friedman8cbca202012-11-06 22:15:52 +00004741 int LastByteScanned =
4742 IvarsInfo[LastIndex].ivar_bytepos +
4743 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004744 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004745 if (LastByteSkipped > LastByteScanned) {
Eli Friedman8cbca202012-11-06 22:15:52 +00004746 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004747 SKIP_SCAN SkScan;
Eli Friedman8cbca202012-11-06 22:15:52 +00004748 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004749 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004750 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004751 }
4752 }
4753 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4754 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004755 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004756 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004757 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4758 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4759 // 0xM0 followed by 0x0N detected.
4760 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4761 for (int j = i+1; j < SkipScan; j++)
4762 SkipScanIvars[j] = SkipScanIvars[j+1];
4763 --SkipScan;
4764 }
4765 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004766
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004767 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004768 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004769 unsigned char byte;
4770 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4771 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4772 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4773 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004774
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004775 // first skip big.
4776 for (unsigned int ix = 0; ix < skip_big; ix++)
4777 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004778
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004779 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004780 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004781 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004782 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004783 byte |= 0xf;
4784 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004785 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004786 byte |= scan_small;
4787 scan_small = 0;
4788 }
4789 BitMap += byte;
4790 }
4791 // next scan big
4792 for (unsigned int ix = 0; ix < scan_big; ix++)
4793 BitMap += (unsigned char)(0x0f);
4794 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004795 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004796 byte = scan_small;
4797 BitMap += byte;
4798 }
4799 }
4800 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004801 unsigned char zero = 0;
4802 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004803
4804 llvm::GlobalVariable * Entry =
4805 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004806 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004807 ((ObjCABI == 2) ?
4808 "__TEXT,__objc_classname,cstring_literals" :
4809 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004810 1, true);
4811 return getConstantGEP(VMContext, Entry, 0, 0);
4812}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004813
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004814/// BuildIvarLayout - Builds ivar layout bitmap for the class
4815/// implementation for the __strong or __weak case.
4816/// The layout map displays which words in ivar list must be skipped
4817/// and which must be scanned by GC (see below). String is built of bytes.
4818/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4819/// of words to skip and right nibble is count of words to scan. So, each
4820/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4821/// represented by a 0x00 byte which also ends the string.
4822/// 1. when ForStrongLayout is true, following ivars are scanned:
4823/// - id, Class
4824/// - object *
4825/// - __strong anything
4826///
4827/// 2. When ForStrongLayout is false, following ivars are scanned:
4828/// - __weak anything
4829///
4830llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4831 const ObjCImplementationDecl *OMD,
4832 bool ForStrongLayout) {
4833 bool hasUnion = false;
4834
Chris Lattnerece04092012-02-07 00:39:47 +00004835 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004836 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4837 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004838 return llvm::Constant::getNullValue(PtrTy);
4839
Jordy Rosea91768e2011-07-22 02:08:32 +00004840 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4841 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004842 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004843 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004844 IVD; IVD = IVD->getNextIvar())
4845 RecFields.push_back(cast<FieldDecl>(IVD));
4846 }
4847 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004848 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004849 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004850
Jordy Rosea91768e2011-07-22 02:08:32 +00004851 // FIXME: This is not ideal; we shouldn't have to do this copy.
4852 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004853 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004854
4855 if (RecFields.empty())
4856 return llvm::Constant::getNullValue(PtrTy);
4857
4858 SkipIvars.clear();
4859 IvarsInfo.clear();
4860
Eli Friedman8cbca202012-11-06 22:15:52 +00004861 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004862 if (IvarsInfo.empty())
4863 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004864 // Sort on byte position in case we encounterred a union nested in
4865 // the ivar list.
4866 if (hasUnion && !IvarsInfo.empty())
4867 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4868 if (hasUnion && !SkipIvars.empty())
4869 std::sort(SkipIvars.begin(), SkipIvars.end());
4870
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004871 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004872 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004873
David Blaikiebbafb8a2012-03-11 07:00:24 +00004874 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004875 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004876 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004877 OMD->getClassInterface()->getName().data());
Roman Divackye6377112012-09-06 15:59:27 +00004878 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004879 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004880 if (!(s[i] & 0xf0))
4881 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4882 else
4883 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4884 printf("\n");
4885 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004886 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004887}
4888
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004889llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004890 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4891
Chris Lattner3def9ae2012-02-06 22:16:34 +00004892 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004893 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004894 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004895 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004896 ((ObjCABI == 2) ?
4897 "__TEXT,__objc_methname,cstring_literals" :
4898 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004899 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004900
Owen Anderson170229f2009-07-14 23:10:40 +00004901 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004902}
4903
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004904// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004905llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004906 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4907}
4908
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004909llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004910 std::string TypeStr;
4911 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4912
4913 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004914
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004915 if (!Entry)
4916 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004917 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004918 ((ObjCABI == 2) ?
4919 "__TEXT,__objc_methtype,cstring_literals" :
4920 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004921 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004922
Owen Anderson170229f2009-07-14 23:10:40 +00004923 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004924}
4925
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004926llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4927 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004928 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004929 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004930 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004931
4932 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4933
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004934 if (!Entry)
4935 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004936 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004937 ((ObjCABI == 2) ?
4938 "__TEXT,__objc_methtype,cstring_literals" :
4939 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004940 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004941
Owen Anderson170229f2009-07-14 23:10:40 +00004942 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004943}
4944
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004945// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004946llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004947 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004948
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004949 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004950 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004951 llvm::ConstantDataArray::getString(VMContext,
4952 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004953 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004954 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004955
Owen Anderson170229f2009-07-14 23:10:40 +00004956 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004957}
4958
4959// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004960// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004961llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4963 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004964 std::string TypeStr;
4965 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004966 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4967}
4968
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004969void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004970 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004971 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004972 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004973 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004974 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4975 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004977 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004978 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004979 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004980}
4981
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004982void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004983 EmitModuleInfo();
4984
Daniel Dunbarc475d422008-10-29 22:36:39 +00004985 // Emit the dummy bodies for any protocols which were referenced but
4986 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004987 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004988 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4989 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004990 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004991
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004992 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004993 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004994 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004995 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004996 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004997 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004998 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004999 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00005000 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005001 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00005002 }
5003
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005004 // Add assembler directives to add lazy undefined symbol references
5005 // for classes which are referenced but not defined. This is
5006 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00005007 //
5008 // FIXME: It would be nice if we had an LLVM construct for this.
5009 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005010 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00005011 Asm += CGM.getModule().getModuleInlineAsm();
5012 if (!Asm.empty() && Asm.back() != '\n')
5013 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005014
Daniel Dunbard027a922009-09-07 00:20:42 +00005015 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00005016 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
5017 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00005018 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
5019 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00005020 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005021 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00005022 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005023 }
5024
Bill Wendling53136852012-02-07 09:06:01 +00005025 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00005026 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
5027 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
5028 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00005029
Daniel Dunbard027a922009-09-07 00:20:42 +00005030 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00005031 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00005032}
5033
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005034CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005035 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00005036 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005037 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005038 ObjCABI = 2;
5039}
5040
Daniel Dunbar3ad53482008-08-11 21:35:06 +00005041/* *** */
5042
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005043ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00005044 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
5045{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005046 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5047 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005048
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005049 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005050 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005051 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005052 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00005053 Int8PtrTy = CGM.Int8PtrTy;
5054 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005056 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005057 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005058 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005059
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005060 // I'm not sure I like this. The implicit coordination is a bit
5061 // gross. We should solve this in a reasonable fashion because this
5062 // is a pretty common task (match some runtime data structure with
5063 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005065 // FIXME: This is leaked.
5066 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005067
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005068 // struct _objc_super {
5069 // id self;
5070 // Class cls;
5071 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00005072 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00005073 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00005074 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005075 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005076 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005077 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005078 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005079 Ctx.getObjCClassType(), 0, 0, false,
5080 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00005081 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005083 SuperCTy = Ctx.getTagDeclType(RD);
5084 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005085
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005086 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
5088
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005089 // struct _prop_t {
5090 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005092 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005093 PropertyTy = llvm::StructType::create("struct._prop_t",
5094 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005095
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005096 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005097 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005098 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005099 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005100 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005101 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005102 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
5103 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005104 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005105 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005106
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005107 // struct _objc_method {
5108 // SEL _cmd;
5109 // char *method_type;
5110 // char *_imp;
5111 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005112 MethodTy = llvm::StructType::create("struct._objc_method",
5113 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5114 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005115
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005116 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00005117 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005118 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00005119
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005120}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005121
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005122ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005123 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005124 // struct _objc_method_description {
5125 // SEL name;
5126 // char *types;
5127 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005128 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005129 llvm::StructType::create("struct._objc_method_description",
5130 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005131
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005132 // struct _objc_method_description_list {
5133 // int count;
5134 // struct _objc_method_description[1];
5135 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005136 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005137 llvm::StructType::create("struct._objc_method_description_list",
5138 IntTy,
5139 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005140
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005141 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005143 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005144
Daniel Dunbarb036db82008-08-13 03:21:16 +00005145 // Protocol description structures
5146
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005147 // struct _objc_protocol_extension {
5148 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5149 // struct _objc_method_description_list *optional_instance_methods;
5150 // struct _objc_method_description_list *optional_class_methods;
5151 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005152 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005153 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005155 llvm::StructType::create("struct._objc_protocol_extension",
5156 IntTy, MethodDescriptionListPtrTy,
5157 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005158 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005159
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005160 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005161 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005162
Daniel Dunbarc475d422008-10-29 22:36:39 +00005163 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00005164
Chris Lattnera5f58b02011-07-09 17:41:47 +00005165 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005166 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00005167
Chris Lattnera5f58b02011-07-09 17:41:47 +00005168 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005169 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005170 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005171 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00005172 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005173 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005174
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005175 // struct _objc_protocol {
5176 // struct _objc_protocol_extension *isa;
5177 // char *protocol_name;
5178 // struct _objc_protocol **_objc_protocol_list;
5179 // struct _objc_method_description_list *instance_methods;
5180 // struct _objc_method_description_list *class_methods;
5181 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005182 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5183 llvm::PointerType::getUnqual(ProtocolListTy),
5184 MethodDescriptionListPtrTy,
5185 MethodDescriptionListPtrTy,
5186 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005187
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005188 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005189 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005190
Owen Anderson9793f0e2009-07-29 22:16:19 +00005191 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005192
5193 // Class description structures
5194
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005195 // struct _objc_ivar {
5196 // char *ivar_name;
5197 // char *ivar_type;
5198 // int ivar_offset;
5199 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005200 IvarTy = llvm::StructType::create("struct._objc_ivar",
5201 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005202
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005203 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005204 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005205 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005206 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005207
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005208 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005209 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005210 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005211 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005212
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005213 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005214 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005215 llvm::StructType::create("struct._objc_class_extension",
5216 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005217 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005218
Chris Lattner5ec04a52011-08-12 17:43:31 +00005219 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005220
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005221 // struct _objc_class {
5222 // Class isa;
5223 // Class super_class;
5224 // char *name;
5225 // long version;
5226 // long info;
5227 // long instance_size;
5228 // struct _objc_ivar_list *ivars;
5229 // struct _objc_method_list *methods;
5230 // struct _objc_cache *cache;
5231 // struct _objc_protocol_list *protocols;
5232 // char *ivar_layout;
5233 // struct _objc_class_ext *ext;
5234 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005235 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5236 llvm::PointerType::getUnqual(ClassTy),
5237 Int8PtrTy,
5238 LongTy,
5239 LongTy,
5240 LongTy,
5241 IvarListPtrTy,
5242 MethodListPtrTy,
5243 CachePtrTy,
5244 ProtocolListPtrTy,
5245 Int8PtrTy,
5246 ClassExtensionPtrTy,
5247 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005248
Owen Anderson9793f0e2009-07-29 22:16:19 +00005249 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005250
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005251 // struct _objc_category {
5252 // char *category_name;
5253 // char *class_name;
5254 // struct _objc_method_list *instance_method;
5255 // struct _objc_method_list *class_method;
5256 // uint32_t size; // sizeof(struct _objc_category)
5257 // struct _objc_property_list *instance_properties;// category's @property
5258 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005259 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005260 llvm::StructType::create("struct._objc_category",
5261 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5262 MethodListPtrTy, ProtocolListPtrTy,
5263 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00005264
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005265 // Global metadata structures
5266
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005267 // struct _objc_symtab {
5268 // long sel_ref_cnt;
5269 // SEL *refs;
5270 // short cls_def_cnt;
5271 // short cat_def_cnt;
5272 // char *defs[cls_def_cnt + cat_def_cnt];
5273 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005274 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005275 llvm::StructType::create("struct._objc_symtab",
5276 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5277 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005278 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005279
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00005280 // struct _objc_module {
5281 // long version;
5282 // long size; // sizeof(struct _objc_module)
5283 // char *name;
5284 // struct _objc_symtab* symtab;
5285 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005286 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005287 llvm::StructType::create("struct._objc_module",
5288 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00005289
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005290
Mike Stump18bb9282009-05-16 07:57:57 +00005291 // FIXME: This is the size of the setjmp buffer and should be target
5292 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00005293 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005294
Anders Carlsson9ff22482008-09-09 10:10:21 +00005295 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00005296 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005297
5298 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005299 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00005300 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5301 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00005302
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005303}
5304
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005305ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005306 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005307 // struct _method_list_t {
5308 // uint32_t entsize; // sizeof(struct _objc_method)
5309 // uint32_t method_count;
5310 // struct _objc_method method_list[method_count];
5311 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005312 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005313 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5314 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005315 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005316 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005317
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005318 // struct _protocol_t {
5319 // id isa; // NULL
5320 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005321 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005322 // const struct method_list_t * const instance_methods;
5323 // const struct method_list_t * const class_methods;
5324 // const struct method_list_t *optionalInstanceMethods;
5325 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005326 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005327 // const uint32_t size; // sizeof(struct _protocol_t)
5328 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005329 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005330 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005331
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005332 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005333 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005334 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005335
Chris Lattnera5f58b02011-07-09 17:41:47 +00005336 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005337 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5338 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5339 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5340 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005341 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5342 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005343
5344 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005345 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005346
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005347 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005348 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005349 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005350 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005351 ProtocolListnfABITy->setBody(LongTy,
5352 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5353 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005354
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005355 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005356 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005357
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005358 // struct _ivar_t {
5359 // unsigned long int *offset; // pointer to ivar offset location
5360 // char *name;
5361 // char *type;
5362 // uint32_t alignment;
5363 // uint32_t size;
5364 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005365 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005366 llvm::StructType::create("struct._ivar_t",
5367 llvm::PointerType::getUnqual(LongTy),
5368 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005369
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005370 // struct _ivar_list_t {
5371 // uint32 entsize; // sizeof(struct _ivar_t)
5372 // uint32 count;
5373 // struct _iver_t list[count];
5374 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005375 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005376 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5377 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378
Owen Anderson9793f0e2009-07-29 22:16:19 +00005379 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005380
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005381 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005382 // uint32_t const flags;
5383 // uint32_t const instanceStart;
5384 // uint32_t const instanceSize;
5385 // uint32_t const reserved; // only when building for 64bit targets
5386 // const uint8_t * const ivarLayout;
5387 // const char *const name;
5388 // const struct _method_list_t * const baseMethods;
5389 // const struct _objc_protocol_list *const baseProtocols;
5390 // const struct _ivar_list_t *const ivars;
5391 // const uint8_t * const weakIvarLayout;
5392 // const struct _prop_list_t * const properties;
5393 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005394
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005395 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00005396 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5397 IntTy, IntTy, IntTy, Int8PtrTy,
5398 Int8PtrTy, MethodListnfABIPtrTy,
5399 ProtocolListnfABIPtrTy,
5400 IvarListnfABIPtrTy,
5401 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005402
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005403 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00005404 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00005405 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5406 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005407
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005408 // struct _class_t {
5409 // struct _class_t *isa;
5410 // struct _class_t * const superclass;
5411 // void *cache;
5412 // IMP *vtable;
5413 // struct class_ro_t *ro;
5414 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005415
Chris Lattner5ec04a52011-08-12 17:43:31 +00005416 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005417 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5418 llvm::PointerType::getUnqual(ClassnfABITy),
5419 CachePtrTy,
5420 llvm::PointerType::getUnqual(ImpnfABITy),
5421 llvm::PointerType::getUnqual(ClassRonfABITy),
5422 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005423
Fariborz Jahanian71394042009-01-23 23:53:38 +00005424 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005425 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005426
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005427 // struct _category_t {
5428 // const char * const name;
5429 // struct _class_t *const cls;
5430 // const struct _method_list_t * const instance_methods;
5431 // const struct _method_list_t * const class_methods;
5432 // const struct _protocol_list_t * const protocols;
5433 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00005434 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005435 CategorynfABITy = llvm::StructType::create("struct._category_t",
5436 Int8PtrTy, ClassnfABIPtrTy,
5437 MethodListnfABIPtrTy,
5438 MethodListnfABIPtrTy,
5439 ProtocolListnfABIPtrTy,
5440 PropertyListPtrTy,
5441 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005442
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005443 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005444 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5445 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005446
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005447 // MessageRefTy - LLVM for:
5448 // struct _message_ref_t {
5449 // IMP messenger;
5450 // SEL name;
5451 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005453 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00005454 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00005455 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00005456 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005457 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005458 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005459 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005460 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005461 Ctx.getObjCSelType(), 0, 0, false,
5462 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00005463 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005464
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005465 MessageRefCTy = Ctx.getTagDeclType(RD);
5466 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5467 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005469 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005470 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005472 // SuperMessageRefTy - LLVM for:
5473 // struct _super_message_ref_t {
5474 // SUPER_IMP messenger;
5475 // SEL name;
5476 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005477 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005478 llvm::StructType::create("struct._super_message_ref_t",
5479 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005480
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005481 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005482 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00005483
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005484
5485 // struct objc_typeinfo {
5486 // const void** vtable; // objc_ehtype_vtable + 2
5487 // const char* name; // c++ typeinfo string
5488 // Class cls;
5489 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005490 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005491 llvm::StructType::create("struct._objc_typeinfo",
5492 llvm::PointerType::getUnqual(Int8PtrTy),
5493 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005494 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005495}
5496
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005497llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005498 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005499
Fariborz Jahanian71394042009-01-23 23:53:38 +00005500 return NULL;
5501}
5502
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005503void CGObjCNonFragileABIMac::
5504AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5505 const char *SymbolName,
5506 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00005507 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005508
Daniel Dunbar19573e72009-05-15 21:48:48 +00005509 if (!NumClasses)
5510 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005511
Chris Lattner3def9ae2012-02-06 22:16:34 +00005512 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005513 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00005514 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00005515 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00005516 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005517 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00005518 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00005519 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005520
Daniel Dunbar19573e72009-05-15 21:48:48 +00005521 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005522 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005523 llvm::GlobalValue::InternalLinkage,
5524 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005525 SymbolName);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005526 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00005527 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005528 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005529}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005530
Fariborz Jahanian71394042009-01-23 23:53:38 +00005531void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5532 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533
Daniel Dunbar19573e72009-05-15 21:48:48 +00005534 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005535 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005536 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005537 "\01L_OBJC_LABEL_CLASS_$",
5538 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00005539
Bill Wendling53136852012-02-07 09:06:01 +00005540 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00005541 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5542 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5543 continue;
5544 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005545 }
5546
Bill Wendling53136852012-02-07 09:06:01 +00005547 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005548 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5549 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5550 continue;
5551 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5552 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00005553
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005554 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005555 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5556 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005557
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005558 // Build list of all implemented category addresses in array
5559 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005560 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005561 "\01L_OBJC_LABEL_CATEGORY_$",
5562 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005563 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005564 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5565 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005566
Daniel Dunbar5e639272010-04-25 20:39:01 +00005567 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005568}
5569
John McCall9e8bb002011-05-14 03:10:52 +00005570/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5571/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005572/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005573/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00005574bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5575 // At various points we've experimented with using vtable-based
5576 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005577 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005578 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00005579 return false;
John McCall9e8bb002011-05-14 03:10:52 +00005580 case CodeGenOptions::NonLegacy:
5581 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005582 case CodeGenOptions::Mixed:
5583 break;
5584 }
5585
5586 // If so, see whether this selector is in the white-list of things which must
5587 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00005588 if (VTableDispatchMethods.empty()) {
5589 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5590 VTableDispatchMethods.insert(GetNullarySelector("class"));
5591 VTableDispatchMethods.insert(GetNullarySelector("self"));
5592 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5593 VTableDispatchMethods.insert(GetNullarySelector("length"));
5594 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005595
John McCall9e8bb002011-05-14 03:10:52 +00005596 // These are vtable-based if GC is disabled.
5597 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005598 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00005599 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5600 VTableDispatchMethods.insert(GetNullarySelector("release"));
5601 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5602 }
5603
5604 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5605 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5606 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5607 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5608 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5609 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5610 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5611
5612 // These are vtable-based if GC is enabled.
5613 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005614 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00005615 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5616 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5617
5618 // "countByEnumeratingWithState:objects:count"
5619 IdentifierInfo *KeyIdents[] = {
5620 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5621 &CGM.getContext().Idents.get("objects"),
5622 &CGM.getContext().Idents.get("count")
5623 };
5624 VTableDispatchMethods.insert(
5625 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5626 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005627 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005628
John McCall9e8bb002011-05-14 03:10:52 +00005629 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005630}
5631
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005632/// BuildClassRoTInitializer - generate meta-data for:
5633/// struct _class_ro_t {
5634/// uint32_t const flags;
5635/// uint32_t const instanceStart;
5636/// uint32_t const instanceSize;
5637/// uint32_t const reserved; // only when building for 64bit targets
5638/// const uint8_t * const ivarLayout;
5639/// const char *const name;
5640/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005641/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005642/// const struct _ivar_list_t *const ivars;
5643/// const uint8_t * const weakIvarLayout;
5644/// const struct _prop_list_t * const properties;
5645/// }
5646///
5647llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005648 unsigned flags,
5649 unsigned InstanceStart,
5650 unsigned InstanceSize,
5651 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005652 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005653 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00005654
David Blaikiebbafb8a2012-03-11 07:00:24 +00005655 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallef19dbb2012-10-17 04:53:23 +00005656 flags |= NonFragileABI_Class_CompiledByARC;
John McCall31168b02011-06-15 23:02:42 +00005657
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005658 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5659 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5660 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005661 // FIXME. For 64bit targets add 0 here.
John McCallef19dbb2012-10-17 04:53:23 +00005662 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5663 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005664 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005665 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005666 // const struct _method_list_t * const baseMethods;
5667 std::vector<llvm::Constant*> Methods;
5668 std::string MethodListName("\01l_OBJC_$_");
John McCallef19dbb2012-10-17 04:53:23 +00005669 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005670 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005671 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005672 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005673 // Class methods should always be defined.
5674 Methods.push_back(GetMethodConstant(*i));
5675 }
5676 } else {
5677 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005678 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005679 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005680 // Instance methods should always be defined.
5681 Methods.push_back(GetMethodConstant(*i));
5682 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005683 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005684 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005685 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005686
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005687 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5688 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005689
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005690 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5691 if (llvm::Constant *C = GetMethodConstant(MD))
5692 Methods.push_back(C);
5693 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5694 if (llvm::Constant *C = GetMethodConstant(MD))
5695 Methods.push_back(C);
5696 }
5697 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005698 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699 Values[ 5] = EmitMethodList(MethodListName,
5700 "__DATA, __objc_const", Methods);
5701
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005702 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5703 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005704 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005705 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005706 OID->all_referenced_protocol_begin(),
5707 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005708
John McCallef19dbb2012-10-17 04:53:23 +00005709 if (flags & NonFragileABI_Class_Meta) {
Owen Anderson0b75f232009-07-31 20:28:54 +00005710 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005711 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Anderson0b75f232009-07-31 20:28:54 +00005712 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005713 } else {
5714 Values[ 7] = EmitIvarList(ID);
5715 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005716 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5717 ID, ID->getClassInterface(), ObjCTypes);
John McCallef19dbb2012-10-17 04:53:23 +00005718 }
Owen Anderson0e0189d2009-07-27 22:29:56 +00005719 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005720 Values);
5721 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005722 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5723 llvm::GlobalValue::InternalLinkage,
5724 Init,
John McCallef19dbb2012-10-17 04:53:23 +00005725 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005726 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5727 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005728 CLASS_RO_GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005729 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005730 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005731 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005732
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005733}
5734
5735/// BuildClassMetaData - This routine defines that to-level meta-data
5736/// for the given ClassName for:
5737/// struct _class_t {
5738/// struct _class_t *isa;
5739/// struct _class_t * const superclass;
5740/// void *cache;
5741/// IMP *vtable;
5742/// struct class_ro_t *ro;
5743/// }
5744///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005745llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005746 std::string &ClassName,
5747 llvm::Constant *IsAGV,
5748 llvm::Constant *SuperClassGV,
5749 llvm::Constant *ClassRoGV,
5750 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005751 llvm::Constant *Values[] = {
5752 IsAGV,
5753 SuperClassGV,
5754 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5755 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5756 ClassRoGV // &CLASS_RO_GV
5757 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005758 if (!Values[1])
5759 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005760 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005761 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005762 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5763 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005764 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005765 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005766 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005767 if (HiddenVisibility)
5768 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005769 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005770}
5771
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005772bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005773CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005774 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005775}
5776
Daniel Dunbar961202372009-05-03 12:57:56 +00005777void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005778 uint32_t &InstanceStart,
5779 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005780 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005781 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005782
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005783 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005784 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005785
5786 // If there are no fields, the start is the same as the end.
5787 if (!RL.getFieldCount())
5788 InstanceStart = InstanceSize;
5789 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005790 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005791}
5792
Fariborz Jahanian71394042009-01-23 23:53:38 +00005793void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5794 std::string ClassName = ID->getNameAsString();
5795 if (!ObjCEmptyCacheVar) {
5796 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005797 CGM.getModule(),
5798 ObjCTypes.CacheTy,
5799 false,
5800 llvm::GlobalValue::ExternalLinkage,
5801 0,
5802 "_objc_empty_cache");
5803
Fariborz Jahanian71394042009-01-23 23:53:38 +00005804 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005805 CGM.getModule(),
5806 ObjCTypes.ImpnfABITy,
5807 false,
5808 llvm::GlobalValue::ExternalLinkage,
5809 0,
5810 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005811 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005812 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005813 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005814 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005815 uint32_t InstanceStart =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005816 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005817 uint32_t InstanceSize = InstanceStart;
John McCallef19dbb2012-10-17 04:53:23 +00005818 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005819 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5820 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005821
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005822 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005823
John McCall0d54a172012-10-17 04:53:31 +00005824 // Build the flags for the metaclass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005825 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005826 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005827 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005828 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005829
5830 // FIXME: why is this flag set on the metaclass?
5831 // ObjC metaclasses have no fields and don't really get constructed.
5832 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005833 flags |= NonFragileABI_Class_HasCXXStructors;
John McCall0d54a172012-10-17 04:53:31 +00005834 if (!ID->hasNonZeroConstructors())
5835 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5836 }
5837
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005838 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005839 // class is root
John McCallef19dbb2012-10-17 04:53:23 +00005840 flags |= NonFragileABI_Class_Root;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005841 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005842 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005843 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005844 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005845 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5846 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5847 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005848 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005849 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005850 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005851 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005852 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005853 ObjCMetaClassName +
5854 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005855 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005856 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005857 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005858 }
5859 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5860 InstanceStart,
5861 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005862 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005863 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005864 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5865 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005866 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005867
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005868 // Metadata for the class
John McCallef19dbb2012-10-17 04:53:23 +00005869 flags = 0;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005870 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005871 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005872
5873 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005874 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005875
John McCall0d54a172012-10-17 04:53:31 +00005876 // Set a flag to enable a runtime optimization when a class has
5877 // fields that require destruction but which don't require
5878 // anything except zero-initialization during construction. This
5879 // is most notably true of __strong and __weak types, but you can
5880 // also imagine there being C++ types with non-trivial default
5881 // constructors that merely set all fields to null.
5882 if (!ID->hasNonZeroConstructors())
5883 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5884 }
5885
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005886 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCallef19dbb2012-10-17 04:53:23 +00005887 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005888
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005889 if (!ID->getClassInterface()->getSuperClass()) {
John McCallef19dbb2012-10-17 04:53:23 +00005890 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005891 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005892 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005893 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005894 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005895 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005896 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005897 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005898 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005899 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005900 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005901 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005902 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005903 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005904 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005905
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005906 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005907 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005908 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5909 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005910 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005911
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005912 // Determine if this class is also "non-lazy".
5913 if (ImplementationIsNonLazy(ID))
5914 DefinedNonLazyClasses.push_back(ClassMD);
5915
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005916 // Force the definition of the EHType if necessary.
John McCallef19dbb2012-10-17 04:53:23 +00005917 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005918 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005919 // Make sure method definition entries are all clear for next implementation.
5920 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005921}
5922
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005923/// GenerateProtocolRef - This routine is called to generate code for
5924/// a protocol reference expression; as in:
5925/// @code
5926/// @protocol(Proto1);
5927/// @endcode
5928/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5929/// which will hold address of the protocol meta-data.
5930///
5931llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932 const ObjCProtocolDecl *PD) {
5933
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005934 // This routine is called for @protocol only. So, we must build definition
5935 // of protocol's meta-data (not a reference to it!)
5936 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005937 llvm::Constant *Init =
5938 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005939 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005940
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005941 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005942 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005944 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5945 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005946 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005947 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005948 CGM.getModule(),
5949 Init->getType(), false,
5950 llvm::GlobalValue::WeakAnyLinkage,
5951 Init,
5952 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005953 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5954 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005955 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005956 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005957}
5958
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005959/// GenerateCategory - Build metadata for a category implementation.
5960/// struct _category_t {
5961/// const char * const name;
5962/// struct _class_t *const cls;
5963/// const struct _method_list_t * const instance_methods;
5964/// const struct _method_list_t * const class_methods;
5965/// const struct _protocol_list_t * const protocols;
5966/// const struct _prop_list_t * const properties;
5967/// }
5968///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005969void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005970 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005971 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005972 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5973 "_$_" + OCD->getNameAsString());
5974 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005975 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005976
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005977 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005978 Values[0] = GetClassName(OCD->getIdentifier());
5979 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005980 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005981 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005982 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5983
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005984 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005985 std::vector<llvm::Constant*> Methods;
5986 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005987 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005988 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005989
5990 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005991 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005992 // Instance methods should always be defined.
5993 Methods.push_back(GetMethodConstant(*i));
5994 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005995
5996 Values[2] = EmitMethodList(MethodListName,
5997 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005998 Methods);
5999
6000 MethodListName = Prefix;
6001 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
6002 OCD->getNameAsString();
6003 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006004 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006005 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00006006 // Class methods should always be defined.
6007 Methods.push_back(GetMethodConstant(*i));
6008 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006009
6010 Values[3] = EmitMethodList(MethodListName,
6011 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00006012 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006013 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00006014 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006015 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00006016 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006017 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
6018 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006019 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006020 + Interface->getName() + "_$_"
6021 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006022 Category->protocol_begin(),
6023 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006024 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
6025 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00006026 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00006027 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
6028 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00006029 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006030
6031 llvm::Constant *Init =
6032 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006033 Values);
6034 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006035 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006036 false,
6037 llvm::GlobalValue::InternalLinkage,
6038 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006039 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006040 GCATV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006041 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006042 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006043 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006044 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00006045
6046 // Determine if this category is also "non-lazy".
6047 if (ImplementationIsNonLazy(OCD))
6048 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00006049 // method definition entries must be clear for next implementation.
6050 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00006051}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006052
6053/// GetMethodConstant - Return a struct objc_method constant for the
6054/// given method if it has been defined. The result is null if the
6055/// method has not been defined. The return value has type MethodPtrTy.
6056llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006057 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00006058 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006059 if (!Fn)
6060 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006061
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006062 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00006063 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006064 ObjCTypes.SelectorPtrTy),
6065 GetMethodVarType(MD),
6066 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
6067 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00006068 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006069}
6070
6071/// EmitMethodList - Build meta-data for method declarations
6072/// struct _method_list_t {
6073/// uint32_t entsize; // sizeof(struct _objc_method)
6074/// uint32_t method_count;
6075/// struct _objc_method method_list[method_count];
6076/// }
6077///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00006078llvm::Constant *
6079CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
6080 const char *Section,
6081 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006082 // Return null for empty list.
6083 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00006084 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006085
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006086 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006087 // sizeof(struct _objc_method)
Micah Villmowdd31ca12012-10-08 16:25:52 +00006088 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006089 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006090 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006091 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00006092 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006093 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00006094 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006095 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006096
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006097 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006098 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006099 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmowdd31ca12012-10-08 16:25:52 +00006100 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006101 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006102 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006103 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00006104}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006105
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006106/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6107/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00006108llvm::GlobalVariable *
6109CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6110 const ObjCIvarDecl *Ivar) {
6111 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00006112 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00006113 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006114 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006115 CGM.getModule().getGlobalVariable(Name);
6116 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006117 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006118 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006119 false,
6120 llvm::GlobalValue::ExternalLinkage,
6121 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006122 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006123 return IvarOffsetGV;
6124}
6125
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00006126llvm::Constant *
6127CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6128 const ObjCIvarDecl *Ivar,
Eli Friedman8cbca202012-11-06 22:15:52 +00006129 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006130 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006131 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedman8cbca202012-11-06 22:15:52 +00006132 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006133 IvarOffsetGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006134 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006135
Mike Stump18bb9282009-05-16 07:57:57 +00006136 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6137 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006138 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6139 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00006140 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00006141 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006142 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00006143 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00006144 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006145 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006146}
6147
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006148/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006149/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006150/// IvarListnfABIPtrTy.
6151/// struct _ivar_t {
6152/// unsigned long int *offset; // pointer to ivar offset location
6153/// char *name;
6154/// char *type;
6155/// uint32_t alignment;
6156/// uint32_t size;
6157/// }
6158/// struct _ivar_list_t {
6159/// uint32 entsize; // sizeof(struct _ivar_t)
6160/// uint32 count;
6161/// struct _iver_t list[count];
6162/// }
6163///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00006164
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006165llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006166 const ObjCImplementationDecl *ID) {
6167
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006168 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006169
Jordy Rosea91768e2011-07-22 02:08:32 +00006170 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006171 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006172
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006173 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006174
Jordy Rosea91768e2011-07-22 02:08:32 +00006175 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00006176 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00006177 // Ignore unnamed bit-fields.
6178 if (!IVD->getDeclName())
6179 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006180 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006181 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00006182 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006183 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6184 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00006185 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006186 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00006187 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006188 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006189 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006190 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006191 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00006192 // NOTE. Size of a bitfield does not match gcc's, because of the
6193 // way bitfields are treated special in each. But I am told that
6194 // 'size' for bitfield ivars is ignored by the runtime so it does
6195 // not matter. If it matters, there is enough info to get the
6196 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006197 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006198 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006199 }
6200 // Return null for empty list.
6201 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00006202 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006203
6204 llvm::Constant *Values[3];
Micah Villmowdd31ca12012-10-08 16:25:52 +00006205 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006206 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6207 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00006208 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006209 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00006210 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006211 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006212 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6213 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006214 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006215 llvm::GlobalValue::InternalLinkage,
6216 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006217 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006218 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006219 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006220 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006221
Chris Lattnerf56501c2009-07-17 23:57:13 +00006222 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00006223 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006224}
6225
6226llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006227 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006228 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006229
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006230 if (!Entry) {
6231 // We use the initializer as a marker of whether this is a forward
6232 // reference or not. At module finalization we add the empty
6233 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006234 Entry =
6235 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6236 llvm::GlobalValue::ExternalLinkage,
6237 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006238 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006239 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006240 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006241
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006242 return Entry;
6243}
6244
6245/// GetOrEmitProtocol - Generate the protocol meta-data:
6246/// @code
6247/// struct _protocol_t {
6248/// id isa; // NULL
6249/// const char * const protocol_name;
6250/// const struct _protocol_list_t * protocol_list; // super protocols
6251/// const struct method_list_t * const instance_methods;
6252/// const struct method_list_t * const class_methods;
6253/// const struct method_list_t *optionalInstanceMethods;
6254/// const struct method_list_t *optionalClassMethods;
6255/// const struct _prop_list_t * properties;
6256/// const uint32_t size; // sizeof(struct _protocol_t)
6257/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006258/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006259/// }
6260/// @endcode
6261///
6262
6263llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006264 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00006265 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006266
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006267 // Early exit if a defining object has already been generated.
6268 if (Entry && Entry->hasInitializer())
6269 return Entry;
6270
Douglas Gregora715bff2012-01-01 19:51:50 +00006271 // Use the protocol definition, if there is one.
6272 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6273 PD = Def;
6274
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006275 // Construct method lists.
6276 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6277 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006278 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006279 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006280 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006281 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006282 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006283 if (!C)
6284 return GetOrEmitProtocolRef(PD);
6285
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006286 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6287 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006288 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006289 } else {
6290 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006291 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006292 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006293 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006294
6295 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006296 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006297 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006298 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006299 if (!C)
6300 return GetOrEmitProtocolRef(PD);
6301
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006302 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6303 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006304 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006305 } else {
6306 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006307 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006308 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006309 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006310
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006311 MethodTypesExt.insert(MethodTypesExt.end(),
6312 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6313
6314 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006315 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00006316 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006317 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006318 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6319 PD->protocol_begin(),
6320 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006321
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006322 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006323 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006324 "__DATA, __objc_const",
6325 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006326 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006327 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006328 "__DATA, __objc_const",
6329 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006330 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006331 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006332 "__DATA, __objc_const",
6333 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006334 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006335 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006336 "__DATA, __objc_const",
6337 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006338 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006339 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006340 uint32_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00006341 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006342 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00006343 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006344 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6345 + PD->getName(),
6346 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006347 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006348 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006349
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006350 if (Entry) {
6351 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00006352 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006353 Entry->setInitializer(Init);
6354 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006355 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006356 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6357 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6358 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006359 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006360 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006361 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00006362
6363 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006364 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006365 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006366 CGM.AddUsedGlobal(Entry);
6367
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006368 // Use this protocol meta-data to build protocol list table in section
6369 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006370 llvm::GlobalVariable *PTGV =
6371 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6372 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6373 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006374 PTGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006375 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00006376 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006377 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006378 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006379 return Entry;
6380}
6381
6382/// EmitProtocolList - Generate protocol list meta-data:
6383/// @code
6384/// struct _protocol_list_t {
6385/// long protocol_count; // Note, this is 32/64 bit
6386/// struct _protocol_t[protocol_count];
6387/// }
6388/// @endcode
6389///
6390llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006391CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006392 ObjCProtocolDecl::protocol_iterator begin,
6393 ObjCProtocolDecl::protocol_iterator end) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006394 SmallVector<llvm::Constant *, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006395
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006396 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006397 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00006398 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006399
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006400 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00006401 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006402 Name.toVector(TmpName);
6403 llvm::GlobalVariable *GV =
6404 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006405 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006406 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006407
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006408 for (; begin != end; ++begin)
6409 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6410
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006411 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00006412 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006413 ObjCTypes.ProtocolnfABIPtrTy));
6414
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006415 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00006416 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006417 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006418 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00006419 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6420 ProtocolRefs.size()),
6421 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006422
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006423 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00006424 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006425 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006426 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006427 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006428 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006429 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00006430 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006431 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006432 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006433}
6434
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006435/// GetMethodDescriptionConstant - This routine build following meta-data:
6436/// struct _objc_method {
6437/// SEL _cmd;
6438/// char *method_type;
6439/// char *_imp;
6440/// }
6441
6442llvm::Constant *
6443CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006444 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00006445 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006446 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6447 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006448 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006449 if (!Desc[1])
6450 return 0;
6451
Fariborz Jahanian097feda2009-01-30 18:58:59 +00006452 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00006453 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006454 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006455}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006456
6457/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6458/// This code gen. amounts to generating code for:
6459/// @code
6460/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6461/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006462///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00006463LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00006464 CodeGen::CodeGenFunction &CGF,
6465 QualType ObjectTy,
6466 llvm::Value *BaseValue,
6467 const ObjCIvarDecl *Ivar,
6468 unsigned CVRQualifiers) {
6469 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006470 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
Saleem Abdulrasool5f25bc32013-02-17 04:03:34 +00006471
6472 if (IsIvarOffsetKnownIdempotent(CGF, ID, Ivar))
6473 if (llvm::LoadInst *LI = cast<llvm::LoadInst>(Offset))
6474 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6475 llvm::MDNode::get(VMContext, ArrayRef<llvm::Value*>()));
6476
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00006477 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006478 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006479}
6480
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006481llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006482 CodeGen::CodeGenFunction &CGF,
6483 const ObjCInterfaceDecl *Interface,
6484 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00006485 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006486}
6487
John McCall234eac82011-05-13 23:16:18 +00006488static void appendSelectorForMessageRefTable(std::string &buffer,
6489 Selector selector) {
6490 if (selector.isUnarySelector()) {
6491 buffer += selector.getNameForSlot(0);
6492 return;
6493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006494
John McCall234eac82011-05-13 23:16:18 +00006495 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6496 buffer += selector.getNameForSlot(i);
6497 buffer += '_';
6498 }
6499}
6500
John McCall9e8bb002011-05-14 03:10:52 +00006501/// Emit a "v-table" message send. We emit a weak hidden-visibility
6502/// struct, initially containing the selector pointer and a pointer to
6503/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6504/// load and call the function pointer, passing the address of the
6505/// struct as the second parameter. The runtime determines whether
6506/// the selector is currently emitted using vtable dispatch; if so, it
6507/// substitutes a stub function which simply tail-calls through the
6508/// appropriate vtable slot, and if not, it substitues a stub function
6509/// which tail-calls objc_msgSend. Both stubs adjust the selector
6510/// argument to correctly point to the selector.
6511RValue
6512CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6513 ReturnValueSlot returnSlot,
6514 QualType resultType,
6515 Selector selector,
6516 llvm::Value *arg0,
6517 QualType arg0Type,
6518 bool isSuper,
6519 const CallArgList &formalArgs,
6520 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00006521 // Compute the actual arguments.
6522 CallArgList args;
6523
John McCall9e8bb002011-05-14 03:10:52 +00006524 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00006525 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00006526 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6527 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00006528
John McCall9e8bb002011-05-14 03:10:52 +00006529 // Second argument: a pointer to the message ref structure. Leave
6530 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00006531 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6532
6533 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6534
John McCalla729c622012-02-17 03:33:10 +00006535 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00006536
John McCall5880fb82011-05-14 21:12:11 +00006537 NullReturnState nullReturn;
6538
John McCall9e8bb002011-05-14 03:10:52 +00006539 // Find the function to call and the mangled name for the message
6540 // ref structure. Using a different mangled name wouldn't actually
6541 // be a problem; it would just be a waste.
6542 //
6543 // The runtime currently never uses vtable dispatch for anything
6544 // except normal, non-super message-sends.
6545 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00006546 llvm::Constant *fn = 0;
6547 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00006548 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00006549 if (isSuper) {
6550 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6551 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006552 } else {
John McCall5880fb82011-05-14 21:12:11 +00006553 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00006554 fn = ObjCTypes.getMessageSendStretFixupFn();
6555 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006556 }
John McCall234eac82011-05-13 23:16:18 +00006557 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6558 fn = ObjCTypes.getMessageSendFpretFixupFn();
6559 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00006560 } else {
John McCall234eac82011-05-13 23:16:18 +00006561 if (isSuper) {
6562 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6563 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006564 } else {
John McCall234eac82011-05-13 23:16:18 +00006565 fn = ObjCTypes.getMessageSendFixupFn();
6566 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006567 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00006568 }
John McCall234eac82011-05-13 23:16:18 +00006569 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6570 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006571
John McCall234eac82011-05-13 23:16:18 +00006572 // Append the selector name, except use underscores anywhere we
6573 // would have used colons.
6574 appendSelectorForMessageRefTable(messageRefName, selector);
6575
6576 llvm::GlobalVariable *messageRef
6577 = CGM.getModule().getGlobalVariable(messageRefName);
6578 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00006579 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00006580 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006581 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00006582 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6583 init->getType(),
6584 /*constant*/ false,
6585 llvm::GlobalValue::WeakAnyLinkage,
6586 init,
6587 messageRefName);
6588 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6589 messageRef->setAlignment(16);
6590 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6591 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006592
6593 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00006594 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006595 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6596 e = method->param_end(); i != e; ++i) {
6597 const ParmVarDecl *ParamDecl = (*i);
6598 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6599 if (!nullReturn.NullBB)
6600 nullReturn.init(CGF, arg0);
6601 requiresnullCheck = true;
6602 break;
6603 }
6604 }
6605
John McCall234eac82011-05-13 23:16:18 +00006606 llvm::Value *mref =
6607 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6608
John McCall9e8bb002011-05-14 03:10:52 +00006609 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00006610 args[1].RV = RValue::get(mref);
6611
6612 // Load the function to call from the message ref table.
6613 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6614 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6615
John McCalla729c622012-02-17 03:33:10 +00006616 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00006617
John McCalla729c622012-02-17 03:33:10 +00006618 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006619 return nullReturn.complete(CGF, result, resultType, formalArgs,
6620 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006621}
6622
6623/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006624CodeGen::RValue
6625CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006626 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006627 QualType ResultType,
6628 Selector Sel,
6629 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006630 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00006631 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006632 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00006633 return isVTableDispatchedSelector(Sel)
6634 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006635 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006636 false, CallArgs, Method)
6637 : EmitMessageSend(CGF, Return, ResultType,
6638 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006639 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006640 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006641}
6642
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006643llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006644CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006645 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6646
Daniel Dunbara6468342009-03-02 05:18:14 +00006647 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006648 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006649 false, llvm::GlobalValue::ExternalLinkage,
6650 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006651 }
6652
6653 return GV;
6654}
6655
John McCall31168b02011-06-15 23:02:42 +00006656llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6657 IdentifierInfo *II) {
6658 llvm::GlobalVariable *&Entry = ClassReferences[II];
6659
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006660 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00006661 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006662 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006663 Entry =
John McCall31168b02011-06-15 23:02:42 +00006664 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6665 false, llvm::GlobalValue::InternalLinkage,
6666 ClassGV,
6667 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006668 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006669 CGM.getDataLayout().getABITypeAlignment(
John McCall31168b02011-06-15 23:02:42 +00006670 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006671 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006672 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006673 }
John McCall31168b02011-06-15 23:02:42 +00006674
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006675 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006676}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006677
John McCall31168b02011-06-15 23:02:42 +00006678llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6679 const ObjCInterfaceDecl *ID) {
6680 return EmitClassRefFromId(Builder, ID->getIdentifier());
6681}
6682
6683llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6684 CGBuilderTy &Builder) {
6685 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6686 return EmitClassRefFromId(Builder, II);
6687}
6688
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006689llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006690CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006691 const ObjCInterfaceDecl *ID) {
6692 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006693
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006694 if (!Entry) {
6695 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6696 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006697 Entry =
6698 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006699 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006700 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006701 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006702 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006703 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006704 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006705 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006706 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006707 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006708
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006709 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006710}
6711
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006712/// EmitMetaClassRef - Return a Value * of the address of _class_t
6713/// meta-data
6714///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006715llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6716 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006717 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6718 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006719 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006720
Daniel Dunbar15894b72009-04-07 05:48:37 +00006721 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006722 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006723 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006724 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006725 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006726 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006727 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006728 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006729 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006730 ObjCTypes.ClassnfABIPtrTy));
6731
Daniel Dunbare60aa052009-04-15 19:03:14 +00006732 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006733 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006734
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006735 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006736}
6737
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006738/// GetClass - Return a reference to the class for the given interface
6739/// decl.
6740llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6741 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006742 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006743 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6744 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6745 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6746 }
6747
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006748 return EmitClassRef(Builder, ID);
6749}
6750
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006751/// Generates a message send where the super is the receiver. This is
6752/// a message send to self with special delivery semantics indicating
6753/// which class's method should be called.
6754CodeGen::RValue
6755CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006756 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006757 QualType ResultType,
6758 Selector Sel,
6759 const ObjCInterfaceDecl *Class,
6760 bool isCategoryImpl,
6761 llvm::Value *Receiver,
6762 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006763 const CodeGen::CallArgList &CallArgs,
6764 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006765 // ...
6766 // Create and init a super structure; this is a (receiver, class)
6767 // pair we will pass to objc_msgSendSuper.
6768 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006769 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006770
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006771 llvm::Value *ReceiverAsObject =
6772 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6773 CGF.Builder.CreateStore(ReceiverAsObject,
6774 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006775
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006776 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006777 llvm::Value *Target;
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006778 if (IsClassMessage)
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006779 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006780 else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006781 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006782
Mike Stump18bb9282009-05-16 07:57:57 +00006783 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6784 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006785 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006786 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6787 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6788 CGF.Builder.CreateStore(Target,
6789 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006790
John McCall9e8bb002011-05-14 03:10:52 +00006791 return (isVTableDispatchedSelector(Sel))
6792 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006793 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006794 true, CallArgs, Method)
6795 : EmitMessageSend(CGF, Return, ResultType,
6796 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006797 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006798 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006799}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006800
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006801llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006802 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006803 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006804
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006805 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006806 llvm::Constant *Casted =
6807 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6808 ObjCTypes.SelectorPtrTy);
6809 Entry =
6810 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6811 llvm::GlobalValue::InternalLinkage,
6812 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Michael Gottesman5c205962013-02-05 23:08:45 +00006813 Entry->setExternallyInitialized(true);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006814 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006815 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006816 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006817
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006818 if (lval)
6819 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006820 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6821
6822 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6823 llvm::MDNode::get(VMContext,
6824 ArrayRef<llvm::Value*>()));
6825 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006826}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006827/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006828/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006829///
6830void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006831 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006832 llvm::Value *dst,
6833 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006834 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006835 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006836 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006837 assert(Size <= 8 && "does not support size > 8");
6838 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6839 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006840 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6841 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006842 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6843 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006844 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6845 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006846 return;
6847}
6848
6849/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6850/// objc_assign_strongCast (id src, id *dst)
6851///
6852void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006853 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006854 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006855 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006856 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006857 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006858 assert(Size <= 8 && "does not support size > 8");
6859 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006860 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006861 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6862 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006863 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6864 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006865 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006866 src, dst, "weakassign");
6867 return;
6868}
6869
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006870void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006871 CodeGen::CodeGenFunction &CGF,
6872 llvm::Value *DestPtr,
6873 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006874 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006875 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6876 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006877 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006878 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006879 return;
6880}
6881
Fariborz Jahanian06292952009-02-16 22:52:32 +00006882/// EmitObjCWeakRead - Code gen for loading value of a __weak
6883/// object: objc_read_weak (id *src)
6884///
6885llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006886 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006887 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006888 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006889 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6890 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006891 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006892 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006893 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006894 return read_weak;
6895}
6896
6897/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6898/// objc_assign_weak (id src, id *dst)
6899///
6900void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006901 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006902 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006903 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006904 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006905 assert(Size <= 8 && "does not support size > 8");
6906 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6907 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006908 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6909 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006910 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6911 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006912 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006913 src, dst, "weakassign");
6914 return;
6915}
6916
6917/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6918/// objc_assign_global (id src, id *dst)
6919///
6920void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006921 llvm::Value *src, llvm::Value *dst,
6922 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006923 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006924 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006925 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006926 assert(Size <= 8 && "does not support size > 8");
6927 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6928 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006929 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6930 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006931 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6932 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006933 if (!threadlocal)
6934 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6935 src, dst, "globalassign");
6936 else
6937 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6938 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006939 return;
6940}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006941
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006942void
John McCallbd309292010-07-06 01:34:17 +00006943CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6944 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006945 EmitAtSynchronizedStmt(CGF, S,
6946 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6947 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006948}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006949
John McCall2ca705e2010-07-24 00:37:23 +00006950llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006951CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006952 // There's a particular fixed type info for 'id'.
6953 if (T->isObjCIdType() ||
6954 T->isObjCQualifiedIdType()) {
6955 llvm::Constant *IDEHType =
6956 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6957 if (!IDEHType)
6958 IDEHType =
6959 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6960 false,
6961 llvm::GlobalValue::ExternalLinkage,
6962 0, "OBJC_EHTYPE_id");
6963 return IDEHType;
6964 }
6965
6966 // All other types should be Objective-C interface pointer types.
6967 const ObjCObjectPointerType *PT =
6968 T->getAs<ObjCObjectPointerType>();
6969 assert(PT && "Invalid @catch type.");
6970 const ObjCInterfaceType *IT = PT->getInterfaceType();
6971 assert(IT && "Invalid @catch type.");
6972 return GetInterfaceEHType(IT->getDecl(), false);
6973}
6974
John McCallbd309292010-07-06 01:34:17 +00006975void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6976 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006977 EmitTryCatchStmt(CGF, S,
6978 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6979 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6980 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006981}
6982
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006983/// EmitThrowStmt - Generate code for a throw statement.
6984void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00006985 const ObjCAtThrowStmt &S,
6986 bool ClearInsertionPoint) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006987 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006988 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006989 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006990 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006991 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006992 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006993 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006994 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006995 }
6996
John McCall17afe452010-10-16 08:21:07 +00006997 CGF.Builder.CreateUnreachable();
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00006998 if (ClearInsertionPoint)
6999 CGF.Builder.ClearInsertionPoint();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00007000}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007001
John McCall2ca705e2010-07-24 00:37:23 +00007002llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007003CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007004 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007005 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007006
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007007 // If we don't need a definition, return the entry if found or check
7008 // if we use an external reference.
7009 if (!ForDefinition) {
7010 if (Entry)
7011 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00007012
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007013 // If this type (or a super class) has the __objc_exception__
7014 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00007015 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007016 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00007017 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007018 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007019 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00007020 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00007021 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007022 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007023
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007024 // Otherwise we need to either make a new entry or fill in the
7025 // initializer.
7026 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00007027 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007028 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007029 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007030 CGM.getModule().getGlobalVariable(VTableName);
7031 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007032 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
7033 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007034 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00007035 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007036
Chris Lattnerece04092012-02-07 00:39:47 +00007037 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007038
Benjamin Kramer22d24c22011-10-15 12:20:02 +00007039 llvm::Constant *Values[] = {
7040 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
7041 GetClassName(ID->getIdentifier()),
7042 GetClassGlobal(ClassName)
7043 };
Owen Anderson170229f2009-07-14 23:10:40 +00007044 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00007045 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007046
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007047 if (Entry) {
7048 Entry->setInitializer(Init);
7049 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00007050 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007051 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007052 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00007053 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00007054 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007055 }
7056
John McCalla4960982013-02-19 01:57:29 +00007057 if (ID->getVisibility() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00007058 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmowdd31ca12012-10-08 16:25:52 +00007059 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00007060 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00007061
7062 if (ForDefinition) {
7063 Entry->setSection("__DATA,__objc_const");
7064 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
7065 } else {
7066 Entry->setSection("__DATA,__datacoal_nt,coalesced");
7067 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00007068
7069 return Entry;
7070}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00007071
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00007072/* *** */
7073
Daniel Dunbarb036db82008-08-13 03:21:16 +00007074CodeGen::CGObjCRuntime *
7075CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00007076 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
7077 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00007078 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00007079
7080 case ObjCRuntime::MacOSX:
7081 case ObjCRuntime::iOS:
7082 return new CGObjCNonFragileABIMac(CGM);
7083
David Chisnallb601c962012-07-03 20:49:52 +00007084 case ObjCRuntime::GNUstep:
7085 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00007086 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00007087 llvm_unreachable("these runtimes are not Mac runtimes");
7088 }
7089 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00007090}