blob: c67e3fd0028f15a29271f6ee9a8ffa9059ebfc4a [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;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +0000814 unsigned block_var_size;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000815 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +0000816 unsigned BytePos = 0, unsigned Size = 0)
817 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000818
819 // Allow sorting based on byte pos.
820 bool operator<(const RUN_SKIP &b) const {
821 return block_var_bytepos < b.block_var_bytepos;
822 }
823 };
824
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000825protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000826 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000827 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000828 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000830 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000831 SmallVector<GC_IVAR, 16> SkipIvars;
832 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahanian196f9382012-10-25 21:15:04 +0000833
834 // arc/mrr layout of captured block literal variables.
835 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000836
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000837 /// LazySymbols - Symbols to generate a lazy reference for. See
838 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000839 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000840
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000841 /// DefinedSymbols - External symbols which are defined by this
842 /// module. The symbols in this list and LazySymbols are used to add
843 /// special linker symbols which ensure that Objective-C modules are
844 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000845 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000847 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000848 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000850 /// MethodVarNames - uniqued method variable names.
851 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000853 /// DefinedCategoryNames - list of category names in form Class_Category.
854 llvm::SetVector<std::string> DefinedCategoryNames;
855
Daniel Dunbarb036db82008-08-13 03:21:16 +0000856 /// MethodVarTypes - uniqued method type signatures. We have to use
857 /// a StringMap here because have no other unique reference.
858 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000859
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000860 /// MethodDefinitions - map of methods which have been defined in
861 /// this translation unit.
862 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000863
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000864 /// PropertyNames - uniqued method variable names.
865 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000867 /// ClassReferences - uniqued class references.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000869
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000870 /// SelectorReferences - uniqued selector references.
871 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872
Daniel Dunbarb036db82008-08-13 03:21:16 +0000873 /// Protocols - Protocols for which an objc_protocol structure has
874 /// been emitted. Forward declarations are handled by creating an
875 /// empty structure whose initializer is filled in when/if defined.
876 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000877
Daniel Dunbarc475d422008-10-29 22:36:39 +0000878 /// DefinedProtocols - Protocols which have actually been
879 /// defined. We should not need this, see FIXME in GenerateProtocol.
880 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000882 /// DefinedClasses - List of defined classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000883 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000884
885 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000886 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000887
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000888 /// DefinedCategories - List of defined categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000889 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000891 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000892 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000894 /// GetNameForMethod - Return a name for the given method.
895 /// \param[out] NameOut - The return value.
896 void GetNameForMethod(const ObjCMethodDecl *OMD,
897 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000898 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000899
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000900 /// GetMethodVarName - Return a unique constant for the given
901 /// selector's name. The return value has type char *.
902 llvm::Constant *GetMethodVarName(Selector Sel);
903 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000904
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000905 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000906 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000908 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000909 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
910 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000911 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000912
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000913 /// GetPropertyName - Return a unique constant for the given
914 /// name. The return value has type char *.
915 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000916
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000917 // FIXME: This can be dropped once string functions are unified.
918 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
919 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000920
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000921 /// GetClassName - Return a unique constant for the given selector's
922 /// name. The return value has type char *.
923 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000924
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000925 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
926
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000927 /// BuildIvarLayout - Builds ivar layout bitmap for the class
928 /// implementation for the __strong or __weak case.
929 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000930 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
931 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000932
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000933 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000934
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000935 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000936 unsigned int BytePos, bool ForStrongLayout,
937 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000938 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000939 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000940 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000941 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000942 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000943 bool &HasUnion);
Fariborz Jahanian39319c42012-10-30 20:05:29 +0000944
945 void UpdateRunSkipBlockVars(bool IsByref,
946 Qualifiers::ObjCLifetime LifeTime,
947 unsigned FieldOffset,
948 unsigned FieldSize);
949
950 void BuildRCBlockVarRecordLayout(const RecordType *RT,
951 unsigned int BytePos, bool &HasUnion);
952
953 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
954 const RecordDecl *RD,
955 ArrayRef<const FieldDecl*> RecFields,
956 unsigned int BytePos, bool &HasUnion);
957
Fariborz Jahanian23290b02012-11-01 18:32:55 +0000958 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
959
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000960
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000961 /// GetIvarLayoutName - Returns a unique constant for the given
962 /// ivar layout bitmap.
963 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
964 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000965
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000966 /// EmitPropertyList - Emit the given property list. The return
967 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000968 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000969 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000970 const ObjCContainerDecl *OCD,
971 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000972
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000973 /// EmitProtocolMethodTypes - Generate the array of extended method type
974 /// strings. The return value has type Int8PtrPtrTy.
975 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000976 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000977 const ObjCCommonTypesHelper &ObjCTypes);
978
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000979 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000980 void PushProtocolProperties(
981 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
982 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
983 const Decl *Container,
984 const ObjCProtocolDecl *PROTO,
985 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000986
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000987 /// GetProtocolRef - Return a reference to the internal protocol
988 /// description, creating an empty one if it has not been
989 /// defined. The return value has type ProtocolPtrTy.
990 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000991
Daniel Dunbar30c65362009-03-09 20:09:19 +0000992 /// CreateMetadataVar - Create a global variable with internal
993 /// linkage for use by the Objective-C runtime.
994 ///
995 /// This is a convenience wrapper which not only creates the
996 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000997 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000998 ///
999 /// \param Name - The variable name.
1000 /// \param Init - The variable initializer; this is also used to
1001 /// define the type of the variable.
1002 /// \param Section - The section the variable should go into, or 0.
1003 /// \param Align - The alignment for the variable, or 0.
1004 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +00001005 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001006 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00001007 llvm::Constant *Init,
1008 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00001009 unsigned Align,
1010 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +00001011
John McCall9e8bb002011-05-14 03:10:52 +00001012 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1013 ReturnValueSlot Return,
1014 QualType ResultType,
1015 llvm::Value *Sel,
1016 llvm::Value *Arg0,
1017 QualType Arg0Ty,
1018 bool IsSuper,
1019 const CallArgList &CallArgs,
1020 const ObjCMethodDecl *OMD,
1021 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001022
Daniel Dunbar5e639272010-04-25 20:39:01 +00001023 /// EmitImageInfo - Emit the image info marker used to encode some module
1024 /// level information.
1025 void EmitImageInfo();
1026
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001027public:
Owen Andersonae86c192009-07-13 04:10:07 +00001028 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +00001029 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001030
David Chisnall481e3a82010-01-23 02:40:42 +00001031 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001032
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001033 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1034 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001035
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001036 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001037
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001038 /// GetOrEmitProtocol - Get the protocol object for the given
1039 /// declaration, emitting it if necessary. The return value has type
1040 /// ProtocolPtrTy.
1041 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001042
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001043 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1044 /// object for the given declaration, emitting it if needed. These
1045 /// forward references will be filled in with empty bodies if no
1046 /// definition is seen. The return value has type ProtocolPtrTy.
1047 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +00001048 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1049 const CGBlockInfo &blockInfo);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001050 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1051 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001052
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001053};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001054
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001055class CGObjCMac : public CGObjCCommonMac {
1056private:
1057 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001058
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001059 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001060 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001061 void EmitModuleInfo();
1062
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001063 /// EmitModuleSymols - Emit module symbols, the list of defined
1064 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001065 llvm::Constant *EmitModuleSymbols();
1066
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001067 /// FinishModule - Write out global data structures at the end of
1068 /// processing a translation unit.
1069 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001070
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001071 /// EmitClassExtension - Generate the class extension structure used
1072 /// to store the weak ivar layout and properties. The return value
1073 /// has type ClassExtensionPtrTy.
1074 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1075
1076 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1077 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001078 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001079 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001080
John McCall31168b02011-06-15 23:02:42 +00001081 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1082 IdentifierInfo *II);
1083
1084 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1085
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001086 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1087 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001088
1089 /// EmitIvarList - Emit the ivar list for the given
1090 /// implementation. If ForClass is true the list of class ivars
1091 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1092 /// interface ivars will be emitted. The return value has type
1093 /// IvarListPtrTy.
1094 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001095 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001096
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001097 /// EmitMetaClass - Emit a forward reference to the class structure
1098 /// for the metaclass of the given interface. The return value has
1099 /// type ClassPtrTy.
1100 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1101
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001102 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001103 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001104 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1105 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001106 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001107
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001108 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001109
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001110 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001111
1112 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001113 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001114 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001115 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001116 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001117
1118 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001119 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001120 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001121 /// - IsProtocol: True iff these methods are for a protocol.
1122 /// - ClassMethds: True iff these are class methods.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001123 /// - Required: When true, only "required" methods are
1124 /// listed. Similarly, when false only "optional" methods are
1125 /// listed. For classes this should always be true.
1126 /// - begin, end: The method list to output.
1127 ///
1128 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001129 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001130 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001131 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001132
Daniel Dunbarc475d422008-10-29 22:36:39 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001137
1138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001143
Daniel Dunbarb036db82008-08-13 03:21:16 +00001144 /// EmitProtocolExtension - Generate the protocol extension
1145 /// structure used to store optional instance and class methods, and
1146 /// protocol properties. The return value has type
1147 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001148 llvm::Constant *
1149 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001150 ArrayRef<llvm::Constant*> OptInstanceMethods,
1151 ArrayRef<llvm::Constant*> OptClassMethods,
1152 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001153
1154 /// EmitProtocolList - Generate the list of referenced
1155 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001156 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001157 ObjCProtocolDecl::protocol_iterator begin,
1158 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001159
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001160 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1161 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001162 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1163 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001164
1165public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001166 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001167
Fariborz Jahanian71394042009-01-23 23:53:38 +00001168 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001169
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001170 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001171 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001172 QualType ResultType,
1173 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001174 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001175 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001176 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001177 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001178
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001179 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001180 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001181 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001182 QualType ResultType,
1183 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001184 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001185 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001186 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001187 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001188 const CallArgList &CallArgs,
1189 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001190
Daniel Dunbarcb463852008-11-01 01:53:16 +00001191 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001192 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001193
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001194 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1195 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001196
1197 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1198 /// untyped one.
1199 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1200 const ObjCMethodDecl *Method);
1201
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001202 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001203
Daniel Dunbar92992502008-08-15 22:20:32 +00001204 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001205
Daniel Dunbar92992502008-08-15 22:20:32 +00001206 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001207
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001208 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001209
Daniel Dunbarcb463852008-11-01 01:53:16 +00001210 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001211 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212
Chris Lattnerd4808922009-03-22 21:03:39 +00001213 virtual llvm::Constant *GetPropertyGetFunction();
1214 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001215 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1216 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001217 virtual llvm::Constant *GetGetStructFunction();
1218 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001219 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001220 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001221
John McCallbd309292010-07-06 01:34:17 +00001222 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1223 const ObjCAtTryStmt &S);
1224 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1225 const ObjCAtSynchronizedStmt &S);
1226 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001227 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1228 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001229 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001230 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001231 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001232 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001233 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001234 llvm::Value *src, llvm::Value *dest,
1235 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001236 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001237 llvm::Value *src, llvm::Value *dest,
1238 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001239 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1240 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001241 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1242 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001243 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001244
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001245 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1246 QualType ObjectTy,
1247 llvm::Value *BaseValue,
1248 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001249 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001250 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001251 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001252 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001253
1254 /// GetClassGlobal - Return the global variable for the Objective-C
1255 /// class of the given name.
1256 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001257 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001258 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001259};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001260
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001261class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001262private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001263 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001264 llvm::GlobalVariable* ObjCEmptyCacheVar;
1265 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001266
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001267 /// SuperClassReferences - uniqued super class references.
1268 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001269
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001270 /// MetaClassReferences - uniqued meta class references.
1271 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001272
1273 /// EHTypeReferences - uniqued class ehtype references.
1274 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001275
John McCall9e8bb002011-05-14 03:10:52 +00001276 /// VTableDispatchMethods - List of methods for which we generate
1277 /// vtable-based message dispatch.
1278 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Fariborz Jahanian67260552009-11-17 21:37:35 +00001280 /// DefinedMetaClasses - List of defined meta-classes.
1281 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1282
John McCall9e8bb002011-05-14 03:10:52 +00001283 /// isVTableDispatchedSelector - Returns true if SEL is a
1284 /// vtable-based selector.
1285 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Fariborz Jahanian71394042009-01-23 23:53:38 +00001287 /// FinishNonFragileABIModule - Write out global data structures at the end of
1288 /// processing a translation unit.
1289 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001290
Daniel Dunbar19573e72009-05-15 21:48:48 +00001291 /// AddModuleClassList - Add the given list of class pointers to the
1292 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001293 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001294 const char *SymbolName,
1295 const char *SectionName);
1296
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001297 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1298 unsigned InstanceStart,
1299 unsigned InstanceSize,
1300 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001301 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001303 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001304 llvm::Constant *ClassRoGV,
1305 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001307 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001309 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001311 /// EmitMethodList - Emit the method list for the given
1312 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001313 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001314 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001315 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001316 /// EmitIvarList - Emit the ivar list for the given
1317 /// implementation. If ForClass is true the list of class ivars
1318 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1319 /// interface ivars will be emitted. The return value has type
1320 /// IvarListnfABIPtrTy.
1321 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001323 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001324 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001325 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001326
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001327 /// GetOrEmitProtocol - Get the protocol object for the given
1328 /// declaration, emitting it if necessary. The return value has type
1329 /// ProtocolPtrTy.
1330 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001331
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001332 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1333 /// object for the given declaration, emitting it if needed. These
1334 /// forward references will be filled in with empty bodies if no
1335 /// definition is seen. The return value has type ProtocolPtrTy.
1336 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001337
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001338 /// EmitProtocolList - Generate the list of referenced
1339 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001340 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001341 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001342 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001343
John McCall9e8bb002011-05-14 03:10:52 +00001344 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1345 ReturnValueSlot Return,
1346 QualType ResultType,
1347 Selector Sel,
1348 llvm::Value *Receiver,
1349 QualType Arg0Ty,
1350 bool IsSuper,
1351 const CallArgList &CallArgs,
1352 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001353
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001354 /// GetClassGlobal - Return the global variable for the Objective-C
1355 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001356 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001357
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001358 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001359 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001360 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001361 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001362
1363 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1364 IdentifierInfo *II);
1365
1366 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001367
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001368 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1369 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001370 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1371 const ObjCInterfaceDecl *ID);
1372
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001373 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1374 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001375 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001376 const ObjCInterfaceDecl *ID);
1377
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001378 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1379 /// the given ivar.
1380 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001381 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001382 const ObjCInterfaceDecl *ID,
1383 const ObjCIvarDecl *Ivar);
1384
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001385 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1386 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001387 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1388 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001389
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001390 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001391 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001392 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001393 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001394
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001395 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001396 return "OBJC_METACLASS_$_";
1397 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001398
Daniel Dunbar15894b72009-04-07 05:48:37 +00001399 const char *getClassSymbolPrefix() const {
1400 return "OBJC_CLASS_$_";
1401 }
1402
Daniel Dunbar961202372009-05-03 12:57:56 +00001403 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001404 uint32_t &InstanceStart,
1405 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001406
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001407 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001408 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001409 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1410 return CGM.getContext().Selectors.getSelector(0, &II);
1411 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001412
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001413 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001414 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1415 return CGM.getContext().Selectors.getSelector(1, &II);
1416 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001417
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001418 /// ImplementationIsNonLazy - Check whether the given category or
1419 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001420 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001421
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001422public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001423 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001424 // FIXME. All stubs for now!
1425 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001426
Fariborz Jahanian71394042009-01-23 23:53:38 +00001427 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001428 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001429 QualType ResultType,
1430 Selector Sel,
1431 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001432 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001433 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001434 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001435
1436 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001437 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001438 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001439 QualType ResultType,
1440 Selector Sel,
1441 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001442 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001443 llvm::Value *Receiver,
1444 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001445 const CallArgList &CallArgs,
1446 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001447
Fariborz Jahanian71394042009-01-23 23:53:38 +00001448 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001449 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001450
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001451 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1452 bool lvalue = false)
1453 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001454
1455 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1456 /// untyped one.
1457 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1458 const ObjCMethodDecl *Method)
1459 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001460
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001461 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001462
Fariborz Jahanian71394042009-01-23 23:53:38 +00001463 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001464
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001465 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001466
Fariborz Jahanian71394042009-01-23 23:53:38 +00001467 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001468 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001469
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001470 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001471
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001472 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001473 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001474 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001475 virtual llvm::Constant *GetPropertySetFunction() {
1476 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001477 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001478
Ted Kremeneke65b0862012-03-06 20:05:56 +00001479 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1480 bool copy) {
1481 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1482 }
1483
David Chisnall168b80f2010-12-26 22:13:16 +00001484 virtual llvm::Constant *GetSetStructFunction() {
1485 return ObjCTypes.getCopyStructFn();
1486 }
1487 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001488 return ObjCTypes.getCopyStructFn();
1489 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001490 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1491 return ObjCTypes.getCppAtomicObjectFunction();
1492 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001493
Chris Lattnerd4808922009-03-22 21:03:39 +00001494 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001495 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001496 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001497
John McCallbd309292010-07-06 01:34:17 +00001498 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1499 const ObjCAtTryStmt &S);
1500 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1501 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001502 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001503 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001504 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001505 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001506 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001507 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001508 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001509 llvm::Value *src, llvm::Value *dest,
1510 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001511 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001512 llvm::Value *src, llvm::Value *dest,
1513 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001514 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001515 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001516 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1517 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001518 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001519 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1520 QualType ObjectTy,
1521 llvm::Value *BaseValue,
1522 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001523 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001524 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001525 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001526 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001527};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001528
1529/// A helper class for performing the null-initialization of a return
1530/// value.
1531struct NullReturnState {
1532 llvm::BasicBlock *NullBB;
1533 llvm::BasicBlock *callBB;
1534 NullReturnState() : NullBB(0), callBB(0) {}
1535
1536 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1537 // Make blocks for the null-init and call edges.
1538 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1539 callBB = CGF.createBasicBlock("msgSend.call");
1540
1541 // Check for a null receiver and, if there is one, jump to the
1542 // null-init test.
1543 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1544 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1545
1546 // Otherwise, start performing the call.
1547 CGF.EmitBlock(callBB);
1548 }
1549
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001550 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1551 const CallArgList &CallArgs,
1552 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001553 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001554
1555 llvm::Value *NullInitPtr = 0;
1556 if (result.isScalar() && !resultType->isVoidType()) {
1557 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1558 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1559 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001560
1561 // Finish the call path.
1562 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1563 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1564
1565 // Emit the null-init block and perform the null-initialization there.
1566 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001567
1568 // Release consumed arguments along the null-receiver path.
1569 if (Method) {
1570 CallArgList::const_iterator I = CallArgs.begin();
1571 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1572 e = Method->param_end(); i != e; ++i, ++I) {
1573 const ParmVarDecl *ParamDecl = (*i);
1574 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1575 RValue RV = I->RV;
1576 assert(RV.isScalar() &&
1577 "NullReturnState::complete - arg not on object");
1578 CGF.EmitARCRelease(RV.getScalarVal(), true);
1579 }
1580 }
1581 }
1582
1583 if (result.isScalar()) {
1584 if (NullInitPtr)
1585 CGF.EmitNullInitialization(NullInitPtr, resultType);
1586 // Jump to the continuation block.
1587 CGF.EmitBlock(contBB);
1588 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1589 : result;
1590 }
1591
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001592 if (!resultType->isAnyComplexType()) {
1593 assert(result.isAggregate() && "null init of non-aggregate result?");
1594 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1595 // Jump to the continuation block.
1596 CGF.EmitBlock(contBB);
1597 return result;
1598 }
1599
1600 // _Complex type
1601 // FIXME. Now easy to handle any other scalar type whose result is returned
1602 // in memory due to ABI limitations.
1603 CGF.EmitBlock(contBB);
1604 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1605 llvm::Type *MemberType = CallCV.first->getType();
1606 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1607 // Create phi instruction for scalar complex value.
1608 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1609 PHIReal->addIncoming(ZeroCV, NullBB);
1610 PHIReal->addIncoming(CallCV.first, callBB);
1611 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1612 PHIImag->addIncoming(ZeroCV, NullBB);
1613 PHIImag->addIncoming(CallCV.second, callBB);
1614 return RValue::getComplex(PHIReal, PHIImag);
1615 }
1616};
1617
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001618} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001619
1620/* *** Helper Functions *** */
1621
1622/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001623static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001624 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001625 unsigned idx0,
1626 unsigned idx1) {
1627 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001628 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1629 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001630 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001631 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001632}
1633
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001634/// hasObjCExceptionAttribute - Return true if this class or any super
1635/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001636static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001637 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001638 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001639 return true;
1640 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001641 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001642 return false;
1643}
1644
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001645/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001646
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001647CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001648 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001649 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001650 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001651}
1652
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001653/// GetClass - Return a reference to the class for the given interface
1654/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001655llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001656 const ObjCInterfaceDecl *ID) {
1657 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001658}
1659
1660/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001661llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1662 bool lval) {
1663 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001664}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001665llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001666 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001667 return EmitSelector(Builder, Method->getSelector());
1668}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001669
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001670llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001671 if (T->isObjCIdType() ||
1672 T->isObjCQualifiedIdType()) {
1673 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001674 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001675 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001676 if (T->isObjCClassType() ||
1677 T->isObjCQualifiedClassType()) {
1678 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001679 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001680 }
1681 if (T->isObjCObjectPointerType())
1682 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1683
John McCall2ca705e2010-07-24 00:37:23 +00001684 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001685}
1686
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001687/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001688/*
1689 struct __builtin_CFString {
1690 const int *isa; // point to __CFConstantStringClassReference
1691 int flags;
1692 const char *str;
1693 long length;
1694 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001695*/
1696
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001697/// or Generate a constant NSString object.
1698/*
1699 struct __builtin_NSString {
1700 const int *isa; // point to __NSConstantStringClassReference
1701 const char *str;
1702 unsigned int length;
1703 };
1704*/
1705
Fariborz Jahanian71394042009-01-23 23:53:38 +00001706llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001707 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001708 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001709 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001710 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001711}
1712
Ted Kremeneke65b0862012-03-06 20:05:56 +00001713enum {
1714 kCFTaggedObjectID_Integer = (1 << 1) + 1
1715};
1716
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001717/// Generates a message send where the super is the receiver. This is
1718/// a message send to self with special delivery semantics indicating
1719/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001720CodeGen::RValue
1721CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001722 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001723 QualType ResultType,
1724 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001725 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001726 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001727 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001728 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001729 const CodeGen::CallArgList &CallArgs,
1730 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001731 // Create and init a super structure; this is a (receiver, class)
1732 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001733 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001734 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001735 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001736 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001737 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001738 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001739
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001740 // If this is a class message the metaclass is passed as the target.
1741 llvm::Value *Target;
1742 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001743 if (isCategoryImpl) {
1744 // Message sent to 'super' in a class method defined in a category
1745 // implementation requires an odd treatment.
1746 // If we are in a class method, we must retrieve the
1747 // _metaclass_ for the current class, pointed at by
1748 // the class's "isa" pointer. The following assumes that
1749 // isa" is the first ivar in a class (which it must be).
1750 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1751 Target = CGF.Builder.CreateStructGEP(Target, 0);
1752 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001753 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001754 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1755 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1756 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1757 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001758 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001759 }
1760 else if (isCategoryImpl)
1761 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1762 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001763 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1764 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1765 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001766 }
Mike Stump18bb9282009-05-16 07:57:57 +00001767 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1768 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001769 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001770 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001771 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001772 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001773 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001774 return EmitMessageSend(CGF, Return, ResultType,
1775 EmitSelector(CGF.Builder, Sel),
1776 ObjCSuper, ObjCTypes.SuperPtrCTy,
1777 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001778}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001779
1780/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001781CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001782 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001783 QualType ResultType,
1784 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001785 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001786 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001787 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001788 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001789 return EmitMessageSend(CGF, Return, ResultType,
1790 EmitSelector(CGF.Builder, Sel),
1791 Receiver, CGF.getContext().getObjCIdType(),
1792 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001793}
1794
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001795CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001796CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1797 ReturnValueSlot Return,
1798 QualType ResultType,
1799 llvm::Value *Sel,
1800 llvm::Value *Arg0,
1801 QualType Arg0Ty,
1802 bool IsSuper,
1803 const CallArgList &CallArgs,
1804 const ObjCMethodDecl *Method,
1805 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001806 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001807 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001808 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001809 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1810 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001811 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812
John McCalla729c622012-02-17 03:33:10 +00001813 // If we're calling a method, use the formal signature.
1814 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001815
Anders Carlsson280e61f12010-06-21 20:59:55 +00001816 if (Method)
1817 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1818 CGM.getContext().getCanonicalType(ResultType) &&
1819 "Result type mismatch!");
1820
John McCall5880fb82011-05-14 21:12:11 +00001821 NullReturnState nullReturn;
1822
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001823 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001824 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001825 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001826 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001827 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001828 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1829 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1830 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001831 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1832 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1833 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001834 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001835 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001836 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001837 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001838
1839 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001840 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001841 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1842 e = Method->param_end(); i != e; ++i) {
1843 const ParmVarDecl *ParamDecl = (*i);
1844 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1845 if (!nullReturn.NullBB)
1846 nullReturn.init(CGF, Arg0);
1847 requiresnullCheck = true;
1848 break;
1849 }
1850 }
1851
John McCalla729c622012-02-17 03:33:10 +00001852 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1853 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001854 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1855 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001856}
1857
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001858static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1859 if (FQT.isObjCGCStrong())
1860 return Qualifiers::Strong;
1861
John McCall31168b02011-06-15 23:02:42 +00001862 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001863 return Qualifiers::Weak;
1864
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001865 // check for __unsafe_unretained
1866 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1867 return Qualifiers::GCNone;
1868
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001869 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1870 return Qualifiers::Strong;
1871
1872 if (const PointerType *PT = FQT->getAs<PointerType>())
1873 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1874
1875 return Qualifiers::GCNone;
1876}
1877
John McCall351762c2011-02-07 10:33:21 +00001878llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1879 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00001880
Chris Lattnerece04092012-02-07 00:39:47 +00001881 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001882 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1883 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001884 return nullPtr;
1885
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001886 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001887 SkipIvars.clear();
1888 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001889 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1890 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001891
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001892 // __isa is the first field in block descriptor and must assume by runtime's
1893 // convention that it is GC'able.
1894 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001895
1896 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1897
1898 // Calculate the basic layout of the block structure.
1899 const llvm::StructLayout *layout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001900 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall351762c2011-02-07 10:33:21 +00001901
1902 // Ignore the optional 'this' capture: C++ objects are not assumed
1903 // to be GC'ed.
1904
1905 // Walk the captured variables.
1906 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1907 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1908 const VarDecl *variable = ci->getVariable();
1909 QualType type = variable->getType();
1910
1911 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1912
1913 // Ignore constant captures.
1914 if (capture.isConstant()) continue;
1915
1916 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1917
1918 // __block variables are passed by their descriptor address.
1919 if (ci->isByRef()) {
1920 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001921 continue;
John McCall351762c2011-02-07 10:33:21 +00001922 }
1923
1924 assert(!type->isArrayType() && "array variable should not be caught");
1925 if (const RecordType *record = type->getAs<RecordType>()) {
1926 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001927 continue;
1928 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001929
John McCall351762c2011-02-07 10:33:21 +00001930 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1931 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1932
1933 if (GCAttr == Qualifiers::Strong)
1934 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1935 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001936 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001937 SkipIvars.push_back(GC_IVAR(fieldOffset,
1938 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001939 }
1940
1941 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001942 return nullPtr;
1943
1944 // Sort on byte position; captures might not be allocated in order,
1945 // and unions can do funny things.
1946 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1947 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001948
1949 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001950 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001951 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001952 printf("\n block variable layout for block: ");
Roman Divackye6377112012-09-06 15:59:27 +00001953 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00001954 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001955 if (!(s[i] & 0xf0))
1956 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1957 else
1958 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1959 printf("\n");
1960 }
1961
1962 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001963}
1964
Fariborz Jahanian39319c42012-10-30 20:05:29 +00001965void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1966 Qualifiers::ObjCLifetime LifeTime,
1967 unsigned FieldOffset,
1968 unsigned FieldSize) {
1969 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
1970 unsigned FieldSizeInBytes = FieldSize/ByteSizeInBits;
1971
1972 // __block variables are passed by their descriptor address.
1973 if (IsByref)
1974 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
1975 FieldSizeInBytes));
1976 else if (LifeTime == Qualifiers::OCL_Strong)
1977 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
1978 FieldSizeInBytes));
1979 else if (LifeTime == Qualifiers::OCL_Weak)
1980 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
1981 FieldSizeInBytes));
1982 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
1983 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
1984 FieldSizeInBytes));
1985 else
1986 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
1987 FieldOffset,
1988 FieldSizeInBytes));
1989}
1990
1991void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
1992 const RecordDecl *RD,
1993 ArrayRef<const FieldDecl*> RecFields,
1994 unsigned int BytePos, bool &HasUnion) {
1995 bool IsUnion = (RD && RD->isUnion());
1996 uint64_t MaxUnionSize = 0;
1997 const FieldDecl *MaxField = 0;
1998 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
1999 uint64_t MaxFieldOffset = 0;
2000 uint64_t LastBitfieldOrUnnamedOffset = 0;
2001
2002 if (RecFields.empty())
2003 return;
2004 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2005
2006 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2007 const FieldDecl *Field = RecFields[i];
2008 uint64_t FieldOffset;
2009 // Note that 'i' here is actually the field index inside RD of Field,
2010 // although this dependency is hidden.
2011 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2012 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits);
2013
2014 // Skip over unnamed or bitfields
2015 if (!Field->getIdentifier() || Field->isBitField()) {
2016 LastFieldBitfieldOrUnnamed = Field;
2017 LastBitfieldOrUnnamedOffset = FieldOffset;
2018 continue;
2019 }
2020
2021 LastFieldBitfieldOrUnnamed = 0;
2022 QualType FQT = Field->getType();
2023 if (FQT->isRecordType() || FQT->isUnionType()) {
2024 if (FQT->isUnionType())
2025 HasUnion = true;
2026
2027 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2028 BytePos + FieldOffset, HasUnion);
2029 continue;
2030 }
2031
2032 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2033 const ConstantArrayType *CArray =
2034 dyn_cast_or_null<ConstantArrayType>(Array);
2035 uint64_t ElCount = CArray->getSize().getZExtValue();
2036 assert(CArray && "only array with known element size is supported");
2037 FQT = CArray->getElementType();
2038 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2039 const ConstantArrayType *CArray =
2040 dyn_cast_or_null<ConstantArrayType>(Array);
2041 ElCount *= CArray->getSize().getZExtValue();
2042 FQT = CArray->getElementType();
2043 }
2044
2045 assert(!FQT->isUnionType() &&
2046 "layout for array of unions not supported");
2047 if (FQT->isRecordType() && ElCount) {
2048 int OldIndex = RunSkipBlockVars.size() - 1;
2049 const RecordType *RT = FQT->getAs<RecordType>();
2050 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2051 HasUnion);
2052
2053 // Replicate layout information for each array element. Note that
2054 // one element is already done.
2055 uint64_t ElIx = 1;
2056 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
2057 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
2058 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2059 RunSkipBlockVars.push_back(
2060 RUN_SKIP(RunSkipBlockVars[i].opcode,
2061 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2062 RunSkipBlockVars[i].block_var_size));
2063 }
2064 continue;
2065 }
2066 }
2067 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
2068 if (IsUnion) {
2069 uint64_t UnionIvarSize = FieldSize;
2070 if (UnionIvarSize > MaxUnionSize) {
2071 MaxUnionSize = UnionIvarSize;
2072 MaxField = Field;
2073 MaxFieldOffset = FieldOffset;
2074 }
2075 } else {
2076 UpdateRunSkipBlockVars(false,
2077 Field->getType().getObjCLifetime(),
2078 BytePos + FieldOffset,
2079 FieldSize);
2080 }
2081 }
2082
2083 if (LastFieldBitfieldOrUnnamed) {
2084 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2085 // Last field was a bitfield. Must update the info.
2086 uint64_t BitFieldSize
2087 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
2088 unsigned Size = (BitFieldSize / ByteSizeInBits) +
2089 ((BitFieldSize % ByteSizeInBits) != 0);
2090 Size += LastBitfieldOrUnnamedOffset;
2091 UpdateRunSkipBlockVars(false,
2092 LastFieldBitfieldOrUnnamed->getType().getObjCLifetime(),
2093 BytePos + LastBitfieldOrUnnamedOffset,
2094 Size*ByteSizeInBits);
2095 } else {
2096 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2097 // Last field was unnamed. Must update skip info.
2098 unsigned FieldSize
2099 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
2100 UpdateRunSkipBlockVars(false,
2101 LastFieldBitfieldOrUnnamed->getType().getObjCLifetime(),
2102 BytePos + LastBitfieldOrUnnamedOffset,
2103 FieldSize);
2104 }
2105 }
2106
2107 if (MaxField)
2108 UpdateRunSkipBlockVars(false,
2109 MaxField->getType().getObjCLifetime(),
2110 BytePos + MaxFieldOffset,
2111 MaxUnionSize);
2112}
2113
2114void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
2115 unsigned int BytePos,
2116 bool &HasUnion) {
2117 const RecordDecl *RD = RT->getDecl();
2118 SmallVector<const FieldDecl*, 16> Fields;
2119 for (RecordDecl::field_iterator i = RD->field_begin(),
2120 e = RD->field_end(); i != e; ++i)
2121 Fields.push_back(*i);
2122 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2123 const llvm::StructLayout *RecLayout =
2124 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2125
2126 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion);
2127}
2128
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002129/// InlineLayoutInstruction - This routine produce an inline instruction for the
2130/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2131/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2132/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2133/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2134/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2135/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2136/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2137/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2138uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2139 SmallVectorImpl<unsigned char> &Layout) {
2140 uint64_t Result = 0;
2141 if (Layout.size() <= 3) {
2142 unsigned size = Layout.size();
2143 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2144 unsigned char inst;
2145 enum BLOCK_LAYOUT_OPCODE opcode ;
2146 switch (size) {
2147 case 3:
2148 inst = Layout[0];
2149 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2150 if (opcode == BLOCK_LAYOUT_STRONG)
2151 strong_word_count = (inst & 0xF)+1;
2152 else
2153 return 0;
2154 inst = Layout[1];
2155 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2156 if (opcode == BLOCK_LAYOUT_BYREF)
2157 byref_word_count = (inst & 0xF)+1;
2158 else
2159 return 0;
2160 inst = Layout[2];
2161 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2162 if (opcode == BLOCK_LAYOUT_WEAK)
2163 weak_word_count = (inst & 0xF)+1;
2164 else
2165 return 0;
2166 break;
2167
2168 case 2:
2169 inst = Layout[0];
2170 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2171 if (opcode == BLOCK_LAYOUT_STRONG) {
2172 strong_word_count = (inst & 0xF)+1;
2173 inst = Layout[1];
2174 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2175 if (opcode == BLOCK_LAYOUT_BYREF)
2176 byref_word_count = (inst & 0xF)+1;
2177 else if (opcode == BLOCK_LAYOUT_WEAK)
2178 weak_word_count = (inst & 0xF)+1;
2179 else
2180 return 0;
2181 }
2182 else if (opcode == BLOCK_LAYOUT_BYREF) {
2183 byref_word_count = (inst & 0xF)+1;
2184 inst = Layout[1];
2185 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2186 if (opcode == BLOCK_LAYOUT_WEAK)
2187 weak_word_count = (inst & 0xF)+1;
2188 else
2189 return 0;
2190 }
2191 else
2192 return 0;
2193 break;
2194
2195 case 1:
2196 inst = Layout[0];
2197 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2198 if (opcode == BLOCK_LAYOUT_STRONG)
2199 strong_word_count = (inst & 0xF)+1;
2200 else if (opcode == BLOCK_LAYOUT_BYREF)
2201 byref_word_count = (inst & 0xF)+1;
2202 else if (opcode == BLOCK_LAYOUT_WEAK)
2203 weak_word_count = (inst & 0xF)+1;
2204 else
2205 return 0;
2206 break;
2207
2208 default:
2209 return 0;
2210 }
2211
2212 // Cannot inline when any of the word counts is 15. Because this is one less
2213 // than the actual work count (so 15 means 16 actual word counts),
2214 // and we can only display 0 thru 15 word counts.
2215 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2216 return 0;
2217
2218 unsigned count =
2219 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2220
2221 if (size == count) {
2222 if (strong_word_count)
2223 Result = strong_word_count;
2224 Result <<= 4;
2225 if (byref_word_count)
2226 Result += byref_word_count;
2227 Result <<= 4;
2228 if (weak_word_count)
2229 Result += weak_word_count;
2230 }
2231 }
2232 return Result;
2233}
2234
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002235llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2236 const CGBlockInfo &blockInfo) {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002237 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2238
2239 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2240
2241 RunSkipBlockVars.clear();
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002242 bool hasUnion = false;
2243
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002244 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2245 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2246 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2247
2248 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2249
2250 // Calculate the basic layout of the block structure.
2251 const llvm::StructLayout *layout =
2252 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2253
2254 // Ignore the optional 'this' capture: C++ objects are not assumed
2255 // to be GC'ed.
2256
2257 // Walk the captured variables.
2258 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2259 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2260 const VarDecl *variable = ci->getVariable();
2261 QualType type = variable->getType();
2262
2263 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2264
2265 // Ignore constant captures.
2266 if (capture.isConstant()) continue;
2267
2268 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
2269
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002270 assert(!type->isArrayType() && "array variable should not be caught");
2271 if (const RecordType *record = type->getAs<RecordType>()) {
2272 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002273 continue;
2274 }
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002275 unsigned fieldSize = ci->isByRef() ? WordSizeInBits
2276 : CGM.getContext().getTypeSize(type);
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002277 UpdateRunSkipBlockVars(ci->isByRef(), type.getObjCLifetime(),
2278 fieldOffset, fieldSize);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002279 }
2280
2281 if (RunSkipBlockVars.empty())
2282 return nullPtr;
2283
2284 // Sort on byte position; captures might not be allocated in order,
2285 // and unions can do funny things.
2286 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002287 SmallVector<unsigned char, 16> Layout;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002288
2289 unsigned size = RunSkipBlockVars.size();
2290 unsigned int shift = (WordSizeInBytes == 8) ? 3 : 2;
2291 unsigned int mask = (WordSizeInBytes == 8) ? 0x7 : 0x3;
2292 for (unsigned i = 0; i < size; i++) {
2293 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2294 unsigned start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2295 unsigned end_byte_pos = start_byte_pos;
2296 unsigned j = i+1;
2297 while (j < size) {
2298 if (opcode == RunSkipBlockVars[j].opcode) {
2299 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2300 i++;
2301 }
2302 else
2303 break;
2304 }
2305 unsigned size_in_bytes =
2306 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
Fariborz Jahanian39319c42012-10-30 20:05:29 +00002307 if (j < size) {
2308 unsigned gap =
2309 RunSkipBlockVars[j].block_var_bytepos -
2310 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2311 size_in_bytes += gap;
2312 }
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002313 unsigned residue_in_bytes = 0;
2314 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2315 residue_in_bytes = size_in_bytes & mask;
2316 size_in_bytes -= residue_in_bytes;
2317 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2318 }
2319
2320 unsigned size_in_words = size_in_bytes >> shift;
2321 while (size_in_words >= 16) {
2322 // Note that value in imm. is one less that the actual
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002323 // value. So, 0xf means 16 words follow!
2324 unsigned char inst = (opcode << 4) | 0xf;
2325 Layout.push_back(inst);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002326 size_in_words -= 16;
2327 }
2328 if (size_in_words > 0) {
2329 // Note that value in imm. is one less that the actual
2330 // value. So, we subtract 1 away!
2331 unsigned char inst = (opcode << 4) | (size_in_words-1);
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002332 Layout.push_back(inst);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002333 }
2334 if (residue_in_bytes > 0) {
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002335 unsigned char inst =
2336 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes-1);
2337 Layout.push_back(inst);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002338 }
2339 }
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002340
2341 int e = Layout.size()-1;
2342 while (e >= 0) {
2343 unsigned char inst = Layout[e--];
2344 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2345 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2346 Layout.pop_back();
2347 else
2348 break;
2349 }
2350
2351 uint64_t Result = InlineLayoutInstruction(Layout);
2352 if (Result != 0) {
2353 // Block variable layout instruction has been inlined.
2354 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2355 printf("\n Inline instruction for block variable layout: ");
2356 printf("0x0%llx\n", Result);
2357 }
2358 if (WordSizeInBytes == 8) {
2359 const llvm::APInt Instruction(64, Result);
2360 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2361 }
2362 else {
2363 const llvm::APInt Instruction(32, Result);
2364 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2365 }
2366 }
2367
2368 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2369 Layout.push_back(inst);
2370 std::string BitMap;
2371 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2372 BitMap += Layout[i];
2373
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002374 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2375 printf("\n block variable layout: ");
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002376 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2377 unsigned char inst = BitMap[i];
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002378 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2379 unsigned delta = 1;
2380 switch (opcode) {
2381 case BLOCK_LAYOUT_OPERATOR:
2382 printf("BL_OPERATOR:");
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002383 delta = 0;
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002384 break;
2385 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2386 printf("BL_NON_OBJECT_BYTES:");
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002387 break;
2388 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2389 printf("BL_NON_OBJECT_WORD:");
2390 break;
2391 case BLOCK_LAYOUT_STRONG:
2392 printf("BL_STRONG:");
2393 break;
2394 case BLOCK_LAYOUT_BYREF:
2395 printf("BL_BYREF:");
2396 break;
2397 case BLOCK_LAYOUT_WEAK:
2398 printf("BL_WEAK:");
2399 break;
2400 case BLOCK_LAYOUT_UNRETAINED:
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002401 printf("BL_UNRETAINED:");
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002402 break;
2403 }
2404 // Actual value of word count is one more that what is in the imm.
2405 // field of the instruction
2406 printf("%d", (inst & 0xf) + delta);
2407 if (i < e-1)
2408 printf(", ");
2409 else
2410 printf("\n");
2411 }
2412 }
Fariborz Jahanian23290b02012-11-01 18:32:55 +00002413
2414 llvm::GlobalVariable * Entry =
2415 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2416 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2417 "__TEXT,__objc_classname,cstring_literals", 1, true);
2418 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +00002419}
2420
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002421llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00002422 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00002423 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002424 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00002425 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2426
Owen Andersonade90fd2009-07-29 18:54:39 +00002427 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00002428 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002429}
2430
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002431void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00002432 // FIXME: We shouldn't need this, the protocol decl should contain enough
2433 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00002434 DefinedProtocols.insert(PD->getIdentifier());
2435
2436 // If we have generated a forward reference to this protocol, emit
2437 // it now. Otherwise do nothing, the protocol objects are lazily
2438 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002439 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00002440 GetOrEmitProtocol(PD);
2441}
2442
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00002443llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002444 if (DefinedProtocols.count(PD->getIdentifier()))
2445 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002446
Daniel Dunbarc475d422008-10-29 22:36:39 +00002447 return GetOrEmitProtocolRef(PD);
2448}
2449
Daniel Dunbarb036db82008-08-13 03:21:16 +00002450/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002451// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2452struct _objc_protocol {
2453struct _objc_protocol_extension *isa;
2454char *protocol_name;
2455struct _objc_protocol_list *protocol_list;
2456struct _objc__method_prototype_list *instance_methods;
2457struct _objc__method_prototype_list *class_methods
2458};
Daniel Dunbarb036db82008-08-13 03:21:16 +00002459
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002460See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00002461*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00002462llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00002463 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00002464
2465 // Early exit if a defining object has already been generated.
2466 if (Entry && Entry->hasInitializer())
2467 return Entry;
2468
Douglas Gregora715bff2012-01-01 19:51:50 +00002469 // Use the protocol definition, if there is one.
2470 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2471 PD = Def;
2472
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002473 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00002474 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002475 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2476
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002477 // Construct method lists.
2478 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2479 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002480 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002481 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002482 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002483 ObjCMethodDecl *MD = *i;
2484 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002485 if (!C)
2486 return GetOrEmitProtocolRef(PD);
2487
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002488 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2489 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002490 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002491 } else {
2492 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002493 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002494 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002495 }
2496
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002497 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002498 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002499 ObjCMethodDecl *MD = *i;
2500 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00002501 if (!C)
2502 return GetOrEmitProtocolRef(PD);
2503
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002504 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2505 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002506 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002507 } else {
2508 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002509 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002510 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002511 }
2512
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002513 MethodTypesExt.insert(MethodTypesExt.end(),
2514 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2515
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002516 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002517 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2518 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002519 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002520 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002521 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002522 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002523 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002524 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002525 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002526 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002527 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002528 ClassMethods)
2529 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002530 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002531 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002532
Daniel Dunbarb036db82008-08-13 03:21:16 +00002533 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002534 // Already created, fix the linkage and update the initializer.
2535 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002536 Entry->setInitializer(Init);
2537 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002539 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002540 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002541 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002542 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002543 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002544 // FIXME: Is this necessary? Why only for protocol?
2545 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002546
2547 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002548 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00002549 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002550
2551 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002552}
2553
Daniel Dunbarc475d422008-10-29 22:36:39 +00002554llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002555 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2556
2557 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002558 // We use the initializer as a marker of whether this is a forward
2559 // reference or not. At module finalization we add the empty
2560 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002561 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002562 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002563 llvm::GlobalValue::ExternalLinkage,
2564 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002565 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002566 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002567 // FIXME: Is this necessary? Why only for protocol?
2568 Entry->setAlignment(4);
2569 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002570
Daniel Dunbarb036db82008-08-13 03:21:16 +00002571 return Entry;
2572}
2573
2574/*
2575 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002576 uint32_t size;
2577 struct objc_method_description_list *optional_instance_methods;
2578 struct objc_method_description_list *optional_class_methods;
2579 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002580 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002581 };
2582*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002583llvm::Constant *
2584CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002585 ArrayRef<llvm::Constant*> OptInstanceMethods,
2586 ArrayRef<llvm::Constant*> OptClassMethods,
2587 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002588 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002589 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002590 llvm::Constant *Values[] = {
2591 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002592 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002593 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002594 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002595 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002596 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002597 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002598 OptClassMethods),
2599 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002600 ObjCTypes),
2601 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2602 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002603 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002604
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002605 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002606 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002607 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002608 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002609
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002610 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002611 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002612
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002613 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002614 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002615 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002616 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002617}
2618
2619/*
2620 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002621 struct objc_protocol_list *next;
2622 long count;
2623 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002624 };
2625*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002626llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002627CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002628 ObjCProtocolDecl::protocol_iterator begin,
2629 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002630 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002631
Daniel Dunbardec75f82008-08-21 21:57:41 +00002632 for (; begin != end; ++begin)
2633 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002634
2635 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002636 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002637 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002638
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002639 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002640 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002641
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002642 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002643 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002644 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002645 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002646 ProtocolRefs.size() - 1);
2647 Values[2] =
2648 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2649 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002650 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002651
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002652 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002653 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002654 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002655 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002656 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002657}
2658
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002659void CGObjCCommonMac::
2660PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendlinga515b582012-02-09 22:16:49 +00002661 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002662 const Decl *Container,
2663 const ObjCProtocolDecl *PROTO,
2664 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002665 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2666 E = PROTO->protocol_end(); P != E; ++P)
2667 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2668 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2669 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002670 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002671 if (!PropertySet.insert(PD->getIdentifier()))
2672 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002673 llvm::Constant *Prop[] = {
2674 GetPropertyName(PD->getIdentifier()),
2675 GetPropertyTypeString(PD, Container)
2676 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002677 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2678 }
2679}
2680
Daniel Dunbarb036db82008-08-13 03:21:16 +00002681/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002682 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002683 const char * const name;
2684 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002685 };
2686
2687 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002688 uint32_t entsize; // sizeof (struct _objc_property)
2689 uint32_t prop_count;
2690 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002691 };
2692*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002693llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002694 const Decl *Container,
2695 const ObjCContainerDecl *OCD,
2696 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002697 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002698 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002699 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2700 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002701 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002702 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002703 llvm::Constant *Prop[] = {
2704 GetPropertyName(PD->getIdentifier()),
2705 GetPropertyTypeString(PD, Container)
2706 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002707 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002708 Prop));
2709 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002710 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002711 for (ObjCInterfaceDecl::all_protocol_iterator
2712 P = OID->all_referenced_protocol_begin(),
2713 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002714 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2715 ObjCTypes);
2716 }
2717 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2718 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2719 E = CD->protocol_end(); P != E; ++P)
2720 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2721 ObjCTypes);
2722 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002723
2724 // Return null for empty list.
2725 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002726 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002727
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002728 unsigned PropertySize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00002729 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002730 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002731 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2732 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002733 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002734 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002735 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002736 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002737
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002738 llvm::GlobalVariable *GV =
2739 CreateMetadataVar(Name, Init,
2740 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002741 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002742 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002743 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002744 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002745}
2746
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002747llvm::Constant *
2748CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2749 ArrayRef<llvm::Constant*> MethodTypes,
2750 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002751 // Return null for empty list.
2752 if (MethodTypes.empty())
2753 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2754
2755 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2756 MethodTypes.size());
2757 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2758
2759 llvm::GlobalVariable *GV =
2760 CreateMetadataVar(Name, Init,
2761 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2762 (ObjCABI == 2) ? 8 : 4,
2763 true);
2764 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2765}
2766
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002767/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002768 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002769 int count;
2770 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002771 };
2772*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002773llvm::Constant *
2774CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002775 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002776 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002777 ObjCTypes.SelectorPtrTy),
2778 GetMethodVarType(MD)
2779 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002780 if (!Desc[1])
2781 return 0;
2782
Owen Anderson0e0189d2009-07-27 22:29:56 +00002783 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002784 Desc);
2785}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002786
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002787llvm::Constant *
2788CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2789 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002790 // Return null for empty list.
2791 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002792 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002793
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002794 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002795 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002796 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002797 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002798 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002799 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002800
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002801 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002802 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002803 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002804}
2805
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002806/*
2807 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002808 char *category_name;
2809 char *class_name;
2810 struct _objc_method_list *instance_methods;
2811 struct _objc_method_list *class_methods;
2812 struct _objc_protocol_list *protocols;
2813 uint32_t size; // <rdar://4585769>
2814 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002815 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002816*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002817void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00002818 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002819
Mike Stump18bb9282009-05-16 07:57:57 +00002820 // FIXME: This is poor design, the OCD should have a pointer to the category
2821 // decl. Additionally, note that Category can be null for the @implementation
2822 // w/o an @interface case. Sema should just create one for us as it does for
2823 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002824 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002825 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002826 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002827
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002828 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002829 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2830 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002831
Bill Wendlinga515b582012-02-09 22:16:49 +00002832 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002833 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002834 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002835 // Instance methods should always be defined.
2836 InstanceMethods.push_back(GetMethodConstant(*i));
2837 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002838 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002839 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002840 // Class methods should always be defined.
2841 ClassMethods.push_back(GetMethodConstant(*i));
2842 }
2843
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002844 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002845 Values[0] = GetClassName(OCD->getIdentifier());
2846 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002847 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002848 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002849 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002850 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002851 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002852 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002853 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002854 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002855 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002856 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002857 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002858 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002859 Category->protocol_begin(),
2860 Category->protocol_end());
2861 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002862 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002863 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002864 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002865
2866 // If there is no category @interface then there can be no properties.
2867 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002868 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002869 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002870 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002871 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002872 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002873
Owen Anderson0e0189d2009-07-27 22:29:56 +00002874 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002875 Values);
2876
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002877 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002878 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002879 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002880 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002881 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002882 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002883 // method definition entries must be clear for next implementation.
2884 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002885}
2886
John McCallef19dbb2012-10-17 04:53:23 +00002887enum FragileClassFlags {
2888 FragileABI_Class_Factory = 0x00001,
2889 FragileABI_Class_Meta = 0x00002,
2890 FragileABI_Class_HasCXXStructors = 0x02000,
2891 FragileABI_Class_Hidden = 0x20000
2892};
2893
2894enum NonFragileClassFlags {
2895 /// Is a meta-class.
2896 NonFragileABI_Class_Meta = 0x00001,
2897
2898 /// Is a root class.
2899 NonFragileABI_Class_Root = 0x00002,
2900
2901 /// Has a C++ constructor and destructor.
2902 NonFragileABI_Class_HasCXXStructors = 0x00004,
2903
2904 /// Has hidden visibility.
2905 NonFragileABI_Class_Hidden = 0x00010,
2906
2907 /// Has the exception attribute.
2908 NonFragileABI_Class_Exception = 0x00020,
2909
2910 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2911 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2912
2913 /// Class implementation was compiled under ARC.
John McCall0d54a172012-10-17 04:53:31 +00002914 NonFragileABI_Class_CompiledByARC = 0x00080,
2915
2916 /// Class has non-trivial destructors, but zero-initialization is okay.
2917 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002918};
2919
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002920/*
2921 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002922 Class isa;
2923 Class super_class;
2924 const char *name;
2925 long version;
2926 long info;
2927 long instance_size;
2928 struct _objc_ivar_list *ivars;
2929 struct _objc_method_list *methods;
2930 struct _objc_cache *cache;
2931 struct _objc_protocol_list *protocols;
2932 // Objective-C 1.0 extensions (<rdr://4585769>)
2933 const char *ivar_layout;
2934 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002935 };
2936
2937 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002938*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002939void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002940 DefinedSymbols.insert(ID->getIdentifier());
2941
Chris Lattner86d7d912008-11-24 03:54:41 +00002942 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002943 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002944 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002945 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002946 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002947 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002948 Interface->all_referenced_protocol_begin(),
2949 Interface->all_referenced_protocol_end());
John McCallef19dbb2012-10-17 04:53:23 +00002950 unsigned Flags = FragileABI_Class_Factory;
John McCall0d54a172012-10-17 04:53:31 +00002951 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCallef19dbb2012-10-17 04:53:23 +00002952 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002953 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002954 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002955
2956 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002957 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00002958 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002959
Bill Wendlinga515b582012-02-09 22:16:49 +00002960 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002961 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002962 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002963 // Instance methods should always be defined.
2964 InstanceMethods.push_back(GetMethodConstant(*i));
2965 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002966 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002967 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002968 // Class methods should always be defined.
2969 ClassMethods.push_back(GetMethodConstant(*i));
2970 }
2971
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002972 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002973 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002974 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002975
2976 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2977 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2978
2979 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2980 if (llvm::Constant *C = GetMethodConstant(MD))
2981 InstanceMethods.push_back(C);
2982 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2983 if (llvm::Constant *C = GetMethodConstant(MD))
2984 InstanceMethods.push_back(C);
2985 }
2986 }
2987
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002988 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002989 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002990 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002991 // Record a reference to the super class.
2992 LazySymbols.insert(Super->getIdentifier());
2993
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002994 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002995 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002996 ObjCTypes.ClassPtrTy);
2997 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002998 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002999 }
3000 Values[ 2] = GetClassName(ID->getIdentifier());
3001 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003002 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3003 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3004 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003005 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003006 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003007 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003008 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003009 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003010 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003011 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003012 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003013 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003014 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003015 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003016 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003017 std::string Name("\01L_OBJC_CLASS_");
3018 Name += ClassName;
3019 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3020 // Check for a forward reference.
3021 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3022 if (GV) {
3023 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3024 "Forward metaclass reference has incorrect type.");
3025 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3026 GV->setInitializer(Init);
3027 GV->setSection(Section);
3028 GV->setAlignment(4);
3029 CGM.AddUsedGlobal(GV);
3030 }
3031 else
3032 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003033 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00003034 // method definition entries must be clear for next implementation.
3035 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003036}
3037
3038llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3039 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003040 ArrayRef<llvm::Constant*> Methods) {
John McCallef19dbb2012-10-17 04:53:23 +00003041 unsigned Flags = FragileABI_Class_Meta;
Micah Villmowdd31ca12012-10-08 16:25:52 +00003042 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003043
John McCall457a04e2010-10-22 21:05:15 +00003044 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCallef19dbb2012-10-17 04:53:23 +00003045 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003046
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003047 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003048 // The isa for the metaclass is the root of the hierarchy.
3049 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3050 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3051 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003052 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003053 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003054 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003055 // The super class for the metaclass is emitted as the name of the
3056 // super class. The runtime fixes this up to point to the
3057 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003058 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003059 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003060 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003061 ObjCTypes.ClassPtrTy);
3062 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00003063 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003064 }
3065 Values[ 2] = GetClassName(ID->getIdentifier());
3066 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003067 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3068 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3069 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003070 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003071 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003072 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003073 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003074 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003075 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003076 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003077 Values[ 9] = Protocols;
3078 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00003079 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003080 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00003081 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003082 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003083 Values);
3084
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003085 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer1bbcbd02012-07-31 11:45:39 +00003086 Name += ID->getName();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003087
3088 // Check for a forward reference.
3089 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3090 if (GV) {
3091 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3092 "Forward metaclass reference has incorrect type.");
3093 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3094 GV->setInitializer(Init);
3095 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00003096 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003097 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00003098 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003099 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003100 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00003101 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003102 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003103
3104 return GV;
3105}
3106
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003107llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00003108 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003109
Mike Stump18bb9282009-05-16 07:57:57 +00003110 // FIXME: Should we look these up somewhere other than the module. Its a bit
3111 // silly since we only generate these while processing an implementation, so
3112 // exactly one pointer would work if know when we entered/exitted an
3113 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003114
3115 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00003116 // Previously, metaclass with internal linkage may have been defined.
3117 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003118 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3119 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003120 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3121 "Forward metaclass reference has incorrect type.");
3122 return GV;
3123 } else {
3124 // Generate as an external reference to keep a consistent
3125 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00003126 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003127 llvm::GlobalValue::ExternalLinkage,
3128 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00003129 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00003130 }
3131}
3132
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00003133llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3134 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3135
3136 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3137 true)) {
3138 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3139 "Forward class metadata reference has incorrect type.");
3140 return GV;
3141 } else {
3142 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3143 llvm::GlobalValue::ExternalLinkage,
3144 0,
3145 Name);
3146 }
3147}
3148
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003149/*
3150 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003151 uint32_t size;
3152 const char *weak_ivar_layout;
3153 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003154 };
3155*/
3156llvm::Constant *
3157CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003158 uint64_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00003159 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003160
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003161 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003162 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00003163 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003164 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00003165 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003166
3167 // Return null if no extension bits are used.
3168 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00003169 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003170
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003171 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00003172 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003173 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003175 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003176}
3177
3178/*
3179 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003180 char *ivar_name;
3181 char *ivar_type;
3182 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003183 };
3184
3185 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003186 int ivar_count;
3187 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003188 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003189*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003190llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00003191 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003192 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003193
3194 // When emitting the root class GCC emits ivar entries for the
3195 // actual class structure. It is not clear if we need to follow this
3196 // behavior; for now lets try and get away with not doing it. If so,
3197 // the cleanest solution would be to make up an ObjCInterfaceDecl
3198 // for the class.
3199 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00003200 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003201
Jordy Rosea91768e2011-07-22 02:08:32 +00003202 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003203
Jordy Rosea91768e2011-07-22 02:08:32 +00003204 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003205 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00003206 // Ignore unnamed bit-fields.
3207 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003208 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003209 llvm::Constant *Ivar[] = {
3210 GetMethodVarName(IVD->getIdentifier()),
3211 GetMethodVarType(IVD),
3212 llvm::ConstantInt::get(ObjCTypes.IntTy,
3213 ComputeIvarBaseOffset(CGM, OID, IVD))
3214 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003215 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003216 }
3217
3218 // Return null for empty list.
3219 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003220 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003221
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003222 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003223 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003224 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003225 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003226 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003227 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003228
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003229 llvm::GlobalVariable *GV;
3230 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003231 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003232 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003233 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003234 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003235 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003236 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003237 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003238 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003239}
3240
3241/*
3242 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003243 SEL method_name;
3244 char *method_types;
3245 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003246 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003247
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003248 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003249 struct objc_method_list *obsolete;
3250 int count;
3251 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003252 };
3253*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003254
3255/// GetMethodConstant - Return a struct objc_method constant for the
3256/// given method if it has been defined. The result is null if the
3257/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00003258llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003259 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003260 if (!Fn)
3261 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003262
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003263 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00003264 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003265 ObjCTypes.SelectorPtrTy),
3266 GetMethodVarType(MD),
3267 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3268 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00003269 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003270}
3271
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003272llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003273 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00003274 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003275 // Return null for empty list.
3276 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003277 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003278
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003279 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00003280 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003281 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003282 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003283 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00003284 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003285 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003286
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003287 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003288 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003289}
3290
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00003291llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003292 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003293 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003294 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003295
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00003296 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00003297 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00003298 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003299 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00003300 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003301 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00003302 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003303 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003304 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003305
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003306 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003307}
3308
Daniel Dunbar30c65362009-03-09 20:09:19 +00003309llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003310CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00003311 llvm::Constant *Init,
3312 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003313 unsigned Align,
3314 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003315 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003316 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00003317 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00003318 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003319 if (Section)
3320 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00003321 if (Align)
3322 GV->setAlignment(Align);
3323 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00003324 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00003325 return GV;
3326}
3327
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003328llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003329 // Abuse this interface function as a place to finalize.
3330 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00003331 return NULL;
3332}
3333
Chris Lattnerd4808922009-03-22 21:03:39 +00003334llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003335 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003336}
3337
Chris Lattnerd4808922009-03-22 21:03:39 +00003338llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003339 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00003340}
3341
Ted Kremeneke65b0862012-03-06 20:05:56 +00003342llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3343 bool copy) {
3344 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3345}
3346
David Chisnall168b80f2010-12-26 22:13:16 +00003347llvm::Constant *CGObjCMac::GetGetStructFunction() {
3348 return ObjCTypes.getCopyStructFn();
3349}
3350llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00003351 return ObjCTypes.getCopyStructFn();
3352}
3353
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00003354llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
3355 return ObjCTypes.getCppAtomicObjectFunction();
3356}
3357
Chris Lattnerd4808922009-03-22 21:03:39 +00003358llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00003359 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00003360}
3361
John McCallbd309292010-07-06 01:34:17 +00003362void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3363 return EmitTryOrSynchronizedStmt(CGF, S);
3364}
3365
3366void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3367 const ObjCAtSynchronizedStmt &S) {
3368 return EmitTryOrSynchronizedStmt(CGF, S);
3369}
3370
John McCall65bea082010-07-21 06:59:36 +00003371namespace {
John McCallcda666c2010-07-21 07:22:38 +00003372 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00003373 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00003374 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00003375 llvm::Value *CallTryExitVar;
3376 llvm::Value *ExceptionData;
3377 ObjCTypesHelper &ObjCTypes;
3378 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00003379 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00003380 llvm::Value *CallTryExitVar,
3381 llvm::Value *ExceptionData,
3382 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00003383 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00003384 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3385
John McCall30317fd2011-07-12 20:27:29 +00003386 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00003387 // Check whether we need to call objc_exception_try_exit.
3388 // In optimized code, this branch will always be folded.
3389 llvm::BasicBlock *FinallyCallExit =
3390 CGF.createBasicBlock("finally.call_exit");
3391 llvm::BasicBlock *FinallyNoCallExit =
3392 CGF.createBasicBlock("finally.no_call_exit");
3393 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3394 FinallyCallExit, FinallyNoCallExit);
3395
3396 CGF.EmitBlock(FinallyCallExit);
3397 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3398 ->setDoesNotThrow();
3399
3400 CGF.EmitBlock(FinallyNoCallExit);
3401
3402 if (isa<ObjCAtTryStmt>(S)) {
3403 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00003404 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3405 // Save the current cleanup destination in case there's
3406 // control flow inside the finally statement.
3407 llvm::Value *CurCleanupDest =
3408 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3409
John McCall65bea082010-07-21 06:59:36 +00003410 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3411
John McCallcebe0ca2010-08-11 00:16:14 +00003412 if (CGF.HaveInsertPoint()) {
3413 CGF.Builder.CreateStore(CurCleanupDest,
3414 CGF.getNormalCleanupDestSlot());
3415 } else {
3416 // Currently, the end of the cleanup must always exist.
3417 CGF.EnsureInsertPoint();
3418 }
3419 }
John McCall65bea082010-07-21 06:59:36 +00003420 } else {
3421 // Emit objc_sync_exit(expr); as finally's sole statement for
3422 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00003423 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00003424 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3425 ->setDoesNotThrow();
3426 }
3427 }
3428 };
John McCall42227ed2010-07-31 23:20:56 +00003429
3430 class FragileHazards {
3431 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003432 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00003433 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3434
3435 llvm::InlineAsm *ReadHazard;
3436 llvm::InlineAsm *WriteHazard;
3437
3438 llvm::FunctionType *GetAsmFnType();
3439
3440 void collectLocals();
3441 void emitReadHazard(CGBuilderTy &Builder);
3442
3443 public:
3444 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00003445
John McCall42227ed2010-07-31 23:20:56 +00003446 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00003447 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003448 };
3449}
3450
3451/// Create the fragile-ABI read and write hazards based on the current
3452/// state of the function, which is presumed to be immediately prior
3453/// to a @try block. These hazards are used to maintain correct
3454/// semantics in the face of optimization and the fragile ABI's
3455/// cavalier use of setjmp/longjmp.
3456FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3457 collectLocals();
3458
3459 if (Locals.empty()) return;
3460
3461 // Collect all the blocks in the function.
3462 for (llvm::Function::iterator
3463 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3464 BlocksBeforeTry.insert(&*I);
3465
3466 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3467
3468 // Create a read hazard for the allocas. This inhibits dead-store
3469 // optimizations and forces the values to memory. This hazard is
3470 // inserted before any 'throwing' calls in the protected scope to
3471 // reflect the possibility that the variables might be read from the
3472 // catch block if the call throws.
3473 {
3474 std::string Constraint;
3475 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3476 if (I) Constraint += ',';
3477 Constraint += "*m";
3478 }
3479
3480 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3481 }
3482
3483 // Create a write hazard for the allocas. This inhibits folding
3484 // loads across the hazard. This hazard is inserted at the
3485 // beginning of the catch path to reflect the possibility that the
3486 // variables might have been written within the protected scope.
3487 {
3488 std::string Constraint;
3489 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3490 if (I) Constraint += ',';
3491 Constraint += "=*m";
3492 }
3493
3494 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3495 }
3496}
3497
3498/// Emit a write hazard at the current location.
3499void FragileHazards::emitWriteHazard() {
3500 if (Locals.empty()) return;
3501
Jay Foad5bd375a2011-07-15 08:37:34 +00003502 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003503}
3504
John McCall42227ed2010-07-31 23:20:56 +00003505void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3506 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00003507 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00003508}
3509
3510/// Emit read hazards in all the protected blocks, i.e. all the blocks
3511/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00003512void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00003513 if (Locals.empty()) return;
3514
3515 CGBuilderTy Builder(CGF.getLLVMContext());
3516
3517 // Iterate through all blocks, skipping those prior to the try.
3518 for (llvm::Function::iterator
3519 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3520 llvm::BasicBlock &BB = *FI;
3521 if (BlocksBeforeTry.count(&BB)) continue;
3522
3523 // Walk through all the calls in the block.
3524 for (llvm::BasicBlock::iterator
3525 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3526 llvm::Instruction &I = *BI;
3527
3528 // Ignore instructions that aren't non-intrinsic calls.
3529 // These are the only calls that can possibly call longjmp.
3530 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3531 if (isa<llvm::IntrinsicInst>(I))
3532 continue;
3533
3534 // Ignore call sites marked nounwind. This may be questionable,
3535 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3536 llvm::CallSite CS(&I);
3537 if (CS.doesNotThrow()) continue;
3538
John McCall2dd7d442010-08-04 05:59:32 +00003539 // Insert a read hazard before the call. This will ensure that
3540 // any writes to the locals are performed before making the
3541 // call. If the call throws, then this is sufficient to
3542 // guarantee correctness as long as it doesn't also write to any
3543 // locals.
John McCall42227ed2010-07-31 23:20:56 +00003544 Builder.SetInsertPoint(&BB, BI);
3545 emitReadHazard(Builder);
3546 }
3547 }
3548}
3549
3550static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3551 if (V) S.insert(V);
3552}
3553
3554void FragileHazards::collectLocals() {
3555 // Compute a set of allocas to ignore.
3556 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3557 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3558 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00003559
3560 // Collect all the allocas currently in the function. This is
3561 // probably way too aggressive.
3562 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3563 for (llvm::BasicBlock::iterator
3564 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3565 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3566 Locals.push_back(&*I);
3567}
3568
3569llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003570 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003571 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3572 tys[i] = Locals[i]->getType();
3573 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003574}
3575
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003576/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003577
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003578 Objective-C setjmp-longjmp (sjlj) Exception Handling
3579 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003580
John McCallbd309292010-07-06 01:34:17 +00003581 A catch buffer is a setjmp buffer plus:
3582 - a pointer to the exception that was caught
3583 - a pointer to the previous exception data buffer
3584 - two pointers of reserved storage
3585 Therefore catch buffers form a stack, with a pointer to the top
3586 of the stack kept in thread-local storage.
3587
3588 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3589 objc_exception_try_exit pops the given catch buffer, which is
3590 required to be the top of the EH stack.
3591 objc_exception_throw pops the top of the EH stack, writes the
3592 thrown exception into the appropriate field, and longjmps
3593 to the setjmp buffer. It crashes the process (with a printf
3594 and an abort()) if there are no catch buffers on the stack.
3595 objc_exception_extract just reads the exception pointer out of the
3596 catch buffer.
3597
3598 There's no reason an implementation couldn't use a light-weight
3599 setjmp here --- something like __builtin_setjmp, but API-compatible
3600 with the heavyweight setjmp. This will be more important if we ever
3601 want to implement correct ObjC/C++ exception interactions for the
3602 fragile ABI.
3603
3604 Note that for this use of setjmp/longjmp to be correct, we may need
3605 to mark some local variables volatile: if a non-volatile local
3606 variable is modified between the setjmp and the longjmp, it has
3607 indeterminate value. For the purposes of LLVM IR, it may be
3608 sufficient to make loads and stores within the @try (to variables
3609 declared outside the @try) volatile. This is necessary for
3610 optimized correctness, but is not currently being done; this is
3611 being tracked as rdar://problem/8160285
3612
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003613 The basic framework for a @try-catch-finally is as follows:
3614 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003615 objc_exception_data d;
3616 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003617 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003618
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003619 objc_exception_try_enter(&d);
3620 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003621 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003622 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003623 // exception path
3624 id _caught = objc_exception_extract(&d);
3625
3626 // enter new try scope for handlers
3627 if (!setjmp(d.jmp_buf)) {
3628 ... match exception and execute catch blocks ...
3629
3630 // fell off end, rethrow.
3631 _rethrow = _caught;
3632 ... jump-through-finally to finally_rethrow ...
3633 } else {
3634 // exception in catch block
3635 _rethrow = objc_exception_extract(&d);
3636 _call_try_exit = false;
3637 ... jump-through-finally to finally_rethrow ...
3638 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003639 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003640 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003641
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003642 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003643 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003644 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003645
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003646 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003647 ... dispatch to finally destination ...
3648
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003649 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003650 objc_exception_throw(_rethrow);
3651
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003652 finally_end:
3653 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003654
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003655 This framework differs slightly from the one gcc uses, in that gcc
3656 uses _rethrow to determine if objc_exception_try_exit should be called
3657 and if the object should be rethrown. This breaks in the face of
3658 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003659
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003660 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003661
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662 - If there are no catch blocks, then we avoid emitting the second
3663 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003664
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003665 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3666 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003667
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003668 - FIXME: If there is no @finally block we can do a few more
3669 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003670
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003671 Rethrows and Jumps-Through-Finally
3672 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003673
John McCallbd309292010-07-06 01:34:17 +00003674 '@throw;' is supported by pushing the currently-caught exception
3675 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003676
John McCallbd309292010-07-06 01:34:17 +00003677 Branches through the @finally block are handled with an ordinary
3678 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3679 exceptions are not compatible with C++ exceptions, and this is
3680 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003681
John McCallbd309292010-07-06 01:34:17 +00003682 @synchronized(expr) { stmt; } is emitted as if it were:
3683 id synch_value = expr;
3684 objc_sync_enter(synch_value);
3685 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003686*/
3687
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003688void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3689 const Stmt &S) {
3690 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003691
3692 // A destination for the fall-through edges of the catch handlers to
3693 // jump to.
3694 CodeGenFunction::JumpDest FinallyEnd =
3695 CGF.getJumpDestInCurrentScope("finally.end");
3696
3697 // A destination for the rethrow edge of the catch handlers to jump
3698 // to.
3699 CodeGenFunction::JumpDest FinallyRethrow =
3700 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003701
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003702 // For @synchronized, call objc_sync_enter(sync.expr). The
3703 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003704 // @synchronized. We can't avoid a temp here because we need the
3705 // value to be preserved. If the backend ever does liveness
3706 // correctly after setjmp, this will be unnecessary.
3707 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003708 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003709 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003710 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3711 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003712 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3713 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003714
3715 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3716 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003717 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003718
John McCall2dd7d442010-08-04 05:59:32 +00003719 // Allocate memory for the setjmp buffer. This needs to be kept
3720 // live throughout the try and catch blocks.
3721 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3722 "exceptiondata.ptr");
3723
John McCall42227ed2010-07-31 23:20:56 +00003724 // Create the fragile hazards. Note that this will not capture any
3725 // of the allocas required for exception processing, but will
3726 // capture the current basic block (which extends all the way to the
3727 // setjmp call) as "before the @try".
3728 FragileHazards Hazards(CGF);
3729
John McCallbd309292010-07-06 01:34:17 +00003730 // Create a flag indicating whether the cleanup needs to call
3731 // objc_exception_try_exit. This is true except when
3732 // - no catches match and we're branching through the cleanup
3733 // just to rethrow the exception, or
3734 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003735 // The setjmp-safety rule here is that we should always store to this
3736 // variable in a place that dominates the branch through the cleanup
3737 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003738 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003739 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003740
John McCall9916e3f2010-10-04 23:42:51 +00003741 // A slot containing the exception to rethrow. Only needed when we
3742 // have both a @catch and a @finally.
3743 llvm::Value *PropagatingExnVar = 0;
3744
John McCallbd309292010-07-06 01:34:17 +00003745 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003746 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003747 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003748 CallTryExitVar,
3749 ExceptionData,
3750 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003751
3752 // Enter a try block:
3753 // - Call objc_exception_try_enter to push ExceptionData on top of
3754 // the EH stack.
3755 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3756 ->setDoesNotThrow();
3757
3758 // - Call setjmp on the exception data buffer.
3759 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3760 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3761 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003762 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003763 llvm::CallInst *SetJmpResult =
3764 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3765 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003766 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003767
3768 // If setjmp returned 0, enter the protected block; otherwise,
3769 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003770 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3771 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003772 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003773 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3774 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003775
John McCallbd309292010-07-06 01:34:17 +00003776 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003777 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003778 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003779 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003780 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003781
3782 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003783
John McCallbd309292010-07-06 01:34:17 +00003784 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003785 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003786
John McCall42227ed2010-07-31 23:20:56 +00003787 // Don't optimize loads of the in-scope locals across this point.
3788 Hazards.emitWriteHazard();
3789
John McCallbd309292010-07-06 01:34:17 +00003790 // For a @synchronized (or a @try with no catches), just branch
3791 // through the cleanup to the rethrow block.
3792 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3793 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003794 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003795 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003796
3797 // Otherwise, we have to match against the caught exceptions.
3798 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003799 // Retrieve the exception object. We may emit multiple blocks but
3800 // nothing can cross this so the value is already in SSA form.
3801 llvm::CallInst *Caught =
3802 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3803 ExceptionData, "caught");
3804 Caught->setDoesNotThrow();
3805
John McCallbd309292010-07-06 01:34:17 +00003806 // Push the exception to rethrow onto the EH value stack for the
3807 // benefit of any @throws in the handlers.
3808 CGF.ObjCEHValueStack.push_back(Caught);
3809
Douglas Gregor96c79492010-04-23 22:50:49 +00003810 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811
John McCall2dd7d442010-08-04 05:59:32 +00003812 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003813
John McCall2dd7d442010-08-04 05:59:32 +00003814 llvm::BasicBlock *CatchBlock = 0;
3815 llvm::BasicBlock *CatchHandler = 0;
3816 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003817 // Save the currently-propagating exception before
3818 // objc_exception_try_enter clears the exception slot.
3819 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3820 "propagating_exception");
3821 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3822
John McCall2dd7d442010-08-04 05:59:32 +00003823 // Enter a new exception try block (in case a @catch block
3824 // throws an exception).
3825 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3826 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003827
John McCall2dd7d442010-08-04 05:59:32 +00003828 llvm::CallInst *SetJmpResult =
3829 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3830 "setjmp.result");
3831 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003832 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003833
John McCall2dd7d442010-08-04 05:59:32 +00003834 llvm::Value *Threw =
3835 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3836
3837 CatchBlock = CGF.createBasicBlock("catch");
3838 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3839 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3840
3841 CGF.EmitBlock(CatchBlock);
3842 }
3843
3844 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003845
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003846 // Handle catch list. As a special case we check if everything is
3847 // matched and avoid generating code for falling off the end if
3848 // so.
3849 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003850 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3851 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003852
Douglas Gregor46a572b2010-04-26 16:46:50 +00003853 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003854 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003855
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003856 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003857 if (!CatchParam) {
3858 AllMatched = true;
3859 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003860 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003861
John McCallbd309292010-07-06 01:34:17 +00003862 // catch(id e) always matches under this ABI, since only
3863 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003864 // FIXME: For the time being we also match id<X>; this should
3865 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003866 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003867 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003868 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003869
John McCallbd309292010-07-06 01:34:17 +00003870 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003871 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003872 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3873
Anders Carlsson9396a892008-09-11 09:15:33 +00003874 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003875 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003876 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003877
3878 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003879 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003880 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003881
Anders Carlsson9396a892008-09-11 09:15:33 +00003882 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003883
3884 // The scope of the catch variable ends right here.
3885 CatchVarCleanups.ForceCleanup();
3886
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003887 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003888 break;
3889 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003890
Steve Naroff7cae42b2009-07-10 23:34:53 +00003891 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003892 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003893
3894 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003895 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3896 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003897
3898 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003899 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003900
John McCallbd309292010-07-06 01:34:17 +00003901 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003902 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3903 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003904 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003905
John McCallbd309292010-07-06 01:34:17 +00003906 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3907 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003908
3909 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003910 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003911
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003912 // Emit the @catch block.
3913 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003914
3915 // Collect any cleanups for the catch variable. The scope lasts until
3916 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003917 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003918
John McCall1c9c3fd2010-10-15 04:57:14 +00003919 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003920 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003921
John McCallbd309292010-07-06 01:34:17 +00003922 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003923 llvm::Value *Tmp =
3924 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003925 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003926 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003927
Anders Carlsson9396a892008-09-11 09:15:33 +00003928 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003929
3930 // We're done with the catch variable.
3931 CatchVarCleanups.ForceCleanup();
3932
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003933 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003934
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003935 CGF.EmitBlock(NextCatchBlock);
3936 }
3937
John McCallbd309292010-07-06 01:34:17 +00003938 CGF.ObjCEHValueStack.pop_back();
3939
John McCall2dd7d442010-08-04 05:59:32 +00003940 // If nothing wanted anything to do with the caught exception,
3941 // kill the extract call.
3942 if (Caught->use_empty())
3943 Caught->eraseFromParent();
3944
3945 if (!AllMatched)
3946 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3947
3948 if (HasFinally) {
3949 // Emit the exception handler for the @catch blocks.
3950 CGF.EmitBlock(CatchHandler);
3951
3952 // In theory we might now need a write hazard, but actually it's
3953 // unnecessary because there's no local-accessing code between
3954 // the try's write hazard and here.
3955 //Hazards.emitWriteHazard();
3956
John McCall9916e3f2010-10-04 23:42:51 +00003957 // Extract the new exception and save it to the
3958 // propagating-exception slot.
3959 assert(PropagatingExnVar);
3960 llvm::CallInst *NewCaught =
3961 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3962 ExceptionData, "caught");
3963 NewCaught->setDoesNotThrow();
3964 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3965
John McCall2dd7d442010-08-04 05:59:32 +00003966 // Don't pop the catch handler; the throw already did.
3967 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003968 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003969 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003970 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003971
John McCall42227ed2010-07-31 23:20:56 +00003972 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003973 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003974
John McCallbd309292010-07-06 01:34:17 +00003975 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003976 CGF.Builder.restoreIP(TryFallthroughIP);
3977 if (CGF.HaveInsertPoint())
3978 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003979 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003980 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003981
John McCallbd309292010-07-06 01:34:17 +00003982 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003983 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003984 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003985 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003986 // If we have a propagating-exception variable, check it.
3987 llvm::Value *PropagatingExn;
3988 if (PropagatingExnVar) {
3989 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003990
John McCall9916e3f2010-10-04 23:42:51 +00003991 // Otherwise, just look in the buffer for the exception to throw.
3992 } else {
3993 llvm::CallInst *Caught =
3994 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3995 ExceptionData);
3996 Caught->setDoesNotThrow();
3997 PropagatingExn = Caught;
3998 }
3999
4000 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00004001 ->setDoesNotThrow();
4002 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00004003 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004004
John McCall42227ed2010-07-31 23:20:56 +00004005 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004006}
4007
4008void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00004009 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00004010 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004011
Anders Carlssone005aa12008-09-09 16:16:55 +00004012 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00004013 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004014 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004015 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00004016 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004017 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00004018 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00004019 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00004020 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004021
John McCallbd309292010-07-06 01:34:17 +00004022 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4023 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00004024 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00004025
4026 // Clear the insertion point to indicate we are in unreachable code.
4027 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00004028}
4029
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004030/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004031/// object: objc_read_weak (id *src)
4032///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004033llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004034 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004035 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004036 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4037 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4038 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00004039 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004040 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00004041 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00004042 return read_weak;
4043}
4044
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004045/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4046/// objc_assign_weak (id src, id *dst)
4047///
4048void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004049 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004050 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004051 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004052 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004053 assert(Size <= 8 && "does not support size > 8");
4054 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004055 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004056 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4057 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004058 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4059 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00004060 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00004061 src, dst, "weakassign");
4062 return;
4063}
4064
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004065/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4066/// objc_assign_global (id src, id *dst)
4067///
4068void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00004069 llvm::Value *src, llvm::Value *dst,
4070 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004071 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004072 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004073 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004074 assert(Size <= 8 && "does not support size > 8");
4075 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004076 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004077 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4078 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004079 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4080 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00004081 if (!threadlocal)
4082 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4083 src, dst, "globalassign");
4084 else
4085 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4086 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004087 return;
4088}
4089
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004090/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004091/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004092///
4093void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004094 llvm::Value *src, llvm::Value *dst,
4095 llvm::Value *ivarOffset) {
4096 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00004097 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004098 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004099 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004100 assert(Size <= 8 && "does not support size > 8");
4101 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004102 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004103 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4104 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004105 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4106 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00004107 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4108 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00004109 return;
4110}
4111
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004112/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4113/// objc_assign_strongCast (id src, id *dst)
4114///
4115void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00004116 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00004117 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004118 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004119 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004120 assert(Size <= 8 && "does not support size > 8");
4121 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00004123 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4124 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00004125 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4126 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00004127 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00004128 src, dst, "weakassign");
4129 return;
4130}
4131
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004132void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004133 llvm::Value *DestPtr,
4134 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00004135 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004136 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4137 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004138 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00004139 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00004140 return;
4141}
4142
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004143/// EmitObjCValueForIvar - Code Gen for ivar reference.
4144///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004145LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4146 QualType ObjectTy,
4147 llvm::Value *BaseValue,
4148 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00004149 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00004150 const ObjCInterfaceDecl *ID =
4151 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00004152 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4153 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00004154}
4155
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004156llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00004157 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004158 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00004159 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004160 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004161 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4162 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00004163}
4164
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004165/* *** Private Interface *** */
4166
4167/// EmitImageInfo - Emit the image info marker used to encode some module
4168/// level information.
4169///
4170/// See: <rdr://4810609&4810587&4810587>
4171/// struct IMAGE_INFO {
4172/// unsigned version;
4173/// unsigned flags;
4174/// };
4175enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00004176 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004177 eImageInfo_GarbageCollected = (1 << 1),
4178 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00004179 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4180
Daniel Dunbar5e639272010-04-25 20:39:01 +00004181 // A flag indicating that the module has no instances of a @synthesize of a
4182 // superclass variable. <rdar://problem/6803242>
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004183 eImageInfo_CorrectedSynthesize = (1 << 4),
4184 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004185};
4186
Daniel Dunbar5e639272010-04-25 20:39:01 +00004187void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004188 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004189 const char *Section = (ObjCABI == 1) ?
4190 "__OBJC, __image_info,regular" :
4191 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004192
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004193 // Generate module-level named metadata to convey this information to the
4194 // linker and code-gen.
4195 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004197 // Add the ObjC ABI version to the module flags.
4198 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4199 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4200 version);
4201 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4202 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004203
David Blaikiebbafb8a2012-03-11 07:00:24 +00004204 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004205 // Non-GC overrides those files which specify GC.
4206 Mod.addModuleFlag(llvm::Module::Override,
4207 "Objective-C Garbage Collection", (uint32_t)0);
4208 } else {
4209 // Add the ObjC garbage collection value.
4210 Mod.addModuleFlag(llvm::Module::Error,
4211 "Objective-C Garbage Collection",
4212 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004213
David Blaikiebbafb8a2012-03-11 07:00:24 +00004214 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00004215 // Add the ObjC GC Only value.
4216 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4217 eImageInfo_GCOnly);
4218
4219 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4220 llvm::Value *Ops[2] = {
4221 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4222 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4223 eImageInfo_GarbageCollected)
4224 };
4225 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4226 llvm::MDNode::get(VMContext, Ops));
4227 }
4228 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00004229
4230 // Indicate whether we're compiling this to run on a simulator.
4231 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4232 if (Triple.getOS() == llvm::Triple::IOS &&
4233 (Triple.getArch() == llvm::Triple::x86 ||
4234 Triple.getArch() == llvm::Triple::x86_64))
4235 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4236 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004237}
4238
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004239// struct objc_module {
4240// unsigned long version;
4241// unsigned long size;
4242// const char *name;
4243// Symtab symtab;
4244// };
4245
4246// FIXME: Get from somewhere
4247static const int ModuleVersion = 7;
4248
4249void CGObjCMac::EmitModuleInfo() {
Micah Villmowdd31ca12012-10-08 16:25:52 +00004250 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004251
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004252 llvm::Constant *Values[] = {
4253 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4254 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4255 // This used to be the filename, now it is unused. <rdr://4327263>
4256 GetClassName(&CGM.getContext().Idents.get("")),
4257 EmitModuleSymbols()
4258 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004259 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00004260 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004261 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00004262 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004263}
4264
4265llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004266 unsigned NumClasses = DefinedClasses.size();
4267 unsigned NumCategories = DefinedCategories.size();
4268
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004269 // Return null if no symbols were defined.
4270 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00004271 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004272
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004273 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004274 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00004275 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004276 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4277 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004278
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004279 // The runtime expects exactly the list of defined classes followed
4280 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00004281 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004282 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004283 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004284 ObjCTypes.Int8PtrTy);
4285 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004286 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00004287 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004288 ObjCTypes.Int8PtrTy);
4289
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004290 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004291 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004292 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004293 Symbols);
4294
Chris Lattnere64d7ba2011-06-20 04:01:35 +00004295 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004296
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004297 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004298 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4299 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004300 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00004301 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004302}
4303
John McCall31168b02011-06-15 23:02:42 +00004304llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4305 IdentifierInfo *II) {
4306 LazySymbols.insert(II);
4307
4308 llvm::GlobalVariable *&Entry = ClassReferences[II];
4309
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004310 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004311 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00004312 llvm::ConstantExpr::getBitCast(GetClassName(II),
4313 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004314 Entry =
John McCall31168b02011-06-15 23:02:42 +00004315 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4316 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4317 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004318 }
John McCall31168b02011-06-15 23:02:42 +00004319
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004320 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004321}
4322
John McCall31168b02011-06-15 23:02:42 +00004323llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4324 const ObjCInterfaceDecl *ID) {
4325 return EmitClassRefFromId(Builder, ID->getIdentifier());
4326}
4327
4328llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4329 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4330 return EmitClassRefFromId(Builder, II);
4331}
4332
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004333llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4334 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004335 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004336
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004337 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004338 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00004339 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004340 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004341 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004342 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4343 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00004344 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004345 }
4346
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00004347 if (lvalue)
4348 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00004349 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004350}
4351
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004352llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00004353 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004354
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004355 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004356 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004357 llvm::ConstantDataArray::getString(VMContext,
4358 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004359 ((ObjCABI == 2) ?
4360 "__TEXT,__objc_classname,cstring_literals" :
4361 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004362 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004363
Owen Anderson170229f2009-07-14 23:10:40 +00004364 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004365}
4366
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004367llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4368 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4369 I = MethodDefinitions.find(MD);
4370 if (I != MethodDefinitions.end())
4371 return I->second;
4372
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00004373 return NULL;
4374}
4375
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004376/// GetIvarLayoutName - Returns a unique constant for the given
4377/// ivar layout bitmap.
4378llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00004380 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00004381}
4382
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004383void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004384 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004385 bool ForStrongLayout,
4386 bool &HasUnion) {
4387 const RecordDecl *RD = RT->getDecl();
4388 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00004389 SmallVector<const FieldDecl*, 16> Fields;
4390 for (RecordDecl::field_iterator i = RD->field_begin(),
4391 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00004392 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00004393 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004394 const llvm::StructLayout *RecLayout =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004395 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004396
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004397 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4398 ForStrongLayout, HasUnion);
4399}
4400
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00004401void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004402 const llvm::StructLayout *Layout,
4403 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00004404 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004405 unsigned int BytePos, bool ForStrongLayout,
4406 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004407 bool IsUnion = (RD && RD->isUnion());
4408 uint64_t MaxUnionIvarSize = 0;
4409 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00004410 const FieldDecl *MaxField = 0;
4411 const FieldDecl *MaxSkippedField = 0;
4412 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004413 uint64_t MaxFieldOffset = 0;
4414 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004415 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00004416 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004417
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004418 if (RecFields.empty())
4419 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004420 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
4421 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004422 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004423 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00004424 FirstFieldDelta =
4425 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
4426 }
4427
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004428 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004429 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00004430 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004431 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00004432 // Note that 'i' here is actually the field index inside RD of Field,
4433 // although this dependency is hidden.
4434 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00004435 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00004436 } else
John McCall31168b02011-06-15 23:02:42 +00004437 FieldOffset =
4438 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004439
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004440 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004441 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004442 LastFieldBitfieldOrUnnamed = Field;
4443 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004444 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004445 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004446
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004447 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004448 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004449 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004450 if (FQT->isUnionType())
4451 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004452
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004453 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004454 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004455 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004456 continue;
4457 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004458
Chris Lattner5b36ddb2009-03-31 08:48:01 +00004459 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004460 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004461 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004462 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004463 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004464 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004465 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4466 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004467 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004468 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004469 FQT = CArray->getElementType();
4470 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004471
4472 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004473 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00004474 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004475 int OldIndex = IvarsInfo.size() - 1;
4476 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004477
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004478 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004479 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004480 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004481
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004482 // Replicate layout information for each array element. Note that
4483 // one element is already done.
4484 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004485 for (int FirstIndex = IvarsInfo.size() - 1,
4486 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004487 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004488 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4489 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4490 IvarsInfo[i].ivar_size));
4491 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4492 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4493 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004494 }
4495 continue;
4496 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00004497 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004498 // At this point, we are done with Record/Union and array there of.
4499 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00004500 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00004501
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004502 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00004503 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4504 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004505 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004506 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004507 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004508 MaxUnionIvarSize = UnionIvarSize;
4509 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004510 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004511 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004512 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004513 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004514 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004515 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004516 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00004517 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4518 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00004519 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00004520 // FIXME: Why the asymmetry? We divide by word size in bits on other
4521 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004522 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00004523 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004524 MaxSkippedUnionIvarSize = UnionIvarSize;
4525 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00004526 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004527 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00004528 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004529 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00004530 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004531 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00004532 }
4533 }
4534 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00004535
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004536 if (LastFieldBitfieldOrUnnamed) {
4537 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4538 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00004539 uint64_t BitFieldSize
4540 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00004541 GC_IVAR skivar;
4542 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
4543 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4544 + ((BitFieldSize % ByteSizeInBits) != 0);
4545 SkipIvars.push_back(skivar);
4546 } else {
4547 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4548 // Last field was unnamed. Must update skip info.
4549 unsigned FieldSize
4550 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4551 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4552 FieldSize / ByteSizeInBits));
4553 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00004554 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004555
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004556 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004557 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004558 MaxUnionIvarSize));
4559 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00004560 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00004561 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004562}
4563
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004564/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4565/// the computations and returning the layout bitmap (for ivar or blocks) in
4566/// the given argument BitMap string container. Routine reads
4567/// two containers, IvarsInfo and SkipIvars which are assumed to be
4568/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004569llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004570 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004571 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004572
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004573 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004574 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004575 unsigned int WordSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +00004576 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004577 if (IvarsInfo[0].ivar_bytepos == 0) {
4578 WordsToSkip = 0;
4579 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004580 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004581 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4582 WordsToScan = IvarsInfo[0].ivar_size;
4583 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004584 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004585 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004586 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004587 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004588 // consecutive 'scanned' object pointers.
4589 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004590 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004591 // Skip over 'gc'able object pointer which lay over each other.
4592 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4593 continue;
4594 // Must skip over 1 or more words. We save current skip/scan values
4595 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004596 SKIP_SCAN SkScan;
4597 SkScan.skip = WordsToSkip;
4598 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004599 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004600
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004601 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004602 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4603 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004604 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004605 WordsToSkip = 0;
4606 WordsToScan = IvarsInfo[i].ivar_size;
4607 }
4608 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004609 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004610 SKIP_SCAN SkScan;
4611 SkScan.skip = WordsToSkip;
4612 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004613 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004614 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004615
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004616 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004617 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004619 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004620 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004621 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004622 IvarsInfo[LastIndex].ivar_bytepos +
4623 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004624 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004625 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004626 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004627 SKIP_SCAN SkScan;
4628 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4629 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004630 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004631 }
4632 }
4633 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4634 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004635 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004636 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004637 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4638 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4639 // 0xM0 followed by 0x0N detected.
4640 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4641 for (int j = i+1; j < SkipScan; j++)
4642 SkipScanIvars[j] = SkipScanIvars[j+1];
4643 --SkipScan;
4644 }
4645 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004646
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004647 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004648 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004649 unsigned char byte;
4650 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4651 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4652 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4653 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004654
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004655 // first skip big.
4656 for (unsigned int ix = 0; ix < skip_big; ix++)
4657 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004658
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004659 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004660 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004661 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004662 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004663 byte |= 0xf;
4664 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004665 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004666 byte |= scan_small;
4667 scan_small = 0;
4668 }
4669 BitMap += byte;
4670 }
4671 // next scan big
4672 for (unsigned int ix = 0; ix < scan_big; ix++)
4673 BitMap += (unsigned char)(0x0f);
4674 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004675 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004676 byte = scan_small;
4677 BitMap += byte;
4678 }
4679 }
4680 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004681 unsigned char zero = 0;
4682 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004683
4684 llvm::GlobalVariable * Entry =
4685 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004686 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004687 ((ObjCABI == 2) ?
4688 "__TEXT,__objc_classname,cstring_literals" :
4689 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004690 1, true);
4691 return getConstantGEP(VMContext, Entry, 0, 0);
4692}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004693
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004694/// BuildIvarLayout - Builds ivar layout bitmap for the class
4695/// implementation for the __strong or __weak case.
4696/// The layout map displays which words in ivar list must be skipped
4697/// and which must be scanned by GC (see below). String is built of bytes.
4698/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4699/// of words to skip and right nibble is count of words to scan. So, each
4700/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4701/// represented by a 0x00 byte which also ends the string.
4702/// 1. when ForStrongLayout is true, following ivars are scanned:
4703/// - id, Class
4704/// - object *
4705/// - __strong anything
4706///
4707/// 2. When ForStrongLayout is false, following ivars are scanned:
4708/// - __weak anything
4709///
4710llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4711 const ObjCImplementationDecl *OMD,
4712 bool ForStrongLayout) {
4713 bool hasUnion = false;
4714
Chris Lattnerece04092012-02-07 00:39:47 +00004715 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004716 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4717 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004718 return llvm::Constant::getNullValue(PtrTy);
4719
Jordy Rosea91768e2011-07-22 02:08:32 +00004720 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4721 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004722 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004723 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004724 IVD; IVD = IVD->getNextIvar())
4725 RecFields.push_back(cast<FieldDecl>(IVD));
4726 }
4727 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004728 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004729 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004730
Jordy Rosea91768e2011-07-22 02:08:32 +00004731 // FIXME: This is not ideal; we shouldn't have to do this copy.
4732 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004733 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004734
4735 if (RecFields.empty())
4736 return llvm::Constant::getNullValue(PtrTy);
4737
4738 SkipIvars.clear();
4739 IvarsInfo.clear();
4740
4741 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4742 if (IvarsInfo.empty())
4743 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004744 // Sort on byte position in case we encounterred a union nested in
4745 // the ivar list.
4746 if (hasUnion && !IvarsInfo.empty())
4747 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4748 if (hasUnion && !SkipIvars.empty())
4749 std::sort(SkipIvars.begin(), SkipIvars.end());
4750
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004751 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004752 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004753
David Blaikiebbafb8a2012-03-11 07:00:24 +00004754 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004755 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004756 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004757 OMD->getClassInterface()->getName().data());
Roman Divackye6377112012-09-06 15:59:27 +00004758 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004759 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004760 if (!(s[i] & 0xf0))
4761 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4762 else
4763 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4764 printf("\n");
4765 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004766 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004767}
4768
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004769llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004770 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4771
Chris Lattner3def9ae2012-02-06 22:16:34 +00004772 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004773 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004774 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004775 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004776 ((ObjCABI == 2) ?
4777 "__TEXT,__objc_methname,cstring_literals" :
4778 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004779 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004780
Owen Anderson170229f2009-07-14 23:10:40 +00004781 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004782}
4783
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004784// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004785llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004786 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4787}
4788
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004789llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004790 std::string TypeStr;
4791 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4792
4793 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004794
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004795 if (!Entry)
4796 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004797 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004798 ((ObjCABI == 2) ?
4799 "__TEXT,__objc_methtype,cstring_literals" :
4800 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004801 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
Owen Anderson170229f2009-07-14 23:10:40 +00004803 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004804}
4805
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004806llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4807 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004808 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004809 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004810 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004811
4812 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4813
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004814 if (!Entry)
4815 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004816 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004817 ((ObjCABI == 2) ?
4818 "__TEXT,__objc_methtype,cstring_literals" :
4819 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004820 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004821
Owen Anderson170229f2009-07-14 23:10:40 +00004822 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004823}
4824
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004825// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004826llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004827 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004828
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004829 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004830 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004831 llvm::ConstantDataArray::getString(VMContext,
4832 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004833 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004834 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004835
Owen Anderson170229f2009-07-14 23:10:40 +00004836 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004837}
4838
4839// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004840// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004841llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4843 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004844 std::string TypeStr;
4845 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004846 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4847}
4848
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004849void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004850 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004851 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004852 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004853 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004854 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4855 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004856 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004857 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004858 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004859 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004860}
4861
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004862void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004863 EmitModuleInfo();
4864
Daniel Dunbarc475d422008-10-29 22:36:39 +00004865 // Emit the dummy bodies for any protocols which were referenced but
4866 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004867 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004868 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4869 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004870 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004871
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004872 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004873 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004874 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004875 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004876 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004877 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004878 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004879 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004880 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004881 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004882 }
4883
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004884 // Add assembler directives to add lazy undefined symbol references
4885 // for classes which are referenced but not defined. This is
4886 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004887 //
4888 // FIXME: It would be nice if we had an LLVM construct for this.
4889 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004890 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004891 Asm += CGM.getModule().getModuleInlineAsm();
4892 if (!Asm.empty() && Asm.back() != '\n')
4893 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004894
Daniel Dunbard027a922009-09-07 00:20:42 +00004895 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004896 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4897 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004898 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4899 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004900 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004901 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004902 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004903 }
4904
Bill Wendling53136852012-02-07 09:06:01 +00004905 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004906 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4907 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4908 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004909
Daniel Dunbard027a922009-09-07 00:20:42 +00004910 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004911 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004912}
4913
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004914CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004915 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004916 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004917 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004918 ObjCABI = 2;
4919}
4920
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004921/* *** */
4922
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004923ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004924 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4925{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004926 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4927 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004929 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004930 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004931 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004932 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00004933 Int8PtrTy = CGM.Int8PtrTy;
4934 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004935
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004936 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004937 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004938 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004939
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004940 // I'm not sure I like this. The implicit coordination is a bit
4941 // gross. We should solve this in a reasonable fashion because this
4942 // is a pretty common task (match some runtime data structure with
4943 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004944
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004945 // FIXME: This is leaked.
4946 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004947
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004948 // struct _objc_super {
4949 // id self;
4950 // Class cls;
4951 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004952 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004953 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004954 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004955 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004956 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004957 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004958 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004959 Ctx.getObjCClassType(), 0, 0, false,
4960 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004961 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004963 SuperCTy = Ctx.getTagDeclType(RD);
4964 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004966 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004967 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4968
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004969 // struct _prop_t {
4970 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004971 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004972 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004973 PropertyTy = llvm::StructType::create("struct._prop_t",
4974 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004975
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004976 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004977 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004978 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004979 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004980 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004981 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004982 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4983 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004984 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004985 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004986
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004987 // struct _objc_method {
4988 // SEL _cmd;
4989 // char *method_type;
4990 // char *_imp;
4991 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004992 MethodTy = llvm::StructType::create("struct._objc_method",
4993 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4994 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004995
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004996 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004997 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004998 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004999
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005000}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00005001
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005002ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005003 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005004 // struct _objc_method_description {
5005 // SEL name;
5006 // char *types;
5007 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005008 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005009 llvm::StructType::create("struct._objc_method_description",
5010 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005011
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005012 // struct _objc_method_description_list {
5013 // int count;
5014 // struct _objc_method_description[1];
5015 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005017 llvm::StructType::create("struct._objc_method_description_list",
5018 IntTy,
5019 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005020
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005021 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005022 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005023 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005024
Daniel Dunbarb036db82008-08-13 03:21:16 +00005025 // Protocol description structures
5026
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005027 // struct _objc_protocol_extension {
5028 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5029 // struct _objc_method_description_list *optional_instance_methods;
5030 // struct _objc_method_description_list *optional_class_methods;
5031 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005032 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005033 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005034 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005035 llvm::StructType::create("struct._objc_protocol_extension",
5036 IntTy, MethodDescriptionListPtrTy,
5037 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005038 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005039
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005040 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005041 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005042
Daniel Dunbarc475d422008-10-29 22:36:39 +00005043 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00005044
Chris Lattnera5f58b02011-07-09 17:41:47 +00005045 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005046 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00005047
Chris Lattnera5f58b02011-07-09 17:41:47 +00005048 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005049 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005050 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005051 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00005052 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005053 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005054
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005055 // struct _objc_protocol {
5056 // struct _objc_protocol_extension *isa;
5057 // char *protocol_name;
5058 // struct _objc_protocol **_objc_protocol_list;
5059 // struct _objc_method_description_list *instance_methods;
5060 // struct _objc_method_description_list *class_methods;
5061 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005062 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5063 llvm::PointerType::getUnqual(ProtocolListTy),
5064 MethodDescriptionListPtrTy,
5065 MethodDescriptionListPtrTy,
5066 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005067
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005068 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005069 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00005070
Owen Anderson9793f0e2009-07-29 22:16:19 +00005071 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005072
5073 // Class description structures
5074
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005075 // struct _objc_ivar {
5076 // char *ivar_name;
5077 // char *ivar_type;
5078 // int ivar_offset;
5079 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005080 IvarTy = llvm::StructType::create("struct._objc_ivar",
5081 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005082
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005083 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005084 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005085 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005086 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005087
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005088 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005089 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005090 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00005091 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005092
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005093 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005094 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005095 llvm::StructType::create("struct._objc_class_extension",
5096 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005097 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005098
Chris Lattner5ec04a52011-08-12 17:43:31 +00005099 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005100
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005101 // struct _objc_class {
5102 // Class isa;
5103 // Class super_class;
5104 // char *name;
5105 // long version;
5106 // long info;
5107 // long instance_size;
5108 // struct _objc_ivar_list *ivars;
5109 // struct _objc_method_list *methods;
5110 // struct _objc_cache *cache;
5111 // struct _objc_protocol_list *protocols;
5112 // char *ivar_layout;
5113 // struct _objc_class_ext *ext;
5114 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005115 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5116 llvm::PointerType::getUnqual(ClassTy),
5117 Int8PtrTy,
5118 LongTy,
5119 LongTy,
5120 LongTy,
5121 IvarListPtrTy,
5122 MethodListPtrTy,
5123 CachePtrTy,
5124 ProtocolListPtrTy,
5125 Int8PtrTy,
5126 ClassExtensionPtrTy,
5127 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005128
Owen Anderson9793f0e2009-07-29 22:16:19 +00005129 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005130
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005131 // struct _objc_category {
5132 // char *category_name;
5133 // char *class_name;
5134 // struct _objc_method_list *instance_method;
5135 // struct _objc_method_list *class_method;
5136 // uint32_t size; // sizeof(struct _objc_category)
5137 // struct _objc_property_list *instance_properties;// category's @property
5138 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005139 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005140 llvm::StructType::create("struct._objc_category",
5141 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5142 MethodListPtrTy, ProtocolListPtrTy,
5143 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00005144
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005145 // Global metadata structures
5146
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00005147 // struct _objc_symtab {
5148 // long sel_ref_cnt;
5149 // SEL *refs;
5150 // short cls_def_cnt;
5151 // short cat_def_cnt;
5152 // char *defs[cls_def_cnt + cat_def_cnt];
5153 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005154 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005155 llvm::StructType::create("struct._objc_symtab",
5156 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5157 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005158 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00005159
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00005160 // struct _objc_module {
5161 // long version;
5162 // long size; // sizeof(struct _objc_module)
5163 // char *name;
5164 // struct _objc_symtab* symtab;
5165 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005166 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005167 llvm::StructType::create("struct._objc_module",
5168 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00005169
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005170
Mike Stump18bb9282009-05-16 07:57:57 +00005171 // FIXME: This is the size of the setjmp buffer and should be target
5172 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00005173 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005174
Anders Carlsson9ff22482008-09-09 10:10:21 +00005175 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00005176 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005177
5178 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005179 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00005180 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5181 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00005182
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005183}
5184
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005185ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00005186 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005187 // struct _method_list_t {
5188 // uint32_t entsize; // sizeof(struct _objc_method)
5189 // uint32_t method_count;
5190 // struct _objc_method method_list[method_count];
5191 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005192 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005193 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5194 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005195 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005196 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005197
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005198 // struct _protocol_t {
5199 // id isa; // NULL
5200 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005201 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005202 // const struct method_list_t * const instance_methods;
5203 // const struct method_list_t * const class_methods;
5204 // const struct method_list_t *optionalInstanceMethods;
5205 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005206 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005207 // const uint32_t size; // sizeof(struct _protocol_t)
5208 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005209 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005210 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005212 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00005213 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005214 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005215
Chris Lattnera5f58b02011-07-09 17:41:47 +00005216 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005217 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5218 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5219 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5220 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005221 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5222 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005223
5224 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005225 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005227 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005228 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005229 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005230 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005231 ProtocolListnfABITy->setBody(LongTy,
5232 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5233 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005234
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005235 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005236 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005237
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005238 // struct _ivar_t {
5239 // unsigned long int *offset; // pointer to ivar offset location
5240 // char *name;
5241 // char *type;
5242 // uint32_t alignment;
5243 // uint32_t size;
5244 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005245 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005246 llvm::StructType::create("struct._ivar_t",
5247 llvm::PointerType::getUnqual(LongTy),
5248 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005249
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005250 // struct _ivar_list_t {
5251 // uint32 entsize; // sizeof(struct _ivar_t)
5252 // uint32 count;
5253 // struct _iver_t list[count];
5254 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00005255 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005256 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5257 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005258
Owen Anderson9793f0e2009-07-29 22:16:19 +00005259 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005260
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005261 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005262 // uint32_t const flags;
5263 // uint32_t const instanceStart;
5264 // uint32_t const instanceSize;
5265 // uint32_t const reserved; // only when building for 64bit targets
5266 // const uint8_t * const ivarLayout;
5267 // const char *const name;
5268 // const struct _method_list_t * const baseMethods;
5269 // const struct _objc_protocol_list *const baseProtocols;
5270 // const struct _ivar_list_t *const ivars;
5271 // const uint8_t * const weakIvarLayout;
5272 // const struct _prop_list_t * const properties;
5273 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005274
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005275 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00005276 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5277 IntTy, IntTy, IntTy, Int8PtrTy,
5278 Int8PtrTy, MethodListnfABIPtrTy,
5279 ProtocolListnfABIPtrTy,
5280 IvarListnfABIPtrTy,
5281 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005282
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005283 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00005284 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00005285 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5286 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005287
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005288 // struct _class_t {
5289 // struct _class_t *isa;
5290 // struct _class_t * const superclass;
5291 // void *cache;
5292 // IMP *vtable;
5293 // struct class_ro_t *ro;
5294 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005295
Chris Lattner5ec04a52011-08-12 17:43:31 +00005296 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00005297 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5298 llvm::PointerType::getUnqual(ClassnfABITy),
5299 CachePtrTy,
5300 llvm::PointerType::getUnqual(ImpnfABITy),
5301 llvm::PointerType::getUnqual(ClassRonfABITy),
5302 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303
Fariborz Jahanian71394042009-01-23 23:53:38 +00005304 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00005305 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005306
Fariborz Jahanian0232c052009-01-23 01:46:23 +00005307 // struct _category_t {
5308 // const char * const name;
5309 // struct _class_t *const cls;
5310 // const struct _method_list_t * const instance_methods;
5311 // const struct _method_list_t * const class_methods;
5312 // const struct _protocol_list_t * const protocols;
5313 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00005314 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00005315 CategorynfABITy = llvm::StructType::create("struct._category_t",
5316 Int8PtrTy, ClassnfABIPtrTy,
5317 MethodListnfABIPtrTy,
5318 MethodListnfABIPtrTy,
5319 ProtocolListnfABIPtrTy,
5320 PropertyListPtrTy,
5321 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005322
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005323 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005324 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5325 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005327 // MessageRefTy - LLVM for:
5328 // struct _message_ref_t {
5329 // IMP messenger;
5330 // SEL name;
5331 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005332
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005333 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00005334 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00005335 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00005336 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005337 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005338 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005339 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00005340 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00005341 Ctx.getObjCSelType(), 0, 0, false,
5342 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00005343 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005344
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005345 MessageRefCTy = Ctx.getTagDeclType(RD);
5346 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5347 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005348
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005349 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00005350 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005351
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005352 // SuperMessageRefTy - LLVM for:
5353 // struct _super_message_ref_t {
5354 // SUPER_IMP messenger;
5355 // SEL name;
5356 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005357 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005358 llvm::StructType::create("struct._super_message_ref_t",
5359 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005360
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00005361 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005362 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00005363
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005364
5365 // struct objc_typeinfo {
5366 // const void** vtable; // objc_ehtype_vtable + 2
5367 // const char* name; // c++ typeinfo string
5368 // Class cls;
5369 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00005370 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00005371 llvm::StructType::create("struct._objc_typeinfo",
5372 llvm::PointerType::getUnqual(Int8PtrTy),
5373 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00005374 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005375}
5376
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005377llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005378 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005379
Fariborz Jahanian71394042009-01-23 23:53:38 +00005380 return NULL;
5381}
5382
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005383void CGObjCNonFragileABIMac::
5384AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5385 const char *SymbolName,
5386 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00005387 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005388
Daniel Dunbar19573e72009-05-15 21:48:48 +00005389 if (!NumClasses)
5390 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005391
Chris Lattner3def9ae2012-02-06 22:16:34 +00005392 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005393 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00005394 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00005395 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00005396 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00005397 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00005398 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00005399 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005400
Daniel Dunbar19573e72009-05-15 21:48:48 +00005401 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005402 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005403 llvm::GlobalValue::InternalLinkage,
5404 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005405 SymbolName);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005406 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00005407 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005408 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00005409}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005410
Fariborz Jahanian71394042009-01-23 23:53:38 +00005411void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5412 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413
Daniel Dunbar19573e72009-05-15 21:48:48 +00005414 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005415 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005416 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005417 "\01L_OBJC_LABEL_CLASS_$",
5418 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00005419
Bill Wendling53136852012-02-07 09:06:01 +00005420 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00005421 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5422 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5423 continue;
5424 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005425 }
5426
Bill Wendling53136852012-02-07 09:06:01 +00005427 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005428 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5429 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5430 continue;
5431 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5432 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00005433
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005435 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5436 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005437
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005438 // Build list of all implemented category addresses in array
5439 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005440 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00005441 "\01L_OBJC_LABEL_CATEGORY_$",
5442 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005443 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005444 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5445 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005446
Daniel Dunbar5e639272010-04-25 20:39:01 +00005447 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005448}
5449
John McCall9e8bb002011-05-14 03:10:52 +00005450/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5451/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005453/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00005454bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5455 // At various points we've experimented with using vtable-based
5456 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005457 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005458 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00005459 return false;
John McCall9e8bb002011-05-14 03:10:52 +00005460 case CodeGenOptions::NonLegacy:
5461 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005462 case CodeGenOptions::Mixed:
5463 break;
5464 }
5465
5466 // If so, see whether this selector is in the white-list of things which must
5467 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00005468 if (VTableDispatchMethods.empty()) {
5469 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5470 VTableDispatchMethods.insert(GetNullarySelector("class"));
5471 VTableDispatchMethods.insert(GetNullarySelector("self"));
5472 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5473 VTableDispatchMethods.insert(GetNullarySelector("length"));
5474 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005475
John McCall9e8bb002011-05-14 03:10:52 +00005476 // These are vtable-based if GC is disabled.
5477 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005478 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00005479 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5480 VTableDispatchMethods.insert(GetNullarySelector("release"));
5481 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5482 }
5483
5484 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5485 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5486 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5487 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5488 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5489 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5490 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5491
5492 // These are vtable-based if GC is enabled.
5493 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005494 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00005495 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5496 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5497
5498 // "countByEnumeratingWithState:objects:count"
5499 IdentifierInfo *KeyIdents[] = {
5500 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5501 &CGM.getContext().Idents.get("objects"),
5502 &CGM.getContext().Idents.get("count")
5503 };
5504 VTableDispatchMethods.insert(
5505 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5506 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005507 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00005508
John McCall9e8bb002011-05-14 03:10:52 +00005509 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005510}
5511
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005512/// BuildClassRoTInitializer - generate meta-data for:
5513/// struct _class_ro_t {
5514/// uint32_t const flags;
5515/// uint32_t const instanceStart;
5516/// uint32_t const instanceSize;
5517/// uint32_t const reserved; // only when building for 64bit targets
5518/// const uint8_t * const ivarLayout;
5519/// const char *const name;
5520/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005521/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005522/// const struct _ivar_list_t *const ivars;
5523/// const uint8_t * const weakIvarLayout;
5524/// const struct _prop_list_t * const properties;
5525/// }
5526///
5527llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005528 unsigned flags,
5529 unsigned InstanceStart,
5530 unsigned InstanceSize,
5531 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005532 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005533 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00005534
David Blaikiebbafb8a2012-03-11 07:00:24 +00005535 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallef19dbb2012-10-17 04:53:23 +00005536 flags |= NonFragileABI_Class_CompiledByARC;
John McCall31168b02011-06-15 23:02:42 +00005537
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005538 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5539 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5540 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005541 // FIXME. For 64bit targets add 0 here.
John McCallef19dbb2012-10-17 04:53:23 +00005542 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5543 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005545 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005546 // const struct _method_list_t * const baseMethods;
5547 std::vector<llvm::Constant*> Methods;
5548 std::string MethodListName("\01l_OBJC_$_");
John McCallef19dbb2012-10-17 04:53:23 +00005549 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005550 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005551 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005552 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005553 // Class methods should always be defined.
5554 Methods.push_back(GetMethodConstant(*i));
5555 }
5556 } else {
5557 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005558 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005559 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005560 // Instance methods should always be defined.
5561 Methods.push_back(GetMethodConstant(*i));
5562 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005563 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005564 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005565 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005566
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005567 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5568 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005569
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005570 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5571 if (llvm::Constant *C = GetMethodConstant(MD))
5572 Methods.push_back(C);
5573 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5574 if (llvm::Constant *C = GetMethodConstant(MD))
5575 Methods.push_back(C);
5576 }
5577 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005578 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579 Values[ 5] = EmitMethodList(MethodListName,
5580 "__DATA, __objc_const", Methods);
5581
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005582 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5583 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005584 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005585 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005586 OID->all_referenced_protocol_begin(),
5587 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005588
John McCallef19dbb2012-10-17 04:53:23 +00005589 if (flags & NonFragileABI_Class_Meta) {
Owen Anderson0b75f232009-07-31 20:28:54 +00005590 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005591 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Anderson0b75f232009-07-31 20:28:54 +00005592 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCallef19dbb2012-10-17 04:53:23 +00005593 } else {
5594 Values[ 7] = EmitIvarList(ID);
5595 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005596 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5597 ID, ID->getClassInterface(), ObjCTypes);
John McCallef19dbb2012-10-17 04:53:23 +00005598 }
Owen Anderson0e0189d2009-07-27 22:29:56 +00005599 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005600 Values);
5601 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005602 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5603 llvm::GlobalValue::InternalLinkage,
5604 Init,
John McCallef19dbb2012-10-17 04:53:23 +00005605 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005606 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5607 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005608 CLASS_RO_GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005609 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005610 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005611 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005612
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005613}
5614
5615/// BuildClassMetaData - This routine defines that to-level meta-data
5616/// for the given ClassName for:
5617/// struct _class_t {
5618/// struct _class_t *isa;
5619/// struct _class_t * const superclass;
5620/// void *cache;
5621/// IMP *vtable;
5622/// struct class_ro_t *ro;
5623/// }
5624///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005625llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005626 std::string &ClassName,
5627 llvm::Constant *IsAGV,
5628 llvm::Constant *SuperClassGV,
5629 llvm::Constant *ClassRoGV,
5630 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005631 llvm::Constant *Values[] = {
5632 IsAGV,
5633 SuperClassGV,
5634 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5635 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5636 ClassRoGV // &CLASS_RO_GV
5637 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005638 if (!Values[1])
5639 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005640 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005641 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005642 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5643 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005644 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005645 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005646 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005647 if (HiddenVisibility)
5648 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005649 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005650}
5651
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005652bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005653CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005654 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005655}
5656
Daniel Dunbar961202372009-05-03 12:57:56 +00005657void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005658 uint32_t &InstanceStart,
5659 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005660 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005661 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005662
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005663 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005664 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005665
5666 // If there are no fields, the start is the same as the end.
5667 if (!RL.getFieldCount())
5668 InstanceStart = InstanceSize;
5669 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005670 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005671}
5672
Fariborz Jahanian71394042009-01-23 23:53:38 +00005673void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5674 std::string ClassName = ID->getNameAsString();
5675 if (!ObjCEmptyCacheVar) {
5676 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005677 CGM.getModule(),
5678 ObjCTypes.CacheTy,
5679 false,
5680 llvm::GlobalValue::ExternalLinkage,
5681 0,
5682 "_objc_empty_cache");
5683
Fariborz Jahanian71394042009-01-23 23:53:38 +00005684 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005685 CGM.getModule(),
5686 ObjCTypes.ImpnfABITy,
5687 false,
5688 llvm::GlobalValue::ExternalLinkage,
5689 0,
5690 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005691 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005692 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005693 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005694 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005695 uint32_t InstanceStart =
Micah Villmowdd31ca12012-10-08 16:25:52 +00005696 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005697 uint32_t InstanceSize = InstanceStart;
John McCallef19dbb2012-10-17 04:53:23 +00005698 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005699 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5700 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005702 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703
John McCall0d54a172012-10-17 04:53:31 +00005704 // Build the flags for the metaclass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005705 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005706 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005707 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005708 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005709
5710 // FIXME: why is this flag set on the metaclass?
5711 // ObjC metaclasses have no fields and don't really get constructed.
5712 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005713 flags |= NonFragileABI_Class_HasCXXStructors;
John McCall0d54a172012-10-17 04:53:31 +00005714 if (!ID->hasNonZeroConstructors())
5715 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5716 }
5717
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005718 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005719 // class is root
John McCallef19dbb2012-10-17 04:53:23 +00005720 flags |= NonFragileABI_Class_Root;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005721 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005722 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005723 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005724 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005725 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5726 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5727 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005728 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005729 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005730 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005731 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005732 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005733 ObjCMetaClassName +
5734 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005735 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005736 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005737 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005738 }
5739 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5740 InstanceStart,
5741 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005742 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005743 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005744 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5745 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005746 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005747
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005748 // Metadata for the class
John McCallef19dbb2012-10-17 04:53:23 +00005749 flags = 0;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005750 if (classIsHidden)
John McCallef19dbb2012-10-17 04:53:23 +00005751 flags |= NonFragileABI_Class_Hidden;
John McCall0d54a172012-10-17 04:53:31 +00005752
5753 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCallef19dbb2012-10-17 04:53:23 +00005754 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005755
John McCall0d54a172012-10-17 04:53:31 +00005756 // Set a flag to enable a runtime optimization when a class has
5757 // fields that require destruction but which don't require
5758 // anything except zero-initialization during construction. This
5759 // is most notably true of __strong and __weak types, but you can
5760 // also imagine there being C++ types with non-trivial default
5761 // constructors that merely set all fields to null.
5762 if (!ID->hasNonZeroConstructors())
5763 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5764 }
5765
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005766 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCallef19dbb2012-10-17 04:53:23 +00005767 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005768
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005769 if (!ID->getClassInterface()->getSuperClass()) {
John McCallef19dbb2012-10-17 04:53:23 +00005770 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005771 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005772 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005773 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005774 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005775 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005776 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005777 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005778 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005779 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005780 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005781 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005782 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005783 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005784 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005785
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005786 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005787 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005788 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5789 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005790 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005791
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005792 // Determine if this class is also "non-lazy".
5793 if (ImplementationIsNonLazy(ID))
5794 DefinedNonLazyClasses.push_back(ClassMD);
5795
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005796 // Force the definition of the EHType if necessary.
John McCallef19dbb2012-10-17 04:53:23 +00005797 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005798 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005799 // Make sure method definition entries are all clear for next implementation.
5800 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005801}
5802
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005803/// GenerateProtocolRef - This routine is called to generate code for
5804/// a protocol reference expression; as in:
5805/// @code
5806/// @protocol(Proto1);
5807/// @endcode
5808/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5809/// which will hold address of the protocol meta-data.
5810///
5811llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005812 const ObjCProtocolDecl *PD) {
5813
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005814 // This routine is called for @protocol only. So, we must build definition
5815 // of protocol's meta-data (not a reference to it!)
5816 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005817 llvm::Constant *Init =
5818 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005819 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005820
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005821 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005822 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005823
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005824 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5825 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005826 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005827 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005828 CGM.getModule(),
5829 Init->getType(), false,
5830 llvm::GlobalValue::WeakAnyLinkage,
5831 Init,
5832 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005833 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5834 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005835 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005836 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005837}
5838
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005839/// GenerateCategory - Build metadata for a category implementation.
5840/// struct _category_t {
5841/// const char * const name;
5842/// struct _class_t *const cls;
5843/// const struct _method_list_t * const instance_methods;
5844/// const struct _method_list_t * const class_methods;
5845/// const struct _protocol_list_t * const protocols;
5846/// const struct _prop_list_t * const properties;
5847/// }
5848///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005849void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005850 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005851 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005852 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5853 "_$_" + OCD->getNameAsString());
5854 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005855 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005856
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005857 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005858 Values[0] = GetClassName(OCD->getIdentifier());
5859 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005860 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005861 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005862 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5863
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005864 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005865 std::vector<llvm::Constant*> Methods;
5866 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005867 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005868 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005869
5870 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005871 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005872 // Instance methods should always be defined.
5873 Methods.push_back(GetMethodConstant(*i));
5874 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875
5876 Values[2] = EmitMethodList(MethodListName,
5877 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005878 Methods);
5879
5880 MethodListName = Prefix;
5881 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5882 OCD->getNameAsString();
5883 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005884 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005885 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005886 // Class methods should always be defined.
5887 Methods.push_back(GetMethodConstant(*i));
5888 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005889
5890 Values[3] = EmitMethodList(MethodListName,
5891 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005892 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005893 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005894 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005895 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005896 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005897 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5898 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005899 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005900 + Interface->getName() + "_$_"
5901 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005902 Category->protocol_begin(),
5903 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005904 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5905 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005906 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005907 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5908 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005909 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005910
5911 llvm::Constant *Init =
5912 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005913 Values);
5914 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005915 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005916 false,
5917 llvm::GlobalValue::InternalLinkage,
5918 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005919 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005920 GCATV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00005921 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005922 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005923 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005924 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005925
5926 // Determine if this category is also "non-lazy".
5927 if (ImplementationIsNonLazy(OCD))
5928 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005929 // method definition entries must be clear for next implementation.
5930 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005931}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005932
5933/// GetMethodConstant - Return a struct objc_method constant for the
5934/// given method if it has been defined. The result is null if the
5935/// method has not been defined. The return value has type MethodPtrTy.
5936llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005937 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005938 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005939 if (!Fn)
5940 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005941
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005942 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005943 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005944 ObjCTypes.SelectorPtrTy),
5945 GetMethodVarType(MD),
5946 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5947 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005948 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005949}
5950
5951/// EmitMethodList - Build meta-data for method declarations
5952/// struct _method_list_t {
5953/// uint32_t entsize; // sizeof(struct _objc_method)
5954/// uint32_t method_count;
5955/// struct _objc_method method_list[method_count];
5956/// }
5957///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005958llvm::Constant *
5959CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5960 const char *Section,
5961 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005962 // Return null for empty list.
5963 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005964 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005965
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005966 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005967 // sizeof(struct _objc_method)
Micah Villmowdd31ca12012-10-08 16:25:52 +00005968 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005969 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005970 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005971 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005972 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005973 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005974 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005975 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005976
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005977 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005978 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005979 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmowdd31ca12012-10-08 16:25:52 +00005980 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005981 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005982 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005983 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005984}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005985
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005986/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5987/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005988llvm::GlobalVariable *
5989CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5990 const ObjCIvarDecl *Ivar) {
5991 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005992 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005993 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005994 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005995 CGM.getModule().getGlobalVariable(Name);
5996 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005997 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005998 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005999 false,
6000 llvm::GlobalValue::ExternalLinkage,
6001 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006002 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00006003 return IvarOffsetGV;
6004}
6005
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00006006llvm::Constant *
6007CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6008 const ObjCIvarDecl *Ivar,
6009 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006010 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006011 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006012 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006013 IvarOffsetGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006014 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006015
Mike Stump18bb9282009-05-16 07:57:57 +00006016 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6017 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00006018 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6019 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00006020 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00006021 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00006022 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00006023 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00006024 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006025 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006026}
6027
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006028/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006029/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006030/// IvarListnfABIPtrTy.
6031/// struct _ivar_t {
6032/// unsigned long int *offset; // pointer to ivar offset location
6033/// char *name;
6034/// char *type;
6035/// uint32_t alignment;
6036/// uint32_t size;
6037/// }
6038/// struct _ivar_list_t {
6039/// uint32 entsize; // sizeof(struct _ivar_t)
6040/// uint32 count;
6041/// struct _iver_t list[count];
6042/// }
6043///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00006044
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006045llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006046 const ObjCImplementationDecl *ID) {
6047
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006048 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006049
Jordy Rosea91768e2011-07-22 02:08:32 +00006050 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006051 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006052
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006053 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006054
Jordy Rosea91768e2011-07-22 02:08:32 +00006055 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00006056 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00006057 // Ignore unnamed bit-fields.
6058 if (!IVD->getDeclName())
6059 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006060 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006061 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00006062 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006063 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6064 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00006065 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00006066 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00006067 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006068 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006069 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006070 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006071 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00006072 // NOTE. Size of a bitfield does not match gcc's, because of the
6073 // way bitfields are treated special in each. But I am told that
6074 // 'size' for bitfield ivars is ignored by the runtime so it does
6075 // not matter. If it matters, there is enough info to get the
6076 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006077 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006078 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006079 }
6080 // Return null for empty list.
6081 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00006082 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006083
6084 llvm::Constant *Values[3];
Micah Villmowdd31ca12012-10-08 16:25:52 +00006085 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006086 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6087 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00006088 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006089 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00006090 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006091 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006092 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6093 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006094 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00006095 llvm::GlobalValue::InternalLinkage,
6096 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006097 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006098 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006099 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00006100 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006101
Chris Lattnerf56501c2009-07-17 23:57:13 +00006102 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00006103 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006104}
6105
6106llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006107 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006108 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006109
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006110 if (!Entry) {
6111 // We use the initializer as a marker of whether this is a forward
6112 // reference or not. At module finalization we add the empty
6113 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006114 Entry =
6115 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6116 llvm::GlobalValue::ExternalLinkage,
6117 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006118 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006119 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006120 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006121
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006122 return Entry;
6123}
6124
6125/// GetOrEmitProtocol - Generate the protocol meta-data:
6126/// @code
6127/// struct _protocol_t {
6128/// id isa; // NULL
6129/// const char * const protocol_name;
6130/// const struct _protocol_list_t * protocol_list; // super protocols
6131/// const struct method_list_t * const instance_methods;
6132/// const struct method_list_t * const class_methods;
6133/// const struct method_list_t *optionalInstanceMethods;
6134/// const struct method_list_t *optionalClassMethods;
6135/// const struct _prop_list_t * properties;
6136/// const uint32_t size; // sizeof(struct _protocol_t)
6137/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006138/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006139/// }
6140/// @endcode
6141///
6142
6143llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006144 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00006145 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006146
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006147 // Early exit if a defining object has already been generated.
6148 if (Entry && Entry->hasInitializer())
6149 return Entry;
6150
Douglas Gregora715bff2012-01-01 19:51:50 +00006151 // Use the protocol definition, if there is one.
6152 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6153 PD = Def;
6154
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006155 // Construct method lists.
6156 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6157 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006158 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006159 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006160 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006161 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006162 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006163 if (!C)
6164 return GetOrEmitProtocolRef(PD);
6165
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006166 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6167 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006168 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006169 } else {
6170 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006171 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006172 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006173 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006174
6175 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00006176 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006177 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006178 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006179 if (!C)
6180 return GetOrEmitProtocolRef(PD);
6181
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006182 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6183 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006184 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006185 } else {
6186 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006187 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006188 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006189 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006190
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006191 MethodTypesExt.insert(MethodTypesExt.end(),
6192 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6193
6194 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006195 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00006196 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006197 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006198 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6199 PD->protocol_begin(),
6200 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006201
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006202 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006203 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006204 "__DATA, __objc_const",
6205 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006206 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006207 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006208 "__DATA, __objc_const",
6209 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006210 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006211 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006212 "__DATA, __objc_const",
6213 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006214 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006215 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006216 "__DATA, __objc_const",
6217 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006218 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006219 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006220 uint32_t Size =
Micah Villmowdd31ca12012-10-08 16:25:52 +00006221 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006222 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00006223 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00006224 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6225 + PD->getName(),
6226 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006227 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006228 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006229
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006230 if (Entry) {
6231 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00006232 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006233 Entry->setInitializer(Init);
6234 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006235 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006236 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6237 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6238 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006239 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006240 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006241 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00006242
6243 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006244 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006245 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006246 CGM.AddUsedGlobal(Entry);
6247
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006248 // Use this protocol meta-data to build protocol list table in section
6249 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006250 llvm::GlobalVariable *PTGV =
6251 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6252 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6253 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006254 PTGV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006255 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00006256 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00006257 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00006258 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006259 return Entry;
6260}
6261
6262/// EmitProtocolList - Generate protocol list meta-data:
6263/// @code
6264/// struct _protocol_list_t {
6265/// long protocol_count; // Note, this is 32/64 bit
6266/// struct _protocol_t[protocol_count];
6267/// }
6268/// @endcode
6269///
6270llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006271CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006272 ObjCProtocolDecl::protocol_iterator begin,
6273 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00006274 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006275
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006276 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006277 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00006278 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006279
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006280 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00006281 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006282 Name.toVector(TmpName);
6283 llvm::GlobalVariable *GV =
6284 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006285 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006286 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006287
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006288 for (; begin != end; ++begin)
6289 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6290
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006291 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00006292 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006293 ObjCTypes.ProtocolnfABIPtrTy));
6294
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006295 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00006296 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00006297 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006298 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00006299 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6300 ProtocolRefs.size()),
6301 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006302
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006303 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00006304 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006305 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006306 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006307 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00006308 GV->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006309 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00006310 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006311 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00006312 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00006313}
6314
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006315/// GetMethodDescriptionConstant - This routine build following meta-data:
6316/// struct _objc_method {
6317/// SEL _cmd;
6318/// char *method_type;
6319/// char *_imp;
6320/// }
6321
6322llvm::Constant *
6323CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006324 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00006325 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006326 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6327 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006328 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00006329 if (!Desc[1])
6330 return 0;
6331
Fariborz Jahanian097feda2009-01-30 18:58:59 +00006332 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00006333 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00006334 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00006335}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006336
6337/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6338/// This code gen. amounts to generating code for:
6339/// @code
6340/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6341/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006342///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00006343LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00006344 CodeGen::CodeGenFunction &CGF,
6345 QualType ObjectTy,
6346 llvm::Value *BaseValue,
6347 const ObjCIvarDecl *Ivar,
6348 unsigned CVRQualifiers) {
6349 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006350 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6351 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6352 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6353 llvm::MDNode::get(VMContext,
6354 ArrayRef<llvm::Value*>()));
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00006355 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00006356 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00006357}
6358
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006359llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006360 CodeGen::CodeGenFunction &CGF,
6361 const ObjCInterfaceDecl *Interface,
6362 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00006363 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00006364}
6365
John McCall234eac82011-05-13 23:16:18 +00006366static void appendSelectorForMessageRefTable(std::string &buffer,
6367 Selector selector) {
6368 if (selector.isUnarySelector()) {
6369 buffer += selector.getNameForSlot(0);
6370 return;
6371 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006372
John McCall234eac82011-05-13 23:16:18 +00006373 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6374 buffer += selector.getNameForSlot(i);
6375 buffer += '_';
6376 }
6377}
6378
John McCall9e8bb002011-05-14 03:10:52 +00006379/// Emit a "v-table" message send. We emit a weak hidden-visibility
6380/// struct, initially containing the selector pointer and a pointer to
6381/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6382/// load and call the function pointer, passing the address of the
6383/// struct as the second parameter. The runtime determines whether
6384/// the selector is currently emitted using vtable dispatch; if so, it
6385/// substitutes a stub function which simply tail-calls through the
6386/// appropriate vtable slot, and if not, it substitues a stub function
6387/// which tail-calls objc_msgSend. Both stubs adjust the selector
6388/// argument to correctly point to the selector.
6389RValue
6390CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6391 ReturnValueSlot returnSlot,
6392 QualType resultType,
6393 Selector selector,
6394 llvm::Value *arg0,
6395 QualType arg0Type,
6396 bool isSuper,
6397 const CallArgList &formalArgs,
6398 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00006399 // Compute the actual arguments.
6400 CallArgList args;
6401
John McCall9e8bb002011-05-14 03:10:52 +00006402 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00006403 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00006404 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6405 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00006406
John McCall9e8bb002011-05-14 03:10:52 +00006407 // Second argument: a pointer to the message ref structure. Leave
6408 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00006409 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6410
6411 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6412
John McCalla729c622012-02-17 03:33:10 +00006413 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00006414
John McCall5880fb82011-05-14 21:12:11 +00006415 NullReturnState nullReturn;
6416
John McCall9e8bb002011-05-14 03:10:52 +00006417 // Find the function to call and the mangled name for the message
6418 // ref structure. Using a different mangled name wouldn't actually
6419 // be a problem; it would just be a waste.
6420 //
6421 // The runtime currently never uses vtable dispatch for anything
6422 // except normal, non-super message-sends.
6423 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00006424 llvm::Constant *fn = 0;
6425 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00006426 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00006427 if (isSuper) {
6428 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6429 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006430 } else {
John McCall5880fb82011-05-14 21:12:11 +00006431 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00006432 fn = ObjCTypes.getMessageSendStretFixupFn();
6433 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006434 }
John McCall234eac82011-05-13 23:16:18 +00006435 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6436 fn = ObjCTypes.getMessageSendFpretFixupFn();
6437 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00006438 } else {
John McCall234eac82011-05-13 23:16:18 +00006439 if (isSuper) {
6440 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6441 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006442 } else {
John McCall234eac82011-05-13 23:16:18 +00006443 fn = ObjCTypes.getMessageSendFixupFn();
6444 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00006445 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00006446 }
John McCall234eac82011-05-13 23:16:18 +00006447 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6448 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006449
John McCall234eac82011-05-13 23:16:18 +00006450 // Append the selector name, except use underscores anywhere we
6451 // would have used colons.
6452 appendSelectorForMessageRefTable(messageRefName, selector);
6453
6454 llvm::GlobalVariable *messageRef
6455 = CGM.getModule().getGlobalVariable(messageRefName);
6456 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00006457 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00006458 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00006459 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00006460 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6461 init->getType(),
6462 /*constant*/ false,
6463 llvm::GlobalValue::WeakAnyLinkage,
6464 init,
6465 messageRefName);
6466 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6467 messageRef->setAlignment(16);
6468 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6469 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006470
6471 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00006472 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006473 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6474 e = method->param_end(); i != e; ++i) {
6475 const ParmVarDecl *ParamDecl = (*i);
6476 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6477 if (!nullReturn.NullBB)
6478 nullReturn.init(CGF, arg0);
6479 requiresnullCheck = true;
6480 break;
6481 }
6482 }
6483
John McCall234eac82011-05-13 23:16:18 +00006484 llvm::Value *mref =
6485 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6486
John McCall9e8bb002011-05-14 03:10:52 +00006487 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00006488 args[1].RV = RValue::get(mref);
6489
6490 // Load the function to call from the message ref table.
6491 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6492 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6493
John McCalla729c622012-02-17 03:33:10 +00006494 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00006495
John McCalla729c622012-02-17 03:33:10 +00006496 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00006497 return nullReturn.complete(CGF, result, resultType, formalArgs,
6498 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006499}
6500
6501/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006502CodeGen::RValue
6503CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006504 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006505 QualType ResultType,
6506 Selector Sel,
6507 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006508 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00006509 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006510 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00006511 return isVTableDispatchedSelector(Sel)
6512 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006513 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006514 false, CallArgs, Method)
6515 : EmitMessageSend(CGF, Return, ResultType,
6516 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006517 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00006518 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00006519}
6520
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006521llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006522CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006523 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6524
Daniel Dunbara6468342009-03-02 05:18:14 +00006525 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006526 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006527 false, llvm::GlobalValue::ExternalLinkage,
6528 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006529 }
6530
6531 return GV;
6532}
6533
John McCall31168b02011-06-15 23:02:42 +00006534llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6535 IdentifierInfo *II) {
6536 llvm::GlobalVariable *&Entry = ClassReferences[II];
6537
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006538 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00006539 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00006540 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006541 Entry =
John McCall31168b02011-06-15 23:02:42 +00006542 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6543 false, llvm::GlobalValue::InternalLinkage,
6544 ClassGV,
6545 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006546 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006547 CGM.getDataLayout().getABITypeAlignment(
John McCall31168b02011-06-15 23:02:42 +00006548 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006549 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006550 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006551 }
John McCall31168b02011-06-15 23:02:42 +00006552
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006553 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006554}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006555
John McCall31168b02011-06-15 23:02:42 +00006556llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6557 const ObjCInterfaceDecl *ID) {
6558 return EmitClassRefFromId(Builder, ID->getIdentifier());
6559}
6560
6561llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6562 CGBuilderTy &Builder) {
6563 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6564 return EmitClassRefFromId(Builder, II);
6565}
6566
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006567llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006568CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006569 const ObjCInterfaceDecl *ID) {
6570 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006571
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006572 if (!Entry) {
6573 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6574 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006575 Entry =
6576 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006577 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006578 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006579 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006580 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006581 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006582 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006583 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006584 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006586
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006587 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006588}
6589
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006590/// EmitMetaClassRef - Return a Value * of the address of _class_t
6591/// meta-data
6592///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006593llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6594 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006595 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6596 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006597 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006598
Daniel Dunbar15894b72009-04-07 05:48:37 +00006599 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006600 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006601 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006602 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006603 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006604 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006605 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006606 Entry->setAlignment(
Micah Villmowdd31ca12012-10-08 16:25:52 +00006607 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006608 ObjCTypes.ClassnfABIPtrTy));
6609
Daniel Dunbare60aa052009-04-15 19:03:14 +00006610 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006611 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006612
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006613 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006614}
6615
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006616/// GetClass - Return a reference to the class for the given interface
6617/// decl.
6618llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6619 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006620 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006621 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6622 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6623 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6624 }
6625
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006626 return EmitClassRef(Builder, ID);
6627}
6628
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006629/// Generates a message send where the super is the receiver. This is
6630/// a message send to self with special delivery semantics indicating
6631/// which class's method should be called.
6632CodeGen::RValue
6633CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006634 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006635 QualType ResultType,
6636 Selector Sel,
6637 const ObjCInterfaceDecl *Class,
6638 bool isCategoryImpl,
6639 llvm::Value *Receiver,
6640 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006641 const CodeGen::CallArgList &CallArgs,
6642 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006643 // ...
6644 // Create and init a super structure; this is a (receiver, class)
6645 // pair we will pass to objc_msgSendSuper.
6646 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006647 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006648
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006649 llvm::Value *ReceiverAsObject =
6650 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6651 CGF.Builder.CreateStore(ReceiverAsObject,
6652 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006653
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006654 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006655 llvm::Value *Target;
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006656 if (IsClassMessage)
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006657 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanian27678b02012-10-10 23:11:18 +00006658 else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006659 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006660
Mike Stump18bb9282009-05-16 07:57:57 +00006661 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6662 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006663 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006664 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6665 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6666 CGF.Builder.CreateStore(Target,
6667 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006668
John McCall9e8bb002011-05-14 03:10:52 +00006669 return (isVTableDispatchedSelector(Sel))
6670 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006671 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006672 true, CallArgs, Method)
6673 : EmitMessageSend(CGF, Return, ResultType,
6674 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006675 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006676 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006677}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006678
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006679llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006680 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006681 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006682
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006683 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006684 llvm::Constant *Casted =
6685 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6686 ObjCTypes.SelectorPtrTy);
6687 Entry =
6688 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6689 llvm::GlobalValue::InternalLinkage,
6690 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006691 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006692 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006693 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006694
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006695 if (lval)
6696 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006697 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6698
6699 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6700 llvm::MDNode::get(VMContext,
6701 ArrayRef<llvm::Value*>()));
6702 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006703}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006704/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006705/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006706///
6707void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006708 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006709 llvm::Value *dst,
6710 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006711 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006712 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006713 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006714 assert(Size <= 8 && "does not support size > 8");
6715 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6716 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006717 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6718 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006719 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6720 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006721 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6722 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006723 return;
6724}
6725
6726/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6727/// objc_assign_strongCast (id src, id *dst)
6728///
6729void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006730 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006731 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006732 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006733 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006734 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006735 assert(Size <= 8 && "does not support size > 8");
6736 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006737 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006738 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6739 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006740 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6741 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006742 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006743 src, dst, "weakassign");
6744 return;
6745}
6746
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006747void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006748 CodeGen::CodeGenFunction &CGF,
6749 llvm::Value *DestPtr,
6750 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006751 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006752 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6753 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006754 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006755 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006756 return;
6757}
6758
Fariborz Jahanian06292952009-02-16 22:52:32 +00006759/// EmitObjCWeakRead - Code gen for loading value of a __weak
6760/// object: objc_read_weak (id *src)
6761///
6762llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006763 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006764 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006765 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006766 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6767 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006768 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006769 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006770 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006771 return read_weak;
6772}
6773
6774/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6775/// objc_assign_weak (id src, id *dst)
6776///
6777void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006778 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006779 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006780 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006781 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006782 assert(Size <= 8 && "does not support size > 8");
6783 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6784 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006785 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6786 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006787 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6788 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006789 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006790 src, dst, "weakassign");
6791 return;
6792}
6793
6794/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6795/// objc_assign_global (id src, id *dst)
6796///
6797void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006798 llvm::Value *src, llvm::Value *dst,
6799 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006800 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006801 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00006802 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006803 assert(Size <= 8 && "does not support size > 8");
6804 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6805 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006806 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6807 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006808 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6809 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006810 if (!threadlocal)
6811 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6812 src, dst, "globalassign");
6813 else
6814 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6815 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006816 return;
6817}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006818
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006819void
John McCallbd309292010-07-06 01:34:17 +00006820CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6821 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006822 EmitAtSynchronizedStmt(CGF, S,
6823 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6824 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006825}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006826
John McCall2ca705e2010-07-24 00:37:23 +00006827llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006828CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006829 // There's a particular fixed type info for 'id'.
6830 if (T->isObjCIdType() ||
6831 T->isObjCQualifiedIdType()) {
6832 llvm::Constant *IDEHType =
6833 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6834 if (!IDEHType)
6835 IDEHType =
6836 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6837 false,
6838 llvm::GlobalValue::ExternalLinkage,
6839 0, "OBJC_EHTYPE_id");
6840 return IDEHType;
6841 }
6842
6843 // All other types should be Objective-C interface pointer types.
6844 const ObjCObjectPointerType *PT =
6845 T->getAs<ObjCObjectPointerType>();
6846 assert(PT && "Invalid @catch type.");
6847 const ObjCInterfaceType *IT = PT->getInterfaceType();
6848 assert(IT && "Invalid @catch type.");
6849 return GetInterfaceEHType(IT->getDecl(), false);
6850}
6851
John McCallbd309292010-07-06 01:34:17 +00006852void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6853 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006854 EmitTryCatchStmt(CGF, S,
6855 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6856 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6857 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006858}
6859
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006860/// EmitThrowStmt - Generate code for a throw statement.
6861void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6862 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006863 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006864 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006865 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006866 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006867 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006868 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006869 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006870 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006871 }
6872
John McCall17afe452010-10-16 08:21:07 +00006873 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006874 CGF.Builder.ClearInsertionPoint();
6875}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006876
John McCall2ca705e2010-07-24 00:37:23 +00006877llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006878CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006879 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006880 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006881
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006882 // If we don't need a definition, return the entry if found or check
6883 // if we use an external reference.
6884 if (!ForDefinition) {
6885 if (Entry)
6886 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006887
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006888 // If this type (or a super class) has the __objc_exception__
6889 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006890 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006891 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006892 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006893 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006894 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006895 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006896 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006897 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006898
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006899 // Otherwise we need to either make a new entry or fill in the
6900 // initializer.
6901 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006902 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006903 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006904 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006905 CGM.getModule().getGlobalVariable(VTableName);
6906 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006907 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6908 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006909 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006910 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006911
Chris Lattnerece04092012-02-07 00:39:47 +00006912 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006913
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006914 llvm::Constant *Values[] = {
6915 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6916 GetClassName(ID->getIdentifier()),
6917 GetClassGlobal(ClassName)
6918 };
Owen Anderson170229f2009-07-14 23:10:40 +00006919 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006920 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006921
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006922 if (Entry) {
6923 Entry->setInitializer(Init);
6924 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006925 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006926 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006927 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006928 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006929 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006930 }
6931
David Blaikiebbafb8a2012-03-11 07:00:24 +00006932 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006933 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmowdd31ca12012-10-08 16:25:52 +00006934 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00006935 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006936
6937 if (ForDefinition) {
6938 Entry->setSection("__DATA,__objc_const");
6939 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6940 } else {
6941 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6942 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006943
6944 return Entry;
6945}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006946
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006947/* *** */
6948
Daniel Dunbarb036db82008-08-13 03:21:16 +00006949CodeGen::CGObjCRuntime *
6950CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00006951 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6952 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006953 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00006954
6955 case ObjCRuntime::MacOSX:
6956 case ObjCRuntime::iOS:
6957 return new CGObjCNonFragileABIMac(CGM);
6958
David Chisnallb601c962012-07-03 20:49:52 +00006959 case ObjCRuntime::GNUstep:
6960 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00006961 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00006962 llvm_unreachable("these runtimes are not Mac runtimes");
6963 }
6964 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006965}