blob: 99a59cbb4dbee49f5b079431ea815ec2e705adbd [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 Jahanian196f9382012-10-25 21:15:04 +0000758 /// opcode for captured block variables layout 'instructions'.
759 /// In the following descriptions, 'I' is the value of the immediate field.
760 /// (field following the opcode).
761 ///
762 enum BLOCK_LAYOUT_OPCODE {
763 /// An operator which affects how the following layout should be
764 /// interpreted.
765 /// I == 0: Halt interpretation and treat everything else as
766 /// a non-pointer. Note that this instruction is equal
767 /// to '\0'.
768 /// I != 0: Currently unused.
769 BLOCK_LAYOUT_OPERATOR = 0,
770
771 /// The next I+1 bytes do not contain a value of object pointer type.
772 /// Note that this can leave the stream unaligned, meaning that
773 /// subsequent word-size instructions do not begin at a multiple of
774 /// the pointer size.
775 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
776
777 /// The next I+1 words do not contain a value of object pointer type.
778 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
779 /// when the required skip quantity is a multiple of the pointer size.
780 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
781
782 /// The next I+1 words are __strong pointers to Objective-C
783 /// objects or blocks.
784 BLOCK_LAYOUT_STRONG = 3,
785
786 /// The next I+1 words are pointers to __block variables.
787 BLOCK_LAYOUT_BYREF = 4,
788
789 /// The next I+1 words are __weak pointers to Objective-C
790 /// objects or blocks.
791 BLOCK_LAYOUT_WEAK = 5,
792
793 /// The next I+1 words are __unsafe_unretained pointers to
794 /// Objective-C objects or blocks.
795 BLOCK_LAYOUT_UNRETAINED = 6
796
797 /// The next I+1 words are block or object pointers with some
798 /// as-yet-unspecified ownership semantics. If we add more
799 /// flavors of ownership semantics, values will be taken from
800 /// this range.
801 ///
802 /// This is included so that older tools can at least continue
803 /// processing the layout past such things.
804 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
805
806 /// All other opcodes are reserved. Halt interpretation and
807 /// treat everything else as opaque.
808 };
809
810 class RUN_SKIP {
811 public:
812 enum BLOCK_LAYOUT_OPCODE opcode;
813 unsigned block_var_bytepos;
814 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
815 unsigned BytePos = 0)
816 : opcode(Opcode), block_var_bytepos(BytePos) {}
817
818 // Allow sorting based on byte pos.
819 bool operator<(const RUN_SKIP &b) const {
820 return block_var_bytepos < b.block_var_bytepos;
821 }
822 };
823
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000824protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000825 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000826 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000827 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000828
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000829 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000830 SmallVector<GC_IVAR, 16> SkipIvars;
831 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000832
833 // arc/mrr layout of captured block literal variables.
834 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000836 /// LazySymbols - Symbols to generate a lazy reference for. See
837 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000838 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000839
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000840 /// DefinedSymbols - External symbols which are defined by this
841 /// module. The symbols in this list and LazySymbols are used to add
842 /// special linker symbols which ensure that Objective-C modules are
843 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000844 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000845
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000846 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000847 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000848
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000849 /// MethodVarNames - uniqued method variable names.
850 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000852 /// DefinedCategoryNames - list of category names in form Class_Category.
853 llvm::SetVector<std::string> DefinedCategoryNames;
854
Daniel Dunbarb036db82008-08-13 03:21:16 +0000855 /// MethodVarTypes - uniqued method type signatures. We have to use
856 /// a StringMap here because have no other unique reference.
857 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000858
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000859 /// MethodDefinitions - map of methods which have been defined in
860 /// this translation unit.
861 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000862
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000863 /// PropertyNames - uniqued method variable names.
864 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000865
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000866 /// ClassReferences - uniqued class references.
867 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000868
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000869 /// SelectorReferences - uniqued selector references.
870 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000871
Daniel Dunbarb036db82008-08-13 03:21:16 +0000872 /// Protocols - Protocols for which an objc_protocol structure has
873 /// been emitted. Forward declarations are handled by creating an
874 /// empty structure whose initializer is filled in when/if defined.
875 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000876
Daniel Dunbarc475d422008-10-29 22:36:39 +0000877 /// DefinedProtocols - Protocols which have actually been
878 /// defined. We should not need this, see FIXME in GenerateProtocol.
879 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000880
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000881 /// DefinedClasses - List of defined classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000882 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000883
884 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000885 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000887 /// DefinedCategories - List of defined categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000888 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000889
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000890 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000891 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000892
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000893 /// GetNameForMethod - Return a name for the given method.
894 /// \param[out] NameOut - The return value.
895 void GetNameForMethod(const ObjCMethodDecl *OMD,
896 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000897 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000898
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000899 /// GetMethodVarName - Return a unique constant for the given
900 /// selector's name. The return value has type char *.
901 llvm::Constant *GetMethodVarName(Selector Sel);
902 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000903
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000904 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000905 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000906
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000907 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000908 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
909 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000910 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000911
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000912 /// GetPropertyName - Return a unique constant for the given
913 /// name. The return value has type char *.
914 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000915
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000916 // FIXME: This can be dropped once string functions are unified.
917 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
918 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000920 /// GetClassName - Return a unique constant for the given selector's
921 /// name. The return value has type char *.
922 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000924 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
925
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000926 /// BuildIvarLayout - Builds ivar layout bitmap for the class
927 /// implementation for the __strong or __weak case.
928 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000929 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
930 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000931
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000932 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000933
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000934 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000935 unsigned int BytePos, bool ForStrongLayout,
936 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000937 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000938 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000939 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000940 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000941 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000942 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000943
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000944 /// GetIvarLayoutName - Returns a unique constant for the given
945 /// ivar layout bitmap.
946 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
947 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000948
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000949 /// EmitPropertyList - Emit the given property list. The return
950 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000951 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000952 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000953 const ObjCContainerDecl *OCD,
954 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000955
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000956 /// EmitProtocolMethodTypes - Generate the array of extended method type
957 /// strings. The return value has type Int8PtrPtrTy.
958 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000959 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000960 const ObjCCommonTypesHelper &ObjCTypes);
961
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000962 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000963 void PushProtocolProperties(
964 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
965 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
966 const Decl *Container,
967 const ObjCProtocolDecl *PROTO,
968 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000969
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000970 /// GetProtocolRef - Return a reference to the internal protocol
971 /// description, creating an empty one if it has not been
972 /// defined. The return value has type ProtocolPtrTy.
973 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000974
Daniel Dunbar30c65362009-03-09 20:09:19 +0000975 /// CreateMetadataVar - Create a global variable with internal
976 /// linkage for use by the Objective-C runtime.
977 ///
978 /// This is a convenience wrapper which not only creates the
979 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000980 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000981 ///
982 /// \param Name - The variable name.
983 /// \param Init - The variable initializer; this is also used to
984 /// define the type of the variable.
985 /// \param Section - The section the variable should go into, or 0.
986 /// \param Align - The alignment for the variable, or 0.
987 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000988 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000989 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000990 llvm::Constant *Init,
991 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000992 unsigned Align,
993 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000994
John McCall9e8bb002011-05-14 03:10:52 +0000995 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
996 ReturnValueSlot Return,
997 QualType ResultType,
998 llvm::Value *Sel,
999 llvm::Value *Arg0,
1000 QualType Arg0Ty,
1001 bool IsSuper,
1002 const CallArgList &CallArgs,
1003 const ObjCMethodDecl *OMD,
1004 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001005
Daniel Dunbar5e639272010-04-25 20:39:01 +00001006 /// EmitImageInfo - Emit the image info marker used to encode some module
1007 /// level information.
1008 void EmitImageInfo();
1009
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001010public:
Owen Andersonae86c192009-07-13 04:10:07 +00001011 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +00001012 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001013
David Chisnall481e3a82010-01-23 02:40:42 +00001014 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001015
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001016 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1017 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001018
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001019 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001020
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001021 /// GetOrEmitProtocol - Get the protocol object for the given
1022 /// declaration, emitting it if necessary. The return value has type
1023 /// ProtocolPtrTy.
1024 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001025
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001026 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1027 /// object for the given declaration, emitting it if needed. These
1028 /// forward references will be filled in with empty bodies if no
1029 /// definition is seen. The return value has type ProtocolPtrTy.
1030 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001031 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1032 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001033
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001034};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001035
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001036class CGObjCMac : public CGObjCCommonMac {
1037private:
1038 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001039
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001040 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001041 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001042 void EmitModuleInfo();
1043
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001044 /// EmitModuleSymols - Emit module symbols, the list of defined
1045 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001046 llvm::Constant *EmitModuleSymbols();
1047
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001048 /// FinishModule - Write out global data structures at the end of
1049 /// processing a translation unit.
1050 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001051
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001052 /// EmitClassExtension - Generate the class extension structure used
1053 /// to store the weak ivar layout and properties. The return value
1054 /// has type ClassExtensionPtrTy.
1055 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1056
1057 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1058 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001059 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001060 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001061
John McCall31168b02011-06-15 23:02:42 +00001062 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1063 IdentifierInfo *II);
1064
1065 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1066
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001067 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1068 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001069
1070 /// EmitIvarList - Emit the ivar list for the given
1071 /// implementation. If ForClass is true the list of class ivars
1072 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1073 /// interface ivars will be emitted. The return value has type
1074 /// IvarListPtrTy.
1075 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001076 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001077
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001078 /// EmitMetaClass - Emit a forward reference to the class structure
1079 /// for the metaclass of the given interface. The return value has
1080 /// type ClassPtrTy.
1081 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1082
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001083 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001084 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001085 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1086 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001087 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001088
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001089 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001090
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001091 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001092
1093 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001094 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001095 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001096 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001097 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001098
1099 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001100 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001101 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001102 /// - IsProtocol: True iff these methods are for a protocol.
1103 /// - ClassMethds: True iff these are class methods.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001104 /// - Required: When true, only "required" methods are
1105 /// listed. Similarly, when false only "optional" methods are
1106 /// listed. For classes this should always be true.
1107 /// - begin, end: The method list to output.
1108 ///
1109 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001110 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001111 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001112 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001113
Daniel Dunbarc475d422008-10-29 22:36:39 +00001114 /// GetOrEmitProtocol - Get the protocol object for the given
1115 /// declaration, emitting it if necessary. The return value has type
1116 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001117 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001118
1119 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1120 /// object for the given declaration, emitting it if needed. These
1121 /// forward references will be filled in with empty bodies if no
1122 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001123 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001124
Daniel Dunbarb036db82008-08-13 03:21:16 +00001125 /// EmitProtocolExtension - Generate the protocol extension
1126 /// structure used to store optional instance and class methods, and
1127 /// protocol properties. The return value has type
1128 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001129 llvm::Constant *
1130 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001131 ArrayRef<llvm::Constant*> OptInstanceMethods,
1132 ArrayRef<llvm::Constant*> OptClassMethods,
1133 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001134
1135 /// EmitProtocolList - Generate the list of referenced
1136 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001137 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001138 ObjCProtocolDecl::protocol_iterator begin,
1139 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001140
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001141 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1142 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001143 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1144 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001145
1146public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001147 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001148
Fariborz Jahanian71394042009-01-23 23:53:38 +00001149 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001150
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001151 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001152 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001153 QualType ResultType,
1154 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001155 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001156 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001157 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001158 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001159
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001160 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001161 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001162 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001163 QualType ResultType,
1164 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001165 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001166 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001167 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001168 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001169 const CallArgList &CallArgs,
1170 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001171
Daniel Dunbarcb463852008-11-01 01:53:16 +00001172 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001173 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001174
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001175 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1176 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001177
1178 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1179 /// untyped one.
1180 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1181 const ObjCMethodDecl *Method);
1182
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001183 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001184
Daniel Dunbar92992502008-08-15 22:20:32 +00001185 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001186
Daniel Dunbar92992502008-08-15 22:20:32 +00001187 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001188
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001189 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001190
Daniel Dunbarcb463852008-11-01 01:53:16 +00001191 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001192 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001193
Chris Lattnerd4808922009-03-22 21:03:39 +00001194 virtual llvm::Constant *GetPropertyGetFunction();
1195 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001196 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1197 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001198 virtual llvm::Constant *GetGetStructFunction();
1199 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001200 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001201 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202
John McCallbd309292010-07-06 01:34:17 +00001203 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1204 const ObjCAtTryStmt &S);
1205 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1206 const ObjCAtSynchronizedStmt &S);
1207 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001208 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1209 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001210 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001211 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001212 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001213 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001214 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001215 llvm::Value *src, llvm::Value *dest,
1216 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001217 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001218 llvm::Value *src, llvm::Value *dest,
1219 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001220 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1221 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001222 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1223 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001224 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001225
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001226 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1227 QualType ObjectTy,
1228 llvm::Value *BaseValue,
1229 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001230 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001231 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001232 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001233 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001234
1235 /// GetClassGlobal - Return the global variable for the Objective-C
1236 /// class of the given name.
1237 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001238 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001239 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001240};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001241
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001242class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001243private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001244 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001245 llvm::GlobalVariable* ObjCEmptyCacheVar;
1246 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001247
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001248 /// SuperClassReferences - uniqued super class references.
1249 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001250
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001251 /// MetaClassReferences - uniqued meta class references.
1252 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001253
1254 /// EHTypeReferences - uniqued class ehtype references.
1255 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
John McCall9e8bb002011-05-14 03:10:52 +00001257 /// VTableDispatchMethods - List of methods for which we generate
1258 /// vtable-based message dispatch.
1259 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001260
Fariborz Jahanian67260552009-11-17 21:37:35 +00001261 /// DefinedMetaClasses - List of defined meta-classes.
1262 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1263
John McCall9e8bb002011-05-14 03:10:52 +00001264 /// isVTableDispatchedSelector - Returns true if SEL is a
1265 /// vtable-based selector.
1266 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
Fariborz Jahanian71394042009-01-23 23:53:38 +00001268 /// FinishNonFragileABIModule - Write out global data structures at the end of
1269 /// processing a translation unit.
1270 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001271
Daniel Dunbar19573e72009-05-15 21:48:48 +00001272 /// AddModuleClassList - Add the given list of class pointers to the
1273 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001274 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001275 const char *SymbolName,
1276 const char *SectionName);
1277
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001278 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1279 unsigned InstanceStart,
1280 unsigned InstanceSize,
1281 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001282 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001283 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001284 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001285 llvm::Constant *ClassRoGV,
1286 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001287
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001288 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001290 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001291
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001292 /// EmitMethodList - Emit the method list for the given
1293 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001294 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001295 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001296 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001297 /// EmitIvarList - Emit the ivar list for the given
1298 /// implementation. If ForClass is true the list of class ivars
1299 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1300 /// interface ivars will be emitted. The return value has type
1301 /// IvarListnfABIPtrTy.
1302 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001303
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001304 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001305 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001306 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001307
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001308 /// GetOrEmitProtocol - Get the protocol object for the given
1309 /// declaration, emitting it if necessary. The return value has type
1310 /// ProtocolPtrTy.
1311 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001312
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001313 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1314 /// object for the given declaration, emitting it if needed. These
1315 /// forward references will be filled in with empty bodies if no
1316 /// definition is seen. The return value has type ProtocolPtrTy.
1317 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001318
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001319 /// EmitProtocolList - Generate the list of referenced
1320 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001321 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001322 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001323 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001324
John McCall9e8bb002011-05-14 03:10:52 +00001325 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1326 ReturnValueSlot Return,
1327 QualType ResultType,
1328 Selector Sel,
1329 llvm::Value *Receiver,
1330 QualType Arg0Ty,
1331 bool IsSuper,
1332 const CallArgList &CallArgs,
1333 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001334
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001335 /// GetClassGlobal - Return the global variable for the Objective-C
1336 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001337 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001338
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001339 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001340 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001341 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001342 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001343
1344 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1345 IdentifierInfo *II);
1346
1347 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001348
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001349 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1350 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001351 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1352 const ObjCInterfaceDecl *ID);
1353
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001354 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1355 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001357 const ObjCInterfaceDecl *ID);
1358
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001359 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1360 /// the given ivar.
1361 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001362 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001363 const ObjCInterfaceDecl *ID,
1364 const ObjCIvarDecl *Ivar);
1365
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001366 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1367 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001368 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1369 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001370
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001371 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001372 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001373 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001374 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001375
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001376 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001377 return "OBJC_METACLASS_$_";
1378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001379
Daniel Dunbar15894b72009-04-07 05:48:37 +00001380 const char *getClassSymbolPrefix() const {
1381 return "OBJC_CLASS_$_";
1382 }
1383
Daniel Dunbar961202372009-05-03 12:57:56 +00001384 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001385 uint32_t &InstanceStart,
1386 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001387
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001388 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001389 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001390 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1391 return CGM.getContext().Selectors.getSelector(0, &II);
1392 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001394 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001395 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1396 return CGM.getContext().Selectors.getSelector(1, &II);
1397 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001398
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001399 /// ImplementationIsNonLazy - Check whether the given category or
1400 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001401 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001402
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001403public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001404 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001405 // FIXME. All stubs for now!
1406 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001407
Fariborz Jahanian71394042009-01-23 23:53:38 +00001408 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001409 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001410 QualType ResultType,
1411 Selector Sel,
1412 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001413 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001414 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001415 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001416
1417 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001418 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001419 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001420 QualType ResultType,
1421 Selector Sel,
1422 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001423 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001424 llvm::Value *Receiver,
1425 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001426 const CallArgList &CallArgs,
1427 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001428
Fariborz Jahanian71394042009-01-23 23:53:38 +00001429 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001430 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001431
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001432 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1433 bool lvalue = false)
1434 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001435
1436 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1437 /// untyped one.
1438 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1439 const ObjCMethodDecl *Method)
1440 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001441
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001442 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001443
Fariborz Jahanian71394042009-01-23 23:53:38 +00001444 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001445
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001446 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001447
Fariborz Jahanian71394042009-01-23 23:53:38 +00001448 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001449 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001450
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001451 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001452
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001453 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001454 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001455 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001456 virtual llvm::Constant *GetPropertySetFunction() {
1457 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001458 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001459
Ted Kremeneke65b0862012-03-06 20:05:56 +00001460 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1461 bool copy) {
1462 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1463 }
1464
David Chisnall168b80f2010-12-26 22:13:16 +00001465 virtual llvm::Constant *GetSetStructFunction() {
1466 return ObjCTypes.getCopyStructFn();
1467 }
1468 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001469 return ObjCTypes.getCopyStructFn();
1470 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001471 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1472 return ObjCTypes.getCppAtomicObjectFunction();
1473 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001474
Chris Lattnerd4808922009-03-22 21:03:39 +00001475 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001476 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001477 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001478
John McCallbd309292010-07-06 01:34:17 +00001479 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1480 const ObjCAtTryStmt &S);
1481 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1482 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001483 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001484 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001485 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001486 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001487 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001488 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001489 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001490 llvm::Value *src, llvm::Value *dest,
1491 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001492 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001493 llvm::Value *src, llvm::Value *dest,
1494 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001495 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001496 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001497 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1498 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001499 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001500 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1501 QualType ObjectTy,
1502 llvm::Value *BaseValue,
1503 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001504 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001505 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001506 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001507 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001508};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001509
1510/// A helper class for performing the null-initialization of a return
1511/// value.
1512struct NullReturnState {
1513 llvm::BasicBlock *NullBB;
1514 llvm::BasicBlock *callBB;
1515 NullReturnState() : NullBB(0), callBB(0) {}
1516
1517 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1518 // Make blocks for the null-init and call edges.
1519 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1520 callBB = CGF.createBasicBlock("msgSend.call");
1521
1522 // Check for a null receiver and, if there is one, jump to the
1523 // null-init test.
1524 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1525 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1526
1527 // Otherwise, start performing the call.
1528 CGF.EmitBlock(callBB);
1529 }
1530
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001531 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1532 const CallArgList &CallArgs,
1533 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001534 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001535
1536 llvm::Value *NullInitPtr = 0;
1537 if (result.isScalar() && !resultType->isVoidType()) {
1538 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1539 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1540 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001541
1542 // Finish the call path.
1543 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1544 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1545
1546 // Emit the null-init block and perform the null-initialization there.
1547 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001548
1549 // Release consumed arguments along the null-receiver path.
1550 if (Method) {
1551 CallArgList::const_iterator I = CallArgs.begin();
1552 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1553 e = Method->param_end(); i != e; ++i, ++I) {
1554 const ParmVarDecl *ParamDecl = (*i);
1555 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1556 RValue RV = I->RV;
1557 assert(RV.isScalar() &&
1558 "NullReturnState::complete - arg not on object");
1559 CGF.EmitARCRelease(RV.getScalarVal(), true);
1560 }
1561 }
1562 }
1563
1564 if (result.isScalar()) {
1565 if (NullInitPtr)
1566 CGF.EmitNullInitialization(NullInitPtr, resultType);
1567 // Jump to the continuation block.
1568 CGF.EmitBlock(contBB);
1569 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1570 : result;
1571 }
1572
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001573 if (!resultType->isAnyComplexType()) {
1574 assert(result.isAggregate() && "null init of non-aggregate result?");
1575 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1576 // Jump to the continuation block.
1577 CGF.EmitBlock(contBB);
1578 return result;
1579 }
1580
1581 // _Complex type
1582 // FIXME. Now easy to handle any other scalar type whose result is returned
1583 // in memory due to ABI limitations.
1584 CGF.EmitBlock(contBB);
1585 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1586 llvm::Type *MemberType = CallCV.first->getType();
1587 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1588 // Create phi instruction for scalar complex value.
1589 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1590 PHIReal->addIncoming(ZeroCV, NullBB);
1591 PHIReal->addIncoming(CallCV.first, callBB);
1592 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1593 PHIImag->addIncoming(ZeroCV, NullBB);
1594 PHIImag->addIncoming(CallCV.second, callBB);
1595 return RValue::getComplex(PHIReal, PHIImag);
1596 }
1597};
1598
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001599} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001600
1601/* *** Helper Functions *** */
1602
1603/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001604static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001605 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001606 unsigned idx0,
1607 unsigned idx1) {
1608 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001609 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1610 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001611 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001612 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001613}
1614
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001615/// hasObjCExceptionAttribute - Return true if this class or any super
1616/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001617static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001618 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001619 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001620 return true;
1621 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001622 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001623 return false;
1624}
1625
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001626/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001627
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001628CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001629 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001630 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001631 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001632}
1633
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001634/// GetClass - Return a reference to the class for the given interface
1635/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001636llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001637 const ObjCInterfaceDecl *ID) {
1638 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001639}
1640
1641/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001642llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1643 bool lval) {
1644 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001645}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001646llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001647 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001648 return EmitSelector(Builder, Method->getSelector());
1649}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001650
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001651llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001652 if (T->isObjCIdType() ||
1653 T->isObjCQualifiedIdType()) {
1654 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001655 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001656 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001657 if (T->isObjCClassType() ||
1658 T->isObjCQualifiedClassType()) {
1659 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001660 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001661 }
1662 if (T->isObjCObjectPointerType())
1663 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1664
John McCall2ca705e2010-07-24 00:37:23 +00001665 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001666}
1667
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001668/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001669/*
1670 struct __builtin_CFString {
1671 const int *isa; // point to __CFConstantStringClassReference
1672 int flags;
1673 const char *str;
1674 long length;
1675 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001676*/
1677
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001678/// or Generate a constant NSString object.
1679/*
1680 struct __builtin_NSString {
1681 const int *isa; // point to __NSConstantStringClassReference
1682 const char *str;
1683 unsigned int length;
1684 };
1685*/
1686
Fariborz Jahanian71394042009-01-23 23:53:38 +00001687llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001688 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001689 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001690 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001691 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001692}
1693
Ted Kremeneke65b0862012-03-06 20:05:56 +00001694enum {
1695 kCFTaggedObjectID_Integer = (1 << 1) + 1
1696};
1697
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001698/// Generates a message send where the super is the receiver. This is
1699/// a message send to self with special delivery semantics indicating
1700/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001701CodeGen::RValue
1702CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001703 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001704 QualType ResultType,
1705 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001706 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001707 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001708 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001709 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001710 const CodeGen::CallArgList &CallArgs,
1711 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001712 // Create and init a super structure; this is a (receiver, class)
1713 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001714 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001715 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001716 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001717 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001718 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001719 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001720
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001721 // If this is a class message the metaclass is passed as the target.
1722 llvm::Value *Target;
1723 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001724 if (isCategoryImpl) {
1725 // Message sent to 'super' in a class method defined in a category
1726 // implementation requires an odd treatment.
1727 // If we are in a class method, we must retrieve the
1728 // _metaclass_ for the current class, pointed at by
1729 // the class's "isa" pointer. The following assumes that
1730 // isa" is the first ivar in a class (which it must be).
1731 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1732 Target = CGF.Builder.CreateStructGEP(Target, 0);
1733 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001734 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001735 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1736 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1737 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1738 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001739 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001740 }
1741 else if (isCategoryImpl)
1742 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1743 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001744 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1745 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1746 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001747 }
Mike Stump18bb9282009-05-16 07:57:57 +00001748 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1749 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001750 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001751 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001753 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001754 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001755 return EmitMessageSend(CGF, Return, ResultType,
1756 EmitSelector(CGF.Builder, Sel),
1757 ObjCSuper, ObjCTypes.SuperPtrCTy,
1758 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001759}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001760
1761/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001762CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001763 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001764 QualType ResultType,
1765 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001766 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001767 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001768 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001769 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001770 return EmitMessageSend(CGF, Return, ResultType,
1771 EmitSelector(CGF.Builder, Sel),
1772 Receiver, CGF.getContext().getObjCIdType(),
1773 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001774}
1775
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001776CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001777CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1778 ReturnValueSlot Return,
1779 QualType ResultType,
1780 llvm::Value *Sel,
1781 llvm::Value *Arg0,
1782 QualType Arg0Ty,
1783 bool IsSuper,
1784 const CallArgList &CallArgs,
1785 const ObjCMethodDecl *Method,
1786 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001787 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001788 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001789 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001790 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1791 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001792 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001793
John McCalla729c622012-02-17 03:33:10 +00001794 // If we're calling a method, use the formal signature.
1795 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001796
Anders Carlsson280e61f12010-06-21 20:59:55 +00001797 if (Method)
1798 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1799 CGM.getContext().getCanonicalType(ResultType) &&
1800 "Result type mismatch!");
1801
John McCall5880fb82011-05-14 21:12:11 +00001802 NullReturnState nullReturn;
1803
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001804 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001805 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001806 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001807 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001808 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001809 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1810 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1811 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001812 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1813 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1814 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001815 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001816 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001817 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001818 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001819
1820 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001821 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001822 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1823 e = Method->param_end(); i != e; ++i) {
1824 const ParmVarDecl *ParamDecl = (*i);
1825 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1826 if (!nullReturn.NullBB)
1827 nullReturn.init(CGF, Arg0);
1828 requiresnullCheck = true;
1829 break;
1830 }
1831 }
1832
John McCalla729c622012-02-17 03:33:10 +00001833 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1834 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001835 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1836 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001837}
1838
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001839static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1840 if (FQT.isObjCGCStrong())
1841 return Qualifiers::Strong;
1842
John McCall31168b02011-06-15 23:02:42 +00001843 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001844 return Qualifiers::Weak;
1845
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001846 // check for __unsafe_unretained
1847 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1848 return Qualifiers::GCNone;
1849
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001850 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1851 return Qualifiers::Strong;
1852
1853 if (const PointerType *PT = FQT->getAs<PointerType>())
1854 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1855
1856 return Qualifiers::GCNone;
1857}
1858
John McCall351762c2011-02-07 10:33:21 +00001859llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1860 const CGBlockInfo &blockInfo) {
Chris Lattnerece04092012-02-07 00:39:47 +00001861 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall351762c2011-02-07 10:33:21 +00001862
David Blaikiebbafb8a2012-03-11 07:00:24 +00001863 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1864 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001865 return nullPtr;
1866
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001867 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001868 SkipIvars.clear();
1869 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001870 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1871 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001872
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001873 // __isa is the first field in block descriptor and must assume by runtime's
1874 // convention that it is GC'able.
1875 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001876
1877 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1878
1879 // Calculate the basic layout of the block structure.
1880 const llvm::StructLayout *layout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001881 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall351762c2011-02-07 10:33:21 +00001882
1883 // Ignore the optional 'this' capture: C++ objects are not assumed
1884 // to be GC'ed.
1885
1886 // Walk the captured variables.
1887 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1888 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1889 const VarDecl *variable = ci->getVariable();
1890 QualType type = variable->getType();
1891
1892 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1893
1894 // Ignore constant captures.
1895 if (capture.isConstant()) continue;
1896
1897 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1898
1899 // __block variables are passed by their descriptor address.
1900 if (ci->isByRef()) {
1901 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001902 continue;
John McCall351762c2011-02-07 10:33:21 +00001903 }
1904
1905 assert(!type->isArrayType() && "array variable should not be caught");
1906 if (const RecordType *record = type->getAs<RecordType>()) {
1907 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001908 continue;
1909 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001910
John McCall351762c2011-02-07 10:33:21 +00001911 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1912 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1913
1914 if (GCAttr == Qualifiers::Strong)
1915 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1916 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001917 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001918 SkipIvars.push_back(GC_IVAR(fieldOffset,
1919 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001920 }
1921
1922 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001923 return nullPtr;
1924
1925 // Sort on byte position; captures might not be allocated in order,
1926 // and unions can do funny things.
1927 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1928 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001929
1930 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001931 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001932 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001933 printf("\n block variable layout for block: ");
Roman Divackye6377112012-09-06 15:59:27 +00001934 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00001935 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001936 if (!(s[i] & 0xf0))
1937 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1938 else
1939 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1940 printf("\n");
1941 }
1942
1943 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001944}
1945
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001946llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001947 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001948 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001949 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001950 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1951
Owen Andersonade90fd2009-07-29 18:54:39 +00001952 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001953 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001954}
1955
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001956void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001957 // FIXME: We shouldn't need this, the protocol decl should contain enough
1958 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001959 DefinedProtocols.insert(PD->getIdentifier());
1960
1961 // If we have generated a forward reference to this protocol, emit
1962 // it now. Otherwise do nothing, the protocol objects are lazily
1963 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001964 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001965 GetOrEmitProtocol(PD);
1966}
1967
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001968llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001969 if (DefinedProtocols.count(PD->getIdentifier()))
1970 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001971
Daniel Dunbarc475d422008-10-29 22:36:39 +00001972 return GetOrEmitProtocolRef(PD);
1973}
1974
Daniel Dunbarb036db82008-08-13 03:21:16 +00001975/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001976// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1977struct _objc_protocol {
1978struct _objc_protocol_extension *isa;
1979char *protocol_name;
1980struct _objc_protocol_list *protocol_list;
1981struct _objc__method_prototype_list *instance_methods;
1982struct _objc__method_prototype_list *class_methods
1983};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001984
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001985See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001986*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001987llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00001988 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00001989
1990 // Early exit if a defining object has already been generated.
1991 if (Entry && Entry->hasInitializer())
1992 return Entry;
1993
Douglas Gregora715bff2012-01-01 19:51:50 +00001994 // Use the protocol definition, if there is one.
1995 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1996 PD = Def;
1997
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001998 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001999 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002000 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2001
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002002 // Construct method lists.
2003 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2004 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002005 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002006 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002007 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002008 ObjCMethodDecl *MD = *i;
2009 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002010 if (!C)
2011 return GetOrEmitProtocolRef(PD);
2012
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002013 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2014 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002015 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002016 } else {
2017 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002018 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002019 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002020 }
2021
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002022 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002023 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002024 ObjCMethodDecl *MD = *i;
2025 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002026 if (!C)
2027 return GetOrEmitProtocolRef(PD);
2028
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002029 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2030 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002031 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002032 } else {
2033 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002034 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002035 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002036 }
2037
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002038 MethodTypesExt.insert(MethodTypesExt.end(),
2039 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2040
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002041 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002042 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2043 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002044 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002045 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002046 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002047 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002048 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002049 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002050 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002051 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002052 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002053 ClassMethods)
2054 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002055 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002056 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002057
Daniel Dunbarb036db82008-08-13 03:21:16 +00002058 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002059 // Already created, fix the linkage and update the initializer.
2060 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002061 Entry->setInitializer(Init);
2062 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002064 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002065 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002066 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002067 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002068 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002069 // FIXME: Is this necessary? Why only for protocol?
2070 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002071
2072 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002073 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00002074 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002075
2076 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002077}
2078
Daniel Dunbarc475d422008-10-29 22:36:39 +00002079llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002080 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2081
2082 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002083 // We use the initializer as a marker of whether this is a forward
2084 // reference or not. At module finalization we add the empty
2085 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002086 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002087 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002088 llvm::GlobalValue::ExternalLinkage,
2089 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002090 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002091 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002092 // FIXME: Is this necessary? Why only for protocol?
2093 Entry->setAlignment(4);
2094 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002095
Daniel Dunbarb036db82008-08-13 03:21:16 +00002096 return Entry;
2097}
2098
2099/*
2100 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002101 uint32_t size;
2102 struct objc_method_description_list *optional_instance_methods;
2103 struct objc_method_description_list *optional_class_methods;
2104 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002105 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002106 };
2107*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002108llvm::Constant *
2109CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002110 ArrayRef<llvm::Constant*> OptInstanceMethods,
2111 ArrayRef<llvm::Constant*> OptClassMethods,
2112 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002113 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002114 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002115 llvm::Constant *Values[] = {
2116 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002117 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002118 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002119 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002120 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002121 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002122 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002123 OptClassMethods),
2124 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002125 ObjCTypes),
2126 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2127 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002128 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002129
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002130 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002132 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002133 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002134
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002135 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002136 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002137
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002138 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002139 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002140 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002141 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002142}
2143
2144/*
2145 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002146 struct objc_protocol_list *next;
2147 long count;
2148 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002149 };
2150*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002151llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002152CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002153 ObjCProtocolDecl::protocol_iterator begin,
2154 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002155 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002156
Daniel Dunbardec75f82008-08-21 21:57:41 +00002157 for (; begin != end; ++begin)
2158 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002159
2160 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002161 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002162 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002163
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002164 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002165 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002166
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002167 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002168 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002169 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002170 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002171 ProtocolRefs.size() - 1);
2172 Values[2] =
2173 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2174 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002175 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002176
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002177 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002178 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002179 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002180 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002181 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002182}
2183
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002184void CGObjCCommonMac::
2185PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendlinga515b582012-02-09 22:16:49 +00002186 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002187 const Decl *Container,
2188 const ObjCProtocolDecl *PROTO,
2189 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002190 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2191 E = PROTO->protocol_end(); P != E; ++P)
2192 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2193 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2194 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002195 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002196 if (!PropertySet.insert(PD->getIdentifier()))
2197 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002198 llvm::Constant *Prop[] = {
2199 GetPropertyName(PD->getIdentifier()),
2200 GetPropertyTypeString(PD, Container)
2201 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002202 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2203 }
2204}
2205
Daniel Dunbarb036db82008-08-13 03:21:16 +00002206/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002207 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002208 const char * const name;
2209 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002210 };
2211
2212 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002213 uint32_t entsize; // sizeof (struct _objc_property)
2214 uint32_t prop_count;
2215 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002216 };
2217*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002218llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002219 const Decl *Container,
2220 const ObjCContainerDecl *OCD,
2221 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002222 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002223 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002224 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2225 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002226 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002227 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002228 llvm::Constant *Prop[] = {
2229 GetPropertyName(PD->getIdentifier()),
2230 GetPropertyTypeString(PD, Container)
2231 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002232 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002233 Prop));
2234 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002235 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002236 for (ObjCInterfaceDecl::all_protocol_iterator
2237 P = OID->all_referenced_protocol_begin(),
2238 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002239 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2240 ObjCTypes);
2241 }
2242 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2243 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2244 E = CD->protocol_end(); P != E; ++P)
2245 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2246 ObjCTypes);
2247 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002248
2249 // Return null for empty list.
2250 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002251 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002252
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002253 unsigned PropertySize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002254 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002255 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002256 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2257 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002258 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002259 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002260 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002261 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002262
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002263 llvm::GlobalVariable *GV =
2264 CreateMetadataVar(Name, Init,
2265 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002266 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002267 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002268 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002269 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002270}
2271
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002272llvm::Constant *
2273CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2274 ArrayRef<llvm::Constant*> MethodTypes,
2275 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002276 // Return null for empty list.
2277 if (MethodTypes.empty())
2278 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2279
2280 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2281 MethodTypes.size());
2282 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2283
2284 llvm::GlobalVariable *GV =
2285 CreateMetadataVar(Name, Init,
2286 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2287 (ObjCABI == 2) ? 8 : 4,
2288 true);
2289 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2290}
2291
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002292/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002293 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002294 int count;
2295 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002296 };
2297*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002298llvm::Constant *
2299CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002300 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002301 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002302 ObjCTypes.SelectorPtrTy),
2303 GetMethodVarType(MD)
2304 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002305 if (!Desc[1])
2306 return 0;
2307
Owen Anderson0e0189d2009-07-27 22:29:56 +00002308 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002309 Desc);
2310}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002311
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002312llvm::Constant *
2313CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2314 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002315 // Return null for empty list.
2316 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002317 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002318
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002319 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002320 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002321 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002322 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002323 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002324 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002325
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002326 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002327 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002328 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002329}
2330
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002331/*
2332 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002333 char *category_name;
2334 char *class_name;
2335 struct _objc_method_list *instance_methods;
2336 struct _objc_method_list *class_methods;
2337 struct _objc_protocol_list *protocols;
2338 uint32_t size; // <rdar://4585769>
2339 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002340 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002341*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002342void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00002343 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002344
Mike Stump18bb9282009-05-16 07:57:57 +00002345 // FIXME: This is poor design, the OCD should have a pointer to the category
2346 // decl. Additionally, note that Category can be null for the @implementation
2347 // w/o an @interface case. Sema should just create one for us as it does for
2348 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002349 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002350 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002351 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002352
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002353 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002354 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2355 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002356
Bill Wendlinga515b582012-02-09 22:16:49 +00002357 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002358 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002359 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002360 // Instance methods should always be defined.
2361 InstanceMethods.push_back(GetMethodConstant(*i));
2362 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002363 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002364 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002365 // Class methods should always be defined.
2366 ClassMethods.push_back(GetMethodConstant(*i));
2367 }
2368
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002369 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002370 Values[0] = GetClassName(OCD->getIdentifier());
2371 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002372 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002373 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002374 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002375 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002376 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002377 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002378 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002379 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002380 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002381 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002382 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002383 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002384 Category->protocol_begin(),
2385 Category->protocol_end());
2386 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002387 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002388 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002389 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002390
2391 // If there is no category @interface then there can be no properties.
2392 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002393 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002394 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002395 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002396 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002397 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002398
Owen Anderson0e0189d2009-07-27 22:29:56 +00002399 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002400 Values);
2401
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002402 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002403 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002404 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002405 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002406 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002407 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002408 // method definition entries must be clear for next implementation.
2409 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002410}
2411
John McCallef19dbb2012-10-17 04:53:23 +00002412enum FragileClassFlags {
2413 FragileABI_Class_Factory = 0x00001,
2414 FragileABI_Class_Meta = 0x00002,
2415 FragileABI_Class_HasCXXStructors = 0x02000,
2416 FragileABI_Class_Hidden = 0x20000
2417};
2418
2419enum NonFragileClassFlags {
2420 /// Is a meta-class.
2421 NonFragileABI_Class_Meta = 0x00001,
2422
2423 /// Is a root class.
2424 NonFragileABI_Class_Root = 0x00002,
2425
2426 /// Has a C++ constructor and destructor.
2427 NonFragileABI_Class_HasCXXStructors = 0x00004,
2428
2429 /// Has hidden visibility.
2430 NonFragileABI_Class_Hidden = 0x00010,
2431
2432 /// Has the exception attribute.
2433 NonFragileABI_Class_Exception = 0x00020,
2434
2435 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2436 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2437
2438 /// Class implementation was compiled under ARC.
John McCall0d54a172012-10-17 04:53:31 +00002439 NonFragileABI_Class_CompiledByARC = 0x00080,
2440
2441 /// Class has non-trivial destructors, but zero-initialization is okay.
2442 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443};
2444
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002445/*
2446 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002447 Class isa;
2448 Class super_class;
2449 const char *name;
2450 long version;
2451 long info;
2452 long instance_size;
2453 struct _objc_ivar_list *ivars;
2454 struct _objc_method_list *methods;
2455 struct _objc_cache *cache;
2456 struct _objc_protocol_list *protocols;
2457 // Objective-C 1.0 extensions (<rdr://4585769>)
2458 const char *ivar_layout;
2459 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002460 };
2461
2462 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002463*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002464void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002465 DefinedSymbols.insert(ID->getIdentifier());
2466
Chris Lattner86d7d912008-11-24 03:54:41 +00002467 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002468 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002469 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002470 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002471 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002472 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002473 Interface->all_referenced_protocol_begin(),
2474 Interface->all_referenced_protocol_end());
John McCallef19dbb2012-10-17 04:53:23 +00002475 unsigned Flags = FragileABI_Class_Factory;
John McCall0d54a172012-10-17 04:53:31 +00002476 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCallef19dbb2012-10-17 04:53:23 +00002477 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002478 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002479 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002480
2481 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002482 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00002483 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002484
Bill Wendlinga515b582012-02-09 22:16:49 +00002485 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002486 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002487 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002488 // Instance methods should always be defined.
2489 InstanceMethods.push_back(GetMethodConstant(*i));
2490 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002491 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002492 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002493 // Class methods should always be defined.
2494 ClassMethods.push_back(GetMethodConstant(*i));
2495 }
2496
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002497 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002498 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002499 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002500
2501 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2502 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2503
2504 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2505 if (llvm::Constant *C = GetMethodConstant(MD))
2506 InstanceMethods.push_back(C);
2507 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2508 if (llvm::Constant *C = GetMethodConstant(MD))
2509 InstanceMethods.push_back(C);
2510 }
2511 }
2512
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002513 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002514 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002515 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002516 // Record a reference to the super class.
2517 LazySymbols.insert(Super->getIdentifier());
2518
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002519 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002520 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002521 ObjCTypes.ClassPtrTy);
2522 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002523 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002524 }
2525 Values[ 2] = GetClassName(ID->getIdentifier());
2526 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002527 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2528 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2529 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002530 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002531 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002532 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002533 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002534 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002535 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002536 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002537 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002539 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002540 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002541 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002542 std::string Name("\01L_OBJC_CLASS_");
2543 Name += ClassName;
2544 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2545 // Check for a forward reference.
2546 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2547 if (GV) {
2548 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2549 "Forward metaclass reference has incorrect type.");
2550 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2551 GV->setInitializer(Init);
2552 GV->setSection(Section);
2553 GV->setAlignment(4);
2554 CGM.AddUsedGlobal(GV);
2555 }
2556 else
2557 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002558 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002559 // method definition entries must be clear for next implementation.
2560 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002561}
2562
2563llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2564 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002565 ArrayRef<llvm::Constant*> Methods) {
John McCallef19dbb2012-10-17 04:53:23 +00002566 unsigned Flags = FragileABI_Class_Meta;
Micah Villmowdd31ca12012-10-08 16:25:52 +00002567 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002568
John McCall457a04e2010-10-22 21:05:15 +00002569 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00002570 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002571
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002572 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002573 // The isa for the metaclass is the root of the hierarchy.
2574 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2575 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2576 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002577 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002578 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002579 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002580 // The super class for the metaclass is emitted as the name of the
2581 // super class. The runtime fixes this up to point to the
2582 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002583 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002584 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002585 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002586 ObjCTypes.ClassPtrTy);
2587 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002588 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002589 }
2590 Values[ 2] = GetClassName(ID->getIdentifier());
2591 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002592 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2593 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2594 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002595 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002596 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002597 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002598 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002599 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002600 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002601 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002602 Values[ 9] = Protocols;
2603 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002604 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002605 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002606 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002607 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002608 Values);
2609
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002610 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer1bbcbd02012-07-31 11:45:39 +00002611 Name += ID->getName();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002612
2613 // Check for a forward reference.
2614 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2615 if (GV) {
2616 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2617 "Forward metaclass reference has incorrect type.");
2618 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2619 GV->setInitializer(Init);
2620 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002621 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002622 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002623 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002624 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002625 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002626 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002627 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002628
2629 return GV;
2630}
2631
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002632llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002633 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002634
Mike Stump18bb9282009-05-16 07:57:57 +00002635 // FIXME: Should we look these up somewhere other than the module. Its a bit
2636 // silly since we only generate these while processing an implementation, so
2637 // exactly one pointer would work if know when we entered/exitted an
2638 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002639
2640 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002641 // Previously, metaclass with internal linkage may have been defined.
2642 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002643 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2644 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002645 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2646 "Forward metaclass reference has incorrect type.");
2647 return GV;
2648 } else {
2649 // Generate as an external reference to keep a consistent
2650 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002651 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002652 llvm::GlobalValue::ExternalLinkage,
2653 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002654 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002655 }
2656}
2657
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002658llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2659 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2660
2661 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2662 true)) {
2663 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2664 "Forward class metadata reference has incorrect type.");
2665 return GV;
2666 } else {
2667 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2668 llvm::GlobalValue::ExternalLinkage,
2669 0,
2670 Name);
2671 }
2672}
2673
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002674/*
2675 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002676 uint32_t size;
2677 const char *weak_ivar_layout;
2678 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002679 };
2680*/
2681llvm::Constant *
2682CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002683 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002684 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002685
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002686 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002687 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002688 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002689 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002690 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002691
2692 // Return null if no extension bits are used.
2693 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002694 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002695
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002696 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002697 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002698 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002699 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002700 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002701}
2702
2703/*
2704 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002705 char *ivar_name;
2706 char *ivar_type;
2707 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002708 };
2709
2710 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002711 int ivar_count;
2712 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002713 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002714*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002715llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002716 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002717 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002718
2719 // When emitting the root class GCC emits ivar entries for the
2720 // actual class structure. It is not clear if we need to follow this
2721 // behavior; for now lets try and get away with not doing it. If so,
2722 // the cleanest solution would be to make up an ObjCInterfaceDecl
2723 // for the class.
2724 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002725 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002726
Jordy Rosea91768e2011-07-22 02:08:32 +00002727 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002728
Jordy Rosea91768e2011-07-22 02:08:32 +00002729 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002730 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002731 // Ignore unnamed bit-fields.
2732 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002733 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002734 llvm::Constant *Ivar[] = {
2735 GetMethodVarName(IVD->getIdentifier()),
2736 GetMethodVarType(IVD),
2737 llvm::ConstantInt::get(ObjCTypes.IntTy,
2738 ComputeIvarBaseOffset(CGM, OID, IVD))
2739 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002740 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002741 }
2742
2743 // Return null for empty list.
2744 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002745 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002746
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002747 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002748 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002749 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002750 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002751 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002752 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002753
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002754 llvm::GlobalVariable *GV;
2755 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002756 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002757 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002758 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002759 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002760 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002761 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002762 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002763 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002764}
2765
2766/*
2767 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002768 SEL method_name;
2769 char *method_types;
2770 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002771 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002772
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002773 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002774 struct objc_method_list *obsolete;
2775 int count;
2776 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002777 };
2778*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002779
2780/// GetMethodConstant - Return a struct objc_method constant for the
2781/// given method if it has been defined. The result is null if the
2782/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002783llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002784 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002785 if (!Fn)
2786 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002787
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002788 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002789 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002790 ObjCTypes.SelectorPtrTy),
2791 GetMethodVarType(MD),
2792 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2793 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002794 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002795}
2796
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002797llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002798 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002799 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002800 // Return null for empty list.
2801 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002802 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002803
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002804 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002805 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002806 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002807 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002808 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002809 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002810 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002811
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002812 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002813 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002814}
2815
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002816llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002817 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002818 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002819 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002820
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002821 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002822 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00002823 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002824 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002825 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002826 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002827 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002828 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002829 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002830
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002831 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002832}
2833
Daniel Dunbar30c65362009-03-09 20:09:19 +00002834llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002835CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002836 llvm::Constant *Init,
2837 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002838 unsigned Align,
2839 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002840 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002841 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002842 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002843 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002844 if (Section)
2845 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002846 if (Align)
2847 GV->setAlignment(Align);
2848 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002849 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002850 return GV;
2851}
2852
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002853llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002854 // Abuse this interface function as a place to finalize.
2855 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002856 return NULL;
2857}
2858
Chris Lattnerd4808922009-03-22 21:03:39 +00002859llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002860 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002861}
2862
Chris Lattnerd4808922009-03-22 21:03:39 +00002863llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002864 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002865}
2866
Ted Kremeneke65b0862012-03-06 20:05:56 +00002867llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2868 bool copy) {
2869 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2870}
2871
David Chisnall168b80f2010-12-26 22:13:16 +00002872llvm::Constant *CGObjCMac::GetGetStructFunction() {
2873 return ObjCTypes.getCopyStructFn();
2874}
2875llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002876 return ObjCTypes.getCopyStructFn();
2877}
2878
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002879llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2880 return ObjCTypes.getCppAtomicObjectFunction();
2881}
2882
Chris Lattnerd4808922009-03-22 21:03:39 +00002883llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002884 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002885}
2886
John McCallbd309292010-07-06 01:34:17 +00002887void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2888 return EmitTryOrSynchronizedStmt(CGF, S);
2889}
2890
2891void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2892 const ObjCAtSynchronizedStmt &S) {
2893 return EmitTryOrSynchronizedStmt(CGF, S);
2894}
2895
John McCall65bea082010-07-21 06:59:36 +00002896namespace {
John McCallcda666c2010-07-21 07:22:38 +00002897 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002898 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002899 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002900 llvm::Value *CallTryExitVar;
2901 llvm::Value *ExceptionData;
2902 ObjCTypesHelper &ObjCTypes;
2903 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002904 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002905 llvm::Value *CallTryExitVar,
2906 llvm::Value *ExceptionData,
2907 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002908 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002909 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2910
John McCall30317fd2011-07-12 20:27:29 +00002911 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002912 // Check whether we need to call objc_exception_try_exit.
2913 // In optimized code, this branch will always be folded.
2914 llvm::BasicBlock *FinallyCallExit =
2915 CGF.createBasicBlock("finally.call_exit");
2916 llvm::BasicBlock *FinallyNoCallExit =
2917 CGF.createBasicBlock("finally.no_call_exit");
2918 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2919 FinallyCallExit, FinallyNoCallExit);
2920
2921 CGF.EmitBlock(FinallyCallExit);
2922 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2923 ->setDoesNotThrow();
2924
2925 CGF.EmitBlock(FinallyNoCallExit);
2926
2927 if (isa<ObjCAtTryStmt>(S)) {
2928 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002929 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2930 // Save the current cleanup destination in case there's
2931 // control flow inside the finally statement.
2932 llvm::Value *CurCleanupDest =
2933 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2934
John McCall65bea082010-07-21 06:59:36 +00002935 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2936
John McCallcebe0ca2010-08-11 00:16:14 +00002937 if (CGF.HaveInsertPoint()) {
2938 CGF.Builder.CreateStore(CurCleanupDest,
2939 CGF.getNormalCleanupDestSlot());
2940 } else {
2941 // Currently, the end of the cleanup must always exist.
2942 CGF.EnsureInsertPoint();
2943 }
2944 }
John McCall65bea082010-07-21 06:59:36 +00002945 } else {
2946 // Emit objc_sync_exit(expr); as finally's sole statement for
2947 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002948 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002949 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2950 ->setDoesNotThrow();
2951 }
2952 }
2953 };
John McCall42227ed2010-07-31 23:20:56 +00002954
2955 class FragileHazards {
2956 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002957 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002958 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2959
2960 llvm::InlineAsm *ReadHazard;
2961 llvm::InlineAsm *WriteHazard;
2962
2963 llvm::FunctionType *GetAsmFnType();
2964
2965 void collectLocals();
2966 void emitReadHazard(CGBuilderTy &Builder);
2967
2968 public:
2969 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002970
John McCall42227ed2010-07-31 23:20:56 +00002971 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002972 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002973 };
2974}
2975
2976/// Create the fragile-ABI read and write hazards based on the current
2977/// state of the function, which is presumed to be immediately prior
2978/// to a @try block. These hazards are used to maintain correct
2979/// semantics in the face of optimization and the fragile ABI's
2980/// cavalier use of setjmp/longjmp.
2981FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2982 collectLocals();
2983
2984 if (Locals.empty()) return;
2985
2986 // Collect all the blocks in the function.
2987 for (llvm::Function::iterator
2988 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2989 BlocksBeforeTry.insert(&*I);
2990
2991 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2992
2993 // Create a read hazard for the allocas. This inhibits dead-store
2994 // optimizations and forces the values to memory. This hazard is
2995 // inserted before any 'throwing' calls in the protected scope to
2996 // reflect the possibility that the variables might be read from the
2997 // catch block if the call throws.
2998 {
2999 std::string Constraint;
3000 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3001 if (I) Constraint += ',';
3002 Constraint += "*m";
3003 }
3004
3005 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3006 }
3007
3008 // Create a write hazard for the allocas. This inhibits folding
3009 // loads across the hazard. This hazard is inserted at the
3010 // beginning of the catch path to reflect the possibility that the
3011 // variables might have been written within the protected scope.
3012 {
3013 std::string Constraint;
3014 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3015 if (I) Constraint += ',';
3016 Constraint += "=*m";
3017 }
3018
3019 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3020 }
3021}
3022
3023/// Emit a write hazard at the current location.
3024void FragileHazards::emitWriteHazard() {
3025 if (Locals.empty()) return;
3026
Jay Foad5bd375a2011-07-15 08:37:34 +00003027 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003028}
3029
John McCall42227ed2010-07-31 23:20:56 +00003030void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3031 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00003032 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003033}
3034
3035/// Emit read hazards in all the protected blocks, i.e. all the blocks
3036/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00003037void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00003038 if (Locals.empty()) return;
3039
3040 CGBuilderTy Builder(CGF.getLLVMContext());
3041
3042 // Iterate through all blocks, skipping those prior to the try.
3043 for (llvm::Function::iterator
3044 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3045 llvm::BasicBlock &BB = *FI;
3046 if (BlocksBeforeTry.count(&BB)) continue;
3047
3048 // Walk through all the calls in the block.
3049 for (llvm::BasicBlock::iterator
3050 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3051 llvm::Instruction &I = *BI;
3052
3053 // Ignore instructions that aren't non-intrinsic calls.
3054 // These are the only calls that can possibly call longjmp.
3055 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3056 if (isa<llvm::IntrinsicInst>(I))
3057 continue;
3058
3059 // Ignore call sites marked nounwind. This may be questionable,
3060 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3061 llvm::CallSite CS(&I);
3062 if (CS.doesNotThrow()) continue;
3063
John McCall2dd7d442010-08-04 05:59:32 +00003064 // Insert a read hazard before the call. This will ensure that
3065 // any writes to the locals are performed before making the
3066 // call. If the call throws, then this is sufficient to
3067 // guarantee correctness as long as it doesn't also write to any
3068 // locals.
John McCall42227ed2010-07-31 23:20:56 +00003069 Builder.SetInsertPoint(&BB, BI);
3070 emitReadHazard(Builder);
3071 }
3072 }
3073}
3074
3075static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3076 if (V) S.insert(V);
3077}
3078
3079void FragileHazards::collectLocals() {
3080 // Compute a set of allocas to ignore.
3081 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3082 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3083 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00003084
3085 // Collect all the allocas currently in the function. This is
3086 // probably way too aggressive.
3087 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3088 for (llvm::BasicBlock::iterator
3089 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3090 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3091 Locals.push_back(&*I);
3092}
3093
3094llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003095 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003096 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3097 tys[i] = Locals[i]->getType();
3098 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003099}
3100
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003101/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003102
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003103 Objective-C setjmp-longjmp (sjlj) Exception Handling
3104 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003105
John McCallbd309292010-07-06 01:34:17 +00003106 A catch buffer is a setjmp buffer plus:
3107 - a pointer to the exception that was caught
3108 - a pointer to the previous exception data buffer
3109 - two pointers of reserved storage
3110 Therefore catch buffers form a stack, with a pointer to the top
3111 of the stack kept in thread-local storage.
3112
3113 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3114 objc_exception_try_exit pops the given catch buffer, which is
3115 required to be the top of the EH stack.
3116 objc_exception_throw pops the top of the EH stack, writes the
3117 thrown exception into the appropriate field, and longjmps
3118 to the setjmp buffer. It crashes the process (with a printf
3119 and an abort()) if there are no catch buffers on the stack.
3120 objc_exception_extract just reads the exception pointer out of the
3121 catch buffer.
3122
3123 There's no reason an implementation couldn't use a light-weight
3124 setjmp here --- something like __builtin_setjmp, but API-compatible
3125 with the heavyweight setjmp. This will be more important if we ever
3126 want to implement correct ObjC/C++ exception interactions for the
3127 fragile ABI.
3128
3129 Note that for this use of setjmp/longjmp to be correct, we may need
3130 to mark some local variables volatile: if a non-volatile local
3131 variable is modified between the setjmp and the longjmp, it has
3132 indeterminate value. For the purposes of LLVM IR, it may be
3133 sufficient to make loads and stores within the @try (to variables
3134 declared outside the @try) volatile. This is necessary for
3135 optimized correctness, but is not currently being done; this is
3136 being tracked as rdar://problem/8160285
3137
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003138 The basic framework for a @try-catch-finally is as follows:
3139 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003140 objc_exception_data d;
3141 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003142 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003143
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003144 objc_exception_try_enter(&d);
3145 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003146 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003147 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003148 // exception path
3149 id _caught = objc_exception_extract(&d);
3150
3151 // enter new try scope for handlers
3152 if (!setjmp(d.jmp_buf)) {
3153 ... match exception and execute catch blocks ...
3154
3155 // fell off end, rethrow.
3156 _rethrow = _caught;
3157 ... jump-through-finally to finally_rethrow ...
3158 } else {
3159 // exception in catch block
3160 _rethrow = objc_exception_extract(&d);
3161 _call_try_exit = false;
3162 ... jump-through-finally to finally_rethrow ...
3163 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003164 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003165 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003166
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003168 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003169 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003170
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003171 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003172 ... dispatch to finally destination ...
3173
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003175 objc_exception_throw(_rethrow);
3176
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003177 finally_end:
3178 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003179
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003180 This framework differs slightly from the one gcc uses, in that gcc
3181 uses _rethrow to determine if objc_exception_try_exit should be called
3182 and if the object should be rethrown. This breaks in the face of
3183 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003184
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003185 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003186
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003187 - If there are no catch blocks, then we avoid emitting the second
3188 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003189
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003190 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3191 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003192
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003193 - FIXME: If there is no @finally block we can do a few more
3194 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003195
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003196 Rethrows and Jumps-Through-Finally
3197 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003198
John McCallbd309292010-07-06 01:34:17 +00003199 '@throw;' is supported by pushing the currently-caught exception
3200 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003201
John McCallbd309292010-07-06 01:34:17 +00003202 Branches through the @finally block are handled with an ordinary
3203 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3204 exceptions are not compatible with C++ exceptions, and this is
3205 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003206
John McCallbd309292010-07-06 01:34:17 +00003207 @synchronized(expr) { stmt; } is emitted as if it were:
3208 id synch_value = expr;
3209 objc_sync_enter(synch_value);
3210 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003211*/
3212
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003213void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3214 const Stmt &S) {
3215 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003216
3217 // A destination for the fall-through edges of the catch handlers to
3218 // jump to.
3219 CodeGenFunction::JumpDest FinallyEnd =
3220 CGF.getJumpDestInCurrentScope("finally.end");
3221
3222 // A destination for the rethrow edge of the catch handlers to jump
3223 // to.
3224 CodeGenFunction::JumpDest FinallyRethrow =
3225 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003226
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003227 // For @synchronized, call objc_sync_enter(sync.expr). The
3228 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003229 // @synchronized. We can't avoid a temp here because we need the
3230 // value to be preserved. If the backend ever does liveness
3231 // correctly after setjmp, this will be unnecessary.
3232 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003233 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003234 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003235 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3236 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003237 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3238 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003239
3240 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3241 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003242 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003243
John McCall2dd7d442010-08-04 05:59:32 +00003244 // Allocate memory for the setjmp buffer. This needs to be kept
3245 // live throughout the try and catch blocks.
3246 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3247 "exceptiondata.ptr");
3248
John McCall42227ed2010-07-31 23:20:56 +00003249 // Create the fragile hazards. Note that this will not capture any
3250 // of the allocas required for exception processing, but will
3251 // capture the current basic block (which extends all the way to the
3252 // setjmp call) as "before the @try".
3253 FragileHazards Hazards(CGF);
3254
John McCallbd309292010-07-06 01:34:17 +00003255 // Create a flag indicating whether the cleanup needs to call
3256 // objc_exception_try_exit. This is true except when
3257 // - no catches match and we're branching through the cleanup
3258 // just to rethrow the exception, or
3259 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003260 // The setjmp-safety rule here is that we should always store to this
3261 // variable in a place that dominates the branch through the cleanup
3262 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003263 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003264 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003265
John McCall9916e3f2010-10-04 23:42:51 +00003266 // A slot containing the exception to rethrow. Only needed when we
3267 // have both a @catch and a @finally.
3268 llvm::Value *PropagatingExnVar = 0;
3269
John McCallbd309292010-07-06 01:34:17 +00003270 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003271 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003272 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003273 CallTryExitVar,
3274 ExceptionData,
3275 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003276
3277 // Enter a try block:
3278 // - Call objc_exception_try_enter to push ExceptionData on top of
3279 // the EH stack.
3280 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3281 ->setDoesNotThrow();
3282
3283 // - Call setjmp on the exception data buffer.
3284 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3285 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3286 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003287 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003288 llvm::CallInst *SetJmpResult =
3289 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3290 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003291 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003292
3293 // If setjmp returned 0, enter the protected block; otherwise,
3294 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003295 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3296 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003297 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003298 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3299 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003300
John McCallbd309292010-07-06 01:34:17 +00003301 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003302 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003303 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003304 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003305 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003306
3307 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003308
John McCallbd309292010-07-06 01:34:17 +00003309 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003310 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003311
John McCall42227ed2010-07-31 23:20:56 +00003312 // Don't optimize loads of the in-scope locals across this point.
3313 Hazards.emitWriteHazard();
3314
John McCallbd309292010-07-06 01:34:17 +00003315 // For a @synchronized (or a @try with no catches), just branch
3316 // through the cleanup to the rethrow block.
3317 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3318 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003319 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003320 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003321
3322 // Otherwise, we have to match against the caught exceptions.
3323 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003324 // Retrieve the exception object. We may emit multiple blocks but
3325 // nothing can cross this so the value is already in SSA form.
3326 llvm::CallInst *Caught =
3327 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3328 ExceptionData, "caught");
3329 Caught->setDoesNotThrow();
3330
John McCallbd309292010-07-06 01:34:17 +00003331 // Push the exception to rethrow onto the EH value stack for the
3332 // benefit of any @throws in the handlers.
3333 CGF.ObjCEHValueStack.push_back(Caught);
3334
Douglas Gregor96c79492010-04-23 22:50:49 +00003335 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003336
John McCall2dd7d442010-08-04 05:59:32 +00003337 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003338
John McCall2dd7d442010-08-04 05:59:32 +00003339 llvm::BasicBlock *CatchBlock = 0;
3340 llvm::BasicBlock *CatchHandler = 0;
3341 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003342 // Save the currently-propagating exception before
3343 // objc_exception_try_enter clears the exception slot.
3344 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3345 "propagating_exception");
3346 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3347
John McCall2dd7d442010-08-04 05:59:32 +00003348 // Enter a new exception try block (in case a @catch block
3349 // throws an exception).
3350 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3351 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003352
John McCall2dd7d442010-08-04 05:59:32 +00003353 llvm::CallInst *SetJmpResult =
3354 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3355 "setjmp.result");
3356 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003357 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003358
John McCall2dd7d442010-08-04 05:59:32 +00003359 llvm::Value *Threw =
3360 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3361
3362 CatchBlock = CGF.createBasicBlock("catch");
3363 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3364 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3365
3366 CGF.EmitBlock(CatchBlock);
3367 }
3368
3369 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003370
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003371 // Handle catch list. As a special case we check if everything is
3372 // matched and avoid generating code for falling off the end if
3373 // so.
3374 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003375 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3376 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003377
Douglas Gregor46a572b2010-04-26 16:46:50 +00003378 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003379 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003380
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003381 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003382 if (!CatchParam) {
3383 AllMatched = true;
3384 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003385 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003386
John McCallbd309292010-07-06 01:34:17 +00003387 // catch(id e) always matches under this ABI, since only
3388 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003389 // FIXME: For the time being we also match id<X>; this should
3390 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003391 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003392 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003393 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003394
John McCallbd309292010-07-06 01:34:17 +00003395 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003396 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003397 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3398
Anders Carlsson9396a892008-09-11 09:15:33 +00003399 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003400 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003401 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003402
3403 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003404 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003405 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003406
Anders Carlsson9396a892008-09-11 09:15:33 +00003407 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003408
3409 // The scope of the catch variable ends right here.
3410 CatchVarCleanups.ForceCleanup();
3411
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003412 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003413 break;
3414 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003415
Steve Naroff7cae42b2009-07-10 23:34:53 +00003416 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003417 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003418
3419 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003420 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3421 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003422
3423 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003424 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003425
John McCallbd309292010-07-06 01:34:17 +00003426 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003427 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3428 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003429 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003430
John McCallbd309292010-07-06 01:34:17 +00003431 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3432 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003433
3434 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003435 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003436
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003437 // Emit the @catch block.
3438 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003439
3440 // Collect any cleanups for the catch variable. The scope lasts until
3441 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003442 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003443
John McCall1c9c3fd2010-10-15 04:57:14 +00003444 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003445 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003446
John McCallbd309292010-07-06 01:34:17 +00003447 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003448 llvm::Value *Tmp =
3449 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003450 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003451 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003452
Anders Carlsson9396a892008-09-11 09:15:33 +00003453 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003454
3455 // We're done with the catch variable.
3456 CatchVarCleanups.ForceCleanup();
3457
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003458 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003459
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003460 CGF.EmitBlock(NextCatchBlock);
3461 }
3462
John McCallbd309292010-07-06 01:34:17 +00003463 CGF.ObjCEHValueStack.pop_back();
3464
John McCall2dd7d442010-08-04 05:59:32 +00003465 // If nothing wanted anything to do with the caught exception,
3466 // kill the extract call.
3467 if (Caught->use_empty())
3468 Caught->eraseFromParent();
3469
3470 if (!AllMatched)
3471 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3472
3473 if (HasFinally) {
3474 // Emit the exception handler for the @catch blocks.
3475 CGF.EmitBlock(CatchHandler);
3476
3477 // In theory we might now need a write hazard, but actually it's
3478 // unnecessary because there's no local-accessing code between
3479 // the try's write hazard and here.
3480 //Hazards.emitWriteHazard();
3481
John McCall9916e3f2010-10-04 23:42:51 +00003482 // Extract the new exception and save it to the
3483 // propagating-exception slot.
3484 assert(PropagatingExnVar);
3485 llvm::CallInst *NewCaught =
3486 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3487 ExceptionData, "caught");
3488 NewCaught->setDoesNotThrow();
3489 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3490
John McCall2dd7d442010-08-04 05:59:32 +00003491 // Don't pop the catch handler; the throw already did.
3492 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003493 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003494 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003495 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003496
John McCall42227ed2010-07-31 23:20:56 +00003497 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003498 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003499
John McCallbd309292010-07-06 01:34:17 +00003500 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003501 CGF.Builder.restoreIP(TryFallthroughIP);
3502 if (CGF.HaveInsertPoint())
3503 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003504 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003505 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003506
John McCallbd309292010-07-06 01:34:17 +00003507 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003508 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003509 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003510 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003511 // If we have a propagating-exception variable, check it.
3512 llvm::Value *PropagatingExn;
3513 if (PropagatingExnVar) {
3514 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003515
John McCall9916e3f2010-10-04 23:42:51 +00003516 // Otherwise, just look in the buffer for the exception to throw.
3517 } else {
3518 llvm::CallInst *Caught =
3519 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3520 ExceptionData);
3521 Caught->setDoesNotThrow();
3522 PropagatingExn = Caught;
3523 }
3524
3525 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003526 ->setDoesNotThrow();
3527 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003528 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003529
John McCall42227ed2010-07-31 23:20:56 +00003530 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003531}
3532
3533void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003534 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003535 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003536
Anders Carlssone005aa12008-09-09 16:16:55 +00003537 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003538 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003539 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003540 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003541 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003542 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003543 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003544 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003545 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003546
John McCallbd309292010-07-06 01:34:17 +00003547 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3548 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003549 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003550
3551 // Clear the insertion point to indicate we are in unreachable code.
3552 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003553}
3554
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003555/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003556/// object: objc_read_weak (id *src)
3557///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003558llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003559 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003560 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3562 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3563 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003564 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003565 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003566 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003567 return read_weak;
3568}
3569
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003570/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3571/// objc_assign_weak (id src, id *dst)
3572///
3573void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003574 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003575 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003576 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003577 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003578 assert(Size <= 8 && "does not support size > 8");
3579 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003580 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003581 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3582 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003583 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3584 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003585 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003586 src, dst, "weakassign");
3587 return;
3588}
3589
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003590/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3591/// objc_assign_global (id src, id *dst)
3592///
3593void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003594 llvm::Value *src, llvm::Value *dst,
3595 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003596 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003597 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003598 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003599 assert(Size <= 8 && "does not support size > 8");
3600 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003601 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003602 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3603 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003604 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3605 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003606 if (!threadlocal)
3607 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3608 src, dst, "globalassign");
3609 else
3610 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3611 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003612 return;
3613}
3614
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003615/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003616/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003617///
3618void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003619 llvm::Value *src, llvm::Value *dst,
3620 llvm::Value *ivarOffset) {
3621 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003622 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003623 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003624 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003625 assert(Size <= 8 && "does not support size > 8");
3626 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003627 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003628 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3629 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003630 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3631 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003632 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3633 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003634 return;
3635}
3636
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003637/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3638/// objc_assign_strongCast (id src, id *dst)
3639///
3640void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003641 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003642 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003643 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003644 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003645 assert(Size <= 8 && "does not support size > 8");
3646 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003647 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003648 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3649 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003650 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3651 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003652 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003653 src, dst, "weakassign");
3654 return;
3655}
3656
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003657void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003658 llvm::Value *DestPtr,
3659 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003660 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003661 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3662 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003663 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003664 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003665 return;
3666}
3667
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003668/// EmitObjCValueForIvar - Code Gen for ivar reference.
3669///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003670LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3671 QualType ObjectTy,
3672 llvm::Value *BaseValue,
3673 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003674 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003675 const ObjCInterfaceDecl *ID =
3676 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003677 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3678 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003679}
3680
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003681llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003682 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003683 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003684 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003685 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003686 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3687 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003688}
3689
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003690/* *** Private Interface *** */
3691
3692/// EmitImageInfo - Emit the image info marker used to encode some module
3693/// level information.
3694///
3695/// See: <rdr://4810609&4810587&4810587>
3696/// struct IMAGE_INFO {
3697/// unsigned version;
3698/// unsigned flags;
3699/// };
3700enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003701 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003702 eImageInfo_GarbageCollected = (1 << 1),
3703 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003704 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3705
Daniel Dunbar5e639272010-04-25 20:39:01 +00003706 // A flag indicating that the module has no instances of a @synthesize of a
3707 // superclass variable. <rdar://problem/6803242>
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003708 eImageInfo_CorrectedSynthesize = (1 << 4),
3709 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003710};
3711
Daniel Dunbar5e639272010-04-25 20:39:01 +00003712void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003713 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003714 const char *Section = (ObjCABI == 1) ?
3715 "__OBJC, __image_info,regular" :
3716 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003717
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003718 // Generate module-level named metadata to convey this information to the
3719 // linker and code-gen.
3720 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003721
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003722 // Add the ObjC ABI version to the module flags.
3723 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3724 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3725 version);
3726 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3727 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003728
David Blaikiebbafb8a2012-03-11 07:00:24 +00003729 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003730 // Non-GC overrides those files which specify GC.
3731 Mod.addModuleFlag(llvm::Module::Override,
3732 "Objective-C Garbage Collection", (uint32_t)0);
3733 } else {
3734 // Add the ObjC garbage collection value.
3735 Mod.addModuleFlag(llvm::Module::Error,
3736 "Objective-C Garbage Collection",
3737 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003738
David Blaikiebbafb8a2012-03-11 07:00:24 +00003739 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003740 // Add the ObjC GC Only value.
3741 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3742 eImageInfo_GCOnly);
3743
3744 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3745 llvm::Value *Ops[2] = {
3746 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3747 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3748 eImageInfo_GarbageCollected)
3749 };
3750 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3751 llvm::MDNode::get(VMContext, Ops));
3752 }
3753 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003754
3755 // Indicate whether we're compiling this to run on a simulator.
3756 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3757 if (Triple.getOS() == llvm::Triple::IOS &&
3758 (Triple.getArch() == llvm::Triple::x86 ||
3759 Triple.getArch() == llvm::Triple::x86_64))
3760 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3761 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003762}
3763
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003764// struct objc_module {
3765// unsigned long version;
3766// unsigned long size;
3767// const char *name;
3768// Symtab symtab;
3769// };
3770
3771// FIXME: Get from somewhere
3772static const int ModuleVersion = 7;
3773
3774void CGObjCMac::EmitModuleInfo() {
Micah Villmowdd31ca12012-10-08 16:25:52 +00003775 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003776
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003777 llvm::Constant *Values[] = {
3778 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3779 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3780 // This used to be the filename, now it is unused. <rdr://4327263>
3781 GetClassName(&CGM.getContext().Idents.get("")),
3782 EmitModuleSymbols()
3783 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003784 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003785 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003786 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003787 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003788}
3789
3790llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003791 unsigned NumClasses = DefinedClasses.size();
3792 unsigned NumCategories = DefinedCategories.size();
3793
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003794 // Return null if no symbols were defined.
3795 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003796 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003797
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003798 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003799 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003800 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003801 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3802 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003803
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003804 // The runtime expects exactly the list of defined classes followed
3805 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00003806 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003807 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003808 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003809 ObjCTypes.Int8PtrTy);
3810 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003812 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003813 ObjCTypes.Int8PtrTy);
3814
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003815 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003816 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00003817 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003818 Symbols);
3819
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003820 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003821
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003822 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003823 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3824 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003825 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003826 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003827}
3828
John McCall31168b02011-06-15 23:02:42 +00003829llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3830 IdentifierInfo *II) {
3831 LazySymbols.insert(II);
3832
3833 llvm::GlobalVariable *&Entry = ClassReferences[II];
3834
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003835 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003836 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003837 llvm::ConstantExpr::getBitCast(GetClassName(II),
3838 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003839 Entry =
John McCall31168b02011-06-15 23:02:42 +00003840 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3841 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3842 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003843 }
John McCall31168b02011-06-15 23:02:42 +00003844
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003845 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003846}
3847
John McCall31168b02011-06-15 23:02:42 +00003848llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3849 const ObjCInterfaceDecl *ID) {
3850 return EmitClassRefFromId(Builder, ID->getIdentifier());
3851}
3852
3853llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3854 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3855 return EmitClassRefFromId(Builder, II);
3856}
3857
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003858llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3859 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003860 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003861
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003862 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003863 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003864 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003865 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003866 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003867 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3868 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003869 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003870 }
3871
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003872 if (lvalue)
3873 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003874 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003875}
3876
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003877llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003878 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003879
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003880 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003881 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00003882 llvm::ConstantDataArray::getString(VMContext,
3883 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003884 ((ObjCABI == 2) ?
3885 "__TEXT,__objc_classname,cstring_literals" :
3886 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003887 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003888
Owen Anderson170229f2009-07-14 23:10:40 +00003889 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003890}
3891
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003892llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3893 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3894 I = MethodDefinitions.find(MD);
3895 if (I != MethodDefinitions.end())
3896 return I->second;
3897
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003898 return NULL;
3899}
3900
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003901/// GetIvarLayoutName - Returns a unique constant for the given
3902/// ivar layout bitmap.
3903llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003904 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003905 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003906}
3907
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003908void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003909 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003910 bool ForStrongLayout,
3911 bool &HasUnion) {
3912 const RecordDecl *RD = RT->getDecl();
3913 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00003914 SmallVector<const FieldDecl*, 16> Fields;
3915 for (RecordDecl::field_iterator i = RD->field_begin(),
3916 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00003917 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00003918 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003919 const llvm::StructLayout *RecLayout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00003920 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003921
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003922 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3923 ForStrongLayout, HasUnion);
3924}
3925
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003926void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003927 const llvm::StructLayout *Layout,
3928 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003929 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003930 unsigned int BytePos, bool ForStrongLayout,
3931 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003932 bool IsUnion = (RD && RD->isUnion());
3933 uint64_t MaxUnionIvarSize = 0;
3934 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003935 const FieldDecl *MaxField = 0;
3936 const FieldDecl *MaxSkippedField = 0;
3937 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003938 uint64_t MaxFieldOffset = 0;
3939 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003940 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003941 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003942
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003943 if (RecFields.empty())
3944 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003945 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3946 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003947 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003948 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003949 FirstFieldDelta =
3950 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3951 }
3952
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003953 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003954 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003955 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003956 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003957 // Note that 'i' here is actually the field index inside RD of Field,
3958 // although this dependency is hidden.
3959 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003960 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003961 } else
John McCall31168b02011-06-15 23:02:42 +00003962 FieldOffset =
3963 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003964
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003965 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003966 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003967 LastFieldBitfieldOrUnnamed = Field;
3968 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003969 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003970 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003971
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003972 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003973 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003974 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003975 if (FQT->isUnionType())
3976 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003977
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003978 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003979 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003980 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003981 continue;
3982 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003983
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003984 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003985 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003986 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003987 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003988 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003989 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003990 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3991 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003992 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003993 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003994 FQT = CArray->getElementType();
3995 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003996
3997 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003998 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003999 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004000 int OldIndex = IvarsInfo.size() - 1;
4001 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004002
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004003 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004004 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004005 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004006
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004007 // Replicate layout information for each array element. Note that
4008 // one element is already done.
4009 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004010 for (int FirstIndex = IvarsInfo.size() - 1,
4011 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004012 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004013 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4014 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4015 IvarsInfo[i].ivar_size));
4016 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4017 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4018 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004019 }
4020 continue;
4021 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004022 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004023 // At this point, we are done with Record/Union and array there of.
4024 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00004025 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004026
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004027 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00004028 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4029 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004030 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004031 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004032 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004033 MaxUnionIvarSize = UnionIvarSize;
4034 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004035 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004036 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004037 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004038 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004039 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004040 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004041 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00004042 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4043 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004044 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00004045 // FIXME: Why the asymmetry? We divide by word size in bits on other
4046 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004047 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004048 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004049 MaxSkippedUnionIvarSize = UnionIvarSize;
4050 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004051 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004052 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004053 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004054 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004055 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004056 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004057 }
4058 }
4059 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004060
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004061 if (LastFieldBitfieldOrUnnamed) {
4062 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4063 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00004064 uint64_t BitFieldSize
4065 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004066 GC_IVAR skivar;
4067 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
4068 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4069 + ((BitFieldSize % ByteSizeInBits) != 0);
4070 SkipIvars.push_back(skivar);
4071 } else {
4072 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4073 // Last field was unnamed. Must update skip info.
4074 unsigned FieldSize
4075 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4076 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4077 FieldSize / ByteSizeInBits));
4078 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004079 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004080
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004081 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004082 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004083 MaxUnionIvarSize));
4084 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00004085 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004086 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004087}
4088
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004089/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4090/// the computations and returning the layout bitmap (for ivar or blocks) in
4091/// the given argument BitMap string container. Routine reads
4092/// two containers, IvarsInfo and SkipIvars which are assumed to be
4093/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004094llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004095 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004096 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004097
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004098 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004099 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004100 unsigned int WordSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004101 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004102 if (IvarsInfo[0].ivar_bytepos == 0) {
4103 WordsToSkip = 0;
4104 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004105 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004106 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4107 WordsToScan = IvarsInfo[0].ivar_size;
4108 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004109 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004110 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004111 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004112 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004113 // consecutive 'scanned' object pointers.
4114 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004115 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004116 // Skip over 'gc'able object pointer which lay over each other.
4117 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4118 continue;
4119 // Must skip over 1 or more words. We save current skip/scan values
4120 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004121 SKIP_SCAN SkScan;
4122 SkScan.skip = WordsToSkip;
4123 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004124 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004125
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004126 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004127 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4128 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004129 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004130 WordsToSkip = 0;
4131 WordsToScan = IvarsInfo[i].ivar_size;
4132 }
4133 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004134 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004135 SKIP_SCAN SkScan;
4136 SkScan.skip = WordsToSkip;
4137 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004138 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004139 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004140
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004141 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004142 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004143 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004144 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004145 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004146 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004147 IvarsInfo[LastIndex].ivar_bytepos +
4148 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004149 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004150 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004151 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004152 SKIP_SCAN SkScan;
4153 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4154 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004155 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004156 }
4157 }
4158 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4159 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004160 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004161 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004162 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4163 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4164 // 0xM0 followed by 0x0N detected.
4165 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4166 for (int j = i+1; j < SkipScan; j++)
4167 SkipScanIvars[j] = SkipScanIvars[j+1];
4168 --SkipScan;
4169 }
4170 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004171
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004172 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004173 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004174 unsigned char byte;
4175 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4176 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4177 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4178 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004179
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004180 // first skip big.
4181 for (unsigned int ix = 0; ix < skip_big; ix++)
4182 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004183
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004184 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004185 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004186 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004187 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004188 byte |= 0xf;
4189 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004190 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004191 byte |= scan_small;
4192 scan_small = 0;
4193 }
4194 BitMap += byte;
4195 }
4196 // next scan big
4197 for (unsigned int ix = 0; ix < scan_big; ix++)
4198 BitMap += (unsigned char)(0x0f);
4199 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004200 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004201 byte = scan_small;
4202 BitMap += byte;
4203 }
4204 }
4205 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004206 unsigned char zero = 0;
4207 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004208
4209 llvm::GlobalVariable * Entry =
4210 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004211 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004212 ((ObjCABI == 2) ?
4213 "__TEXT,__objc_classname,cstring_literals" :
4214 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004215 1, true);
4216 return getConstantGEP(VMContext, Entry, 0, 0);
4217}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004218
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004219/// BuildIvarLayout - Builds ivar layout bitmap for the class
4220/// implementation for the __strong or __weak case.
4221/// The layout map displays which words in ivar list must be skipped
4222/// and which must be scanned by GC (see below). String is built of bytes.
4223/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4224/// of words to skip and right nibble is count of words to scan. So, each
4225/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4226/// represented by a 0x00 byte which also ends the string.
4227/// 1. when ForStrongLayout is true, following ivars are scanned:
4228/// - id, Class
4229/// - object *
4230/// - __strong anything
4231///
4232/// 2. When ForStrongLayout is false, following ivars are scanned:
4233/// - __weak anything
4234///
4235llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4236 const ObjCImplementationDecl *OMD,
4237 bool ForStrongLayout) {
4238 bool hasUnion = false;
4239
Chris Lattnerece04092012-02-07 00:39:47 +00004240 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004241 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4242 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004243 return llvm::Constant::getNullValue(PtrTy);
4244
Jordy Rosea91768e2011-07-22 02:08:32 +00004245 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4246 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004247 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004248 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004249 IVD; IVD = IVD->getNextIvar())
4250 RecFields.push_back(cast<FieldDecl>(IVD));
4251 }
4252 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004253 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004254 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004255
Jordy Rosea91768e2011-07-22 02:08:32 +00004256 // FIXME: This is not ideal; we shouldn't have to do this copy.
4257 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004258 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004259
4260 if (RecFields.empty())
4261 return llvm::Constant::getNullValue(PtrTy);
4262
4263 SkipIvars.clear();
4264 IvarsInfo.clear();
4265
4266 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4267 if (IvarsInfo.empty())
4268 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004269 // Sort on byte position in case we encounterred a union nested in
4270 // the ivar list.
4271 if (hasUnion && !IvarsInfo.empty())
4272 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4273 if (hasUnion && !SkipIvars.empty())
4274 std::sort(SkipIvars.begin(), SkipIvars.end());
4275
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004276 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004277 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004278
David Blaikiebbafb8a2012-03-11 07:00:24 +00004279 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004280 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004281 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004282 OMD->getClassInterface()->getName().data());
Roman Divackye6377112012-09-06 15:59:27 +00004283 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004284 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004285 if (!(s[i] & 0xf0))
4286 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4287 else
4288 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4289 printf("\n");
4290 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004291 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004292}
4293
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004294llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004295 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4296
Chris Lattner3def9ae2012-02-06 22:16:34 +00004297 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004298 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004300 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004301 ((ObjCABI == 2) ?
4302 "__TEXT,__objc_methname,cstring_literals" :
4303 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004304 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004305
Owen Anderson170229f2009-07-14 23:10:40 +00004306 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004307}
4308
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004309// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004310llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004311 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4312}
4313
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004314llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004315 std::string TypeStr;
4316 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4317
4318 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004319
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004320 if (!Entry)
4321 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004322 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004323 ((ObjCABI == 2) ?
4324 "__TEXT,__objc_methtype,cstring_literals" :
4325 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004326 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004327
Owen Anderson170229f2009-07-14 23:10:40 +00004328 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004329}
4330
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004331llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4332 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004333 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004334 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004335 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004336
4337 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4338
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004339 if (!Entry)
4340 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004341 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004342 ((ObjCABI == 2) ?
4343 "__TEXT,__objc_methtype,cstring_literals" :
4344 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004345 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004346
Owen Anderson170229f2009-07-14 23:10:40 +00004347 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004348}
4349
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004350// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004351llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004352 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004353
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004354 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004355 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004356 llvm::ConstantDataArray::getString(VMContext,
4357 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004358 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004359 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004360
Owen Anderson170229f2009-07-14 23:10:40 +00004361 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004362}
4363
4364// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004365// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004366llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4368 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004369 std::string TypeStr;
4370 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004371 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4372}
4373
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004374void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004375 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004376 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004377 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004378 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004379 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4380 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004381 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004382 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004383 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004384 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004385}
4386
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004387void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004388 EmitModuleInfo();
4389
Daniel Dunbarc475d422008-10-29 22:36:39 +00004390 // Emit the dummy bodies for any protocols which were referenced but
4391 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004392 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004393 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4394 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004395 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004396
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004397 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004398 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004399 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004400 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004401 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004402 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004403 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004404 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004405 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004406 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004407 }
4408
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004409 // Add assembler directives to add lazy undefined symbol references
4410 // for classes which are referenced but not defined. This is
4411 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004412 //
4413 // FIXME: It would be nice if we had an LLVM construct for this.
4414 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004415 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004416 Asm += CGM.getModule().getModuleInlineAsm();
4417 if (!Asm.empty() && Asm.back() != '\n')
4418 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004419
Daniel Dunbard027a922009-09-07 00:20:42 +00004420 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004421 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4422 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004423 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4424 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004425 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004426 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004427 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004428 }
4429
Bill Wendling53136852012-02-07 09:06:01 +00004430 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004431 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4432 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4433 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004434
Daniel Dunbard027a922009-09-07 00:20:42 +00004435 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004436 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004437}
4438
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004440 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004441 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004442 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004443 ObjCABI = 2;
4444}
4445
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004446/* *** */
4447
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004448ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004449 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4450{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004451 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4452 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004453
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004454 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004455 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004456 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004457 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00004458 Int8PtrTy = CGM.Int8PtrTy;
4459 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004460
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004461 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004462 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004463 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004464
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004465 // I'm not sure I like this. The implicit coordination is a bit
4466 // gross. We should solve this in a reasonable fashion because this
4467 // is a pretty common task (match some runtime data structure with
4468 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004470 // FIXME: This is leaked.
4471 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004472
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004473 // struct _objc_super {
4474 // id self;
4475 // Class cls;
4476 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004477 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004478 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004479 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004480 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004481 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004482 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004483 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004484 Ctx.getObjCClassType(), 0, 0, false,
4485 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004486 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004487
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004488 SuperCTy = Ctx.getTagDeclType(RD);
4489 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004490
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004491 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004492 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4493
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004494 // struct _prop_t {
4495 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004496 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004497 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004498 PropertyTy = llvm::StructType::create("struct._prop_t",
4499 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004500
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004501 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004502 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004503 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004504 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004505 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004506 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004507 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4508 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004509 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004510 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004511
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004512 // struct _objc_method {
4513 // SEL _cmd;
4514 // char *method_type;
4515 // char *_imp;
4516 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004517 MethodTy = llvm::StructType::create("struct._objc_method",
4518 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4519 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004520
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004521 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004522 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004523 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004524
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004525}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004526
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004527ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004528 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004529 // struct _objc_method_description {
4530 // SEL name;
4531 // char *types;
4532 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004533 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004534 llvm::StructType::create("struct._objc_method_description",
4535 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004536
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004537 // struct _objc_method_description_list {
4538 // int count;
4539 // struct _objc_method_description[1];
4540 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004541 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004542 llvm::StructType::create("struct._objc_method_description_list",
4543 IntTy,
4544 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004545
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004546 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004547 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004548 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004549
Daniel Dunbarb036db82008-08-13 03:21:16 +00004550 // Protocol description structures
4551
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004552 // struct _objc_protocol_extension {
4553 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4554 // struct _objc_method_description_list *optional_instance_methods;
4555 // struct _objc_method_description_list *optional_class_methods;
4556 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004557 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004558 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004559 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004560 llvm::StructType::create("struct._objc_protocol_extension",
4561 IntTy, MethodDescriptionListPtrTy,
4562 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004563 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004564
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004565 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004566 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004567
Daniel Dunbarc475d422008-10-29 22:36:39 +00004568 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004569
Chris Lattnera5f58b02011-07-09 17:41:47 +00004570 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004571 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004572
Chris Lattnera5f58b02011-07-09 17:41:47 +00004573 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004574 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004575 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004576 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004577 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004578 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004579
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004580 // struct _objc_protocol {
4581 // struct _objc_protocol_extension *isa;
4582 // char *protocol_name;
4583 // struct _objc_protocol **_objc_protocol_list;
4584 // struct _objc_method_description_list *instance_methods;
4585 // struct _objc_method_description_list *class_methods;
4586 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004587 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4588 llvm::PointerType::getUnqual(ProtocolListTy),
4589 MethodDescriptionListPtrTy,
4590 MethodDescriptionListPtrTy,
4591 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004592
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004593 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004594 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004595
Owen Anderson9793f0e2009-07-29 22:16:19 +00004596 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004597
4598 // Class description structures
4599
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004600 // struct _objc_ivar {
4601 // char *ivar_name;
4602 // char *ivar_type;
4603 // int ivar_offset;
4604 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004605 IvarTy = llvm::StructType::create("struct._objc_ivar",
4606 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004607
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004608 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004609 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004610 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004611 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004612
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004613 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004614 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004615 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004616 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004617
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004618 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004619 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004620 llvm::StructType::create("struct._objc_class_extension",
4621 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004622 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004623
Chris Lattner5ec04a52011-08-12 17:43:31 +00004624 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004625
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004626 // struct _objc_class {
4627 // Class isa;
4628 // Class super_class;
4629 // char *name;
4630 // long version;
4631 // long info;
4632 // long instance_size;
4633 // struct _objc_ivar_list *ivars;
4634 // struct _objc_method_list *methods;
4635 // struct _objc_cache *cache;
4636 // struct _objc_protocol_list *protocols;
4637 // char *ivar_layout;
4638 // struct _objc_class_ext *ext;
4639 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004640 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4641 llvm::PointerType::getUnqual(ClassTy),
4642 Int8PtrTy,
4643 LongTy,
4644 LongTy,
4645 LongTy,
4646 IvarListPtrTy,
4647 MethodListPtrTy,
4648 CachePtrTy,
4649 ProtocolListPtrTy,
4650 Int8PtrTy,
4651 ClassExtensionPtrTy,
4652 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Owen Anderson9793f0e2009-07-29 22:16:19 +00004654 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004655
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004656 // struct _objc_category {
4657 // char *category_name;
4658 // char *class_name;
4659 // struct _objc_method_list *instance_method;
4660 // struct _objc_method_list *class_method;
4661 // uint32_t size; // sizeof(struct _objc_category)
4662 // struct _objc_property_list *instance_properties;// category's @property
4663 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004664 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004665 llvm::StructType::create("struct._objc_category",
4666 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4667 MethodListPtrTy, ProtocolListPtrTy,
4668 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004669
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004670 // Global metadata structures
4671
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004672 // struct _objc_symtab {
4673 // long sel_ref_cnt;
4674 // SEL *refs;
4675 // short cls_def_cnt;
4676 // short cat_def_cnt;
4677 // char *defs[cls_def_cnt + cat_def_cnt];
4678 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004679 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004680 llvm::StructType::create("struct._objc_symtab",
4681 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4682 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004683 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004684
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004685 // struct _objc_module {
4686 // long version;
4687 // long size; // sizeof(struct _objc_module)
4688 // char *name;
4689 // struct _objc_symtab* symtab;
4690 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004691 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004692 llvm::StructType::create("struct._objc_module",
4693 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004694
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004695
Mike Stump18bb9282009-05-16 07:57:57 +00004696 // FIXME: This is the size of the setjmp buffer and should be target
4697 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004698 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004699
Anders Carlsson9ff22482008-09-09 10:10:21 +00004700 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00004701 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004702
4703 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004704 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00004705 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4706 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004707
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004708}
4709
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004710ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004711 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004712 // struct _method_list_t {
4713 // uint32_t entsize; // sizeof(struct _objc_method)
4714 // uint32_t method_count;
4715 // struct _objc_method method_list[method_count];
4716 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004717 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004718 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4719 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004720 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004721 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004722
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004723 // struct _protocol_t {
4724 // id isa; // NULL
4725 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004726 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004727 // const struct method_list_t * const instance_methods;
4728 // const struct method_list_t * const class_methods;
4729 // const struct method_list_t *optionalInstanceMethods;
4730 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004731 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004732 // const uint32_t size; // sizeof(struct _protocol_t)
4733 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004734 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004735 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004736
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004737 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004738 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004739 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004740
Chris Lattnera5f58b02011-07-09 17:41:47 +00004741 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004742 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4743 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4744 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4745 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004746 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4747 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004748
4749 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004750 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004751
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004752 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004753 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004754 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004755 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004756 ProtocolListnfABITy->setBody(LongTy,
4757 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4758 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004760 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004761 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004762
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004763 // struct _ivar_t {
4764 // unsigned long int *offset; // pointer to ivar offset location
4765 // char *name;
4766 // char *type;
4767 // uint32_t alignment;
4768 // uint32_t size;
4769 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004770 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004771 llvm::StructType::create("struct._ivar_t",
4772 llvm::PointerType::getUnqual(LongTy),
4773 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004774
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004775 // struct _ivar_list_t {
4776 // uint32 entsize; // sizeof(struct _ivar_t)
4777 // uint32 count;
4778 // struct _iver_t list[count];
4779 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004780 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004781 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4782 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004783
Owen Anderson9793f0e2009-07-29 22:16:19 +00004784 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004785
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004786 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004787 // uint32_t const flags;
4788 // uint32_t const instanceStart;
4789 // uint32_t const instanceSize;
4790 // uint32_t const reserved; // only when building for 64bit targets
4791 // const uint8_t * const ivarLayout;
4792 // const char *const name;
4793 // const struct _method_list_t * const baseMethods;
4794 // const struct _objc_protocol_list *const baseProtocols;
4795 // const struct _ivar_list_t *const ivars;
4796 // const uint8_t * const weakIvarLayout;
4797 // const struct _prop_list_t * const properties;
4798 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004800 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004801 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4802 IntTy, IntTy, IntTy, Int8PtrTy,
4803 Int8PtrTy, MethodListnfABIPtrTy,
4804 ProtocolListnfABIPtrTy,
4805 IvarListnfABIPtrTy,
4806 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004807
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004808 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004809 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004810 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4811 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004812
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004813 // struct _class_t {
4814 // struct _class_t *isa;
4815 // struct _class_t * const superclass;
4816 // void *cache;
4817 // IMP *vtable;
4818 // struct class_ro_t *ro;
4819 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004820
Chris Lattner5ec04a52011-08-12 17:43:31 +00004821 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004822 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4823 llvm::PointerType::getUnqual(ClassnfABITy),
4824 CachePtrTy,
4825 llvm::PointerType::getUnqual(ImpnfABITy),
4826 llvm::PointerType::getUnqual(ClassRonfABITy),
4827 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004828
Fariborz Jahanian71394042009-01-23 23:53:38 +00004829 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004830 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004831
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004832 // struct _category_t {
4833 // const char * const name;
4834 // struct _class_t *const cls;
4835 // const struct _method_list_t * const instance_methods;
4836 // const struct _method_list_t * const class_methods;
4837 // const struct _protocol_list_t * const protocols;
4838 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004839 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004840 CategorynfABITy = llvm::StructType::create("struct._category_t",
4841 Int8PtrTy, ClassnfABIPtrTy,
4842 MethodListnfABIPtrTy,
4843 MethodListnfABIPtrTy,
4844 ProtocolListnfABIPtrTy,
4845 PropertyListPtrTy,
4846 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004847
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004848 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004849 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4850 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004851
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004852 // MessageRefTy - LLVM for:
4853 // struct _message_ref_t {
4854 // IMP messenger;
4855 // SEL name;
4856 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004857
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004858 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004859 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004860 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004861 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004862 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004863 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004864 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004865 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004866 Ctx.getObjCSelType(), 0, 0, false,
4867 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004868 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004869
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004870 MessageRefCTy = Ctx.getTagDeclType(RD);
4871 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4872 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004873
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004874 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004875 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004877 // SuperMessageRefTy - LLVM for:
4878 // struct _super_message_ref_t {
4879 // SUPER_IMP messenger;
4880 // SEL name;
4881 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004882 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004883 llvm::StructType::create("struct._super_message_ref_t",
4884 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004885
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004886 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004887 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004888
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004889
4890 // struct objc_typeinfo {
4891 // const void** vtable; // objc_ehtype_vtable + 2
4892 // const char* name; // c++ typeinfo string
4893 // Class cls;
4894 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004895 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004896 llvm::StructType::create("struct._objc_typeinfo",
4897 llvm::PointerType::getUnqual(Int8PtrTy),
4898 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004899 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004900}
4901
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004902llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004903 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004904
Fariborz Jahanian71394042009-01-23 23:53:38 +00004905 return NULL;
4906}
4907
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00004908void CGObjCNonFragileABIMac::
4909AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4910 const char *SymbolName,
4911 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00004912 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004913
Daniel Dunbar19573e72009-05-15 21:48:48 +00004914 if (!NumClasses)
4915 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004916
Chris Lattner3def9ae2012-02-06 22:16:34 +00004917 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004918 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004919 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004920 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00004921 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004922 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004923 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00004924 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004925
Daniel Dunbar19573e72009-05-15 21:48:48 +00004926 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004927 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004928 llvm::GlobalValue::InternalLinkage,
4929 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004930 SymbolName);
Micah Villmowdd31ca12012-10-08 16:25:52 +00004931 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004932 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004933 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004934}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004935
Fariborz Jahanian71394042009-01-23 23:53:38 +00004936void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4937 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004938
Daniel Dunbar19573e72009-05-15 21:48:48 +00004939 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004940 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004941 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004942 "\01L_OBJC_LABEL_CLASS_$",
4943 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004944
Bill Wendling53136852012-02-07 09:06:01 +00004945 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00004946 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4947 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4948 continue;
4949 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004950 }
4951
Bill Wendling53136852012-02-07 09:06:01 +00004952 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004953 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4954 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4955 continue;
4956 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4957 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004958
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004959 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004960 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4961 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004963 // Build list of all implemented category addresses in array
4964 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004966 "\01L_OBJC_LABEL_CATEGORY_$",
4967 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004968 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004969 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4970 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004971
Daniel Dunbar5e639272010-04-25 20:39:01 +00004972 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004973}
4974
John McCall9e8bb002011-05-14 03:10:52 +00004975/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4976/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004977/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004978/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004979bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4980 // At various points we've experimented with using vtable-based
4981 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004982 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004983 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004984 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004985 case CodeGenOptions::NonLegacy:
4986 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004987 case CodeGenOptions::Mixed:
4988 break;
4989 }
4990
4991 // If so, see whether this selector is in the white-list of things which must
4992 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004993 if (VTableDispatchMethods.empty()) {
4994 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4995 VTableDispatchMethods.insert(GetNullarySelector("class"));
4996 VTableDispatchMethods.insert(GetNullarySelector("self"));
4997 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4998 VTableDispatchMethods.insert(GetNullarySelector("length"));
4999 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005000
John McCall9e8bb002011-05-14 03:10:52 +00005001 // These are vtable-based if GC is disabled.
5002 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005003 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00005004 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5005 VTableDispatchMethods.insert(GetNullarySelector("release"));
5006 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5007 }
5008
5009 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5010 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5011 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5012 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5013 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5014 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5015 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5016
5017 // These are vtable-based if GC is enabled.
5018 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005019 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00005020 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5021 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5022
5023 // "countByEnumeratingWithState:objects:count"
5024 IdentifierInfo *KeyIdents[] = {
5025 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5026 &CGM.getContext().Idents.get("objects"),
5027 &CGM.getContext().Idents.get("count")
5028 };
5029 VTableDispatchMethods.insert(
5030 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5031 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005032 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005033
John McCall9e8bb002011-05-14 03:10:52 +00005034 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005035}
5036
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005037/// BuildClassRoTInitializer - generate meta-data for:
5038/// struct _class_ro_t {
5039/// uint32_t const flags;
5040/// uint32_t const instanceStart;
5041/// uint32_t const instanceSize;
5042/// uint32_t const reserved; // only when building for 64bit targets
5043/// const uint8_t * const ivarLayout;
5044/// const char *const name;
5045/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005046/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005047/// const struct _ivar_list_t *const ivars;
5048/// const uint8_t * const weakIvarLayout;
5049/// const struct _prop_list_t * const properties;
5050/// }
5051///
5052llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005053 unsigned flags,
5054 unsigned InstanceStart,
5055 unsigned InstanceSize,
5056 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005057 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005058 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00005059
David Blaikiebbafb8a2012-03-11 07:00:24 +00005060 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallef19dbb2012-10-17 04:53:23 +00005061 flags |= NonFragileABI_Class_CompiledByARC;
John McCall31168b02011-06-15 23:02:42 +00005062
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005063 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5064 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5065 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005066 // FIXME. For 64bit targets add 0 here.
John McCallef19dbb2012-10-17 04:53:23 +00005067 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5068 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005069 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005070 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005071 // const struct _method_list_t * const baseMethods;
5072 std::vector<llvm::Constant*> Methods;
5073 std::string MethodListName("\01l_OBJC_$_");
John McCallef19dbb2012-10-17 04:53:23 +00005074 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005075 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005076 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005077 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005078 // Class methods should always be defined.
5079 Methods.push_back(GetMethodConstant(*i));
5080 }
5081 } else {
5082 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005083 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005084 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005085 // Instance methods should always be defined.
5086 Methods.push_back(GetMethodConstant(*i));
5087 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005089 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005090 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005091
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005092 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5093 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005094
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005095 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5096 if (llvm::Constant *C = GetMethodConstant(MD))
5097 Methods.push_back(C);
5098 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5099 if (llvm::Constant *C = GetMethodConstant(MD))
5100 Methods.push_back(C);
5101 }
5102 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005103 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005104 Values[ 5] = EmitMethodList(MethodListName,
5105 "__DATA, __objc_const", Methods);
5106
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005107 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5108 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005109 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005110 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005111 OID->all_referenced_protocol_begin(),
5112 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005113
John McCallef19dbb2012-10-17 04:53:23 +00005114 if (flags & NonFragileABI_Class_Meta) {
Owen Anderson0b75f232009-07-31 20:28:54 +00005115 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005116 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Anderson0b75f232009-07-31 20:28:54 +00005117 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005118 } else {
5119 Values[ 7] = EmitIvarList(ID);
5120 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005121 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5122 ID, ID->getClassInterface(), ObjCTypes);
John McCallef19dbb2012-10-17 04:53:23 +00005123 }
Owen Anderson0e0189d2009-07-27 22:29:56 +00005124 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005125 Values);
5126 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005127 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5128 llvm::GlobalValue::InternalLinkage,
5129 Init,
John McCallef19dbb2012-10-17 04:53:23 +00005130 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005131 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5132 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005133 CLASS_RO_GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005134 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005135 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005136 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005137
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005138}
5139
5140/// BuildClassMetaData - This routine defines that to-level meta-data
5141/// for the given ClassName for:
5142/// struct _class_t {
5143/// struct _class_t *isa;
5144/// struct _class_t * const superclass;
5145/// void *cache;
5146/// IMP *vtable;
5147/// struct class_ro_t *ro;
5148/// }
5149///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005150llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005151 std::string &ClassName,
5152 llvm::Constant *IsAGV,
5153 llvm::Constant *SuperClassGV,
5154 llvm::Constant *ClassRoGV,
5155 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005156 llvm::Constant *Values[] = {
5157 IsAGV,
5158 SuperClassGV,
5159 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5160 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5161 ClassRoGV // &CLASS_RO_GV
5162 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005163 if (!Values[1])
5164 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005165 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005166 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005167 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5168 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005169 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005170 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005171 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005172 if (HiddenVisibility)
5173 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005174 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005175}
5176
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005177bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005178CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005179 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005180}
5181
Daniel Dunbar961202372009-05-03 12:57:56 +00005182void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005183 uint32_t &InstanceStart,
5184 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005185 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005186 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005187
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005188 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005189 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005190
5191 // If there are no fields, the start is the same as the end.
5192 if (!RL.getFieldCount())
5193 InstanceStart = InstanceSize;
5194 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005195 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005196}
5197
Fariborz Jahanian71394042009-01-23 23:53:38 +00005198void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5199 std::string ClassName = ID->getNameAsString();
5200 if (!ObjCEmptyCacheVar) {
5201 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005202 CGM.getModule(),
5203 ObjCTypes.CacheTy,
5204 false,
5205 llvm::GlobalValue::ExternalLinkage,
5206 0,
5207 "_objc_empty_cache");
5208
Fariborz Jahanian71394042009-01-23 23:53:38 +00005209 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005210 CGM.getModule(),
5211 ObjCTypes.ImpnfABITy,
5212 false,
5213 llvm::GlobalValue::ExternalLinkage,
5214 0,
5215 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005216 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005217 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005218 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005219 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005220 uint32_t InstanceStart =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005221 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005222 uint32_t InstanceSize = InstanceStart;
John McCallef19dbb2012-10-17 04:53:23 +00005223 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005224 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5225 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005227 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005228
John McCall0d54a172012-10-17 04:53:31 +00005229 // Build the flags for the metaclass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005230 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005231 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005232 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005233 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005234
5235 // FIXME: why is this flag set on the metaclass?
5236 // ObjC metaclasses have no fields and don't really get constructed.
5237 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005238 flags |= NonFragileABI_Class_HasCXXStructors;
John McCall0d54a172012-10-17 04:53:31 +00005239 if (!ID->hasNonZeroConstructors())
5240 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5241 }
5242
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005243 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005244 // class is root
John McCallef19dbb2012-10-17 04:53:23 +00005245 flags |= NonFragileABI_Class_Root;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005246 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005247 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005248 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005249 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005250 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5251 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5252 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005253 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005254 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005255 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005256 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005257 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005258 ObjCMetaClassName +
5259 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005260 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005261 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005262 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005263 }
5264 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5265 InstanceStart,
5266 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005267 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005268 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005269 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5270 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005271 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005272
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005273 // Metadata for the class
John McCallef19dbb2012-10-17 04:53:23 +00005274 flags = 0;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005275 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005276 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005277
5278 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005279 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005280
John McCall0d54a172012-10-17 04:53:31 +00005281 // Set a flag to enable a runtime optimization when a class has
5282 // fields that require destruction but which don't require
5283 // anything except zero-initialization during construction. This
5284 // is most notably true of __strong and __weak types, but you can
5285 // also imagine there being C++ types with non-trivial default
5286 // constructors that merely set all fields to null.
5287 if (!ID->hasNonZeroConstructors())
5288 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5289 }
5290
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005291 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCallef19dbb2012-10-17 04:53:23 +00005292 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005293
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005294 if (!ID->getClassInterface()->getSuperClass()) {
John McCallef19dbb2012-10-17 04:53:23 +00005295 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005296 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005297 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005298 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005299 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005300 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005301 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005302 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005303 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005304 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005305 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005306 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005307 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005308 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005309 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005310
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005311 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005312 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005313 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5314 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005315 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005316
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005317 // Determine if this class is also "non-lazy".
5318 if (ImplementationIsNonLazy(ID))
5319 DefinedNonLazyClasses.push_back(ClassMD);
5320
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005321 // Force the definition of the EHType if necessary.
John McCallef19dbb2012-10-17 04:53:23 +00005322 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005323 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005324 // Make sure method definition entries are all clear for next implementation.
5325 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005326}
5327
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005328/// GenerateProtocolRef - This routine is called to generate code for
5329/// a protocol reference expression; as in:
5330/// @code
5331/// @protocol(Proto1);
5332/// @endcode
5333/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5334/// which will hold address of the protocol meta-data.
5335///
5336llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005337 const ObjCProtocolDecl *PD) {
5338
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005339 // This routine is called for @protocol only. So, we must build definition
5340 // of protocol's meta-data (not a reference to it!)
5341 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005342 llvm::Constant *Init =
5343 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005344 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005345
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005346 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005347 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005348
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005349 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5350 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005351 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005352 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005353 CGM.getModule(),
5354 Init->getType(), false,
5355 llvm::GlobalValue::WeakAnyLinkage,
5356 Init,
5357 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005358 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5359 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005360 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005361 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005362}
5363
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005364/// GenerateCategory - Build metadata for a category implementation.
5365/// struct _category_t {
5366/// const char * const name;
5367/// struct _class_t *const cls;
5368/// const struct _method_list_t * const instance_methods;
5369/// const struct _method_list_t * const class_methods;
5370/// const struct _protocol_list_t * const protocols;
5371/// const struct _prop_list_t * const properties;
5372/// }
5373///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005374void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005375 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005376 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005377 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5378 "_$_" + OCD->getNameAsString());
5379 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005380 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005381
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005382 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005383 Values[0] = GetClassName(OCD->getIdentifier());
5384 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005385 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005386 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005387 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5388
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005389 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005390 std::vector<llvm::Constant*> Methods;
5391 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005392 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005393 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005394
5395 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005396 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005397 // Instance methods should always be defined.
5398 Methods.push_back(GetMethodConstant(*i));
5399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005400
5401 Values[2] = EmitMethodList(MethodListName,
5402 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005403 Methods);
5404
5405 MethodListName = Prefix;
5406 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5407 OCD->getNameAsString();
5408 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005409 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005410 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005411 // Class methods should always be defined.
5412 Methods.push_back(GetMethodConstant(*i));
5413 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005414
5415 Values[3] = EmitMethodList(MethodListName,
5416 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005417 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005418 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005419 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005420 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005421 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005422 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5423 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005424 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005425 + Interface->getName() + "_$_"
5426 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005427 Category->protocol_begin(),
5428 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005429 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5430 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005431 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005432 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5433 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005434 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005435
5436 llvm::Constant *Init =
5437 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005438 Values);
5439 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005440 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005441 false,
5442 llvm::GlobalValue::InternalLinkage,
5443 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005444 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005445 GCATV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005446 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005447 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005448 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005449 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005450
5451 // Determine if this category is also "non-lazy".
5452 if (ImplementationIsNonLazy(OCD))
5453 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005454 // method definition entries must be clear for next implementation.
5455 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005456}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005457
5458/// GetMethodConstant - Return a struct objc_method constant for the
5459/// given method if it has been defined. The result is null if the
5460/// method has not been defined. The return value has type MethodPtrTy.
5461llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005462 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005463 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005464 if (!Fn)
5465 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005466
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005467 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005468 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005469 ObjCTypes.SelectorPtrTy),
5470 GetMethodVarType(MD),
5471 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5472 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005473 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005474}
5475
5476/// EmitMethodList - Build meta-data for method declarations
5477/// struct _method_list_t {
5478/// uint32_t entsize; // sizeof(struct _objc_method)
5479/// uint32_t method_count;
5480/// struct _objc_method method_list[method_count];
5481/// }
5482///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005483llvm::Constant *
5484CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5485 const char *Section,
5486 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005487 // Return null for empty list.
5488 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005489 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005490
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005491 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005492 // sizeof(struct _objc_method)
Micah Villmowdd31ca12012-10-08 16:25:52 +00005493 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005494 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005495 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005496 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005497 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005498 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005499 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005500 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005501
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005502 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005503 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005504 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005505 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005506 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005507 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005508 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005509}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005510
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005511/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5512/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005513llvm::GlobalVariable *
5514CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5515 const ObjCIvarDecl *Ivar) {
5516 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005517 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005518 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005519 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005520 CGM.getModule().getGlobalVariable(Name);
5521 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005522 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005523 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005524 false,
5525 llvm::GlobalValue::ExternalLinkage,
5526 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005527 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005528 return IvarOffsetGV;
5529}
5530
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005531llvm::Constant *
5532CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5533 const ObjCIvarDecl *Ivar,
5534 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005535 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005536 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005537 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005538 IvarOffsetGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005539 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005540
Mike Stump18bb9282009-05-16 07:57:57 +00005541 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5542 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005543 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5544 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005545 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005546 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005547 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005548 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005549 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005550 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005551}
5552
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005553/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005554/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005555/// IvarListnfABIPtrTy.
5556/// struct _ivar_t {
5557/// unsigned long int *offset; // pointer to ivar offset location
5558/// char *name;
5559/// char *type;
5560/// uint32_t alignment;
5561/// uint32_t size;
5562/// }
5563/// struct _ivar_list_t {
5564/// uint32 entsize; // sizeof(struct _ivar_t)
5565/// uint32 count;
5566/// struct _iver_t list[count];
5567/// }
5568///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005569
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005570llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005571 const ObjCImplementationDecl *ID) {
5572
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005573 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005574
Jordy Rosea91768e2011-07-22 02:08:32 +00005575 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005576 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005577
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005578 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579
Jordy Rosea91768e2011-07-22 02:08:32 +00005580 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005581 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005582 // Ignore unnamed bit-fields.
5583 if (!IVD->getDeclName())
5584 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005585 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005586 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005587 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005588 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5589 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005590 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005591 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00005592 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005593 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005594 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005595 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005596 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005597 // NOTE. Size of a bitfield does not match gcc's, because of the
5598 // way bitfields are treated special in each. But I am told that
5599 // 'size' for bitfield ivars is ignored by the runtime so it does
5600 // not matter. If it matters, there is enough info to get the
5601 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005602 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005603 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005604 }
5605 // Return null for empty list.
5606 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005607 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005608
5609 llvm::Constant *Values[3];
Micah Villmowdd31ca12012-10-08 16:25:52 +00005610 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005611 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5612 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005613 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005614 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005615 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005616 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005617 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5618 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005619 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005620 llvm::GlobalValue::InternalLinkage,
5621 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005622 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005623 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005624 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005625 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005626
Chris Lattnerf56501c2009-07-17 23:57:13 +00005627 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005628 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005629}
5630
5631llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005632 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005633 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005634
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005635 if (!Entry) {
5636 // We use the initializer as a marker of whether this is a forward
5637 // reference or not. At module finalization we add the empty
5638 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005639 Entry =
5640 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5641 llvm::GlobalValue::ExternalLinkage,
5642 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005643 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005644 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005646
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005647 return Entry;
5648}
5649
5650/// GetOrEmitProtocol - Generate the protocol meta-data:
5651/// @code
5652/// struct _protocol_t {
5653/// id isa; // NULL
5654/// const char * const protocol_name;
5655/// const struct _protocol_list_t * protocol_list; // super protocols
5656/// const struct method_list_t * const instance_methods;
5657/// const struct method_list_t * const class_methods;
5658/// const struct method_list_t *optionalInstanceMethods;
5659/// const struct method_list_t *optionalClassMethods;
5660/// const struct _prop_list_t * properties;
5661/// const uint32_t size; // sizeof(struct _protocol_t)
5662/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005663/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005664/// }
5665/// @endcode
5666///
5667
5668llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005669 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00005670 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005671
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005672 // Early exit if a defining object has already been generated.
5673 if (Entry && Entry->hasInitializer())
5674 return Entry;
5675
Douglas Gregora715bff2012-01-01 19:51:50 +00005676 // Use the protocol definition, if there is one.
5677 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5678 PD = Def;
5679
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005680 // Construct method lists.
5681 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5682 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005683 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005684 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005685 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005686 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005687 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005688 if (!C)
5689 return GetOrEmitProtocolRef(PD);
5690
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005691 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5692 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005693 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005694 } else {
5695 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005696 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005697 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005698 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699
5700 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005701 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005702 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005703 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005704 if (!C)
5705 return GetOrEmitProtocolRef(PD);
5706
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005707 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5708 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005709 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005710 } else {
5711 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005712 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005713 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005714 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005715
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005716 MethodTypesExt.insert(MethodTypesExt.end(),
5717 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5718
5719 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005720 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005721 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005722 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005723 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5724 PD->protocol_begin(),
5725 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005726
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005727 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005728 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005729 "__DATA, __objc_const",
5730 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005731 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005732 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005733 "__DATA, __objc_const",
5734 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005735 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005736 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005737 "__DATA, __objc_const",
5738 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005739 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005740 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005741 "__DATA, __objc_const",
5742 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005743 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005744 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005745 uint32_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005746 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005747 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005748 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005749 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5750 + PD->getName(),
5751 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005752 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005753 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005754
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005755 if (Entry) {
5756 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005757 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005758 Entry->setInitializer(Init);
5759 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005760 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005761 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5762 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5763 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005764 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005765 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005766 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00005767
5768 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005769 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005770 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005771 CGM.AddUsedGlobal(Entry);
5772
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005773 // Use this protocol meta-data to build protocol list table in section
5774 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005775 llvm::GlobalVariable *PTGV =
5776 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5777 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5778 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005779 PTGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005780 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005781 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005782 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005783 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005784 return Entry;
5785}
5786
5787/// EmitProtocolList - Generate protocol list meta-data:
5788/// @code
5789/// struct _protocol_list_t {
5790/// long protocol_count; // Note, this is 32/64 bit
5791/// struct _protocol_t[protocol_count];
5792/// }
5793/// @endcode
5794///
5795llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005796CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005797 ObjCProtocolDecl::protocol_iterator begin,
5798 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00005799 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005800
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005801 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005802 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005803 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005804
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005805 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005806 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005807 Name.toVector(TmpName);
5808 llvm::GlobalVariable *GV =
5809 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005810 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005811 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005812
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005813 for (; begin != end; ++begin)
5814 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5815
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005816 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005817 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005818 ObjCTypes.ProtocolnfABIPtrTy));
5819
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005820 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005821 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005822 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005823 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00005824 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5825 ProtocolRefs.size()),
5826 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005827
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005828 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005829 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005830 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005831 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005832 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005833 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005834 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005835 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005836 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005837 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005838}
5839
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005840/// GetMethodDescriptionConstant - This routine build following meta-data:
5841/// struct _objc_method {
5842/// SEL _cmd;
5843/// char *method_type;
5844/// char *_imp;
5845/// }
5846
5847llvm::Constant *
5848CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005849 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005850 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005851 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5852 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005853 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005854 if (!Desc[1])
5855 return 0;
5856
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005857 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005858 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005859 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005860}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005861
5862/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5863/// This code gen. amounts to generating code for:
5864/// @code
5865/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5866/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005867///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005868LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005869 CodeGen::CodeGenFunction &CGF,
5870 QualType ObjectTy,
5871 llvm::Value *BaseValue,
5872 const ObjCIvarDecl *Ivar,
5873 unsigned CVRQualifiers) {
5874 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005875 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5876 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5877 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5878 llvm::MDNode::get(VMContext,
5879 ArrayRef<llvm::Value*>()));
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005880 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005881 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005882}
5883
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005884llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005885 CodeGen::CodeGenFunction &CGF,
5886 const ObjCInterfaceDecl *Interface,
5887 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005888 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005889}
5890
John McCall234eac82011-05-13 23:16:18 +00005891static void appendSelectorForMessageRefTable(std::string &buffer,
5892 Selector selector) {
5893 if (selector.isUnarySelector()) {
5894 buffer += selector.getNameForSlot(0);
5895 return;
5896 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005897
John McCall234eac82011-05-13 23:16:18 +00005898 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5899 buffer += selector.getNameForSlot(i);
5900 buffer += '_';
5901 }
5902}
5903
John McCall9e8bb002011-05-14 03:10:52 +00005904/// Emit a "v-table" message send. We emit a weak hidden-visibility
5905/// struct, initially containing the selector pointer and a pointer to
5906/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5907/// load and call the function pointer, passing the address of the
5908/// struct as the second parameter. The runtime determines whether
5909/// the selector is currently emitted using vtable dispatch; if so, it
5910/// substitutes a stub function which simply tail-calls through the
5911/// appropriate vtable slot, and if not, it substitues a stub function
5912/// which tail-calls objc_msgSend. Both stubs adjust the selector
5913/// argument to correctly point to the selector.
5914RValue
5915CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5916 ReturnValueSlot returnSlot,
5917 QualType resultType,
5918 Selector selector,
5919 llvm::Value *arg0,
5920 QualType arg0Type,
5921 bool isSuper,
5922 const CallArgList &formalArgs,
5923 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005924 // Compute the actual arguments.
5925 CallArgList args;
5926
John McCall9e8bb002011-05-14 03:10:52 +00005927 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005928 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005929 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5930 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005931
John McCall9e8bb002011-05-14 03:10:52 +00005932 // Second argument: a pointer to the message ref structure. Leave
5933 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005934 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5935
5936 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5937
John McCalla729c622012-02-17 03:33:10 +00005938 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00005939
John McCall5880fb82011-05-14 21:12:11 +00005940 NullReturnState nullReturn;
5941
John McCall9e8bb002011-05-14 03:10:52 +00005942 // Find the function to call and the mangled name for the message
5943 // ref structure. Using a different mangled name wouldn't actually
5944 // be a problem; it would just be a waste.
5945 //
5946 // The runtime currently never uses vtable dispatch for anything
5947 // except normal, non-super message-sends.
5948 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005949 llvm::Constant *fn = 0;
5950 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00005951 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005952 if (isSuper) {
5953 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5954 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005955 } else {
John McCall5880fb82011-05-14 21:12:11 +00005956 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005957 fn = ObjCTypes.getMessageSendStretFixupFn();
5958 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005959 }
John McCall234eac82011-05-13 23:16:18 +00005960 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5961 fn = ObjCTypes.getMessageSendFpretFixupFn();
5962 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005963 } else {
John McCall234eac82011-05-13 23:16:18 +00005964 if (isSuper) {
5965 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5966 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005967 } else {
John McCall234eac82011-05-13 23:16:18 +00005968 fn = ObjCTypes.getMessageSendFixupFn();
5969 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005970 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005971 }
John McCall234eac82011-05-13 23:16:18 +00005972 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5973 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005974
John McCall234eac82011-05-13 23:16:18 +00005975 // Append the selector name, except use underscores anywhere we
5976 // would have used colons.
5977 appendSelectorForMessageRefTable(messageRefName, selector);
5978
5979 llvm::GlobalVariable *messageRef
5980 = CGM.getModule().getGlobalVariable(messageRefName);
5981 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005982 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005983 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005984 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005985 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5986 init->getType(),
5987 /*constant*/ false,
5988 llvm::GlobalValue::WeakAnyLinkage,
5989 init,
5990 messageRefName);
5991 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5992 messageRef->setAlignment(16);
5993 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5994 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005995
5996 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00005997 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005998 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5999 e = method->param_end(); i != e; ++i) {
6000 const ParmVarDecl *ParamDecl = (*i);
6001 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6002 if (!nullReturn.NullBB)
6003 nullReturn.init(CGF, arg0);
6004 requiresnullCheck = true;
6005 break;
6006 }
6007 }
6008
John McCall234eac82011-05-13 23:16:18 +00006009 llvm::Value *mref =
6010 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6011
John McCall9e8bb002011-05-14 03:10:52 +00006012 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00006013 args[1].RV = RValue::get(mref);
6014
6015 // Load the function to call from the message ref table.
6016 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6017 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6018
John McCalla729c622012-02-17 03:33:10 +00006019 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00006020
John McCalla729c622012-02-17 03:33:10 +00006021 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006022 return nullReturn.complete(CGF, result, resultType, formalArgs,
6023 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006024}
6025
6026/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006027CodeGen::RValue
6028CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006029 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006030 QualType ResultType,
6031 Selector Sel,
6032 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006033 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00006034 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006035 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00006036 return isVTableDispatchedSelector(Sel)
6037 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006038 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006039 false, CallArgs, Method)
6040 : EmitMessageSend(CGF, Return, ResultType,
6041 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006042 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006043 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006044}
6045
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006046llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006047CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006048 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6049
Daniel Dunbara6468342009-03-02 05:18:14 +00006050 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006051 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006052 false, llvm::GlobalValue::ExternalLinkage,
6053 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006054 }
6055
6056 return GV;
6057}
6058
John McCall31168b02011-06-15 23:02:42 +00006059llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6060 IdentifierInfo *II) {
6061 llvm::GlobalVariable *&Entry = ClassReferences[II];
6062
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006063 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00006064 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006065 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006066 Entry =
John McCall31168b02011-06-15 23:02:42 +00006067 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6068 false, llvm::GlobalValue::InternalLinkage,
6069 ClassGV,
6070 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006071 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006072 CGM.getDataLayout().getABITypeAlignment(
John McCall31168b02011-06-15 23:02:42 +00006073 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006074 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006075 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006076 }
John McCall31168b02011-06-15 23:02:42 +00006077
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006078 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006079}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006080
John McCall31168b02011-06-15 23:02:42 +00006081llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6082 const ObjCInterfaceDecl *ID) {
6083 return EmitClassRefFromId(Builder, ID->getIdentifier());
6084}
6085
6086llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6087 CGBuilderTy &Builder) {
6088 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6089 return EmitClassRefFromId(Builder, II);
6090}
6091
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006092llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006093CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006094 const ObjCInterfaceDecl *ID) {
6095 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006096
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006097 if (!Entry) {
6098 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6099 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006100 Entry =
6101 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006102 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006103 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006104 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006105 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006106 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006107 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006108 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006109 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006110 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006111
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006112 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006113}
6114
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006115/// EmitMetaClassRef - Return a Value * of the address of _class_t
6116/// meta-data
6117///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006118llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6119 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006120 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6121 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006122 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006123
Daniel Dunbar15894b72009-04-07 05:48:37 +00006124 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006125 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006126 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006127 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006128 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006129 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006130 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006131 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006132 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006133 ObjCTypes.ClassnfABIPtrTy));
6134
Daniel Dunbare60aa052009-04-15 19:03:14 +00006135 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006136 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006137
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006138 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006139}
6140
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006141/// GetClass - Return a reference to the class for the given interface
6142/// decl.
6143llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6144 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006145 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006146 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6147 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6148 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6149 }
6150
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006151 return EmitClassRef(Builder, ID);
6152}
6153
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006154/// Generates a message send where the super is the receiver. This is
6155/// a message send to self with special delivery semantics indicating
6156/// which class's method should be called.
6157CodeGen::RValue
6158CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006159 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006160 QualType ResultType,
6161 Selector Sel,
6162 const ObjCInterfaceDecl *Class,
6163 bool isCategoryImpl,
6164 llvm::Value *Receiver,
6165 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006166 const CodeGen::CallArgList &CallArgs,
6167 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006168 // ...
6169 // Create and init a super structure; this is a (receiver, class)
6170 // pair we will pass to objc_msgSendSuper.
6171 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006172 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006173
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006174 llvm::Value *ReceiverAsObject =
6175 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6176 CGF.Builder.CreateStore(ReceiverAsObject,
6177 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006178
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006179 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006180 llvm::Value *Target;
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006181 if (IsClassMessage)
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006182 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006183 else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006184 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006185
Mike Stump18bb9282009-05-16 07:57:57 +00006186 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6187 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006188 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006189 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6190 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6191 CGF.Builder.CreateStore(Target,
6192 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006193
John McCall9e8bb002011-05-14 03:10:52 +00006194 return (isVTableDispatchedSelector(Sel))
6195 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006196 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006197 true, CallArgs, Method)
6198 : EmitMessageSend(CGF, Return, ResultType,
6199 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006200 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006201 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006202}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006203
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006204llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006205 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006206 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006207
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006208 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006209 llvm::Constant *Casted =
6210 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6211 ObjCTypes.SelectorPtrTy);
6212 Entry =
6213 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6214 llvm::GlobalValue::InternalLinkage,
6215 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006216 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006217 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006218 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006219
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006220 if (lval)
6221 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006222 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6223
6224 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6225 llvm::MDNode::get(VMContext,
6226 ArrayRef<llvm::Value*>()));
6227 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006228}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006229/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006230/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006231///
6232void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006233 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006234 llvm::Value *dst,
6235 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006236 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006237 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006238 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006239 assert(Size <= 8 && "does not support size > 8");
6240 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6241 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006242 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6243 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006244 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6245 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006246 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6247 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006248 return;
6249}
6250
6251/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6252/// objc_assign_strongCast (id src, id *dst)
6253///
6254void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006255 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006256 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006257 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006258 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006259 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006260 assert(Size <= 8 && "does not support size > 8");
6261 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006262 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006263 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6264 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006265 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6266 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006267 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006268 src, dst, "weakassign");
6269 return;
6270}
6271
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006272void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006273 CodeGen::CodeGenFunction &CGF,
6274 llvm::Value *DestPtr,
6275 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006276 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006277 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6278 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006279 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006280 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006281 return;
6282}
6283
Fariborz Jahanian06292952009-02-16 22:52:32 +00006284/// EmitObjCWeakRead - Code gen for loading value of a __weak
6285/// object: objc_read_weak (id *src)
6286///
6287llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006288 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006289 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006290 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006291 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6292 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006293 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006294 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006295 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006296 return read_weak;
6297}
6298
6299/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6300/// objc_assign_weak (id src, id *dst)
6301///
6302void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006303 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006304 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006305 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006306 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006307 assert(Size <= 8 && "does not support size > 8");
6308 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6309 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006310 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6311 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006312 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6313 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006314 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006315 src, dst, "weakassign");
6316 return;
6317}
6318
6319/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6320/// objc_assign_global (id src, id *dst)
6321///
6322void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006323 llvm::Value *src, llvm::Value *dst,
6324 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006325 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006326 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006327 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006328 assert(Size <= 8 && "does not support size > 8");
6329 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6330 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006331 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6332 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006333 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6334 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006335 if (!threadlocal)
6336 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6337 src, dst, "globalassign");
6338 else
6339 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6340 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006341 return;
6342}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006343
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006344void
John McCallbd309292010-07-06 01:34:17 +00006345CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6346 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006347 EmitAtSynchronizedStmt(CGF, S,
6348 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6349 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006350}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006351
John McCall2ca705e2010-07-24 00:37:23 +00006352llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006353CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006354 // There's a particular fixed type info for 'id'.
6355 if (T->isObjCIdType() ||
6356 T->isObjCQualifiedIdType()) {
6357 llvm::Constant *IDEHType =
6358 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6359 if (!IDEHType)
6360 IDEHType =
6361 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6362 false,
6363 llvm::GlobalValue::ExternalLinkage,
6364 0, "OBJC_EHTYPE_id");
6365 return IDEHType;
6366 }
6367
6368 // All other types should be Objective-C interface pointer types.
6369 const ObjCObjectPointerType *PT =
6370 T->getAs<ObjCObjectPointerType>();
6371 assert(PT && "Invalid @catch type.");
6372 const ObjCInterfaceType *IT = PT->getInterfaceType();
6373 assert(IT && "Invalid @catch type.");
6374 return GetInterfaceEHType(IT->getDecl(), false);
6375}
6376
John McCallbd309292010-07-06 01:34:17 +00006377void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6378 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006379 EmitTryCatchStmt(CGF, S,
6380 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6381 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6382 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006383}
6384
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006385/// EmitThrowStmt - Generate code for a throw statement.
6386void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6387 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006388 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006389 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006390 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006391 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006392 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006393 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006394 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006395 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006396 }
6397
John McCall17afe452010-10-16 08:21:07 +00006398 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006399 CGF.Builder.ClearInsertionPoint();
6400}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006401
John McCall2ca705e2010-07-24 00:37:23 +00006402llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006403CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006404 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006405 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006406
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006407 // If we don't need a definition, return the entry if found or check
6408 // if we use an external reference.
6409 if (!ForDefinition) {
6410 if (Entry)
6411 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006412
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006413 // If this type (or a super class) has the __objc_exception__
6414 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006415 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006416 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006417 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006418 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006419 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006420 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006421 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006422 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006423
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006424 // Otherwise we need to either make a new entry or fill in the
6425 // initializer.
6426 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006427 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006428 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006429 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006430 CGM.getModule().getGlobalVariable(VTableName);
6431 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006432 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6433 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006434 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006435 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006436
Chris Lattnerece04092012-02-07 00:39:47 +00006437 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006438
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006439 llvm::Constant *Values[] = {
6440 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6441 GetClassName(ID->getIdentifier()),
6442 GetClassGlobal(ClassName)
6443 };
Owen Anderson170229f2009-07-14 23:10:40 +00006444 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006445 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006446
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006447 if (Entry) {
6448 Entry->setInitializer(Init);
6449 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006450 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006451 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006452 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006453 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006454 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006455 }
6456
David Blaikiebbafb8a2012-03-11 07:00:24 +00006457 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006458 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmowdd31ca12012-10-08 16:25:52 +00006459 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00006460 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006461
6462 if (ForDefinition) {
6463 Entry->setSection("__DATA,__objc_const");
6464 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6465 } else {
6466 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6467 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006468
6469 return Entry;
6470}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006471
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006472/* *** */
6473
Daniel Dunbarb036db82008-08-13 03:21:16 +00006474CodeGen::CGObjCRuntime *
6475CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00006476 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6477 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006478 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00006479
6480 case ObjCRuntime::MacOSX:
6481 case ObjCRuntime::iOS:
6482 return new CGObjCNonFragileABIMac(CGM);
6483
David Chisnallb601c962012-07-03 20:49:52 +00006484 case ObjCRuntime::GNUstep:
6485 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00006486 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00006487 llvm_unreachable("these runtimes are not Mac runtimes");
6488 }
6489 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006490}