blob: 01179cca6b975c8cb5b492cd4aa1875b58689979 [file] [log] [blame]
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbar303e2c22008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Micah Villmowdd31ca12012-10-08 16:25:52 +000039#include "llvm/DataLayout.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000046
Daniel Dunbar59e476b2009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000049
Fariborz Jahanian279eda62009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000053
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000054private:
John McCall9dc0db22011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCall31168b02011-06-15 23:02:42 +000068 "objc_msgSend",
Bill Wendling311c8322012-10-15 04:47:45 +000069 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendling507c3512012-10-16 05:23:44 +000070 llvm::Attributes::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() };
John McCall9dc0db22011-05-15 01:53:33 +0000584 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
585 params, false),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000586 "_setjmp",
Bill Wendling311c8322012-10-15 04:47:45 +0000587 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendling507c3512012-10-16 05:23:44 +0000588 llvm::Attributes::NonLazyBind));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000589 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000591public:
592 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000593 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000594};
595
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000596/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000597/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000598class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000599public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000600
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000601 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000602 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000603
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000604 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000605 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000606
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000607 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000608 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000610 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000611 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000612
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000613 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000614 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000615
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000616 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000617 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000618
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000619 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000620 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621
Fariborz Jahanian71394042009-01-23 23:53:38 +0000622 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000623 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000624
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000625 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000626 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000627
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000628 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000629 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000630
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000631 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000632 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000633
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000634 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000635 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000636
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000637 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000638 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000639
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000640 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000641 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000642
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000643 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000645 // MessageRefTy - LLVM for:
646 // struct _message_ref_t {
647 // IMP messenger;
648 // SEL name;
649 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000650 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000651 // MessageRefCTy - clang type for struct _message_ref_t
652 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000653
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000654 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000655 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000656 // MessageRefCPtrTy - clang type for struct _message_ref_t*
657 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000659 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000660 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000662 // SuperMessageRefTy - LLVM for:
663 // struct _super_message_ref_t {
664 // SUPER_IMP messenger;
665 // SEL name;
666 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000667 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000668
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000669 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000670 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000671
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendFixupFn() {
673 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000674 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000676 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000677 "objc_msgSend_fixup");
678 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000679
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000680 llvm::Constant *getMessageSendFpretFixupFn() {
681 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000682 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000684 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000685 "objc_msgSend_fpret_fixup");
686 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000688 llvm::Constant *getMessageSendStretFixupFn() {
689 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000690 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000692 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000693 "objc_msgSend_stret_fixup");
694 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000696 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000697 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000698 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000699 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000700 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000701 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000702 "objc_msgSendSuper2_fixup");
703 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000705 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000706 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000707 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000708 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000710 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000711 "objc_msgSendSuper2_stret_fixup");
712 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000713
Chris Lattnera7c00b42009-04-22 02:15:23 +0000714 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000716 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000717
Chris Lattnera7c00b42009-04-22 02:15:23 +0000718 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000719
Chris Lattnera7c00b42009-04-22 02:15:23 +0000720 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000721 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000723 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000724 "objc_begin_catch");
725 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000726
Chris Lattnera5f58b02011-07-09 17:41:47 +0000727 llvm::StructType *EHTypeTy;
728 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000729
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000730 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
731 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000732};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000733
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000734class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000735public:
736 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000737 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000738 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000739 unsigned ivar_bytepos;
740 unsigned ivar_size;
741 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000743
744 // Allow sorting based on byte pos.
745 bool operator<(const GC_IVAR &b) const {
746 return ivar_bytepos < b.ivar_bytepos;
747 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000748 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000750 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000751 public:
752 unsigned skip;
753 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000754 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000755 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000756 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000758protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000759 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000760 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000761 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000762
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000763 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000764 SmallVector<GC_IVAR, 16> SkipIvars;
765 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000766
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000767 /// LazySymbols - Symbols to generate a lazy reference for. See
768 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000769 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000771 /// DefinedSymbols - External symbols which are defined by this
772 /// module. The symbols in this list and LazySymbols are used to add
773 /// special linker symbols which ensure that Objective-C modules are
774 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000775 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000776
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000777 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000778 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000780 /// MethodVarNames - uniqued method variable names.
781 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000782
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000783 /// DefinedCategoryNames - list of category names in form Class_Category.
784 llvm::SetVector<std::string> DefinedCategoryNames;
785
Daniel Dunbarb036db82008-08-13 03:21:16 +0000786 /// MethodVarTypes - uniqued method type signatures. We have to use
787 /// a StringMap here because have no other unique reference.
788 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000789
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000790 /// MethodDefinitions - map of methods which have been defined in
791 /// this translation unit.
792 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000793
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000794 /// PropertyNames - uniqued method variable names.
795 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000796
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000797 /// ClassReferences - uniqued class references.
798 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000799
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000800 /// SelectorReferences - uniqued selector references.
801 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000802
Daniel Dunbarb036db82008-08-13 03:21:16 +0000803 /// Protocols - Protocols for which an objc_protocol structure has
804 /// been emitted. Forward declarations are handled by creating an
805 /// empty structure whose initializer is filled in when/if defined.
806 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000807
Daniel Dunbarc475d422008-10-29 22:36:39 +0000808 /// DefinedProtocols - Protocols which have actually been
809 /// defined. We should not need this, see FIXME in GenerateProtocol.
810 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000811
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000812 /// DefinedClasses - List of defined classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000813 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000814
815 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000816 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000817
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000818 /// DefinedCategories - List of defined categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000819 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000820
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000821 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000822 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000823
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000824 /// GetNameForMethod - Return a name for the given method.
825 /// \param[out] NameOut - The return value.
826 void GetNameForMethod(const ObjCMethodDecl *OMD,
827 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000828 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000830 /// GetMethodVarName - Return a unique constant for the given
831 /// selector's name. The return value has type char *.
832 llvm::Constant *GetMethodVarName(Selector Sel);
833 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000834
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000835 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000836 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000837
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000838 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000839 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
840 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000841 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000842
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000843 /// GetPropertyName - Return a unique constant for the given
844 /// name. The return value has type char *.
845 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000847 // FIXME: This can be dropped once string functions are unified.
848 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
849 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000850
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000851 /// GetClassName - Return a unique constant for the given selector's
852 /// name. The return value has type char *.
853 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000854
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000855 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
856
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000857 /// BuildIvarLayout - Builds ivar layout bitmap for the class
858 /// implementation for the __strong or __weak case.
859 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000860 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
861 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000862
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000863 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000864
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000865 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866 unsigned int BytePos, bool ForStrongLayout,
867 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000868 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000869 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000870 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000871 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000872 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000873 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000874
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000875 /// GetIvarLayoutName - Returns a unique constant for the given
876 /// ivar layout bitmap.
877 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
878 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000879
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000880 /// EmitPropertyList - Emit the given property list. The return
881 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000882 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000883 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000884 const ObjCContainerDecl *OCD,
885 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000887 /// EmitProtocolMethodTypes - Generate the array of extended method type
888 /// strings. The return value has type Int8PtrPtrTy.
889 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000890 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000891 const ObjCCommonTypesHelper &ObjCTypes);
892
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000893 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000894 void PushProtocolProperties(
895 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
896 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
897 const Decl *Container,
898 const ObjCProtocolDecl *PROTO,
899 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000900
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000901 /// GetProtocolRef - Return a reference to the internal protocol
902 /// description, creating an empty one if it has not been
903 /// defined. The return value has type ProtocolPtrTy.
904 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000905
Daniel Dunbar30c65362009-03-09 20:09:19 +0000906 /// CreateMetadataVar - Create a global variable with internal
907 /// linkage for use by the Objective-C runtime.
908 ///
909 /// This is a convenience wrapper which not only creates the
910 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000911 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000912 ///
913 /// \param Name - The variable name.
914 /// \param Init - The variable initializer; this is also used to
915 /// define the type of the variable.
916 /// \param Section - The section the variable should go into, or 0.
917 /// \param Align - The alignment for the variable, or 0.
918 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000919 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000920 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000921 llvm::Constant *Init,
922 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000923 unsigned Align,
924 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000925
John McCall9e8bb002011-05-14 03:10:52 +0000926 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
927 ReturnValueSlot Return,
928 QualType ResultType,
929 llvm::Value *Sel,
930 llvm::Value *Arg0,
931 QualType Arg0Ty,
932 bool IsSuper,
933 const CallArgList &CallArgs,
934 const ObjCMethodDecl *OMD,
935 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000936
Daniel Dunbar5e639272010-04-25 20:39:01 +0000937 /// EmitImageInfo - Emit the image info marker used to encode some module
938 /// level information.
939 void EmitImageInfo();
940
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000941public:
Owen Andersonae86c192009-07-13 04:10:07 +0000942 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +0000943 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000944
David Chisnall481e3a82010-01-23 02:40:42 +0000945 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000946
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000947 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
948 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000949
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000950 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000951
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000952 /// GetOrEmitProtocol - Get the protocol object for the given
953 /// declaration, emitting it if necessary. The return value has type
954 /// ProtocolPtrTy.
955 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000956
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000957 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
958 /// object for the given declaration, emitting it if needed. These
959 /// forward references will be filled in with empty bodies if no
960 /// definition is seen. The return value has type ProtocolPtrTy.
961 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000962 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
963 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000964
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000965};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000966
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000967class CGObjCMac : public CGObjCCommonMac {
968private:
969 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000970
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000971 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000972 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000973 void EmitModuleInfo();
974
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000975 /// EmitModuleSymols - Emit module symbols, the list of defined
976 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000977 llvm::Constant *EmitModuleSymbols();
978
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000979 /// FinishModule - Write out global data structures at the end of
980 /// processing a translation unit.
981 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000982
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000983 /// EmitClassExtension - Generate the class extension structure used
984 /// to store the weak ivar layout and properties. The return value
985 /// has type ClassExtensionPtrTy.
986 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
987
988 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
989 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000990 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000991 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000992
John McCall31168b02011-06-15 23:02:42 +0000993 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
994 IdentifierInfo *II);
995
996 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
997
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000998 /// EmitSuperClassRef - Emits reference to class's main metadata class.
999 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001000
1001 /// EmitIvarList - Emit the ivar list for the given
1002 /// implementation. If ForClass is true the list of class ivars
1003 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1004 /// interface ivars will be emitted. The return value has type
1005 /// IvarListPtrTy.
1006 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001007 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001008
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001009 /// EmitMetaClass - Emit a forward reference to the class structure
1010 /// for the metaclass of the given interface. The return value has
1011 /// type ClassPtrTy.
1012 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1013
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001014 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001015 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001016 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1017 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001018 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001019
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001020 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001021
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001022 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001023
1024 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001025 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001026 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001027 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001028 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001029
1030 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001031 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001032 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001033 /// - IsProtocol: True iff these methods are for a protocol.
1034 /// - ClassMethds: True iff these are class methods.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001035 /// - Required: When true, only "required" methods are
1036 /// listed. Similarly, when false only "optional" methods are
1037 /// listed. For classes this should always be true.
1038 /// - begin, end: The method list to output.
1039 ///
1040 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001041 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001042 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001043 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001044
Daniel Dunbarc475d422008-10-29 22:36:39 +00001045 /// GetOrEmitProtocol - Get the protocol object for the given
1046 /// declaration, emitting it if necessary. The return value has type
1047 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001048 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001049
1050 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1051 /// object for the given declaration, emitting it if needed. These
1052 /// forward references will be filled in with empty bodies if no
1053 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001054 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001055
Daniel Dunbarb036db82008-08-13 03:21:16 +00001056 /// EmitProtocolExtension - Generate the protocol extension
1057 /// structure used to store optional instance and class methods, and
1058 /// protocol properties. The return value has type
1059 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001060 llvm::Constant *
1061 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001062 ArrayRef<llvm::Constant*> OptInstanceMethods,
1063 ArrayRef<llvm::Constant*> OptClassMethods,
1064 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001065
1066 /// EmitProtocolList - Generate the list of referenced
1067 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001068 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001069 ObjCProtocolDecl::protocol_iterator begin,
1070 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001071
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001072 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1073 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001074 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1075 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001076
1077public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001078 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001079
Fariborz Jahanian71394042009-01-23 23:53:38 +00001080 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001081
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001082 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001083 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001084 QualType ResultType,
1085 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001086 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001087 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001088 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001089 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001090
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001091 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001092 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001093 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001094 QualType ResultType,
1095 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001096 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001097 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001098 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001099 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001100 const CallArgList &CallArgs,
1101 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001102
Daniel Dunbarcb463852008-11-01 01:53:16 +00001103 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001104 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001105
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001106 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1107 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001108
1109 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1110 /// untyped one.
1111 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1112 const ObjCMethodDecl *Method);
1113
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001114 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001115
Daniel Dunbar92992502008-08-15 22:20:32 +00001116 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001117
Daniel Dunbar92992502008-08-15 22:20:32 +00001118 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001119
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001120 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001121
Daniel Dunbarcb463852008-11-01 01:53:16 +00001122 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001123 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001124
Chris Lattnerd4808922009-03-22 21:03:39 +00001125 virtual llvm::Constant *GetPropertyGetFunction();
1126 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001127 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1128 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001129 virtual llvm::Constant *GetGetStructFunction();
1130 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001131 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001132 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001133
John McCallbd309292010-07-06 01:34:17 +00001134 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1135 const ObjCAtTryStmt &S);
1136 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1137 const ObjCAtSynchronizedStmt &S);
1138 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001139 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1140 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001141 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001142 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001143 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001144 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001145 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001146 llvm::Value *src, llvm::Value *dest,
1147 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001148 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001149 llvm::Value *src, llvm::Value *dest,
1150 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001151 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1152 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001153 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1154 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001155 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001157 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1158 QualType ObjectTy,
1159 llvm::Value *BaseValue,
1160 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001161 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001162 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001163 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001164 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001165
1166 /// GetClassGlobal - Return the global variable for the Objective-C
1167 /// class of the given name.
1168 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001169 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001170 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001171};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001172
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001173class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001174private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001175 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001176 llvm::GlobalVariable* ObjCEmptyCacheVar;
1177 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001178
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001179 /// SuperClassReferences - uniqued super class references.
1180 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001182 /// MetaClassReferences - uniqued meta class references.
1183 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001184
1185 /// EHTypeReferences - uniqued class ehtype references.
1186 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001187
John McCall9e8bb002011-05-14 03:10:52 +00001188 /// VTableDispatchMethods - List of methods for which we generate
1189 /// vtable-based message dispatch.
1190 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001191
Fariborz Jahanian67260552009-11-17 21:37:35 +00001192 /// DefinedMetaClasses - List of defined meta-classes.
1193 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1194
John McCall9e8bb002011-05-14 03:10:52 +00001195 /// isVTableDispatchedSelector - Returns true if SEL is a
1196 /// vtable-based selector.
1197 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001198
Fariborz Jahanian71394042009-01-23 23:53:38 +00001199 /// FinishNonFragileABIModule - Write out global data structures at the end of
1200 /// processing a translation unit.
1201 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001202
Daniel Dunbar19573e72009-05-15 21:48:48 +00001203 /// AddModuleClassList - Add the given list of class pointers to the
1204 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001205 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001206 const char *SymbolName,
1207 const char *SectionName);
1208
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1210 unsigned InstanceStart,
1211 unsigned InstanceSize,
1212 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001213 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001214 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001215 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001216 llvm::Constant *ClassRoGV,
1217 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001219 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001220
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001221 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001222
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001223 /// EmitMethodList - Emit the method list for the given
1224 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001225 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001226 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001227 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001228 /// EmitIvarList - Emit the ivar list for the given
1229 /// implementation. If ForClass is true the list of class ivars
1230 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1231 /// interface ivars will be emitted. The return value has type
1232 /// IvarListnfABIPtrTy.
1233 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001235 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001236 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001237 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001238
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001239 /// GetOrEmitProtocol - Get the protocol object for the given
1240 /// declaration, emitting it if necessary. The return value has type
1241 /// ProtocolPtrTy.
1242 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001244 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1245 /// object for the given declaration, emitting it if needed. These
1246 /// forward references will be filled in with empty bodies if no
1247 /// definition is seen. The return value has type ProtocolPtrTy.
1248 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001249
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001250 /// EmitProtocolList - Generate the list of referenced
1251 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001252 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001253 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001254 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001255
John McCall9e8bb002011-05-14 03:10:52 +00001256 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1257 ReturnValueSlot Return,
1258 QualType ResultType,
1259 Selector Sel,
1260 llvm::Value *Receiver,
1261 QualType Arg0Ty,
1262 bool IsSuper,
1263 const CallArgList &CallArgs,
1264 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001265
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001266 /// GetClassGlobal - Return the global variable for the Objective-C
1267 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001268 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001269
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001270 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001271 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001272 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001273 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001274
1275 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1276 IdentifierInfo *II);
1277
1278 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001280 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1281 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001282 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1283 const ObjCInterfaceDecl *ID);
1284
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001285 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1286 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001287 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001288 const ObjCInterfaceDecl *ID);
1289
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001290 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1291 /// the given ivar.
1292 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001293 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001294 const ObjCInterfaceDecl *ID,
1295 const ObjCIvarDecl *Ivar);
1296
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001297 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1298 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001299 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1300 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001301
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001302 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001303 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001304 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001305 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001306
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001307 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001308 return "OBJC_METACLASS_$_";
1309 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310
Daniel Dunbar15894b72009-04-07 05:48:37 +00001311 const char *getClassSymbolPrefix() const {
1312 return "OBJC_CLASS_$_";
1313 }
1314
Daniel Dunbar961202372009-05-03 12:57:56 +00001315 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001316 uint32_t &InstanceStart,
1317 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001318
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001319 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001320 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001321 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1322 return CGM.getContext().Selectors.getSelector(0, &II);
1323 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001324
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001325 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001326 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1327 return CGM.getContext().Selectors.getSelector(1, &II);
1328 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001329
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001330 /// ImplementationIsNonLazy - Check whether the given category or
1331 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001332 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001333
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001334public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001335 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001336 // FIXME. All stubs for now!
1337 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001338
Fariborz Jahanian71394042009-01-23 23:53:38 +00001339 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001340 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001341 QualType ResultType,
1342 Selector Sel,
1343 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001344 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001345 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001346 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001347
1348 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001349 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001350 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001351 QualType ResultType,
1352 Selector Sel,
1353 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001354 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001355 llvm::Value *Receiver,
1356 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001357 const CallArgList &CallArgs,
1358 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001359
Fariborz Jahanian71394042009-01-23 23:53:38 +00001360 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001361 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001362
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001363 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1364 bool lvalue = false)
1365 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001366
1367 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1368 /// untyped one.
1369 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1370 const ObjCMethodDecl *Method)
1371 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001372
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001373 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001374
Fariborz Jahanian71394042009-01-23 23:53:38 +00001375 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001376
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001377 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001378
Fariborz Jahanian71394042009-01-23 23:53:38 +00001379 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001380 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001381
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001382 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001383
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001384 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001385 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001386 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001387 virtual llvm::Constant *GetPropertySetFunction() {
1388 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001389 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001390
Ted Kremeneke65b0862012-03-06 20:05:56 +00001391 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1392 bool copy) {
1393 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1394 }
1395
David Chisnall168b80f2010-12-26 22:13:16 +00001396 virtual llvm::Constant *GetSetStructFunction() {
1397 return ObjCTypes.getCopyStructFn();
1398 }
1399 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001400 return ObjCTypes.getCopyStructFn();
1401 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001402 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1403 return ObjCTypes.getCppAtomicObjectFunction();
1404 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001405
Chris Lattnerd4808922009-03-22 21:03:39 +00001406 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001407 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001408 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001409
John McCallbd309292010-07-06 01:34:17 +00001410 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1411 const ObjCAtTryStmt &S);
1412 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1413 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001414 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001415 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001417 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001418 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001419 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001420 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001421 llvm::Value *src, llvm::Value *dest,
1422 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001423 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001424 llvm::Value *src, llvm::Value *dest,
1425 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001426 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001427 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001428 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1429 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001430 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001431 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1432 QualType ObjectTy,
1433 llvm::Value *BaseValue,
1434 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001435 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001436 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001437 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001438 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001439};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001440
1441/// A helper class for performing the null-initialization of a return
1442/// value.
1443struct NullReturnState {
1444 llvm::BasicBlock *NullBB;
1445 llvm::BasicBlock *callBB;
1446 NullReturnState() : NullBB(0), callBB(0) {}
1447
1448 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1449 // Make blocks for the null-init and call edges.
1450 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1451 callBB = CGF.createBasicBlock("msgSend.call");
1452
1453 // Check for a null receiver and, if there is one, jump to the
1454 // null-init test.
1455 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1456 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1457
1458 // Otherwise, start performing the call.
1459 CGF.EmitBlock(callBB);
1460 }
1461
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001462 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1463 const CallArgList &CallArgs,
1464 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001465 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001466
1467 llvm::Value *NullInitPtr = 0;
1468 if (result.isScalar() && !resultType->isVoidType()) {
1469 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1470 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1471 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001472
1473 // Finish the call path.
1474 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1475 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1476
1477 // Emit the null-init block and perform the null-initialization there.
1478 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001479
1480 // Release consumed arguments along the null-receiver path.
1481 if (Method) {
1482 CallArgList::const_iterator I = CallArgs.begin();
1483 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1484 e = Method->param_end(); i != e; ++i, ++I) {
1485 const ParmVarDecl *ParamDecl = (*i);
1486 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1487 RValue RV = I->RV;
1488 assert(RV.isScalar() &&
1489 "NullReturnState::complete - arg not on object");
1490 CGF.EmitARCRelease(RV.getScalarVal(), true);
1491 }
1492 }
1493 }
1494
1495 if (result.isScalar()) {
1496 if (NullInitPtr)
1497 CGF.EmitNullInitialization(NullInitPtr, resultType);
1498 // Jump to the continuation block.
1499 CGF.EmitBlock(contBB);
1500 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1501 : result;
1502 }
1503
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001504 if (!resultType->isAnyComplexType()) {
1505 assert(result.isAggregate() && "null init of non-aggregate result?");
1506 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1507 // Jump to the continuation block.
1508 CGF.EmitBlock(contBB);
1509 return result;
1510 }
1511
1512 // _Complex type
1513 // FIXME. Now easy to handle any other scalar type whose result is returned
1514 // in memory due to ABI limitations.
1515 CGF.EmitBlock(contBB);
1516 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1517 llvm::Type *MemberType = CallCV.first->getType();
1518 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1519 // Create phi instruction for scalar complex value.
1520 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1521 PHIReal->addIncoming(ZeroCV, NullBB);
1522 PHIReal->addIncoming(CallCV.first, callBB);
1523 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1524 PHIImag->addIncoming(ZeroCV, NullBB);
1525 PHIImag->addIncoming(CallCV.second, callBB);
1526 return RValue::getComplex(PHIReal, PHIImag);
1527 }
1528};
1529
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001530} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001531
1532/* *** Helper Functions *** */
1533
1534/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001535static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001536 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001537 unsigned idx0,
1538 unsigned idx1) {
1539 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001540 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1541 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001542 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001543 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001544}
1545
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001546/// hasObjCExceptionAttribute - Return true if this class or any super
1547/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001548static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001549 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001550 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001551 return true;
1552 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001553 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001554 return false;
1555}
1556
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001557/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001558
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001559CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001560 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001561 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001562 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001563}
1564
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001565/// GetClass - Return a reference to the class for the given interface
1566/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001567llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001568 const ObjCInterfaceDecl *ID) {
1569 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001570}
1571
1572/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001573llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1574 bool lval) {
1575 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001576}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001577llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001578 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001579 return EmitSelector(Builder, Method->getSelector());
1580}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001581
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001582llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001583 if (T->isObjCIdType() ||
1584 T->isObjCQualifiedIdType()) {
1585 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001586 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001587 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001588 if (T->isObjCClassType() ||
1589 T->isObjCQualifiedClassType()) {
1590 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001591 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001592 }
1593 if (T->isObjCObjectPointerType())
1594 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1595
John McCall2ca705e2010-07-24 00:37:23 +00001596 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001597}
1598
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001599/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001600/*
1601 struct __builtin_CFString {
1602 const int *isa; // point to __CFConstantStringClassReference
1603 int flags;
1604 const char *str;
1605 long length;
1606 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001607*/
1608
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001609/// or Generate a constant NSString object.
1610/*
1611 struct __builtin_NSString {
1612 const int *isa; // point to __NSConstantStringClassReference
1613 const char *str;
1614 unsigned int length;
1615 };
1616*/
1617
Fariborz Jahanian71394042009-01-23 23:53:38 +00001618llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001619 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001620 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001621 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001622 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001623}
1624
Ted Kremeneke65b0862012-03-06 20:05:56 +00001625enum {
1626 kCFTaggedObjectID_Integer = (1 << 1) + 1
1627};
1628
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001629/// Generates a message send where the super is the receiver. This is
1630/// a message send to self with special delivery semantics indicating
1631/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001632CodeGen::RValue
1633CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001634 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001635 QualType ResultType,
1636 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001637 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001638 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001639 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001640 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001641 const CodeGen::CallArgList &CallArgs,
1642 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001643 // Create and init a super structure; this is a (receiver, class)
1644 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001645 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001646 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001647 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001648 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001649 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001650 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001651
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001652 // If this is a class message the metaclass is passed as the target.
1653 llvm::Value *Target;
1654 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001655 if (isCategoryImpl) {
1656 // Message sent to 'super' in a class method defined in a category
1657 // implementation requires an odd treatment.
1658 // If we are in a class method, we must retrieve the
1659 // _metaclass_ for the current class, pointed at by
1660 // the class's "isa" pointer. The following assumes that
1661 // isa" is the first ivar in a class (which it must be).
1662 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1663 Target = CGF.Builder.CreateStructGEP(Target, 0);
1664 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001665 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001666 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1667 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1668 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1669 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001670 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001671 }
1672 else if (isCategoryImpl)
1673 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1674 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001675 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1676 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1677 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001678 }
Mike Stump18bb9282009-05-16 07:57:57 +00001679 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1680 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001681 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001682 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001683 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001684 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001685 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001686 return EmitMessageSend(CGF, Return, ResultType,
1687 EmitSelector(CGF.Builder, Sel),
1688 ObjCSuper, ObjCTypes.SuperPtrCTy,
1689 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001690}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001691
1692/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001693CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001694 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001695 QualType ResultType,
1696 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001697 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001698 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001699 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001700 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001701 return EmitMessageSend(CGF, Return, ResultType,
1702 EmitSelector(CGF.Builder, Sel),
1703 Receiver, CGF.getContext().getObjCIdType(),
1704 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001705}
1706
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001707CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001708CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1709 ReturnValueSlot Return,
1710 QualType ResultType,
1711 llvm::Value *Sel,
1712 llvm::Value *Arg0,
1713 QualType Arg0Ty,
1714 bool IsSuper,
1715 const CallArgList &CallArgs,
1716 const ObjCMethodDecl *Method,
1717 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001718 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001719 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001720 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001721 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1722 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001723 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001724
John McCalla729c622012-02-17 03:33:10 +00001725 // If we're calling a method, use the formal signature.
1726 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001727
Anders Carlsson280e61f12010-06-21 20:59:55 +00001728 if (Method)
1729 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1730 CGM.getContext().getCanonicalType(ResultType) &&
1731 "Result type mismatch!");
1732
John McCall5880fb82011-05-14 21:12:11 +00001733 NullReturnState nullReturn;
1734
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001735 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001736 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001737 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001738 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001739 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001740 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1741 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1742 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001743 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1744 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1745 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001746 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001747 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001748 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001749 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001750
1751 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001752 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001753 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1754 e = Method->param_end(); i != e; ++i) {
1755 const ParmVarDecl *ParamDecl = (*i);
1756 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1757 if (!nullReturn.NullBB)
1758 nullReturn.init(CGF, Arg0);
1759 requiresnullCheck = true;
1760 break;
1761 }
1762 }
1763
John McCalla729c622012-02-17 03:33:10 +00001764 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1765 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001766 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1767 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001768}
1769
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001770static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1771 if (FQT.isObjCGCStrong())
1772 return Qualifiers::Strong;
1773
John McCall31168b02011-06-15 23:02:42 +00001774 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001775 return Qualifiers::Weak;
1776
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001777 // check for __unsafe_unretained
1778 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1779 return Qualifiers::GCNone;
1780
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001781 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1782 return Qualifiers::Strong;
1783
1784 if (const PointerType *PT = FQT->getAs<PointerType>())
1785 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1786
1787 return Qualifiers::GCNone;
1788}
1789
John McCall351762c2011-02-07 10:33:21 +00001790llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1791 const CGBlockInfo &blockInfo) {
Chris Lattnerece04092012-02-07 00:39:47 +00001792 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall351762c2011-02-07 10:33:21 +00001793
David Blaikiebbafb8a2012-03-11 07:00:24 +00001794 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1795 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001796 return nullPtr;
1797
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001798 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001799 SkipIvars.clear();
1800 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001801 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1802 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001803
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001804 // __isa is the first field in block descriptor and must assume by runtime's
1805 // convention that it is GC'able.
1806 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001807
1808 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1809
1810 // Calculate the basic layout of the block structure.
1811 const llvm::StructLayout *layout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001812 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall351762c2011-02-07 10:33:21 +00001813
1814 // Ignore the optional 'this' capture: C++ objects are not assumed
1815 // to be GC'ed.
1816
1817 // Walk the captured variables.
1818 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1819 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1820 const VarDecl *variable = ci->getVariable();
1821 QualType type = variable->getType();
1822
1823 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1824
1825 // Ignore constant captures.
1826 if (capture.isConstant()) continue;
1827
1828 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1829
1830 // __block variables are passed by their descriptor address.
1831 if (ci->isByRef()) {
1832 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001833 continue;
John McCall351762c2011-02-07 10:33:21 +00001834 }
1835
1836 assert(!type->isArrayType() && "array variable should not be caught");
1837 if (const RecordType *record = type->getAs<RecordType>()) {
1838 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001839 continue;
1840 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001841
John McCall351762c2011-02-07 10:33:21 +00001842 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1843 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1844
1845 if (GCAttr == Qualifiers::Strong)
1846 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1847 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001848 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001849 SkipIvars.push_back(GC_IVAR(fieldOffset,
1850 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001851 }
1852
1853 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001854 return nullPtr;
1855
1856 // Sort on byte position; captures might not be allocated in order,
1857 // and unions can do funny things.
1858 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1859 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001860
1861 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001862 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001863 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001864 printf("\n block variable layout for block: ");
Roman Divackye6377112012-09-06 15:59:27 +00001865 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00001866 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001867 if (!(s[i] & 0xf0))
1868 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1869 else
1870 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1871 printf("\n");
1872 }
1873
1874 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001875}
1876
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001877llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001878 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001879 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001880 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001881 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1882
Owen Andersonade90fd2009-07-29 18:54:39 +00001883 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001884 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001885}
1886
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001887void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001888 // FIXME: We shouldn't need this, the protocol decl should contain enough
1889 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001890 DefinedProtocols.insert(PD->getIdentifier());
1891
1892 // If we have generated a forward reference to this protocol, emit
1893 // it now. Otherwise do nothing, the protocol objects are lazily
1894 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001895 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001896 GetOrEmitProtocol(PD);
1897}
1898
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001899llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001900 if (DefinedProtocols.count(PD->getIdentifier()))
1901 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001902
Daniel Dunbarc475d422008-10-29 22:36:39 +00001903 return GetOrEmitProtocolRef(PD);
1904}
1905
Daniel Dunbarb036db82008-08-13 03:21:16 +00001906/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001907// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1908struct _objc_protocol {
1909struct _objc_protocol_extension *isa;
1910char *protocol_name;
1911struct _objc_protocol_list *protocol_list;
1912struct _objc__method_prototype_list *instance_methods;
1913struct _objc__method_prototype_list *class_methods
1914};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001915
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001916See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001917*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001918llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00001919 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00001920
1921 // Early exit if a defining object has already been generated.
1922 if (Entry && Entry->hasInitializer())
1923 return Entry;
1924
Douglas Gregora715bff2012-01-01 19:51:50 +00001925 // Use the protocol definition, if there is one.
1926 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1927 PD = Def;
1928
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001929 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001930 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001931 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1932
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001933 // Construct method lists.
1934 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1935 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001936 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001937 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001938 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001939 ObjCMethodDecl *MD = *i;
1940 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001941 if (!C)
1942 return GetOrEmitProtocolRef(PD);
1943
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001944 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1945 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001946 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001947 } else {
1948 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001949 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001950 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001951 }
1952
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001953 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001954 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001955 ObjCMethodDecl *MD = *i;
1956 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001957 if (!C)
1958 return GetOrEmitProtocolRef(PD);
1959
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001960 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1961 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001962 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001963 } else {
1964 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001965 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001966 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001967 }
1968
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001969 MethodTypesExt.insert(MethodTypesExt.end(),
1970 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1971
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001972 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001973 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1974 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001975 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001976 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001977 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001978 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001979 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001980 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001981 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001982 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001983 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001984 ClassMethods)
1985 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001986 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001987 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988
Daniel Dunbarb036db82008-08-13 03:21:16 +00001989 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001990 // Already created, fix the linkage and update the initializer.
1991 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992 Entry->setInitializer(Init);
1993 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001995 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001996 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001997 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001998 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001999 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002000 // FIXME: Is this necessary? Why only for protocol?
2001 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002002
2003 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002004 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00002005 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002006
2007 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002008}
2009
Daniel Dunbarc475d422008-10-29 22:36:39 +00002010llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002011 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2012
2013 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002014 // We use the initializer as a marker of whether this is a forward
2015 // reference or not. At module finalization we add the empty
2016 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002017 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002018 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002019 llvm::GlobalValue::ExternalLinkage,
2020 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002021 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002022 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002023 // FIXME: Is this necessary? Why only for protocol?
2024 Entry->setAlignment(4);
2025 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002026
Daniel Dunbarb036db82008-08-13 03:21:16 +00002027 return Entry;
2028}
2029
2030/*
2031 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002032 uint32_t size;
2033 struct objc_method_description_list *optional_instance_methods;
2034 struct objc_method_description_list *optional_class_methods;
2035 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002036 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002037 };
2038*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002039llvm::Constant *
2040CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002041 ArrayRef<llvm::Constant*> OptInstanceMethods,
2042 ArrayRef<llvm::Constant*> OptClassMethods,
2043 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002044 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002045 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002046 llvm::Constant *Values[] = {
2047 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002048 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002049 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002050 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002051 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002052 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002053 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002054 OptClassMethods),
2055 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002056 ObjCTypes),
2057 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2058 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002059 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002060
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002061 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002062 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002063 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002064 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002065
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002066 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002067 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002068
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002069 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002070 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002071 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002072 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002073}
2074
2075/*
2076 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002077 struct objc_protocol_list *next;
2078 long count;
2079 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002080 };
2081*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002082llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002083CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002084 ObjCProtocolDecl::protocol_iterator begin,
2085 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002086 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002087
Daniel Dunbardec75f82008-08-21 21:57:41 +00002088 for (; begin != end; ++begin)
2089 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002090
2091 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002092 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002093 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002094
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002095 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002096 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002097
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002098 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002099 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002100 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002101 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002102 ProtocolRefs.size() - 1);
2103 Values[2] =
2104 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2105 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002106 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002107
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002108 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002109 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002110 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002111 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002112 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002113}
2114
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002115void CGObjCCommonMac::
2116PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendlinga515b582012-02-09 22:16:49 +00002117 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002118 const Decl *Container,
2119 const ObjCProtocolDecl *PROTO,
2120 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002121 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2122 E = PROTO->protocol_end(); P != E; ++P)
2123 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2124 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2125 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002126 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002127 if (!PropertySet.insert(PD->getIdentifier()))
2128 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002129 llvm::Constant *Prop[] = {
2130 GetPropertyName(PD->getIdentifier()),
2131 GetPropertyTypeString(PD, Container)
2132 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002133 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2134 }
2135}
2136
Daniel Dunbarb036db82008-08-13 03:21:16 +00002137/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002138 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002139 const char * const name;
2140 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002141 };
2142
2143 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002144 uint32_t entsize; // sizeof (struct _objc_property)
2145 uint32_t prop_count;
2146 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002147 };
2148*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002149llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002150 const Decl *Container,
2151 const ObjCContainerDecl *OCD,
2152 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002153 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002154 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002155 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2156 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002157 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002158 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002159 llvm::Constant *Prop[] = {
2160 GetPropertyName(PD->getIdentifier()),
2161 GetPropertyTypeString(PD, Container)
2162 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002163 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002164 Prop));
2165 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002166 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002167 for (ObjCInterfaceDecl::all_protocol_iterator
2168 P = OID->all_referenced_protocol_begin(),
2169 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002170 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2171 ObjCTypes);
2172 }
2173 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2174 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2175 E = CD->protocol_end(); P != E; ++P)
2176 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2177 ObjCTypes);
2178 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002179
2180 // Return null for empty list.
2181 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002182 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002183
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002184 unsigned PropertySize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002185 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002186 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002187 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2188 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002189 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002190 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002191 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002192 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002193
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002194 llvm::GlobalVariable *GV =
2195 CreateMetadataVar(Name, Init,
2196 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002197 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002198 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002199 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002200 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002201}
2202
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002203llvm::Constant *
2204CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2205 ArrayRef<llvm::Constant*> MethodTypes,
2206 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002207 // Return null for empty list.
2208 if (MethodTypes.empty())
2209 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2210
2211 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2212 MethodTypes.size());
2213 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2214
2215 llvm::GlobalVariable *GV =
2216 CreateMetadataVar(Name, Init,
2217 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2218 (ObjCABI == 2) ? 8 : 4,
2219 true);
2220 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2221}
2222
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002223/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002224 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002225 int count;
2226 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002227 };
2228*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002229llvm::Constant *
2230CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002231 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002232 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002233 ObjCTypes.SelectorPtrTy),
2234 GetMethodVarType(MD)
2235 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002236 if (!Desc[1])
2237 return 0;
2238
Owen Anderson0e0189d2009-07-27 22:29:56 +00002239 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002240 Desc);
2241}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002242
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002243llvm::Constant *
2244CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2245 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002246 // Return null for empty list.
2247 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002248 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002249
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002250 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002251 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002252 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002253 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002254 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002255 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002256
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002257 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002258 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002259 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002260}
2261
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002262/*
2263 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002264 char *category_name;
2265 char *class_name;
2266 struct _objc_method_list *instance_methods;
2267 struct _objc_method_list *class_methods;
2268 struct _objc_protocol_list *protocols;
2269 uint32_t size; // <rdar://4585769>
2270 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002271 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002272*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002273void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00002274 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002275
Mike Stump18bb9282009-05-16 07:57:57 +00002276 // FIXME: This is poor design, the OCD should have a pointer to the category
2277 // decl. Additionally, note that Category can be null for the @implementation
2278 // w/o an @interface case. Sema should just create one for us as it does for
2279 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002280 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002281 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002282 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002283
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002284 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002285 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2286 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002287
Bill Wendlinga515b582012-02-09 22:16:49 +00002288 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002289 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002290 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002291 // Instance methods should always be defined.
2292 InstanceMethods.push_back(GetMethodConstant(*i));
2293 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002294 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002295 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002296 // Class methods should always be defined.
2297 ClassMethods.push_back(GetMethodConstant(*i));
2298 }
2299
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002300 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002301 Values[0] = GetClassName(OCD->getIdentifier());
2302 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002303 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002304 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002305 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002306 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002307 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002308 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002309 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002310 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002311 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002312 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002313 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002314 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002315 Category->protocol_begin(),
2316 Category->protocol_end());
2317 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002318 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002319 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002320 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002321
2322 // If there is no category @interface then there can be no properties.
2323 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002324 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002325 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002326 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002327 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002328 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002329
Owen Anderson0e0189d2009-07-27 22:29:56 +00002330 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002331 Values);
2332
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002333 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002334 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002335 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002336 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002337 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002338 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002339 // method definition entries must be clear for next implementation.
2340 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002341}
2342
John McCallef19dbb2012-10-17 04:53:23 +00002343enum FragileClassFlags {
2344 FragileABI_Class_Factory = 0x00001,
2345 FragileABI_Class_Meta = 0x00002,
2346 FragileABI_Class_HasCXXStructors = 0x02000,
2347 FragileABI_Class_Hidden = 0x20000
2348};
2349
2350enum NonFragileClassFlags {
2351 /// Is a meta-class.
2352 NonFragileABI_Class_Meta = 0x00001,
2353
2354 /// Is a root class.
2355 NonFragileABI_Class_Root = 0x00002,
2356
2357 /// Has a C++ constructor and destructor.
2358 NonFragileABI_Class_HasCXXStructors = 0x00004,
2359
2360 /// Has hidden visibility.
2361 NonFragileABI_Class_Hidden = 0x00010,
2362
2363 /// Has the exception attribute.
2364 NonFragileABI_Class_Exception = 0x00020,
2365
2366 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2367 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2368
2369 /// Class implementation was compiled under ARC.
2370 NonFragileABI_Class_CompiledByARC = 0x00080
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002371};
2372
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002373/*
2374 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002375 Class isa;
2376 Class super_class;
2377 const char *name;
2378 long version;
2379 long info;
2380 long instance_size;
2381 struct _objc_ivar_list *ivars;
2382 struct _objc_method_list *methods;
2383 struct _objc_cache *cache;
2384 struct _objc_protocol_list *protocols;
2385 // Objective-C 1.0 extensions (<rdr://4585769>)
2386 const char *ivar_layout;
2387 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002388 };
2389
2390 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002391*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002392void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002393 DefinedSymbols.insert(ID->getIdentifier());
2394
Chris Lattner86d7d912008-11-24 03:54:41 +00002395 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002396 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002397 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002399 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002400 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002401 Interface->all_referenced_protocol_begin(),
2402 Interface->all_referenced_protocol_end());
John McCallef19dbb2012-10-17 04:53:23 +00002403 unsigned Flags = FragileABI_Class_Factory;
John McCall31168b02011-06-15 23:02:42 +00002404 if (ID->hasCXXStructors())
John McCallef19dbb2012-10-17 04:53:23 +00002405 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002406 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002407 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002408
2409 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002410 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00002411 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002412
Bill Wendlinga515b582012-02-09 22:16:49 +00002413 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002414 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002415 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002416 // Instance methods should always be defined.
2417 InstanceMethods.push_back(GetMethodConstant(*i));
2418 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002419 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002420 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002421 // Class methods should always be defined.
2422 ClassMethods.push_back(GetMethodConstant(*i));
2423 }
2424
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002426 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002427 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002428
2429 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2430 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2431
2432 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2433 if (llvm::Constant *C = GetMethodConstant(MD))
2434 InstanceMethods.push_back(C);
2435 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2436 if (llvm::Constant *C = GetMethodConstant(MD))
2437 InstanceMethods.push_back(C);
2438 }
2439 }
2440
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002441 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002442 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002444 // Record a reference to the super class.
2445 LazySymbols.insert(Super->getIdentifier());
2446
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002447 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002448 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 ObjCTypes.ClassPtrTy);
2450 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002451 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002452 }
2453 Values[ 2] = GetClassName(ID->getIdentifier());
2454 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002455 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2456 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2457 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002458 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002459 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002460 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002461 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002462 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002463 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002464 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002465 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002466 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002467 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002468 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002469 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002470 std::string Name("\01L_OBJC_CLASS_");
2471 Name += ClassName;
2472 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2473 // Check for a forward reference.
2474 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2475 if (GV) {
2476 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2477 "Forward metaclass reference has incorrect type.");
2478 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2479 GV->setInitializer(Init);
2480 GV->setSection(Section);
2481 GV->setAlignment(4);
2482 CGM.AddUsedGlobal(GV);
2483 }
2484 else
2485 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002486 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002487 // method definition entries must be clear for next implementation.
2488 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002489}
2490
2491llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2492 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002493 ArrayRef<llvm::Constant*> Methods) {
John McCallef19dbb2012-10-17 04:53:23 +00002494 unsigned Flags = FragileABI_Class_Meta;
Micah Villmowdd31ca12012-10-08 16:25:52 +00002495 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002496
John McCall457a04e2010-10-22 21:05:15 +00002497 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00002498 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002499
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002500 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002501 // The isa for the metaclass is the root of the hierarchy.
2502 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2503 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2504 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002505 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002506 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002507 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002508 // The super class for the metaclass is emitted as the name of the
2509 // super class. The runtime fixes this up to point to the
2510 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002511 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002512 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002513 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002514 ObjCTypes.ClassPtrTy);
2515 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002516 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002517 }
2518 Values[ 2] = GetClassName(ID->getIdentifier());
2519 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002520 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2521 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2522 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002523 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002524 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002525 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002526 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002527 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002528 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002529 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002530 Values[ 9] = Protocols;
2531 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002532 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002533 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002534 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002535 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002536 Values);
2537
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002538 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer1bbcbd02012-07-31 11:45:39 +00002539 Name += ID->getName();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002540
2541 // Check for a forward reference.
2542 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2543 if (GV) {
2544 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2545 "Forward metaclass reference has incorrect type.");
2546 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2547 GV->setInitializer(Init);
2548 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002549 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002550 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002551 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002552 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002553 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002554 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002555 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002556
2557 return GV;
2558}
2559
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002560llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002561 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002562
Mike Stump18bb9282009-05-16 07:57:57 +00002563 // FIXME: Should we look these up somewhere other than the module. Its a bit
2564 // silly since we only generate these while processing an implementation, so
2565 // exactly one pointer would work if know when we entered/exitted an
2566 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002567
2568 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002569 // Previously, metaclass with internal linkage may have been defined.
2570 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002571 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2572 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002573 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2574 "Forward metaclass reference has incorrect type.");
2575 return GV;
2576 } else {
2577 // Generate as an external reference to keep a consistent
2578 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002579 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002580 llvm::GlobalValue::ExternalLinkage,
2581 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002582 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002583 }
2584}
2585
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002586llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2587 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2588
2589 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2590 true)) {
2591 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2592 "Forward class metadata reference has incorrect type.");
2593 return GV;
2594 } else {
2595 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2596 llvm::GlobalValue::ExternalLinkage,
2597 0,
2598 Name);
2599 }
2600}
2601
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002602/*
2603 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002604 uint32_t size;
2605 const char *weak_ivar_layout;
2606 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002607 };
2608*/
2609llvm::Constant *
2610CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002611 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002612 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002613
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002614 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002615 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002616 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002617 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002618 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002619
2620 // Return null if no extension bits are used.
2621 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002622 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002623
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002624 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002625 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002626 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002627 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002628 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002629}
2630
2631/*
2632 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002633 char *ivar_name;
2634 char *ivar_type;
2635 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002636 };
2637
2638 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002639 int ivar_count;
2640 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002641 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002642*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002643llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002644 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002645 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002646
2647 // When emitting the root class GCC emits ivar entries for the
2648 // actual class structure. It is not clear if we need to follow this
2649 // behavior; for now lets try and get away with not doing it. If so,
2650 // the cleanest solution would be to make up an ObjCInterfaceDecl
2651 // for the class.
2652 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002653 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002654
Jordy Rosea91768e2011-07-22 02:08:32 +00002655 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002656
Jordy Rosea91768e2011-07-22 02:08:32 +00002657 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002658 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002659 // Ignore unnamed bit-fields.
2660 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002661 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002662 llvm::Constant *Ivar[] = {
2663 GetMethodVarName(IVD->getIdentifier()),
2664 GetMethodVarType(IVD),
2665 llvm::ConstantInt::get(ObjCTypes.IntTy,
2666 ComputeIvarBaseOffset(CGM, OID, IVD))
2667 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002668 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002669 }
2670
2671 // Return null for empty list.
2672 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002673 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002674
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002675 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002676 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002677 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002678 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002679 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002680 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002681
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002682 llvm::GlobalVariable *GV;
2683 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002684 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002685 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002686 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002687 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002688 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002689 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002690 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002691 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002692}
2693
2694/*
2695 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002696 SEL method_name;
2697 char *method_types;
2698 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002699 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002700
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002701 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002702 struct objc_method_list *obsolete;
2703 int count;
2704 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002705 };
2706*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002707
2708/// GetMethodConstant - Return a struct objc_method constant for the
2709/// given method if it has been defined. The result is null if the
2710/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002711llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002712 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002713 if (!Fn)
2714 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002715
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002716 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002717 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002718 ObjCTypes.SelectorPtrTy),
2719 GetMethodVarType(MD),
2720 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2721 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002722 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002723}
2724
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002725llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002726 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002727 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002728 // Return null for empty list.
2729 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002730 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002731
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002732 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002733 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002734 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002735 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002736 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002737 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002738 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002739
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002740 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002741 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002742}
2743
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002744llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002745 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002746 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002747 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002748
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002749 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002750 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00002751 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002752 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002753 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002754 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002755 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002756 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002757 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002758
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002759 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002760}
2761
Daniel Dunbar30c65362009-03-09 20:09:19 +00002762llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002764 llvm::Constant *Init,
2765 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002766 unsigned Align,
2767 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002768 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002769 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002770 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002771 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002772 if (Section)
2773 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002774 if (Align)
2775 GV->setAlignment(Align);
2776 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002777 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002778 return GV;
2779}
2780
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002781llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002782 // Abuse this interface function as a place to finalize.
2783 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002784 return NULL;
2785}
2786
Chris Lattnerd4808922009-03-22 21:03:39 +00002787llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002788 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002789}
2790
Chris Lattnerd4808922009-03-22 21:03:39 +00002791llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002792 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002793}
2794
Ted Kremeneke65b0862012-03-06 20:05:56 +00002795llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2796 bool copy) {
2797 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2798}
2799
David Chisnall168b80f2010-12-26 22:13:16 +00002800llvm::Constant *CGObjCMac::GetGetStructFunction() {
2801 return ObjCTypes.getCopyStructFn();
2802}
2803llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002804 return ObjCTypes.getCopyStructFn();
2805}
2806
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002807llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2808 return ObjCTypes.getCppAtomicObjectFunction();
2809}
2810
Chris Lattnerd4808922009-03-22 21:03:39 +00002811llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002812 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002813}
2814
John McCallbd309292010-07-06 01:34:17 +00002815void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2816 return EmitTryOrSynchronizedStmt(CGF, S);
2817}
2818
2819void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2820 const ObjCAtSynchronizedStmt &S) {
2821 return EmitTryOrSynchronizedStmt(CGF, S);
2822}
2823
John McCall65bea082010-07-21 06:59:36 +00002824namespace {
John McCallcda666c2010-07-21 07:22:38 +00002825 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002826 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002827 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002828 llvm::Value *CallTryExitVar;
2829 llvm::Value *ExceptionData;
2830 ObjCTypesHelper &ObjCTypes;
2831 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002832 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002833 llvm::Value *CallTryExitVar,
2834 llvm::Value *ExceptionData,
2835 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002836 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002837 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2838
John McCall30317fd2011-07-12 20:27:29 +00002839 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002840 // Check whether we need to call objc_exception_try_exit.
2841 // In optimized code, this branch will always be folded.
2842 llvm::BasicBlock *FinallyCallExit =
2843 CGF.createBasicBlock("finally.call_exit");
2844 llvm::BasicBlock *FinallyNoCallExit =
2845 CGF.createBasicBlock("finally.no_call_exit");
2846 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2847 FinallyCallExit, FinallyNoCallExit);
2848
2849 CGF.EmitBlock(FinallyCallExit);
2850 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2851 ->setDoesNotThrow();
2852
2853 CGF.EmitBlock(FinallyNoCallExit);
2854
2855 if (isa<ObjCAtTryStmt>(S)) {
2856 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002857 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2858 // Save the current cleanup destination in case there's
2859 // control flow inside the finally statement.
2860 llvm::Value *CurCleanupDest =
2861 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2862
John McCall65bea082010-07-21 06:59:36 +00002863 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2864
John McCallcebe0ca2010-08-11 00:16:14 +00002865 if (CGF.HaveInsertPoint()) {
2866 CGF.Builder.CreateStore(CurCleanupDest,
2867 CGF.getNormalCleanupDestSlot());
2868 } else {
2869 // Currently, the end of the cleanup must always exist.
2870 CGF.EnsureInsertPoint();
2871 }
2872 }
John McCall65bea082010-07-21 06:59:36 +00002873 } else {
2874 // Emit objc_sync_exit(expr); as finally's sole statement for
2875 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002876 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002877 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2878 ->setDoesNotThrow();
2879 }
2880 }
2881 };
John McCall42227ed2010-07-31 23:20:56 +00002882
2883 class FragileHazards {
2884 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002885 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002886 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2887
2888 llvm::InlineAsm *ReadHazard;
2889 llvm::InlineAsm *WriteHazard;
2890
2891 llvm::FunctionType *GetAsmFnType();
2892
2893 void collectLocals();
2894 void emitReadHazard(CGBuilderTy &Builder);
2895
2896 public:
2897 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002898
John McCall42227ed2010-07-31 23:20:56 +00002899 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002900 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002901 };
2902}
2903
2904/// Create the fragile-ABI read and write hazards based on the current
2905/// state of the function, which is presumed to be immediately prior
2906/// to a @try block. These hazards are used to maintain correct
2907/// semantics in the face of optimization and the fragile ABI's
2908/// cavalier use of setjmp/longjmp.
2909FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2910 collectLocals();
2911
2912 if (Locals.empty()) return;
2913
2914 // Collect all the blocks in the function.
2915 for (llvm::Function::iterator
2916 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2917 BlocksBeforeTry.insert(&*I);
2918
2919 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2920
2921 // Create a read hazard for the allocas. This inhibits dead-store
2922 // optimizations and forces the values to memory. This hazard is
2923 // inserted before any 'throwing' calls in the protected scope to
2924 // reflect the possibility that the variables might be read from the
2925 // catch block if the call throws.
2926 {
2927 std::string Constraint;
2928 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2929 if (I) Constraint += ',';
2930 Constraint += "*m";
2931 }
2932
2933 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2934 }
2935
2936 // Create a write hazard for the allocas. This inhibits folding
2937 // loads across the hazard. This hazard is inserted at the
2938 // beginning of the catch path to reflect the possibility that the
2939 // variables might have been written within the protected scope.
2940 {
2941 std::string Constraint;
2942 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2943 if (I) Constraint += ',';
2944 Constraint += "=*m";
2945 }
2946
2947 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2948 }
2949}
2950
2951/// Emit a write hazard at the current location.
2952void FragileHazards::emitWriteHazard() {
2953 if (Locals.empty()) return;
2954
Jay Foad5bd375a2011-07-15 08:37:34 +00002955 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002956}
2957
John McCall42227ed2010-07-31 23:20:56 +00002958void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2959 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002960 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002961}
2962
2963/// Emit read hazards in all the protected blocks, i.e. all the blocks
2964/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002965void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002966 if (Locals.empty()) return;
2967
2968 CGBuilderTy Builder(CGF.getLLVMContext());
2969
2970 // Iterate through all blocks, skipping those prior to the try.
2971 for (llvm::Function::iterator
2972 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2973 llvm::BasicBlock &BB = *FI;
2974 if (BlocksBeforeTry.count(&BB)) continue;
2975
2976 // Walk through all the calls in the block.
2977 for (llvm::BasicBlock::iterator
2978 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2979 llvm::Instruction &I = *BI;
2980
2981 // Ignore instructions that aren't non-intrinsic calls.
2982 // These are the only calls that can possibly call longjmp.
2983 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2984 if (isa<llvm::IntrinsicInst>(I))
2985 continue;
2986
2987 // Ignore call sites marked nounwind. This may be questionable,
2988 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2989 llvm::CallSite CS(&I);
2990 if (CS.doesNotThrow()) continue;
2991
John McCall2dd7d442010-08-04 05:59:32 +00002992 // Insert a read hazard before the call. This will ensure that
2993 // any writes to the locals are performed before making the
2994 // call. If the call throws, then this is sufficient to
2995 // guarantee correctness as long as it doesn't also write to any
2996 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002997 Builder.SetInsertPoint(&BB, BI);
2998 emitReadHazard(Builder);
2999 }
3000 }
3001}
3002
3003static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3004 if (V) S.insert(V);
3005}
3006
3007void FragileHazards::collectLocals() {
3008 // Compute a set of allocas to ignore.
3009 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3010 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3011 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00003012
3013 // Collect all the allocas currently in the function. This is
3014 // probably way too aggressive.
3015 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3016 for (llvm::BasicBlock::iterator
3017 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3018 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3019 Locals.push_back(&*I);
3020}
3021
3022llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003023 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003024 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3025 tys[i] = Locals[i]->getType();
3026 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003027}
3028
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003029/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003030
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003031 Objective-C setjmp-longjmp (sjlj) Exception Handling
3032 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003033
John McCallbd309292010-07-06 01:34:17 +00003034 A catch buffer is a setjmp buffer plus:
3035 - a pointer to the exception that was caught
3036 - a pointer to the previous exception data buffer
3037 - two pointers of reserved storage
3038 Therefore catch buffers form a stack, with a pointer to the top
3039 of the stack kept in thread-local storage.
3040
3041 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3042 objc_exception_try_exit pops the given catch buffer, which is
3043 required to be the top of the EH stack.
3044 objc_exception_throw pops the top of the EH stack, writes the
3045 thrown exception into the appropriate field, and longjmps
3046 to the setjmp buffer. It crashes the process (with a printf
3047 and an abort()) if there are no catch buffers on the stack.
3048 objc_exception_extract just reads the exception pointer out of the
3049 catch buffer.
3050
3051 There's no reason an implementation couldn't use a light-weight
3052 setjmp here --- something like __builtin_setjmp, but API-compatible
3053 with the heavyweight setjmp. This will be more important if we ever
3054 want to implement correct ObjC/C++ exception interactions for the
3055 fragile ABI.
3056
3057 Note that for this use of setjmp/longjmp to be correct, we may need
3058 to mark some local variables volatile: if a non-volatile local
3059 variable is modified between the setjmp and the longjmp, it has
3060 indeterminate value. For the purposes of LLVM IR, it may be
3061 sufficient to make loads and stores within the @try (to variables
3062 declared outside the @try) volatile. This is necessary for
3063 optimized correctness, but is not currently being done; this is
3064 being tracked as rdar://problem/8160285
3065
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003066 The basic framework for a @try-catch-finally is as follows:
3067 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003068 objc_exception_data d;
3069 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003070 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003071
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003072 objc_exception_try_enter(&d);
3073 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003074 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003075 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003076 // exception path
3077 id _caught = objc_exception_extract(&d);
3078
3079 // enter new try scope for handlers
3080 if (!setjmp(d.jmp_buf)) {
3081 ... match exception and execute catch blocks ...
3082
3083 // fell off end, rethrow.
3084 _rethrow = _caught;
3085 ... jump-through-finally to finally_rethrow ...
3086 } else {
3087 // exception in catch block
3088 _rethrow = objc_exception_extract(&d);
3089 _call_try_exit = false;
3090 ... jump-through-finally to finally_rethrow ...
3091 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003092 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003093 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003094
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003095 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003096 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003097 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003098
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003099 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003100 ... dispatch to finally destination ...
3101
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003102 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003103 objc_exception_throw(_rethrow);
3104
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003105 finally_end:
3106 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003107
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003108 This framework differs slightly from the one gcc uses, in that gcc
3109 uses _rethrow to determine if objc_exception_try_exit should be called
3110 and if the object should be rethrown. This breaks in the face of
3111 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003112
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003113 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003114
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003115 - If there are no catch blocks, then we avoid emitting the second
3116 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003117
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003118 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3119 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003120
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003121 - FIXME: If there is no @finally block we can do a few more
3122 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003123
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003124 Rethrows and Jumps-Through-Finally
3125 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003126
John McCallbd309292010-07-06 01:34:17 +00003127 '@throw;' is supported by pushing the currently-caught exception
3128 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003129
John McCallbd309292010-07-06 01:34:17 +00003130 Branches through the @finally block are handled with an ordinary
3131 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3132 exceptions are not compatible with C++ exceptions, and this is
3133 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003134
John McCallbd309292010-07-06 01:34:17 +00003135 @synchronized(expr) { stmt; } is emitted as if it were:
3136 id synch_value = expr;
3137 objc_sync_enter(synch_value);
3138 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003139*/
3140
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003141void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3142 const Stmt &S) {
3143 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003144
3145 // A destination for the fall-through edges of the catch handlers to
3146 // jump to.
3147 CodeGenFunction::JumpDest FinallyEnd =
3148 CGF.getJumpDestInCurrentScope("finally.end");
3149
3150 // A destination for the rethrow edge of the catch handlers to jump
3151 // to.
3152 CodeGenFunction::JumpDest FinallyRethrow =
3153 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003154
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003155 // For @synchronized, call objc_sync_enter(sync.expr). The
3156 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003157 // @synchronized. We can't avoid a temp here because we need the
3158 // value to be preserved. If the backend ever does liveness
3159 // correctly after setjmp, this will be unnecessary.
3160 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003161 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003162 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003163 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3164 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003165 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3166 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003167
3168 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3169 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003170 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003171
John McCall2dd7d442010-08-04 05:59:32 +00003172 // Allocate memory for the setjmp buffer. This needs to be kept
3173 // live throughout the try and catch blocks.
3174 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3175 "exceptiondata.ptr");
3176
John McCall42227ed2010-07-31 23:20:56 +00003177 // Create the fragile hazards. Note that this will not capture any
3178 // of the allocas required for exception processing, but will
3179 // capture the current basic block (which extends all the way to the
3180 // setjmp call) as "before the @try".
3181 FragileHazards Hazards(CGF);
3182
John McCallbd309292010-07-06 01:34:17 +00003183 // Create a flag indicating whether the cleanup needs to call
3184 // objc_exception_try_exit. This is true except when
3185 // - no catches match and we're branching through the cleanup
3186 // just to rethrow the exception, or
3187 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003188 // The setjmp-safety rule here is that we should always store to this
3189 // variable in a place that dominates the branch through the cleanup
3190 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003191 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003192 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003193
John McCall9916e3f2010-10-04 23:42:51 +00003194 // A slot containing the exception to rethrow. Only needed when we
3195 // have both a @catch and a @finally.
3196 llvm::Value *PropagatingExnVar = 0;
3197
John McCallbd309292010-07-06 01:34:17 +00003198 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003199 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003200 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003201 CallTryExitVar,
3202 ExceptionData,
3203 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003204
3205 // Enter a try block:
3206 // - Call objc_exception_try_enter to push ExceptionData on top of
3207 // the EH stack.
3208 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3209 ->setDoesNotThrow();
3210
3211 // - Call setjmp on the exception data buffer.
3212 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3213 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3214 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003215 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003216 llvm::CallInst *SetJmpResult =
3217 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3218 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003219 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003220
3221 // If setjmp returned 0, enter the protected block; otherwise,
3222 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003223 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3224 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003225 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003226 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3227 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003228
John McCallbd309292010-07-06 01:34:17 +00003229 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003230 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003231 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003232 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003233 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003234
3235 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003236
John McCallbd309292010-07-06 01:34:17 +00003237 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003238 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003239
John McCall42227ed2010-07-31 23:20:56 +00003240 // Don't optimize loads of the in-scope locals across this point.
3241 Hazards.emitWriteHazard();
3242
John McCallbd309292010-07-06 01:34:17 +00003243 // For a @synchronized (or a @try with no catches), just branch
3244 // through the cleanup to the rethrow block.
3245 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3246 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003247 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003248 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003249
3250 // Otherwise, we have to match against the caught exceptions.
3251 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003252 // Retrieve the exception object. We may emit multiple blocks but
3253 // nothing can cross this so the value is already in SSA form.
3254 llvm::CallInst *Caught =
3255 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3256 ExceptionData, "caught");
3257 Caught->setDoesNotThrow();
3258
John McCallbd309292010-07-06 01:34:17 +00003259 // Push the exception to rethrow onto the EH value stack for the
3260 // benefit of any @throws in the handlers.
3261 CGF.ObjCEHValueStack.push_back(Caught);
3262
Douglas Gregor96c79492010-04-23 22:50:49 +00003263 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003264
John McCall2dd7d442010-08-04 05:59:32 +00003265 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003266
John McCall2dd7d442010-08-04 05:59:32 +00003267 llvm::BasicBlock *CatchBlock = 0;
3268 llvm::BasicBlock *CatchHandler = 0;
3269 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003270 // Save the currently-propagating exception before
3271 // objc_exception_try_enter clears the exception slot.
3272 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3273 "propagating_exception");
3274 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3275
John McCall2dd7d442010-08-04 05:59:32 +00003276 // Enter a new exception try block (in case a @catch block
3277 // throws an exception).
3278 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3279 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003280
John McCall2dd7d442010-08-04 05:59:32 +00003281 llvm::CallInst *SetJmpResult =
3282 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3283 "setjmp.result");
3284 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003285 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003286
John McCall2dd7d442010-08-04 05:59:32 +00003287 llvm::Value *Threw =
3288 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3289
3290 CatchBlock = CGF.createBasicBlock("catch");
3291 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3292 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3293
3294 CGF.EmitBlock(CatchBlock);
3295 }
3296
3297 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003298
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003299 // Handle catch list. As a special case we check if everything is
3300 // matched and avoid generating code for falling off the end if
3301 // so.
3302 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003303 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3304 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003305
Douglas Gregor46a572b2010-04-26 16:46:50 +00003306 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003307 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003308
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003309 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003310 if (!CatchParam) {
3311 AllMatched = true;
3312 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003313 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003314
John McCallbd309292010-07-06 01:34:17 +00003315 // catch(id e) always matches under this ABI, since only
3316 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003317 // FIXME: For the time being we also match id<X>; this should
3318 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003319 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003320 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003322
John McCallbd309292010-07-06 01:34:17 +00003323 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003324 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003325 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3326
Anders Carlsson9396a892008-09-11 09:15:33 +00003327 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003328 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003329 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003330
3331 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003332 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003333 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003334
Anders Carlsson9396a892008-09-11 09:15:33 +00003335 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003336
3337 // The scope of the catch variable ends right here.
3338 CatchVarCleanups.ForceCleanup();
3339
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003340 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003341 break;
3342 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003343
Steve Naroff7cae42b2009-07-10 23:34:53 +00003344 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003345 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003346
3347 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003348 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3349 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003350
3351 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003352 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003353
John McCallbd309292010-07-06 01:34:17 +00003354 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003355 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3356 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003357 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003358
John McCallbd309292010-07-06 01:34:17 +00003359 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3360 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003361
3362 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003363 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003364
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003365 // Emit the @catch block.
3366 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003367
3368 // Collect any cleanups for the catch variable. The scope lasts until
3369 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003370 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003371
John McCall1c9c3fd2010-10-15 04:57:14 +00003372 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003373 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003374
John McCallbd309292010-07-06 01:34:17 +00003375 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003376 llvm::Value *Tmp =
3377 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003378 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003379 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003380
Anders Carlsson9396a892008-09-11 09:15:33 +00003381 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003382
3383 // We're done with the catch variable.
3384 CatchVarCleanups.ForceCleanup();
3385
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003386 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003387
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003388 CGF.EmitBlock(NextCatchBlock);
3389 }
3390
John McCallbd309292010-07-06 01:34:17 +00003391 CGF.ObjCEHValueStack.pop_back();
3392
John McCall2dd7d442010-08-04 05:59:32 +00003393 // If nothing wanted anything to do with the caught exception,
3394 // kill the extract call.
3395 if (Caught->use_empty())
3396 Caught->eraseFromParent();
3397
3398 if (!AllMatched)
3399 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3400
3401 if (HasFinally) {
3402 // Emit the exception handler for the @catch blocks.
3403 CGF.EmitBlock(CatchHandler);
3404
3405 // In theory we might now need a write hazard, but actually it's
3406 // unnecessary because there's no local-accessing code between
3407 // the try's write hazard and here.
3408 //Hazards.emitWriteHazard();
3409
John McCall9916e3f2010-10-04 23:42:51 +00003410 // Extract the new exception and save it to the
3411 // propagating-exception slot.
3412 assert(PropagatingExnVar);
3413 llvm::CallInst *NewCaught =
3414 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3415 ExceptionData, "caught");
3416 NewCaught->setDoesNotThrow();
3417 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3418
John McCall2dd7d442010-08-04 05:59:32 +00003419 // Don't pop the catch handler; the throw already did.
3420 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003421 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003422 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003423 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003424
John McCall42227ed2010-07-31 23:20:56 +00003425 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003426 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003427
John McCallbd309292010-07-06 01:34:17 +00003428 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003429 CGF.Builder.restoreIP(TryFallthroughIP);
3430 if (CGF.HaveInsertPoint())
3431 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003432 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003433 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003434
John McCallbd309292010-07-06 01:34:17 +00003435 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003436 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003437 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003438 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003439 // If we have a propagating-exception variable, check it.
3440 llvm::Value *PropagatingExn;
3441 if (PropagatingExnVar) {
3442 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003443
John McCall9916e3f2010-10-04 23:42:51 +00003444 // Otherwise, just look in the buffer for the exception to throw.
3445 } else {
3446 llvm::CallInst *Caught =
3447 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3448 ExceptionData);
3449 Caught->setDoesNotThrow();
3450 PropagatingExn = Caught;
3451 }
3452
3453 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003454 ->setDoesNotThrow();
3455 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003456 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003457
John McCall42227ed2010-07-31 23:20:56 +00003458 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003459}
3460
3461void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003462 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003463 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003464
Anders Carlssone005aa12008-09-09 16:16:55 +00003465 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003466 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003467 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003468 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003469 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003470 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003471 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003472 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003473 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003474
John McCallbd309292010-07-06 01:34:17 +00003475 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3476 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003477 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003478
3479 // Clear the insertion point to indicate we are in unreachable code.
3480 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003481}
3482
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003483/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003484/// object: objc_read_weak (id *src)
3485///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003486llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003487 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003488 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3490 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3491 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003492 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003493 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003494 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003495 return read_weak;
3496}
3497
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003498/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3499/// objc_assign_weak (id src, id *dst)
3500///
3501void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003502 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003503 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003504 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003505 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003506 assert(Size <= 8 && "does not support size > 8");
3507 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003508 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003509 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3510 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003511 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3512 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003513 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003514 src, dst, "weakassign");
3515 return;
3516}
3517
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003518/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3519/// objc_assign_global (id src, id *dst)
3520///
3521void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003522 llvm::Value *src, llvm::Value *dst,
3523 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003524 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003525 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003526 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003527 assert(Size <= 8 && "does not support size > 8");
3528 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003529 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003530 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3531 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003532 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3533 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003534 if (!threadlocal)
3535 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3536 src, dst, "globalassign");
3537 else
3538 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3539 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003540 return;
3541}
3542
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003543/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003544/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003545///
3546void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003547 llvm::Value *src, llvm::Value *dst,
3548 llvm::Value *ivarOffset) {
3549 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003550 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003551 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003552 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003553 assert(Size <= 8 && "does not support size > 8");
3554 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003555 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003556 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3557 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003558 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3559 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003560 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3561 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003562 return;
3563}
3564
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003565/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3566/// objc_assign_strongCast (id src, id *dst)
3567///
3568void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003569 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003570 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003571 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003572 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003573 assert(Size <= 8 && "does not support size > 8");
3574 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003575 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003576 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3577 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003578 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3579 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003580 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003581 src, dst, "weakassign");
3582 return;
3583}
3584
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003585void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003586 llvm::Value *DestPtr,
3587 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003588 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003589 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3590 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003591 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003592 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003593 return;
3594}
3595
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003596/// EmitObjCValueForIvar - Code Gen for ivar reference.
3597///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003598LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3599 QualType ObjectTy,
3600 llvm::Value *BaseValue,
3601 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003602 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003603 const ObjCInterfaceDecl *ID =
3604 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003605 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3606 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003607}
3608
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003609llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003610 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003611 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003612 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003613 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003614 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3615 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003616}
3617
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003618/* *** Private Interface *** */
3619
3620/// EmitImageInfo - Emit the image info marker used to encode some module
3621/// level information.
3622///
3623/// See: <rdr://4810609&4810587&4810587>
3624/// struct IMAGE_INFO {
3625/// unsigned version;
3626/// unsigned flags;
3627/// };
3628enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003629 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003630 eImageInfo_GarbageCollected = (1 << 1),
3631 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003632 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3633
Daniel Dunbar5e639272010-04-25 20:39:01 +00003634 // A flag indicating that the module has no instances of a @synthesize of a
3635 // superclass variable. <rdar://problem/6803242>
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003636 eImageInfo_CorrectedSynthesize = (1 << 4),
3637 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003638};
3639
Daniel Dunbar5e639272010-04-25 20:39:01 +00003640void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003641 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003642 const char *Section = (ObjCABI == 1) ?
3643 "__OBJC, __image_info,regular" :
3644 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003645
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003646 // Generate module-level named metadata to convey this information to the
3647 // linker and code-gen.
3648 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003649
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003650 // Add the ObjC ABI version to the module flags.
3651 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3652 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3653 version);
3654 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3655 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003656
David Blaikiebbafb8a2012-03-11 07:00:24 +00003657 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003658 // Non-GC overrides those files which specify GC.
3659 Mod.addModuleFlag(llvm::Module::Override,
3660 "Objective-C Garbage Collection", (uint32_t)0);
3661 } else {
3662 // Add the ObjC garbage collection value.
3663 Mod.addModuleFlag(llvm::Module::Error,
3664 "Objective-C Garbage Collection",
3665 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003666
David Blaikiebbafb8a2012-03-11 07:00:24 +00003667 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003668 // Add the ObjC GC Only value.
3669 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3670 eImageInfo_GCOnly);
3671
3672 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3673 llvm::Value *Ops[2] = {
3674 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3675 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3676 eImageInfo_GarbageCollected)
3677 };
3678 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3679 llvm::MDNode::get(VMContext, Ops));
3680 }
3681 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003682
3683 // Indicate whether we're compiling this to run on a simulator.
3684 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3685 if (Triple.getOS() == llvm::Triple::IOS &&
3686 (Triple.getArch() == llvm::Triple::x86 ||
3687 Triple.getArch() == llvm::Triple::x86_64))
3688 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3689 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003690}
3691
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003692// struct objc_module {
3693// unsigned long version;
3694// unsigned long size;
3695// const char *name;
3696// Symtab symtab;
3697// };
3698
3699// FIXME: Get from somewhere
3700static const int ModuleVersion = 7;
3701
3702void CGObjCMac::EmitModuleInfo() {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003703 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003704
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003705 llvm::Constant *Values[] = {
3706 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3707 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3708 // This used to be the filename, now it is unused. <rdr://4327263>
3709 GetClassName(&CGM.getContext().Idents.get("")),
3710 EmitModuleSymbols()
3711 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003712 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003713 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003714 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003715 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003716}
3717
3718llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003719 unsigned NumClasses = DefinedClasses.size();
3720 unsigned NumCategories = DefinedCategories.size();
3721
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003722 // Return null if no symbols were defined.
3723 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003724 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003725
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003726 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003727 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003728 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003729 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3730 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003731
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003732 // The runtime expects exactly the list of defined classes followed
3733 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00003734 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003735 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003736 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003737 ObjCTypes.Int8PtrTy);
3738 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003739 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003740 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003741 ObjCTypes.Int8PtrTy);
3742
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003743 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003744 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00003745 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003746 Symbols);
3747
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003748 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003749
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003750 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003751 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3752 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003753 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003754 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003755}
3756
John McCall31168b02011-06-15 23:02:42 +00003757llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3758 IdentifierInfo *II) {
3759 LazySymbols.insert(II);
3760
3761 llvm::GlobalVariable *&Entry = ClassReferences[II];
3762
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003763 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003764 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003765 llvm::ConstantExpr::getBitCast(GetClassName(II),
3766 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003767 Entry =
John McCall31168b02011-06-15 23:02:42 +00003768 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3769 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3770 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003771 }
John McCall31168b02011-06-15 23:02:42 +00003772
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003773 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003774}
3775
John McCall31168b02011-06-15 23:02:42 +00003776llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3777 const ObjCInterfaceDecl *ID) {
3778 return EmitClassRefFromId(Builder, ID->getIdentifier());
3779}
3780
3781llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3782 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3783 return EmitClassRefFromId(Builder, II);
3784}
3785
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003786llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3787 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003788 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003789
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003790 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003791 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003792 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003793 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003794 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003795 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3796 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003797 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003798 }
3799
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003800 if (lvalue)
3801 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003802 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003803}
3804
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003805llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003806 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003807
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003808 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003809 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00003810 llvm::ConstantDataArray::getString(VMContext,
3811 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003812 ((ObjCABI == 2) ?
3813 "__TEXT,__objc_classname,cstring_literals" :
3814 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003815 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003816
Owen Anderson170229f2009-07-14 23:10:40 +00003817 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003818}
3819
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003820llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3821 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3822 I = MethodDefinitions.find(MD);
3823 if (I != MethodDefinitions.end())
3824 return I->second;
3825
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003826 return NULL;
3827}
3828
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003829/// GetIvarLayoutName - Returns a unique constant for the given
3830/// ivar layout bitmap.
3831llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003832 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003833 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003834}
3835
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003836void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003837 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003838 bool ForStrongLayout,
3839 bool &HasUnion) {
3840 const RecordDecl *RD = RT->getDecl();
3841 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00003842 SmallVector<const FieldDecl*, 16> Fields;
3843 for (RecordDecl::field_iterator i = RD->field_begin(),
3844 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00003845 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00003846 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003847 const llvm::StructLayout *RecLayout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00003848 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003849
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003850 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3851 ForStrongLayout, HasUnion);
3852}
3853
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003854void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003855 const llvm::StructLayout *Layout,
3856 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003857 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003858 unsigned int BytePos, bool ForStrongLayout,
3859 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003860 bool IsUnion = (RD && RD->isUnion());
3861 uint64_t MaxUnionIvarSize = 0;
3862 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003863 const FieldDecl *MaxField = 0;
3864 const FieldDecl *MaxSkippedField = 0;
3865 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003866 uint64_t MaxFieldOffset = 0;
3867 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003868 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003869 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003870
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003871 if (RecFields.empty())
3872 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003873 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3874 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003875 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003876 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003877 FirstFieldDelta =
3878 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3879 }
3880
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003881 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003882 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003883 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003884 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003885 // Note that 'i' here is actually the field index inside RD of Field,
3886 // although this dependency is hidden.
3887 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003888 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003889 } else
John McCall31168b02011-06-15 23:02:42 +00003890 FieldOffset =
3891 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003892
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003893 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003894 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003895 LastFieldBitfieldOrUnnamed = Field;
3896 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003897 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003898 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003899
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003900 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003901 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003902 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003903 if (FQT->isUnionType())
3904 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003905
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003906 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003907 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003908 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003909 continue;
3910 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003911
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003912 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003913 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003914 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003915 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003916 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003917 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003918 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3919 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003920 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003921 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003922 FQT = CArray->getElementType();
3923 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003924
3925 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003926 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003927 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003928 int OldIndex = IvarsInfo.size() - 1;
3929 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003930
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003931 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003932 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003933 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003934
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003935 // Replicate layout information for each array element. Note that
3936 // one element is already done.
3937 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003938 for (int FirstIndex = IvarsInfo.size() - 1,
3939 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003940 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003941 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3942 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3943 IvarsInfo[i].ivar_size));
3944 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3945 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3946 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003947 }
3948 continue;
3949 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003950 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003951 // At this point, we are done with Record/Union and array there of.
3952 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003953 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003954
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003955 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003956 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3957 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003958 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003959 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003960 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003961 MaxUnionIvarSize = UnionIvarSize;
3962 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003963 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003964 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003965 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003966 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003967 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003968 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003969 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003970 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3971 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003972 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003973 // FIXME: Why the asymmetry? We divide by word size in bits on other
3974 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003975 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003976 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003977 MaxSkippedUnionIvarSize = UnionIvarSize;
3978 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003979 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003980 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003981 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003982 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003983 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003984 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003985 }
3986 }
3987 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003988
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003989 if (LastFieldBitfieldOrUnnamed) {
3990 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3991 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003992 uint64_t BitFieldSize
3993 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003994 GC_IVAR skivar;
3995 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3996 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3997 + ((BitFieldSize % ByteSizeInBits) != 0);
3998 SkipIvars.push_back(skivar);
3999 } else {
4000 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4001 // Last field was unnamed. Must update skip info.
4002 unsigned FieldSize
4003 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4004 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4005 FieldSize / ByteSizeInBits));
4006 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004007 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004008
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004009 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004010 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004011 MaxUnionIvarSize));
4012 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00004013 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004014 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004015}
4016
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004017/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4018/// the computations and returning the layout bitmap (for ivar or blocks) in
4019/// the given argument BitMap string container. Routine reads
4020/// two containers, IvarsInfo and SkipIvars which are assumed to be
4021/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004022llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004023 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004024 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004025
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004026 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004027 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004028 unsigned int WordSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004029 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004030 if (IvarsInfo[0].ivar_bytepos == 0) {
4031 WordsToSkip = 0;
4032 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004033 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004034 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4035 WordsToScan = IvarsInfo[0].ivar_size;
4036 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004037 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004038 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004039 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004040 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004041 // consecutive 'scanned' object pointers.
4042 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004043 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004044 // Skip over 'gc'able object pointer which lay over each other.
4045 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4046 continue;
4047 // Must skip over 1 or more words. We save current skip/scan values
4048 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004049 SKIP_SCAN SkScan;
4050 SkScan.skip = WordsToSkip;
4051 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004052 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004053
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004054 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004055 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4056 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004057 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004058 WordsToSkip = 0;
4059 WordsToScan = IvarsInfo[i].ivar_size;
4060 }
4061 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004062 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004063 SKIP_SCAN SkScan;
4064 SkScan.skip = WordsToSkip;
4065 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004066 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004067 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004068
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004069 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004070 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004071 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004072 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004073 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004074 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004075 IvarsInfo[LastIndex].ivar_bytepos +
4076 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004077 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004078 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004079 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004080 SKIP_SCAN SkScan;
4081 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4082 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004083 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004084 }
4085 }
4086 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4087 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004088 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004089 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004090 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4091 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4092 // 0xM0 followed by 0x0N detected.
4093 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4094 for (int j = i+1; j < SkipScan; j++)
4095 SkipScanIvars[j] = SkipScanIvars[j+1];
4096 --SkipScan;
4097 }
4098 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004099
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004100 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004101 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004102 unsigned char byte;
4103 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4104 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4105 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4106 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004107
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004108 // first skip big.
4109 for (unsigned int ix = 0; ix < skip_big; ix++)
4110 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004111
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004112 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004113 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004114 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004115 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004116 byte |= 0xf;
4117 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004118 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004119 byte |= scan_small;
4120 scan_small = 0;
4121 }
4122 BitMap += byte;
4123 }
4124 // next scan big
4125 for (unsigned int ix = 0; ix < scan_big; ix++)
4126 BitMap += (unsigned char)(0x0f);
4127 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004128 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004129 byte = scan_small;
4130 BitMap += byte;
4131 }
4132 }
4133 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004134 unsigned char zero = 0;
4135 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004136
4137 llvm::GlobalVariable * Entry =
4138 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004139 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004140 ((ObjCABI == 2) ?
4141 "__TEXT,__objc_classname,cstring_literals" :
4142 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004143 1, true);
4144 return getConstantGEP(VMContext, Entry, 0, 0);
4145}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004146
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004147/// BuildIvarLayout - Builds ivar layout bitmap for the class
4148/// implementation for the __strong or __weak case.
4149/// The layout map displays which words in ivar list must be skipped
4150/// and which must be scanned by GC (see below). String is built of bytes.
4151/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4152/// of words to skip and right nibble is count of words to scan. So, each
4153/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4154/// represented by a 0x00 byte which also ends the string.
4155/// 1. when ForStrongLayout is true, following ivars are scanned:
4156/// - id, Class
4157/// - object *
4158/// - __strong anything
4159///
4160/// 2. When ForStrongLayout is false, following ivars are scanned:
4161/// - __weak anything
4162///
4163llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4164 const ObjCImplementationDecl *OMD,
4165 bool ForStrongLayout) {
4166 bool hasUnion = false;
4167
Chris Lattnerece04092012-02-07 00:39:47 +00004168 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004169 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4170 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004171 return llvm::Constant::getNullValue(PtrTy);
4172
Jordy Rosea91768e2011-07-22 02:08:32 +00004173 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4174 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004175 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004176 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004177 IVD; IVD = IVD->getNextIvar())
4178 RecFields.push_back(cast<FieldDecl>(IVD));
4179 }
4180 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004181 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004182 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004183
Jordy Rosea91768e2011-07-22 02:08:32 +00004184 // FIXME: This is not ideal; we shouldn't have to do this copy.
4185 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004186 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004187
4188 if (RecFields.empty())
4189 return llvm::Constant::getNullValue(PtrTy);
4190
4191 SkipIvars.clear();
4192 IvarsInfo.clear();
4193
4194 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4195 if (IvarsInfo.empty())
4196 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004197 // Sort on byte position in case we encounterred a union nested in
4198 // the ivar list.
4199 if (hasUnion && !IvarsInfo.empty())
4200 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4201 if (hasUnion && !SkipIvars.empty())
4202 std::sort(SkipIvars.begin(), SkipIvars.end());
4203
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004204 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004205 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004206
David Blaikiebbafb8a2012-03-11 07:00:24 +00004207 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004209 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004210 OMD->getClassInterface()->getName().data());
Roman Divackye6377112012-09-06 15:59:27 +00004211 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004212 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004213 if (!(s[i] & 0xf0))
4214 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4215 else
4216 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4217 printf("\n");
4218 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004219 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004220}
4221
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004222llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004223 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4224
Chris Lattner3def9ae2012-02-06 22:16:34 +00004225 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004226 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004227 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004228 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004229 ((ObjCABI == 2) ?
4230 "__TEXT,__objc_methname,cstring_literals" :
4231 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004232 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004233
Owen Anderson170229f2009-07-14 23:10:40 +00004234 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004235}
4236
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004237// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004238llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004239 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4240}
4241
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004242llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004243 std::string TypeStr;
4244 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4245
4246 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004247
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004248 if (!Entry)
4249 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004250 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004251 ((ObjCABI == 2) ?
4252 "__TEXT,__objc_methtype,cstring_literals" :
4253 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004254 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004255
Owen Anderson170229f2009-07-14 23:10:40 +00004256 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004257}
4258
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004259llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4260 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004261 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004262 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004263 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004264
4265 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4266
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004267 if (!Entry)
4268 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004269 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004270 ((ObjCABI == 2) ?
4271 "__TEXT,__objc_methtype,cstring_literals" :
4272 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004273 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004274
Owen Anderson170229f2009-07-14 23:10:40 +00004275 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004276}
4277
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004278// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004279llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004280 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004281
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004282 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004283 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004284 llvm::ConstantDataArray::getString(VMContext,
4285 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004286 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004287 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004288
Owen Anderson170229f2009-07-14 23:10:40 +00004289 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004290}
4291
4292// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004293// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004294llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004295CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4296 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004297 std::string TypeStr;
4298 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004299 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4300}
4301
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004302void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004303 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004304 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004305 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004306 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004307 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4308 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004309 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004310 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004311 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004312 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004313}
4314
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004315void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004316 EmitModuleInfo();
4317
Daniel Dunbarc475d422008-10-29 22:36:39 +00004318 // Emit the dummy bodies for any protocols which were referenced but
4319 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004320 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004321 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4322 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004323 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004324
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004325 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004326 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004327 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004328 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004329 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004330 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004331 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004332 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004333 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004334 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004335 }
4336
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004337 // Add assembler directives to add lazy undefined symbol references
4338 // for classes which are referenced but not defined. This is
4339 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004340 //
4341 // FIXME: It would be nice if we had an LLVM construct for this.
4342 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004343 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004344 Asm += CGM.getModule().getModuleInlineAsm();
4345 if (!Asm.empty() && Asm.back() != '\n')
4346 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004347
Daniel Dunbard027a922009-09-07 00:20:42 +00004348 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004349 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4350 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004351 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4352 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004353 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004354 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004355 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004356 }
4357
Bill Wendling53136852012-02-07 09:06:01 +00004358 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004359 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4360 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4361 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004362
Daniel Dunbard027a922009-09-07 00:20:42 +00004363 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004364 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004365}
4366
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004368 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004369 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004370 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004371 ObjCABI = 2;
4372}
4373
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004374/* *** */
4375
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004376ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004377 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4378{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004379 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4380 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004381
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004382 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004383 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004384 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004385 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00004386 Int8PtrTy = CGM.Int8PtrTy;
4387 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004389 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004390 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004391 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004392
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004393 // I'm not sure I like this. The implicit coordination is a bit
4394 // gross. We should solve this in a reasonable fashion because this
4395 // is a pretty common task (match some runtime data structure with
4396 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004397
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004398 // FIXME: This is leaked.
4399 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004400
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004401 // struct _objc_super {
4402 // id self;
4403 // Class cls;
4404 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004405 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004406 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004407 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004408 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004409 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004410 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004411 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004412 Ctx.getObjCClassType(), 0, 0, false,
4413 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004414 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004415
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004416 SuperCTy = Ctx.getTagDeclType(RD);
4417 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004418
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004419 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004420 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4421
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004422 // struct _prop_t {
4423 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004424 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004425 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004426 PropertyTy = llvm::StructType::create("struct._prop_t",
4427 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004428
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004429 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004430 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004431 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004432 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004433 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004434 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004435 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4436 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004437 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004438 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004440 // struct _objc_method {
4441 // SEL _cmd;
4442 // char *method_type;
4443 // char *_imp;
4444 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004445 MethodTy = llvm::StructType::create("struct._objc_method",
4446 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4447 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004449 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004450 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004451 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004452
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004453}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004454
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004455ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004456 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004457 // struct _objc_method_description {
4458 // SEL name;
4459 // char *types;
4460 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004461 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004462 llvm::StructType::create("struct._objc_method_description",
4463 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004464
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004465 // struct _objc_method_description_list {
4466 // int count;
4467 // struct _objc_method_description[1];
4468 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004470 llvm::StructType::create("struct._objc_method_description_list",
4471 IntTy,
4472 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004473
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004474 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004475 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004476 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004477
Daniel Dunbarb036db82008-08-13 03:21:16 +00004478 // Protocol description structures
4479
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004480 // struct _objc_protocol_extension {
4481 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4482 // struct _objc_method_description_list *optional_instance_methods;
4483 // struct _objc_method_description_list *optional_class_methods;
4484 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004485 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004486 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004487 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004488 llvm::StructType::create("struct._objc_protocol_extension",
4489 IntTy, MethodDescriptionListPtrTy,
4490 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004491 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004492
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004493 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004494 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004495
Daniel Dunbarc475d422008-10-29 22:36:39 +00004496 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004497
Chris Lattnera5f58b02011-07-09 17:41:47 +00004498 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004499 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004500
Chris Lattnera5f58b02011-07-09 17:41:47 +00004501 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004502 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004503 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004504 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004505 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004506 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004507
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004508 // struct _objc_protocol {
4509 // struct _objc_protocol_extension *isa;
4510 // char *protocol_name;
4511 // struct _objc_protocol **_objc_protocol_list;
4512 // struct _objc_method_description_list *instance_methods;
4513 // struct _objc_method_description_list *class_methods;
4514 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004515 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4516 llvm::PointerType::getUnqual(ProtocolListTy),
4517 MethodDescriptionListPtrTy,
4518 MethodDescriptionListPtrTy,
4519 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004520
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004521 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004522 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004523
Owen Anderson9793f0e2009-07-29 22:16:19 +00004524 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004525
4526 // Class description structures
4527
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004528 // struct _objc_ivar {
4529 // char *ivar_name;
4530 // char *ivar_type;
4531 // int ivar_offset;
4532 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004533 IvarTy = llvm::StructType::create("struct._objc_ivar",
4534 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004535
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004536 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004537 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004538 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004539 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004540
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004541 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004542 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004543 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004544 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004545
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004546 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004547 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004548 llvm::StructType::create("struct._objc_class_extension",
4549 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004550 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004551
Chris Lattner5ec04a52011-08-12 17:43:31 +00004552 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004553
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004554 // struct _objc_class {
4555 // Class isa;
4556 // Class super_class;
4557 // char *name;
4558 // long version;
4559 // long info;
4560 // long instance_size;
4561 // struct _objc_ivar_list *ivars;
4562 // struct _objc_method_list *methods;
4563 // struct _objc_cache *cache;
4564 // struct _objc_protocol_list *protocols;
4565 // char *ivar_layout;
4566 // struct _objc_class_ext *ext;
4567 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004568 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4569 llvm::PointerType::getUnqual(ClassTy),
4570 Int8PtrTy,
4571 LongTy,
4572 LongTy,
4573 LongTy,
4574 IvarListPtrTy,
4575 MethodListPtrTy,
4576 CachePtrTy,
4577 ProtocolListPtrTy,
4578 Int8PtrTy,
4579 ClassExtensionPtrTy,
4580 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004581
Owen Anderson9793f0e2009-07-29 22:16:19 +00004582 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004583
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004584 // struct _objc_category {
4585 // char *category_name;
4586 // char *class_name;
4587 // struct _objc_method_list *instance_method;
4588 // struct _objc_method_list *class_method;
4589 // uint32_t size; // sizeof(struct _objc_category)
4590 // struct _objc_property_list *instance_properties;// category's @property
4591 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004592 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004593 llvm::StructType::create("struct._objc_category",
4594 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4595 MethodListPtrTy, ProtocolListPtrTy,
4596 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004597
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004598 // Global metadata structures
4599
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004600 // struct _objc_symtab {
4601 // long sel_ref_cnt;
4602 // SEL *refs;
4603 // short cls_def_cnt;
4604 // short cat_def_cnt;
4605 // char *defs[cls_def_cnt + cat_def_cnt];
4606 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004607 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004608 llvm::StructType::create("struct._objc_symtab",
4609 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4610 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004611 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004612
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004613 // struct _objc_module {
4614 // long version;
4615 // long size; // sizeof(struct _objc_module)
4616 // char *name;
4617 // struct _objc_symtab* symtab;
4618 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004619 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004620 llvm::StructType::create("struct._objc_module",
4621 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004622
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004623
Mike Stump18bb9282009-05-16 07:57:57 +00004624 // FIXME: This is the size of the setjmp buffer and should be target
4625 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004626 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004627
Anders Carlsson9ff22482008-09-09 10:10:21 +00004628 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00004629 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004630
4631 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004632 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00004633 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4634 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004635
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004636}
4637
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004638ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004639 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004640 // struct _method_list_t {
4641 // uint32_t entsize; // sizeof(struct _objc_method)
4642 // uint32_t method_count;
4643 // struct _objc_method method_list[method_count];
4644 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004645 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004646 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4647 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004648 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004649 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004651 // struct _protocol_t {
4652 // id isa; // NULL
4653 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004654 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004655 // const struct method_list_t * const instance_methods;
4656 // const struct method_list_t * const class_methods;
4657 // const struct method_list_t *optionalInstanceMethods;
4658 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004659 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004660 // const uint32_t size; // sizeof(struct _protocol_t)
4661 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004662 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004663 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004664
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004665 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004666 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004667 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004668
Chris Lattnera5f58b02011-07-09 17:41:47 +00004669 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004670 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4671 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4672 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4673 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004674 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4675 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004676
4677 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004678 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004679
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004680 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004681 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004682 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004683 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004684 ProtocolListnfABITy->setBody(LongTy,
4685 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4686 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004687
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004688 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004689 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004690
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004691 // struct _ivar_t {
4692 // unsigned long int *offset; // pointer to ivar offset location
4693 // char *name;
4694 // char *type;
4695 // uint32_t alignment;
4696 // uint32_t size;
4697 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004698 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004699 llvm::StructType::create("struct._ivar_t",
4700 llvm::PointerType::getUnqual(LongTy),
4701 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004702
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004703 // struct _ivar_list_t {
4704 // uint32 entsize; // sizeof(struct _ivar_t)
4705 // uint32 count;
4706 // struct _iver_t list[count];
4707 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004708 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004709 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4710 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004711
Owen Anderson9793f0e2009-07-29 22:16:19 +00004712 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004713
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004714 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004715 // uint32_t const flags;
4716 // uint32_t const instanceStart;
4717 // uint32_t const instanceSize;
4718 // uint32_t const reserved; // only when building for 64bit targets
4719 // const uint8_t * const ivarLayout;
4720 // const char *const name;
4721 // const struct _method_list_t * const baseMethods;
4722 // const struct _objc_protocol_list *const baseProtocols;
4723 // const struct _ivar_list_t *const ivars;
4724 // const uint8_t * const weakIvarLayout;
4725 // const struct _prop_list_t * const properties;
4726 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004727
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004728 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004729 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4730 IntTy, IntTy, IntTy, Int8PtrTy,
4731 Int8PtrTy, MethodListnfABIPtrTy,
4732 ProtocolListnfABIPtrTy,
4733 IvarListnfABIPtrTy,
4734 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004735
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004736 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004737 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004738 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4739 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004740
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004741 // struct _class_t {
4742 // struct _class_t *isa;
4743 // struct _class_t * const superclass;
4744 // void *cache;
4745 // IMP *vtable;
4746 // struct class_ro_t *ro;
4747 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004748
Chris Lattner5ec04a52011-08-12 17:43:31 +00004749 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004750 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4751 llvm::PointerType::getUnqual(ClassnfABITy),
4752 CachePtrTy,
4753 llvm::PointerType::getUnqual(ImpnfABITy),
4754 llvm::PointerType::getUnqual(ClassRonfABITy),
4755 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004756
Fariborz Jahanian71394042009-01-23 23:53:38 +00004757 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004758 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004760 // struct _category_t {
4761 // const char * const name;
4762 // struct _class_t *const cls;
4763 // const struct _method_list_t * const instance_methods;
4764 // const struct _method_list_t * const class_methods;
4765 // const struct _protocol_list_t * const protocols;
4766 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004767 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004768 CategorynfABITy = llvm::StructType::create("struct._category_t",
4769 Int8PtrTy, ClassnfABIPtrTy,
4770 MethodListnfABIPtrTy,
4771 MethodListnfABIPtrTy,
4772 ProtocolListnfABIPtrTy,
4773 PropertyListPtrTy,
4774 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004775
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004776 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004777 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4778 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004779
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004780 // MessageRefTy - LLVM for:
4781 // struct _message_ref_t {
4782 // IMP messenger;
4783 // SEL name;
4784 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004785
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004786 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004787 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004788 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004789 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004790 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004791 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004792 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004793 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004794 Ctx.getObjCSelType(), 0, 0, false,
4795 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004796 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004797
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004798 MessageRefCTy = Ctx.getTagDeclType(RD);
4799 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4800 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004801
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004802 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004803 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004804
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004805 // SuperMessageRefTy - LLVM for:
4806 // struct _super_message_ref_t {
4807 // SUPER_IMP messenger;
4808 // SEL name;
4809 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004810 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004811 llvm::StructType::create("struct._super_message_ref_t",
4812 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004813
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004814 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004815 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004816
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004817
4818 // struct objc_typeinfo {
4819 // const void** vtable; // objc_ehtype_vtable + 2
4820 // const char* name; // c++ typeinfo string
4821 // Class cls;
4822 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004823 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004824 llvm::StructType::create("struct._objc_typeinfo",
4825 llvm::PointerType::getUnqual(Int8PtrTy),
4826 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004827 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004828}
4829
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004830llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004831 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832
Fariborz Jahanian71394042009-01-23 23:53:38 +00004833 return NULL;
4834}
4835
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00004836void CGObjCNonFragileABIMac::
4837AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4838 const char *SymbolName,
4839 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00004840 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004841
Daniel Dunbar19573e72009-05-15 21:48:48 +00004842 if (!NumClasses)
4843 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004844
Chris Lattner3def9ae2012-02-06 22:16:34 +00004845 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004846 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004847 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004848 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00004849 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004850 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004851 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00004852 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004853
Daniel Dunbar19573e72009-05-15 21:48:48 +00004854 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004855 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004856 llvm::GlobalValue::InternalLinkage,
4857 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004858 SymbolName);
Micah Villmowdd31ca12012-10-08 16:25:52 +00004859 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004860 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004861 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004862}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004863
Fariborz Jahanian71394042009-01-23 23:53:38 +00004864void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4865 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004866
Daniel Dunbar19573e72009-05-15 21:48:48 +00004867 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004868 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004869 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004870 "\01L_OBJC_LABEL_CLASS_$",
4871 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004872
Bill Wendling53136852012-02-07 09:06:01 +00004873 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00004874 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4875 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4876 continue;
4877 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004878 }
4879
Bill Wendling53136852012-02-07 09:06:01 +00004880 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004881 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4882 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4883 continue;
4884 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4885 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004886
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004887 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004888 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4889 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004890
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004891 // Build list of all implemented category addresses in array
4892 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004893 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004894 "\01L_OBJC_LABEL_CATEGORY_$",
4895 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004896 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004897 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4898 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004899
Daniel Dunbar5e639272010-04-25 20:39:01 +00004900 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004901}
4902
John McCall9e8bb002011-05-14 03:10:52 +00004903/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4904/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004905/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004906/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004907bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4908 // At various points we've experimented with using vtable-based
4909 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004910 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004911 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004912 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004913 case CodeGenOptions::NonLegacy:
4914 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004915 case CodeGenOptions::Mixed:
4916 break;
4917 }
4918
4919 // If so, see whether this selector is in the white-list of things which must
4920 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004921 if (VTableDispatchMethods.empty()) {
4922 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4923 VTableDispatchMethods.insert(GetNullarySelector("class"));
4924 VTableDispatchMethods.insert(GetNullarySelector("self"));
4925 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4926 VTableDispatchMethods.insert(GetNullarySelector("length"));
4927 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928
John McCall9e8bb002011-05-14 03:10:52 +00004929 // These are vtable-based if GC is disabled.
4930 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004931 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004932 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4933 VTableDispatchMethods.insert(GetNullarySelector("release"));
4934 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4935 }
4936
4937 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4938 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4939 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4940 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4941 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4942 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4943 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4944
4945 // These are vtable-based if GC is enabled.
4946 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004947 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004948 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4949 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4950
4951 // "countByEnumeratingWithState:objects:count"
4952 IdentifierInfo *KeyIdents[] = {
4953 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4954 &CGM.getContext().Idents.get("objects"),
4955 &CGM.getContext().Idents.get("count")
4956 };
4957 VTableDispatchMethods.insert(
4958 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4959 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004960 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004961
John McCall9e8bb002011-05-14 03:10:52 +00004962 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004963}
4964
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004965/// BuildClassRoTInitializer - generate meta-data for:
4966/// struct _class_ro_t {
4967/// uint32_t const flags;
4968/// uint32_t const instanceStart;
4969/// uint32_t const instanceSize;
4970/// uint32_t const reserved; // only when building for 64bit targets
4971/// const uint8_t * const ivarLayout;
4972/// const char *const name;
4973/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004974/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004975/// const struct _ivar_list_t *const ivars;
4976/// const uint8_t * const weakIvarLayout;
4977/// const struct _prop_list_t * const properties;
4978/// }
4979///
4980llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004981 unsigned flags,
4982 unsigned InstanceStart,
4983 unsigned InstanceSize,
4984 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004985 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004986 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004987
David Blaikiebbafb8a2012-03-11 07:00:24 +00004988 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallef19dbb2012-10-17 04:53:23 +00004989 flags |= NonFragileABI_Class_CompiledByARC;
John McCall31168b02011-06-15 23:02:42 +00004990
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004991 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4992 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4993 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004994 // FIXME. For 64bit targets add 0 here.
John McCallef19dbb2012-10-17 04:53:23 +00004995 Values[ 3] = (flags & NonFragileABI_Class_Meta)
4996 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004997 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004998 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004999 // const struct _method_list_t * const baseMethods;
5000 std::vector<llvm::Constant*> Methods;
5001 std::string MethodListName("\01l_OBJC_$_");
John McCallef19dbb2012-10-17 04:53:23 +00005002 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005003 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005004 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005005 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005006 // Class methods should always be defined.
5007 Methods.push_back(GetMethodConstant(*i));
5008 }
5009 } else {
5010 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005011 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005012 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005013 // Instance methods should always be defined.
5014 Methods.push_back(GetMethodConstant(*i));
5015 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005017 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005018 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005019
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005020 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5021 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005022
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005023 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5024 if (llvm::Constant *C = GetMethodConstant(MD))
5025 Methods.push_back(C);
5026 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5027 if (llvm::Constant *C = GetMethodConstant(MD))
5028 Methods.push_back(C);
5029 }
5030 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005031 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005032 Values[ 5] = EmitMethodList(MethodListName,
5033 "__DATA, __objc_const", Methods);
5034
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005035 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5036 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005037 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005038 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005039 OID->all_referenced_protocol_begin(),
5040 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005041
John McCallef19dbb2012-10-17 04:53:23 +00005042 if (flags & NonFragileABI_Class_Meta) {
Owen Anderson0b75f232009-07-31 20:28:54 +00005043 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005044 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Anderson0b75f232009-07-31 20:28:54 +00005045 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005046 } else {
5047 Values[ 7] = EmitIvarList(ID);
5048 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005049 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5050 ID, ID->getClassInterface(), ObjCTypes);
John McCallef19dbb2012-10-17 04:53:23 +00005051 }
Owen Anderson0e0189d2009-07-27 22:29:56 +00005052 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005053 Values);
5054 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5056 llvm::GlobalValue::InternalLinkage,
5057 Init,
John McCallef19dbb2012-10-17 04:53:23 +00005058 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005059 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5060 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005061 CLASS_RO_GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005062 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005063 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005064 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005065
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005066}
5067
5068/// BuildClassMetaData - This routine defines that to-level meta-data
5069/// for the given ClassName for:
5070/// struct _class_t {
5071/// struct _class_t *isa;
5072/// struct _class_t * const superclass;
5073/// void *cache;
5074/// IMP *vtable;
5075/// struct class_ro_t *ro;
5076/// }
5077///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005078llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079 std::string &ClassName,
5080 llvm::Constant *IsAGV,
5081 llvm::Constant *SuperClassGV,
5082 llvm::Constant *ClassRoGV,
5083 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005084 llvm::Constant *Values[] = {
5085 IsAGV,
5086 SuperClassGV,
5087 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5088 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5089 ClassRoGV // &CLASS_RO_GV
5090 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091 if (!Values[1])
5092 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005094 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005095 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5096 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005097 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005098 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005099 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005100 if (HiddenVisibility)
5101 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005102 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005103}
5104
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005105bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005106CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005107 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005108}
5109
Daniel Dunbar961202372009-05-03 12:57:56 +00005110void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005111 uint32_t &InstanceStart,
5112 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005113 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005114 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005115
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005116 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005117 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005118
5119 // If there are no fields, the start is the same as the end.
5120 if (!RL.getFieldCount())
5121 InstanceStart = InstanceSize;
5122 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005123 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005124}
5125
Fariborz Jahanian71394042009-01-23 23:53:38 +00005126void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5127 std::string ClassName = ID->getNameAsString();
5128 if (!ObjCEmptyCacheVar) {
5129 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005130 CGM.getModule(),
5131 ObjCTypes.CacheTy,
5132 false,
5133 llvm::GlobalValue::ExternalLinkage,
5134 0,
5135 "_objc_empty_cache");
5136
Fariborz Jahanian71394042009-01-23 23:53:38 +00005137 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005138 CGM.getModule(),
5139 ObjCTypes.ImpnfABITy,
5140 false,
5141 llvm::GlobalValue::ExternalLinkage,
5142 0,
5143 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005144 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005145 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005146 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005147 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005148 uint32_t InstanceStart =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005149 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005150 uint32_t InstanceSize = InstanceStart;
John McCallef19dbb2012-10-17 04:53:23 +00005151 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005152 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5153 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005155 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005156
5157 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005158 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005159 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005160 flags |= NonFragileABI_Class_Hidden;
John McCall31168b02011-06-15 23:02:42 +00005161 if (ID->hasCXXStructors())
John McCallef19dbb2012-10-17 04:53:23 +00005162 flags |= NonFragileABI_Class_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005163 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005164 // class is root
John McCallef19dbb2012-10-17 04:53:23 +00005165 flags |= NonFragileABI_Class_Root;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005166 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005167 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005168 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005169 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005170 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5171 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5172 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005173 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005174 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005175 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005176 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005177 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005178 ObjCMetaClassName +
5179 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005180 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005181 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005182 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005183 }
5184 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5185 InstanceStart,
5186 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005187 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005188 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005189 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5190 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005191 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005192
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005193 // Metadata for the class
John McCallef19dbb2012-10-17 04:53:23 +00005194 flags = 0;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005195 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005196 flags |= NonFragileABI_Class_Hidden;
John McCall31168b02011-06-15 23:02:42 +00005197 if (ID->hasCXXStructors())
John McCallef19dbb2012-10-17 04:53:23 +00005198 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005199
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005200 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCallef19dbb2012-10-17 04:53:23 +00005201 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005202
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005203 if (!ID->getClassInterface()->getSuperClass()) {
John McCallef19dbb2012-10-17 04:53:23 +00005204 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005205 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005206 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005207 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005208 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005209 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005210 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005211 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005212 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005213 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005214 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005215 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005216 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005217 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005218 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005219
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005220 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005221 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005222 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5223 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005224 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005225
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005226 // Determine if this class is also "non-lazy".
5227 if (ImplementationIsNonLazy(ID))
5228 DefinedNonLazyClasses.push_back(ClassMD);
5229
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005230 // Force the definition of the EHType if necessary.
John McCallef19dbb2012-10-17 04:53:23 +00005231 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005232 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005233 // Make sure method definition entries are all clear for next implementation.
5234 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005235}
5236
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005237/// GenerateProtocolRef - This routine is called to generate code for
5238/// a protocol reference expression; as in:
5239/// @code
5240/// @protocol(Proto1);
5241/// @endcode
5242/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5243/// which will hold address of the protocol meta-data.
5244///
5245llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005246 const ObjCProtocolDecl *PD) {
5247
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005248 // This routine is called for @protocol only. So, we must build definition
5249 // of protocol's meta-data (not a reference to it!)
5250 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005251 llvm::Constant *Init =
5252 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005253 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005255 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005256 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005257
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005258 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5259 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005260 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005261 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005262 CGM.getModule(),
5263 Init->getType(), false,
5264 llvm::GlobalValue::WeakAnyLinkage,
5265 Init,
5266 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005267 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5268 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005269 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005270 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005271}
5272
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005273/// GenerateCategory - Build metadata for a category implementation.
5274/// struct _category_t {
5275/// const char * const name;
5276/// struct _class_t *const cls;
5277/// const struct _method_list_t * const instance_methods;
5278/// const struct _method_list_t * const class_methods;
5279/// const struct _protocol_list_t * const protocols;
5280/// const struct _prop_list_t * const properties;
5281/// }
5282///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005283void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005284 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005285 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005286 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5287 "_$_" + OCD->getNameAsString());
5288 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005289 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005290
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005291 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005292 Values[0] = GetClassName(OCD->getIdentifier());
5293 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005294 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005295 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005296 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5297
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005298 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005299 std::vector<llvm::Constant*> Methods;
5300 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005301 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005302 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303
5304 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005305 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005306 // Instance methods should always be defined.
5307 Methods.push_back(GetMethodConstant(*i));
5308 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005309
5310 Values[2] = EmitMethodList(MethodListName,
5311 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005312 Methods);
5313
5314 MethodListName = Prefix;
5315 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5316 OCD->getNameAsString();
5317 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005318 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005319 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005320 // Class methods should always be defined.
5321 Methods.push_back(GetMethodConstant(*i));
5322 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323
5324 Values[3] = EmitMethodList(MethodListName,
5325 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005326 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005327 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005328 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005329 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005330 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005331 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5332 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005333 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005334 + Interface->getName() + "_$_"
5335 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005336 Category->protocol_begin(),
5337 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005338 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5339 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005340 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005341 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5342 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005343 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005344
5345 llvm::Constant *Init =
5346 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005347 Values);
5348 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005349 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005350 false,
5351 llvm::GlobalValue::InternalLinkage,
5352 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005353 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005354 GCATV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005355 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005356 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005357 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005358 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005359
5360 // Determine if this category is also "non-lazy".
5361 if (ImplementationIsNonLazy(OCD))
5362 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005363 // method definition entries must be clear for next implementation.
5364 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005365}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005366
5367/// GetMethodConstant - Return a struct objc_method constant for the
5368/// given method if it has been defined. The result is null if the
5369/// method has not been defined. The return value has type MethodPtrTy.
5370llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005371 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005372 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005373 if (!Fn)
5374 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005375
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005376 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005377 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005378 ObjCTypes.SelectorPtrTy),
5379 GetMethodVarType(MD),
5380 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5381 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005382 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005383}
5384
5385/// EmitMethodList - Build meta-data for method declarations
5386/// struct _method_list_t {
5387/// uint32_t entsize; // sizeof(struct _objc_method)
5388/// uint32_t method_count;
5389/// struct _objc_method method_list[method_count];
5390/// }
5391///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005392llvm::Constant *
5393CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5394 const char *Section,
5395 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005396 // Return null for empty list.
5397 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005398 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005399
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005400 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005401 // sizeof(struct _objc_method)
Micah Villmowdd31ca12012-10-08 16:25:52 +00005402 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005403 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005404 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005405 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005406 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005407 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005408 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005409 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005410
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005411 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005412 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005413 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005414 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005415 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005416 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005417 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005418}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005419
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005420/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5421/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005422llvm::GlobalVariable *
5423CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5424 const ObjCIvarDecl *Ivar) {
5425 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005426 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005427 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005428 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005429 CGM.getModule().getGlobalVariable(Name);
5430 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005431 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005432 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005433 false,
5434 llvm::GlobalValue::ExternalLinkage,
5435 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005436 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005437 return IvarOffsetGV;
5438}
5439
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005440llvm::Constant *
5441CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5442 const ObjCIvarDecl *Ivar,
5443 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005444 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005445 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005446 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005447 IvarOffsetGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005448 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005449
Mike Stump18bb9282009-05-16 07:57:57 +00005450 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5451 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005452 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5453 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005454 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005455 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005456 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005457 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005458 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005459 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005460}
5461
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005462/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005463/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005464/// IvarListnfABIPtrTy.
5465/// struct _ivar_t {
5466/// unsigned long int *offset; // pointer to ivar offset location
5467/// char *name;
5468/// char *type;
5469/// uint32_t alignment;
5470/// uint32_t size;
5471/// }
5472/// struct _ivar_list_t {
5473/// uint32 entsize; // sizeof(struct _ivar_t)
5474/// uint32 count;
5475/// struct _iver_t list[count];
5476/// }
5477///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005478
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005479llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005480 const ObjCImplementationDecl *ID) {
5481
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005482 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483
Jordy Rosea91768e2011-07-22 02:08:32 +00005484 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005485 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005486
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005487 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005488
Jordy Rosea91768e2011-07-22 02:08:32 +00005489 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005490 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005491 // Ignore unnamed bit-fields.
5492 if (!IVD->getDeclName())
5493 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005494 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005495 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005496 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005497 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5498 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005499 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005500 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00005501 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005502 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005503 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005504 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005505 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005506 // NOTE. Size of a bitfield does not match gcc's, because of the
5507 // way bitfields are treated special in each. But I am told that
5508 // 'size' for bitfield ivars is ignored by the runtime so it does
5509 // not matter. If it matters, there is enough info to get the
5510 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005511 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005512 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005513 }
5514 // Return null for empty list.
5515 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005516 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005517
5518 llvm::Constant *Values[3];
Micah Villmowdd31ca12012-10-08 16:25:52 +00005519 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005520 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5521 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005522 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005523 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005524 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005525 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005526 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5527 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005528 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005529 llvm::GlobalValue::InternalLinkage,
5530 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005531 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005532 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005533 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005534 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005535
Chris Lattnerf56501c2009-07-17 23:57:13 +00005536 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005537 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005538}
5539
5540llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005541 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005542 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005543
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005544 if (!Entry) {
5545 // We use the initializer as a marker of whether this is a forward
5546 // reference or not. At module finalization we add the empty
5547 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005548 Entry =
5549 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5550 llvm::GlobalValue::ExternalLinkage,
5551 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005552 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005553 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005554 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005556 return Entry;
5557}
5558
5559/// GetOrEmitProtocol - Generate the protocol meta-data:
5560/// @code
5561/// struct _protocol_t {
5562/// id isa; // NULL
5563/// const char * const protocol_name;
5564/// const struct _protocol_list_t * protocol_list; // super protocols
5565/// const struct method_list_t * const instance_methods;
5566/// const struct method_list_t * const class_methods;
5567/// const struct method_list_t *optionalInstanceMethods;
5568/// const struct method_list_t *optionalClassMethods;
5569/// const struct _prop_list_t * properties;
5570/// const uint32_t size; // sizeof(struct _protocol_t)
5571/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005572/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005573/// }
5574/// @endcode
5575///
5576
5577llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005578 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00005579 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005580
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005581 // Early exit if a defining object has already been generated.
5582 if (Entry && Entry->hasInitializer())
5583 return Entry;
5584
Douglas Gregora715bff2012-01-01 19:51:50 +00005585 // Use the protocol definition, if there is one.
5586 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5587 PD = Def;
5588
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005589 // Construct method lists.
5590 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5591 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005592 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005593 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005594 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005595 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005596 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005597 if (!C)
5598 return GetOrEmitProtocolRef(PD);
5599
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005600 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5601 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005602 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005603 } else {
5604 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005605 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005606 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005607 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005608
5609 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005610 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005611 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005612 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005613 if (!C)
5614 return GetOrEmitProtocolRef(PD);
5615
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005616 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5617 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005618 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005619 } else {
5620 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005621 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005622 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005623 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005624
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005625 MethodTypesExt.insert(MethodTypesExt.end(),
5626 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5627
5628 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005629 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005630 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005631 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005632 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5633 PD->protocol_begin(),
5634 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005636 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005637 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005638 "__DATA, __objc_const",
5639 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005640 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005641 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005642 "__DATA, __objc_const",
5643 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005644 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005645 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005646 "__DATA, __objc_const",
5647 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005648 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005649 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005650 "__DATA, __objc_const",
5651 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005652 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005653 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005654 uint32_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005655 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005656 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005657 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005658 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5659 + PD->getName(),
5660 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005661 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005662 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005663
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005664 if (Entry) {
5665 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005666 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005667 Entry->setInitializer(Init);
5668 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005669 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005670 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5671 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5672 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005673 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005674 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005675 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00005676
5677 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005678 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005679 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005680 CGM.AddUsedGlobal(Entry);
5681
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005682 // Use this protocol meta-data to build protocol list table in section
5683 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005684 llvm::GlobalVariable *PTGV =
5685 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5686 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5687 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005688 PTGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005689 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005690 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005691 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005692 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005693 return Entry;
5694}
5695
5696/// EmitProtocolList - Generate protocol list meta-data:
5697/// @code
5698/// struct _protocol_list_t {
5699/// long protocol_count; // Note, this is 32/64 bit
5700/// struct _protocol_t[protocol_count];
5701/// }
5702/// @endcode
5703///
5704llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005705CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005706 ObjCProtocolDecl::protocol_iterator begin,
5707 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00005708 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005709
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005710 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005711 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005712 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005713
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005714 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005715 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005716 Name.toVector(TmpName);
5717 llvm::GlobalVariable *GV =
5718 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005719 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005720 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005721
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005722 for (; begin != end; ++begin)
5723 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5724
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005725 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005726 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727 ObjCTypes.ProtocolnfABIPtrTy));
5728
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005729 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005730 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005731 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005732 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00005733 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5734 ProtocolRefs.size()),
5735 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005736
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005737 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005738 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005739 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005740 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005741 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005742 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005743 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005744 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005745 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005746 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005747}
5748
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005749/// GetMethodDescriptionConstant - This routine build following meta-data:
5750/// struct _objc_method {
5751/// SEL _cmd;
5752/// char *method_type;
5753/// char *_imp;
5754/// }
5755
5756llvm::Constant *
5757CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005758 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005759 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005760 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5761 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005762 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005763 if (!Desc[1])
5764 return 0;
5765
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005766 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005767 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005768 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005769}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005770
5771/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5772/// This code gen. amounts to generating code for:
5773/// @code
5774/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5775/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005776///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005777LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005778 CodeGen::CodeGenFunction &CGF,
5779 QualType ObjectTy,
5780 llvm::Value *BaseValue,
5781 const ObjCIvarDecl *Ivar,
5782 unsigned CVRQualifiers) {
5783 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005784 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5785 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5786 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5787 llvm::MDNode::get(VMContext,
5788 ArrayRef<llvm::Value*>()));
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005789 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005790 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005791}
5792
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005793llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005794 CodeGen::CodeGenFunction &CGF,
5795 const ObjCInterfaceDecl *Interface,
5796 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005797 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005798}
5799
John McCall234eac82011-05-13 23:16:18 +00005800static void appendSelectorForMessageRefTable(std::string &buffer,
5801 Selector selector) {
5802 if (selector.isUnarySelector()) {
5803 buffer += selector.getNameForSlot(0);
5804 return;
5805 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005806
John McCall234eac82011-05-13 23:16:18 +00005807 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5808 buffer += selector.getNameForSlot(i);
5809 buffer += '_';
5810 }
5811}
5812
John McCall9e8bb002011-05-14 03:10:52 +00005813/// Emit a "v-table" message send. We emit a weak hidden-visibility
5814/// struct, initially containing the selector pointer and a pointer to
5815/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5816/// load and call the function pointer, passing the address of the
5817/// struct as the second parameter. The runtime determines whether
5818/// the selector is currently emitted using vtable dispatch; if so, it
5819/// substitutes a stub function which simply tail-calls through the
5820/// appropriate vtable slot, and if not, it substitues a stub function
5821/// which tail-calls objc_msgSend. Both stubs adjust the selector
5822/// argument to correctly point to the selector.
5823RValue
5824CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5825 ReturnValueSlot returnSlot,
5826 QualType resultType,
5827 Selector selector,
5828 llvm::Value *arg0,
5829 QualType arg0Type,
5830 bool isSuper,
5831 const CallArgList &formalArgs,
5832 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005833 // Compute the actual arguments.
5834 CallArgList args;
5835
John McCall9e8bb002011-05-14 03:10:52 +00005836 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005837 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005838 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5839 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005840
John McCall9e8bb002011-05-14 03:10:52 +00005841 // Second argument: a pointer to the message ref structure. Leave
5842 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005843 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5844
5845 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5846
John McCalla729c622012-02-17 03:33:10 +00005847 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00005848
John McCall5880fb82011-05-14 21:12:11 +00005849 NullReturnState nullReturn;
5850
John McCall9e8bb002011-05-14 03:10:52 +00005851 // Find the function to call and the mangled name for the message
5852 // ref structure. Using a different mangled name wouldn't actually
5853 // be a problem; it would just be a waste.
5854 //
5855 // The runtime currently never uses vtable dispatch for anything
5856 // except normal, non-super message-sends.
5857 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005858 llvm::Constant *fn = 0;
5859 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00005860 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005861 if (isSuper) {
5862 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5863 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005864 } else {
John McCall5880fb82011-05-14 21:12:11 +00005865 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005866 fn = ObjCTypes.getMessageSendStretFixupFn();
5867 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005868 }
John McCall234eac82011-05-13 23:16:18 +00005869 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5870 fn = ObjCTypes.getMessageSendFpretFixupFn();
5871 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005872 } else {
John McCall234eac82011-05-13 23:16:18 +00005873 if (isSuper) {
5874 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5875 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005876 } else {
John McCall234eac82011-05-13 23:16:18 +00005877 fn = ObjCTypes.getMessageSendFixupFn();
5878 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005879 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005880 }
John McCall234eac82011-05-13 23:16:18 +00005881 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5882 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005883
John McCall234eac82011-05-13 23:16:18 +00005884 // Append the selector name, except use underscores anywhere we
5885 // would have used colons.
5886 appendSelectorForMessageRefTable(messageRefName, selector);
5887
5888 llvm::GlobalVariable *messageRef
5889 = CGM.getModule().getGlobalVariable(messageRefName);
5890 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005891 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005892 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005893 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005894 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5895 init->getType(),
5896 /*constant*/ false,
5897 llvm::GlobalValue::WeakAnyLinkage,
5898 init,
5899 messageRefName);
5900 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5901 messageRef->setAlignment(16);
5902 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5903 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005904
5905 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00005906 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005907 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5908 e = method->param_end(); i != e; ++i) {
5909 const ParmVarDecl *ParamDecl = (*i);
5910 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5911 if (!nullReturn.NullBB)
5912 nullReturn.init(CGF, arg0);
5913 requiresnullCheck = true;
5914 break;
5915 }
5916 }
5917
John McCall234eac82011-05-13 23:16:18 +00005918 llvm::Value *mref =
5919 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5920
John McCall9e8bb002011-05-14 03:10:52 +00005921 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005922 args[1].RV = RValue::get(mref);
5923
5924 // Load the function to call from the message ref table.
5925 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5926 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5927
John McCalla729c622012-02-17 03:33:10 +00005928 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00005929
John McCalla729c622012-02-17 03:33:10 +00005930 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005931 return nullReturn.complete(CGF, result, resultType, formalArgs,
5932 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005933}
5934
5935/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005936CodeGen::RValue
5937CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005938 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005939 QualType ResultType,
5940 Selector Sel,
5941 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005942 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005943 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005944 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005945 return isVTableDispatchedSelector(Sel)
5946 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005947 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005948 false, CallArgs, Method)
5949 : EmitMessageSend(CGF, Return, ResultType,
5950 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005951 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005952 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005953}
5954
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005955llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005956CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005957 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5958
Daniel Dunbara6468342009-03-02 05:18:14 +00005959 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005960 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005961 false, llvm::GlobalValue::ExternalLinkage,
5962 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005963 }
5964
5965 return GV;
5966}
5967
John McCall31168b02011-06-15 23:02:42 +00005968llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5969 IdentifierInfo *II) {
5970 llvm::GlobalVariable *&Entry = ClassReferences[II];
5971
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005972 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005973 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005974 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005975 Entry =
John McCall31168b02011-06-15 23:02:42 +00005976 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5977 false, llvm::GlobalValue::InternalLinkage,
5978 ClassGV,
5979 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005980 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005981 CGM.getDataLayout().getABITypeAlignment(
John McCall31168b02011-06-15 23:02:42 +00005982 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005983 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005984 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005985 }
John McCall31168b02011-06-15 23:02:42 +00005986
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005987 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005988}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005989
John McCall31168b02011-06-15 23:02:42 +00005990llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5991 const ObjCInterfaceDecl *ID) {
5992 return EmitClassRefFromId(Builder, ID->getIdentifier());
5993}
5994
5995llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5996 CGBuilderTy &Builder) {
5997 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5998 return EmitClassRefFromId(Builder, II);
5999}
6000
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006001llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006002CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006003 const ObjCInterfaceDecl *ID) {
6004 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006005
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006006 if (!Entry) {
6007 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6008 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006009 Entry =
6010 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006011 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006012 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006013 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006014 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006015 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006016 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006017 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006018 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006019 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006020
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006021 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006022}
6023
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006024/// EmitMetaClassRef - Return a Value * of the address of _class_t
6025/// meta-data
6026///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006027llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6028 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006029 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6030 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006031 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006032
Daniel Dunbar15894b72009-04-07 05:48:37 +00006033 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006034 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006035 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006036 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006037 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006038 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006039 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006040 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006041 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006042 ObjCTypes.ClassnfABIPtrTy));
6043
Daniel Dunbare60aa052009-04-15 19:03:14 +00006044 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006045 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006046
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006047 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006048}
6049
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006050/// GetClass - Return a reference to the class for the given interface
6051/// decl.
6052llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6053 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006054 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006055 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6056 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6057 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6058 }
6059
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006060 return EmitClassRef(Builder, ID);
6061}
6062
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006063/// Generates a message send where the super is the receiver. This is
6064/// a message send to self with special delivery semantics indicating
6065/// which class's method should be called.
6066CodeGen::RValue
6067CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006068 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006069 QualType ResultType,
6070 Selector Sel,
6071 const ObjCInterfaceDecl *Class,
6072 bool isCategoryImpl,
6073 llvm::Value *Receiver,
6074 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006075 const CodeGen::CallArgList &CallArgs,
6076 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006077 // ...
6078 // Create and init a super structure; this is a (receiver, class)
6079 // pair we will pass to objc_msgSendSuper.
6080 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006081 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006082
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006083 llvm::Value *ReceiverAsObject =
6084 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6085 CGF.Builder.CreateStore(ReceiverAsObject,
6086 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006087
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006088 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006089 llvm::Value *Target;
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006090 if (IsClassMessage)
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006091 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006092 else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006093 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006094
Mike Stump18bb9282009-05-16 07:57:57 +00006095 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6096 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006097 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006098 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6099 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6100 CGF.Builder.CreateStore(Target,
6101 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006102
John McCall9e8bb002011-05-14 03:10:52 +00006103 return (isVTableDispatchedSelector(Sel))
6104 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006105 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006106 true, CallArgs, Method)
6107 : EmitMessageSend(CGF, Return, ResultType,
6108 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006109 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006110 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006111}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006112
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006113llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006114 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006115 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006116
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006117 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006118 llvm::Constant *Casted =
6119 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6120 ObjCTypes.SelectorPtrTy);
6121 Entry =
6122 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6123 llvm::GlobalValue::InternalLinkage,
6124 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006125 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006126 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006127 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006128
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006129 if (lval)
6130 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006131 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6132
6133 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6134 llvm::MDNode::get(VMContext,
6135 ArrayRef<llvm::Value*>()));
6136 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006137}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006138/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006139/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006140///
6141void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006142 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006143 llvm::Value *dst,
6144 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006145 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006146 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006147 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006148 assert(Size <= 8 && "does not support size > 8");
6149 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6150 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006151 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6152 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006153 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6154 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006155 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6156 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006157 return;
6158}
6159
6160/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6161/// objc_assign_strongCast (id src, id *dst)
6162///
6163void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006164 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006165 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006166 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006167 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006168 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006169 assert(Size <= 8 && "does not support size > 8");
6170 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006171 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006172 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6173 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006174 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6175 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006176 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006177 src, dst, "weakassign");
6178 return;
6179}
6180
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006181void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006182 CodeGen::CodeGenFunction &CGF,
6183 llvm::Value *DestPtr,
6184 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006185 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006186 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6187 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006188 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006189 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006190 return;
6191}
6192
Fariborz Jahanian06292952009-02-16 22:52:32 +00006193/// EmitObjCWeakRead - Code gen for loading value of a __weak
6194/// object: objc_read_weak (id *src)
6195///
6196llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006197 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006198 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006199 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006200 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6201 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006202 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006203 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006204 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006205 return read_weak;
6206}
6207
6208/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6209/// objc_assign_weak (id src, id *dst)
6210///
6211void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006212 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006213 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006214 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006215 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006216 assert(Size <= 8 && "does not support size > 8");
6217 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6218 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006219 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6220 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006221 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6222 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006223 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006224 src, dst, "weakassign");
6225 return;
6226}
6227
6228/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6229/// objc_assign_global (id src, id *dst)
6230///
6231void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006232 llvm::Value *src, llvm::Value *dst,
6233 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006234 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006235 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006236 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006237 assert(Size <= 8 && "does not support size > 8");
6238 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6239 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006240 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6241 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006242 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6243 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006244 if (!threadlocal)
6245 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6246 src, dst, "globalassign");
6247 else
6248 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6249 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006250 return;
6251}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006252
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006253void
John McCallbd309292010-07-06 01:34:17 +00006254CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6255 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006256 EmitAtSynchronizedStmt(CGF, S,
6257 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6258 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006259}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006260
John McCall2ca705e2010-07-24 00:37:23 +00006261llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006262CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006263 // There's a particular fixed type info for 'id'.
6264 if (T->isObjCIdType() ||
6265 T->isObjCQualifiedIdType()) {
6266 llvm::Constant *IDEHType =
6267 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6268 if (!IDEHType)
6269 IDEHType =
6270 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6271 false,
6272 llvm::GlobalValue::ExternalLinkage,
6273 0, "OBJC_EHTYPE_id");
6274 return IDEHType;
6275 }
6276
6277 // All other types should be Objective-C interface pointer types.
6278 const ObjCObjectPointerType *PT =
6279 T->getAs<ObjCObjectPointerType>();
6280 assert(PT && "Invalid @catch type.");
6281 const ObjCInterfaceType *IT = PT->getInterfaceType();
6282 assert(IT && "Invalid @catch type.");
6283 return GetInterfaceEHType(IT->getDecl(), false);
6284}
6285
John McCallbd309292010-07-06 01:34:17 +00006286void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6287 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006288 EmitTryCatchStmt(CGF, S,
6289 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6290 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6291 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006292}
6293
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006294/// EmitThrowStmt - Generate code for a throw statement.
6295void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6296 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006297 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006298 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006299 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006300 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006301 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006302 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006303 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006304 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006305 }
6306
John McCall17afe452010-10-16 08:21:07 +00006307 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006308 CGF.Builder.ClearInsertionPoint();
6309}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006310
John McCall2ca705e2010-07-24 00:37:23 +00006311llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006312CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006313 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006314 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006315
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006316 // If we don't need a definition, return the entry if found or check
6317 // if we use an external reference.
6318 if (!ForDefinition) {
6319 if (Entry)
6320 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006321
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006322 // If this type (or a super class) has the __objc_exception__
6323 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006324 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006325 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006326 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006327 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006328 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006329 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006330 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006331 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006332
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006333 // Otherwise we need to either make a new entry or fill in the
6334 // initializer.
6335 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006336 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006337 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006338 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006339 CGM.getModule().getGlobalVariable(VTableName);
6340 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006341 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6342 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006343 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006344 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006345
Chris Lattnerece04092012-02-07 00:39:47 +00006346 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006347
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006348 llvm::Constant *Values[] = {
6349 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6350 GetClassName(ID->getIdentifier()),
6351 GetClassGlobal(ClassName)
6352 };
Owen Anderson170229f2009-07-14 23:10:40 +00006353 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006354 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006355
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006356 if (Entry) {
6357 Entry->setInitializer(Init);
6358 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006359 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006360 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006361 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006362 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006363 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006364 }
6365
David Blaikiebbafb8a2012-03-11 07:00:24 +00006366 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006367 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmowdd31ca12012-10-08 16:25:52 +00006368 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00006369 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006370
6371 if (ForDefinition) {
6372 Entry->setSection("__DATA,__objc_const");
6373 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6374 } else {
6375 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6376 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006377
6378 return Entry;
6379}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006380
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006381/* *** */
6382
Daniel Dunbarb036db82008-08-13 03:21:16 +00006383CodeGen::CGObjCRuntime *
6384CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00006385 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6386 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006387 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00006388
6389 case ObjCRuntime::MacOSX:
6390 case ObjCRuntime::iOS:
6391 return new CGObjCNonFragileABIMac(CGM);
6392
David Chisnallb601c962012-07-03 20:49:52 +00006393 case ObjCRuntime::GNUstep:
6394 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00006395 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00006396 llvm_unreachable("these runtimes are not Mac runtimes");
6397 }
6398 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006399}