blob: 827055de4d2d0a6add5a4250f031e755423d008a [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"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.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
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045
Daniel Dunbar303e2c22008-08-11 02:45:11 +000046namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000047
Daniel Dunbar59e476b2009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000049
Daniel Dunbar59e476b2009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000052
Fariborz Jahanian279eda62009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000056
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000057private:
John McCall9dc0db22011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCall31168b02011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000073 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000074
John McCall9dc0db22011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000085
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000087
John McCall9dc0db22011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall9dc0db22011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000099
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000101
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000102 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
103 ///
104 /// The messenger used when the return value is returned in two values on the
105 /// x87 floating point stack; without a special entrypoint, the nil case
106 /// would be unbalanced. Only used on 64-bit X86.
107 llvm::Constant *getMessageSendFp2retFn() const {
108 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
109 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
110 llvm::Type *resultType =
111 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
112
113 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
114 params, true),
115 "objc_msgSend_fp2ret");
116 }
117
John McCall9dc0db22011-05-15 01:53:33 +0000118 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
119 ///
120 /// The messenger used for super calls, which have different dispatch
121 /// semantics. The class passed is the superclass of the current
122 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000123 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000124 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000126 params, true),
127 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000129
John McCall9dc0db22011-05-15 01:53:33 +0000130 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
131 ///
132 /// A slightly different messenger used for super calls. The class
133 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000134 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000135 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000136 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000137 params, true),
138 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000139 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000140
John McCall9dc0db22011-05-15 01:53:33 +0000141 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
142 /// SEL op, ...)
143 ///
144 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000145 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000146 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000147 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000148 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000149 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000150 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000151
John McCall9dc0db22011-05-15 01:53:33 +0000152 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
153 /// SEL op, ...)
154 ///
155 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000157 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000158 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000159 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000161 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000162
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000163 llvm::Constant *getMessageSendSuperFpretFn() const {
164 // There is no objc_msgSendSuper_fpret? How can that work?
165 return getMessageSendSuperFn();
166 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000167
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000168 llvm::Constant *getMessageSendSuperFpretFn2() const {
169 // There is no objc_msgSendSuper_fpret? How can that work?
170 return getMessageSendSuperFn2();
171 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000173protected:
174 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000176public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000177 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000178 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000179
Daniel Dunbar5d715592008-08-12 05:28:47 +0000180 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000181 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000183 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000184 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000186 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000187 llvm::Type *SelectorPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000188
189private:
Daniel Dunbarb036db82008-08-13 03:21:16 +0000190 /// ProtocolPtrTy - LLVM type for external protocol handles
191 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000192 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000193
194public:
195 llvm::Type *getExternalProtocolPtrTy() {
196 if (!ExternalProtocolPtrTy) {
197 // FIXME: It would be nice to unify this with the opaque type, so that the
198 // IR comes out a bit cleaner.
199 CodeGen::CodeGenTypes &Types = CGM.getTypes();
200 ASTContext &Ctx = CGM.getContext();
201 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
202 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
203 }
204
205 return ExternalProtocolPtrTy;
206 }
207
Daniel Dunbarc722b852008-08-30 03:02:31 +0000208 // SuperCTy - clang type for struct objc_super.
209 QualType SuperCTy;
210 // SuperPtrCTy - clang type for struct objc_super *.
211 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000212
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000213 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000214 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000215 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000216 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000217
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000218 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
219 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000220 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000221
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000222 /// PropertyListTy - LLVM type for struct objc_property_list
223 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000224 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000225 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000226 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000227
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000228 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000229 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000230
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000231 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000232 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000233 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000234 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000235
Chris Lattnerce8754e2009-04-22 02:44:54 +0000236 llvm::Constant *getGetPropertyFn() {
237 CodeGen::CodeGenTypes &Types = CGM.getTypes();
238 ASTContext &Ctx = CGM.getContext();
239 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000241 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
242 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000243 Params.push_back(IdType);
244 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000245 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000246 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000247 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000248 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000249 FunctionType::ExtInfo()),
250 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000251 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
252 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000253
Chris Lattnerce8754e2009-04-22 02:44:54 +0000254 llvm::Constant *getSetPropertyFn() {
255 CodeGen::CodeGenTypes &Types = CGM.getTypes();
256 ASTContext &Ctx = CGM.getContext();
257 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000258 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000259 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
260 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000263 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000264 Params.push_back(IdType);
265 Params.push_back(Ctx.BoolTy);
266 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000267 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000268 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000269 FunctionType::ExtInfo()),
270 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000271 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
272 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000273
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000274
275 llvm::Constant *getCopyStructFn() {
276 CodeGen::CodeGenTypes &Types = CGM.getTypes();
277 ASTContext &Ctx = CGM.getContext();
278 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000280 Params.push_back(Ctx.VoidPtrTy);
281 Params.push_back(Ctx.VoidPtrTy);
282 Params.push_back(Ctx.LongTy);
283 Params.push_back(Ctx.BoolTy);
284 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000285 llvm::FunctionType *FTy =
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000286 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
287 FunctionType::ExtInfo()),
288 false);
289 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
290 }
291
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000292 /// This routine declares and returns address of:
293 /// void objc_copyCppObjectAtomic(
294 /// void *dest, const void *src,
295 /// void (*copyHelper) (void *dest, const void *source));
296 llvm::Constant *getCppAtomicObjectFunction() {
297 CodeGen::CodeGenTypes &Types = CGM.getTypes();
298 ASTContext &Ctx = CGM.getContext();
299 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
300 SmallVector<CanQualType,3> Params;
301 Params.push_back(Ctx.VoidPtrTy);
302 Params.push_back(Ctx.VoidPtrTy);
303 Params.push_back(Ctx.VoidPtrTy);
304 llvm::FunctionType *FTy =
305 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
306 FunctionType::ExtInfo()),
307 false);
308 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
309 }
310
Chris Lattnerce8754e2009-04-22 02:44:54 +0000311 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000312 CodeGen::CodeGenTypes &Types = CGM.getTypes();
313 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000314 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000315 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000316 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000318 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000319 FunctionType::ExtInfo()),
320 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
322 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000323
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000324 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000325 llvm::Constant *getGcReadWeakFn() {
326 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000327 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000328 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000329 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000331 }
332
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000333 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000334 llvm::Constant *getGcAssignWeakFn() {
335 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000336 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000337 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000338 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
340 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000341
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000342 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000343 llvm::Constant *getGcAssignGlobalFn() {
344 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000345 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000346 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000347 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000348 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
349 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000350
Fariborz Jahanian217af242010-07-20 20:30:03 +0000351 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
352 llvm::Constant *getGcAssignThreadLocalFn() {
353 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000354 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000355 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000356 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000357 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
358 }
359
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000360 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000361 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000362 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000363 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
364 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000365 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000366 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
368 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000369
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000370 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
371 llvm::Constant *GcMemmoveCollectableFn() {
372 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000373 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000374 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000375 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
376 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000377
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000378 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000379 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000380 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000381 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000382 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000383 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000384 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
385 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000386
387 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000388 llvm::Constant *getExceptionThrowFn() {
389 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000390 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000391 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000392 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
394 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000395
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000396 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
397 llvm::Constant *getExceptionRethrowFn() {
398 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000399 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
401 }
402
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000403 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000404 llvm::Constant *getSyncEnterFn() {
405 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000406 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000407 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000408 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
410 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000411
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000412 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000413 llvm::Constant *getSyncExitFn() {
414 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000415 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000416 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000417 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000420
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
423 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000424
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
427 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000428
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000429 llvm::Constant *getSendStretFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
431 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000432
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000433 llvm::Constant *getSendStretFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
435 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000436
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000437 llvm::Constant *getSendFpretFn(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
439 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000440
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000441 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
443 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000444
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000445 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
446 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
447 }
448
449 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
450 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
451 }
452
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000453 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
454 ~ObjCCommonTypesHelper(){}
455};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000456
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000457/// ObjCTypesHelper - Helper class that encapsulates lazy
458/// construction of varies types used during ObjC generation.
459class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000460public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000461 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000462 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000463 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000464 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000465 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000466 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000467
Daniel Dunbarb036db82008-08-13 03:21:16 +0000468 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000469 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000470 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000471 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000472 /// ProtocolExtensionTy - LLVM type for struct
473 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000474 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000475 /// ProtocolExtensionTy - LLVM type for struct
476 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000477 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000478 /// MethodDescriptionTy - LLVM type for struct
479 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000480 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000481 /// MethodDescriptionListTy - LLVM type for struct
482 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000483 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000484 /// MethodDescriptionListPtrTy - LLVM type for struct
485 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000486 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000487 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000488 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000489 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000490 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000491 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000492 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000493 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000494 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000495 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000496 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000497 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000498 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000499 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000500 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000501 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000502 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000503 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000504 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000505 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000506 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000507 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000508 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000509 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000510 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000511
Anders Carlsson9ff22482008-09-09 10:10:21 +0000512 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000513 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000514
Anders Carlsson9ff22482008-09-09 10:10:21 +0000515 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000516 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000517 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000518 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000519 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000520 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000521 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000522
523 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000524 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000525 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000526 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000527 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000528 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000529 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000530
531 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000532 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000533 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000534 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000535 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000536 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000537 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000538
Anders Carlsson9ff22482008-09-09 10:10:21 +0000539 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000540 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000541 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000542 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000543 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000544 "objc_exception_match");
545
Chris Lattnerc6406db2009-04-22 02:26:14 +0000546 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000547
Anders Carlsson9ff22482008-09-09 10:10:21 +0000548 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000549 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000550 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000551 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000552 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
553 params, false),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000554 "_setjmp",
555 llvm::Attribute::ReturnsTwice);
Chris Lattnerc6406db2009-04-22 02:26:14 +0000556 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000557
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000558public:
559 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000560 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000561};
562
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000563/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000564/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000565class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000566public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000567
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000568 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000569 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000570
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000571 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000572 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000574 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000575 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000576
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000577 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000578 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000579
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000580 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000581 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000582
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000583 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000584 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000586 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000587 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000588
Fariborz Jahanian71394042009-01-23 23:53:38 +0000589 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000590 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000591
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000592 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000593 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000594
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000595 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000596 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000597
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000598 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000599 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000600
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000601 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000602 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000603
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000604 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000605 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000606
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000607 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000608 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000610 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000612 // MessageRefTy - LLVM for:
613 // struct _message_ref_t {
614 // IMP messenger;
615 // SEL name;
616 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000617 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000618 // MessageRefCTy - clang type for struct _message_ref_t
619 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000620
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000621 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000622 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000623 // MessageRefCPtrTy - clang type for struct _message_ref_t*
624 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000626 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000627 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000628
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000629 // SuperMessageRefTy - LLVM for:
630 // struct _super_message_ref_t {
631 // SUPER_IMP messenger;
632 // SEL name;
633 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000634 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000635
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000636 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000637 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000638
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendFixupFn() {
640 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000641 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000642 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000643 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000644 "objc_msgSend_fixup");
645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000647 llvm::Constant *getMessageSendFpretFixupFn() {
648 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000649 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000650 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000651 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000652 "objc_msgSend_fpret_fixup");
653 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000654
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000655 llvm::Constant *getMessageSendStretFixupFn() {
656 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000657 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000658 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000659 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000660 "objc_msgSend_stret_fixup");
661 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000662
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000663 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000665 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000666 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000667 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000668 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000669 "objc_msgSendSuper2_fixup");
670 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000671
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000674 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000675 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000676 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000677 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000678 "objc_msgSendSuper2_stret_fixup");
679 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000680
Chris Lattnera7c00b42009-04-22 02:15:23 +0000681 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000682 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000683 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Chris Lattnera7c00b42009-04-22 02:15:23 +0000685 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000686
Chris Lattnera7c00b42009-04-22 02:15:23 +0000687 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000688 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000689 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000690 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000691 "objc_begin_catch");
692 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000693
Chris Lattnera5f58b02011-07-09 17:41:47 +0000694 llvm::StructType *EHTypeTy;
695 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000696
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000697 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
698 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000699};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000700
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000701class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000702public:
703 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000704 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000705 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000706 unsigned ivar_bytepos;
707 unsigned ivar_size;
708 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000709 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000710
711 // Allow sorting based on byte pos.
712 bool operator<(const GC_IVAR &b) const {
713 return ivar_bytepos < b.ivar_bytepos;
714 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000715 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000716
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000717 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000718 public:
719 unsigned skip;
720 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000721 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000722 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000723 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000724
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000725protected:
726 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000727 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000728 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000729 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000730
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000731 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000732 SmallVector<GC_IVAR, 16> SkipIvars;
733 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000734
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000735 /// LazySymbols - Symbols to generate a lazy reference for. See
736 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000737 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000738
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000739 /// DefinedSymbols - External symbols which are defined by this
740 /// module. The symbols in this list and LazySymbols are used to add
741 /// special linker symbols which ensure that Objective-C modules are
742 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000743 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000744
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000745 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000746 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000747
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000748 /// MethodVarNames - uniqued method variable names.
749 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000751 /// DefinedCategoryNames - list of category names in form Class_Category.
752 llvm::SetVector<std::string> DefinedCategoryNames;
753
Daniel Dunbarb036db82008-08-13 03:21:16 +0000754 /// MethodVarTypes - uniqued method type signatures. We have to use
755 /// a StringMap here because have no other unique reference.
756 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000758 /// MethodDefinitions - map of methods which have been defined in
759 /// this translation unit.
760 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000762 /// PropertyNames - uniqued method variable names.
763 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000764
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000765 /// ClassReferences - uniqued class references.
766 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000767
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000768 /// SelectorReferences - uniqued selector references.
769 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770
Daniel Dunbarb036db82008-08-13 03:21:16 +0000771 /// Protocols - Protocols for which an objc_protocol structure has
772 /// been emitted. Forward declarations are handled by creating an
773 /// empty structure whose initializer is filled in when/if defined.
774 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000775
Daniel Dunbarc475d422008-10-29 22:36:39 +0000776 /// DefinedProtocols - Protocols which have actually been
777 /// defined. We should not need this, see FIXME in GenerateProtocol.
778 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000780 /// DefinedClasses - List of defined classes.
781 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000782
783 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
784 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000785
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000786 /// DefinedCategories - List of defined categories.
787 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000788
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000789 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
790 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000792 /// GetNameForMethod - Return a name for the given method.
793 /// \param[out] NameOut - The return value.
794 void GetNameForMethod(const ObjCMethodDecl *OMD,
795 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000796 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000798 /// GetMethodVarName - Return a unique constant for the given
799 /// selector's name. The return value has type char *.
800 llvm::Constant *GetMethodVarName(Selector Sel);
801 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000802
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000803 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000804 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000805
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000806 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000807 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
808 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000809 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000810
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000811 /// GetPropertyName - Return a unique constant for the given
812 /// name. The return value has type char *.
813 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000814
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000815 // FIXME: This can be dropped once string functions are unified.
816 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
817 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000818
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000819 /// GetClassName - Return a unique constant for the given selector's
820 /// name. The return value has type char *.
821 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000822
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000823 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
824
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000825 /// BuildIvarLayout - Builds ivar layout bitmap for the class
826 /// implementation for the __strong or __weak case.
827 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000828 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
829 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000830
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000831 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000833 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000834 unsigned int BytePos, bool ForStrongLayout,
835 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000836 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000837 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000838 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +0000839 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000840 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000841 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000842
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000843 /// GetIvarLayoutName - Returns a unique constant for the given
844 /// ivar layout bitmap.
845 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
846 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000847
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000848 /// EmitPropertyList - Emit the given property list. The return
849 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000850 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000852 const ObjCContainerDecl *OCD,
853 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000854
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000855 /// EmitProtocolMethodTypes - Generate the array of extended method type
856 /// strings. The return value has type Int8PtrPtrTy.
857 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
858 const ConstantVector &MethodTypes,
859 const ObjCCommonTypesHelper &ObjCTypes);
860
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000861 /// PushProtocolProperties - Push protocol's property on the input stack.
862 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
863 std::vector<llvm::Constant*> &Properties,
864 const Decl *Container,
865 const ObjCProtocolDecl *PROTO,
866 const ObjCCommonTypesHelper &ObjCTypes);
867
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000868 /// GetProtocolRef - Return a reference to the internal protocol
869 /// description, creating an empty one if it has not been
870 /// defined. The return value has type ProtocolPtrTy.
871 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000872
Daniel Dunbar30c65362009-03-09 20:09:19 +0000873 /// CreateMetadataVar - Create a global variable with internal
874 /// linkage for use by the Objective-C runtime.
875 ///
876 /// This is a convenience wrapper which not only creates the
877 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000878 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000879 ///
880 /// \param Name - The variable name.
881 /// \param Init - The variable initializer; this is also used to
882 /// define the type of the variable.
883 /// \param Section - The section the variable should go into, or 0.
884 /// \param Align - The alignment for the variable, or 0.
885 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000886 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000887 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000888 llvm::Constant *Init,
889 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000890 unsigned Align,
891 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000892
John McCall9e8bb002011-05-14 03:10:52 +0000893 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
894 ReturnValueSlot Return,
895 QualType ResultType,
896 llvm::Value *Sel,
897 llvm::Value *Arg0,
898 QualType Arg0Ty,
899 bool IsSuper,
900 const CallArgList &CallArgs,
901 const ObjCMethodDecl *OMD,
902 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000903
Daniel Dunbar5e639272010-04-25 20:39:01 +0000904 /// EmitImageInfo - Emit the image info marker used to encode some module
905 /// level information.
906 void EmitImageInfo();
907
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000908public:
Owen Andersonae86c192009-07-13 04:10:07 +0000909 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000910 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000911
David Chisnall481e3a82010-01-23 02:40:42 +0000912 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000914 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
915 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000916
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000917 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000918
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000919 /// GetOrEmitProtocol - Get the protocol object for the given
920 /// declaration, emitting it if necessary. The return value has type
921 /// ProtocolPtrTy.
922 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000924 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
925 /// object for the given declaration, emitting it if needed. These
926 /// forward references will be filled in with empty bodies if no
927 /// definition is seen. The return value has type ProtocolPtrTy.
928 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000929 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
930 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000931
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000932};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000933
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000934class CGObjCMac : public CGObjCCommonMac {
935private:
936 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000937
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000938 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000939 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000940 void EmitModuleInfo();
941
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000942 /// EmitModuleSymols - Emit module symbols, the list of defined
943 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000944 llvm::Constant *EmitModuleSymbols();
945
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000946 /// FinishModule - Write out global data structures at the end of
947 /// processing a translation unit.
948 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000949
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000950 /// EmitClassExtension - Generate the class extension structure used
951 /// to store the weak ivar layout and properties. The return value
952 /// has type ClassExtensionPtrTy.
953 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
954
955 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
956 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000957 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000958 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000959
John McCall31168b02011-06-15 23:02:42 +0000960 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
961 IdentifierInfo *II);
962
963 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
964
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000965 /// EmitSuperClassRef - Emits reference to class's main metadata class.
966 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000967
968 /// EmitIvarList - Emit the ivar list for the given
969 /// implementation. If ForClass is true the list of class ivars
970 /// (i.e. metaclass ivars) is emitted, otherwise the list of
971 /// interface ivars will be emitted. The return value has type
972 /// IvarListPtrTy.
973 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000974 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000975
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000976 /// EmitMetaClass - Emit a forward reference to the class structure
977 /// for the metaclass of the given interface. The return value has
978 /// type ClassPtrTy.
979 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
980
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000981 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000982 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000983 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
984 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000985 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000986
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000987 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000988
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000989 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000990
991 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000992 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000993 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000994 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000995 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000996
997 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000998 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000999 /// - TypeName: The name for the type containing the methods.
1000 /// - IsProtocol: True iff these methods are for a protocol.
1001 /// - ClassMethds: True iff these are class methods.
1002 /// - Required: When true, only "required" methods are
1003 /// listed. Similarly, when false only "optional" methods are
1004 /// listed. For classes this should always be true.
1005 /// - begin, end: The method list to output.
1006 ///
1007 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001008 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001009 const char *Section,
1010 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001011
Daniel Dunbarc475d422008-10-29 22:36:39 +00001012 /// GetOrEmitProtocol - Get the protocol object for the given
1013 /// declaration, emitting it if necessary. The return value has type
1014 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001015 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001016
1017 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1018 /// object for the given declaration, emitting it if needed. These
1019 /// forward references will be filled in with empty bodies if no
1020 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001021 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001022
Daniel Dunbarb036db82008-08-13 03:21:16 +00001023 /// EmitProtocolExtension - Generate the protocol extension
1024 /// structure used to store optional instance and class methods, and
1025 /// protocol properties. The return value has type
1026 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001027 llvm::Constant *
1028 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1029 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001030 const ConstantVector &OptClassMethods,
1031 const ConstantVector &MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001032
1033 /// EmitProtocolList - Generate the list of referenced
1034 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001035 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001036 ObjCProtocolDecl::protocol_iterator begin,
1037 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001038
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001039 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1040 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001041 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1042 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001043
1044public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001045 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001046
Fariborz Jahanian71394042009-01-23 23:53:38 +00001047 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001048
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001049 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001050 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001051 QualType ResultType,
1052 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001053 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001054 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001055 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001056 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001057
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001058 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001059 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001060 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001061 QualType ResultType,
1062 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001063 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001064 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001065 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001066 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001067 const CallArgList &CallArgs,
1068 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001069
Daniel Dunbarcb463852008-11-01 01:53:16 +00001070 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001071 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001072
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001073 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1074 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001075
1076 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1077 /// untyped one.
1078 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1079 const ObjCMethodDecl *Method);
1080
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001081 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001082
Daniel Dunbar92992502008-08-15 22:20:32 +00001083 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001084
Daniel Dunbar92992502008-08-15 22:20:32 +00001085 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001086
David Chisnall92d436b2012-01-31 18:59:20 +00001087 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1088
Daniel Dunbarcb463852008-11-01 01:53:16 +00001089 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001090 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001091
Chris Lattnerd4808922009-03-22 21:03:39 +00001092 virtual llvm::Constant *GetPropertyGetFunction();
1093 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001094 virtual llvm::Constant *GetGetStructFunction();
1095 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001096 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001097 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001098
John McCallbd309292010-07-06 01:34:17 +00001099 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1100 const ObjCAtTryStmt &S);
1101 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1102 const ObjCAtSynchronizedStmt &S);
1103 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001104 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1105 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001106 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001107 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001108 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001109 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001110 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001111 llvm::Value *src, llvm::Value *dest,
1112 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001113 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001114 llvm::Value *src, llvm::Value *dest,
1115 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001116 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1117 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001118 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1119 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001120 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001121
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001122 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1123 QualType ObjectTy,
1124 llvm::Value *BaseValue,
1125 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001126 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001127 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001128 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001129 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001130
1131 /// GetClassGlobal - Return the global variable for the Objective-C
1132 /// class of the given name.
1133 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001134 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001135 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001136};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001137
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001138class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001139private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001140 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001141 llvm::GlobalVariable* ObjCEmptyCacheVar;
1142 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001143
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001144 /// SuperClassReferences - uniqued super class references.
1145 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001146
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001147 /// MetaClassReferences - uniqued meta class references.
1148 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001149
1150 /// EHTypeReferences - uniqued class ehtype references.
1151 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001152
John McCall9e8bb002011-05-14 03:10:52 +00001153 /// VTableDispatchMethods - List of methods for which we generate
1154 /// vtable-based message dispatch.
1155 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156
Fariborz Jahanian67260552009-11-17 21:37:35 +00001157 /// DefinedMetaClasses - List of defined meta-classes.
1158 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1159
John McCall9e8bb002011-05-14 03:10:52 +00001160 /// isVTableDispatchedSelector - Returns true if SEL is a
1161 /// vtable-based selector.
1162 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001163
Fariborz Jahanian71394042009-01-23 23:53:38 +00001164 /// FinishNonFragileABIModule - Write out global data structures at the end of
1165 /// processing a translation unit.
1166 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001167
Daniel Dunbar19573e72009-05-15 21:48:48 +00001168 /// AddModuleClassList - Add the given list of class pointers to the
1169 /// module with the provided symbol and section names.
1170 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1171 const char *SymbolName,
1172 const char *SectionName);
1173
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001174 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1175 unsigned InstanceStart,
1176 unsigned InstanceSize,
1177 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001178 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001179 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001180 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001181 llvm::Constant *ClassRoGV,
1182 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001183
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001184 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001185
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001186 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001187
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001188 /// EmitMethodList - Emit the method list for the given
1189 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001190 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001191 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001192 const ConstantVector &Methods);
1193 /// EmitIvarList - Emit the ivar list for the given
1194 /// implementation. If ForClass is true the list of class ivars
1195 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1196 /// interface ivars will be emitted. The return value has type
1197 /// IvarListnfABIPtrTy.
1198 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001200 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001201 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001202 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001203
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001204 /// GetOrEmitProtocol - Get the protocol object for the given
1205 /// declaration, emitting it if necessary. The return value has type
1206 /// ProtocolPtrTy.
1207 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001208
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001209 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1210 /// object for the given declaration, emitting it if needed. These
1211 /// forward references will be filled in with empty bodies if no
1212 /// definition is seen. The return value has type ProtocolPtrTy.
1213 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001214
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001215 /// EmitProtocolList - Generate the list of referenced
1216 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001217 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001218 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001219 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001220
John McCall9e8bb002011-05-14 03:10:52 +00001221 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1222 ReturnValueSlot Return,
1223 QualType ResultType,
1224 Selector Sel,
1225 llvm::Value *Receiver,
1226 QualType Arg0Ty,
1227 bool IsSuper,
1228 const CallArgList &CallArgs,
1229 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001230
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001231 /// GetClassGlobal - Return the global variable for the Objective-C
1232 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001233 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001234
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001235 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001236 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001237 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001238 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001239
1240 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1241 IdentifierInfo *II);
1242
1243 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001244
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001245 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1246 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001247 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1248 const ObjCInterfaceDecl *ID);
1249
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001250 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1251 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001252 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001253 const ObjCInterfaceDecl *ID);
1254
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001255 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1256 /// the given ivar.
1257 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001258 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001259 const ObjCInterfaceDecl *ID,
1260 const ObjCIvarDecl *Ivar);
1261
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001262 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1263 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001264 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1265 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001266
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001267 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001268 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001269 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001270 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001271
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001272 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001273 return "OBJC_METACLASS_$_";
1274 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001275
Daniel Dunbar15894b72009-04-07 05:48:37 +00001276 const char *getClassSymbolPrefix() const {
1277 return "OBJC_CLASS_$_";
1278 }
1279
Daniel Dunbar961202372009-05-03 12:57:56 +00001280 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001281 uint32_t &InstanceStart,
1282 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001283
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001284 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001285 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001286 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1287 return CGM.getContext().Selectors.getSelector(0, &II);
1288 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001290 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001291 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1292 return CGM.getContext().Selectors.getSelector(1, &II);
1293 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001294
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001295 /// ImplementationIsNonLazy - Check whether the given category or
1296 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001297 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001298
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001299public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001300 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001301 // FIXME. All stubs for now!
1302 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001303
Fariborz Jahanian71394042009-01-23 23:53:38 +00001304 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001305 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001306 QualType ResultType,
1307 Selector Sel,
1308 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001309 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001310 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001311 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001312
1313 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001314 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001315 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001316 QualType ResultType,
1317 Selector Sel,
1318 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001319 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001320 llvm::Value *Receiver,
1321 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001322 const CallArgList &CallArgs,
1323 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001324
Fariborz Jahanian71394042009-01-23 23:53:38 +00001325 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001326 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001327
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001328 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1329 bool lvalue = false)
1330 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001331
1332 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1333 /// untyped one.
1334 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1335 const ObjCMethodDecl *Method)
1336 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001337
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001338 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001339
Fariborz Jahanian71394042009-01-23 23:53:38 +00001340 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001341
1342 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1343
Fariborz Jahanian71394042009-01-23 23:53:38 +00001344 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001345 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001346
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001347 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001348
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001349 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001350 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001351 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001352 virtual llvm::Constant *GetPropertySetFunction() {
1353 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001354 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001355
David Chisnall168b80f2010-12-26 22:13:16 +00001356 virtual llvm::Constant *GetSetStructFunction() {
1357 return ObjCTypes.getCopyStructFn();
1358 }
1359 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001360 return ObjCTypes.getCopyStructFn();
1361 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001362 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1363 return ObjCTypes.getCppAtomicObjectFunction();
1364 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001365
Chris Lattnerd4808922009-03-22 21:03:39 +00001366 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001367 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001368 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001369
John McCallbd309292010-07-06 01:34:17 +00001370 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1371 const ObjCAtTryStmt &S);
1372 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1373 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001374 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001375 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001376 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001377 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001378 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001379 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001380 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001381 llvm::Value *src, llvm::Value *dest,
1382 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001383 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001384 llvm::Value *src, llvm::Value *dest,
1385 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001386 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001387 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001388 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1389 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001390 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001391 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1392 QualType ObjectTy,
1393 llvm::Value *BaseValue,
1394 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001395 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001396 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001397 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001398 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001399};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001400
1401/// A helper class for performing the null-initialization of a return
1402/// value.
1403struct NullReturnState {
1404 llvm::BasicBlock *NullBB;
1405 llvm::BasicBlock *callBB;
1406 NullReturnState() : NullBB(0), callBB(0) {}
1407
1408 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1409 // Make blocks for the null-init and call edges.
1410 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1411 callBB = CGF.createBasicBlock("msgSend.call");
1412
1413 // Check for a null receiver and, if there is one, jump to the
1414 // null-init test.
1415 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1416 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1417
1418 // Otherwise, start performing the call.
1419 CGF.EmitBlock(callBB);
1420 }
1421
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001422 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1423 const CallArgList &CallArgs,
1424 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001425 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001426
1427 llvm::Value *NullInitPtr = 0;
1428 if (result.isScalar() && !resultType->isVoidType()) {
1429 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1430 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1431 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001432
1433 // Finish the call path.
1434 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1435 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1436
1437 // Emit the null-init block and perform the null-initialization there.
1438 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001439
1440 // Release consumed arguments along the null-receiver path.
1441 if (Method) {
1442 CallArgList::const_iterator I = CallArgs.begin();
1443 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1444 e = Method->param_end(); i != e; ++i, ++I) {
1445 const ParmVarDecl *ParamDecl = (*i);
1446 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1447 RValue RV = I->RV;
1448 assert(RV.isScalar() &&
1449 "NullReturnState::complete - arg not on object");
1450 CGF.EmitARCRelease(RV.getScalarVal(), true);
1451 }
1452 }
1453 }
1454
1455 if (result.isScalar()) {
1456 if (NullInitPtr)
1457 CGF.EmitNullInitialization(NullInitPtr, resultType);
1458 // Jump to the continuation block.
1459 CGF.EmitBlock(contBB);
1460 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1461 : result;
1462 }
1463
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001464 if (!resultType->isAnyComplexType()) {
1465 assert(result.isAggregate() && "null init of non-aggregate result?");
1466 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1467 // Jump to the continuation block.
1468 CGF.EmitBlock(contBB);
1469 return result;
1470 }
1471
1472 // _Complex type
1473 // FIXME. Now easy to handle any other scalar type whose result is returned
1474 // in memory due to ABI limitations.
1475 CGF.EmitBlock(contBB);
1476 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1477 llvm::Type *MemberType = CallCV.first->getType();
1478 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1479 // Create phi instruction for scalar complex value.
1480 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1481 PHIReal->addIncoming(ZeroCV, NullBB);
1482 PHIReal->addIncoming(CallCV.first, callBB);
1483 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1484 PHIImag->addIncoming(ZeroCV, NullBB);
1485 PHIImag->addIncoming(CallCV.second, callBB);
1486 return RValue::getComplex(PHIReal, PHIImag);
1487 }
1488};
1489
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001490} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001491
1492/* *** Helper Functions *** */
1493
1494/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001495static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001496 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001497 unsigned idx0,
1498 unsigned idx1) {
1499 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001500 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1501 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001502 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001503 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001504}
1505
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001506/// hasObjCExceptionAttribute - Return true if this class or any super
1507/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001508static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001509 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001510 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001511 return true;
1512 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001513 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001514 return false;
1515}
1516
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001517/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001518
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001519CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001520 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001521 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001522 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001523}
1524
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001525/// GetClass - Return a reference to the class for the given interface
1526/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001527llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001528 const ObjCInterfaceDecl *ID) {
1529 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001530}
1531
1532/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001533llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1534 bool lval) {
1535 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001536}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001537llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001538 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001539 return EmitSelector(Builder, Method->getSelector());
1540}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001541
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001542llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001543 if (T->isObjCIdType() ||
1544 T->isObjCQualifiedIdType()) {
1545 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001546 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001547 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001548 if (T->isObjCClassType() ||
1549 T->isObjCQualifiedClassType()) {
1550 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001551 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001552 }
1553 if (T->isObjCObjectPointerType())
1554 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1555
John McCall2ca705e2010-07-24 00:37:23 +00001556 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001557}
1558
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001559/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001560/*
1561 struct __builtin_CFString {
1562 const int *isa; // point to __CFConstantStringClassReference
1563 int flags;
1564 const char *str;
1565 long length;
1566 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001567*/
1568
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001569/// or Generate a constant NSString object.
1570/*
1571 struct __builtin_NSString {
1572 const int *isa; // point to __NSConstantStringClassReference
1573 const char *str;
1574 unsigned int length;
1575 };
1576*/
1577
Fariborz Jahanian71394042009-01-23 23:53:38 +00001578llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001579 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001580 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1581 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001582 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001583}
1584
1585/// Generates a message send where the super is the receiver. This is
1586/// a message send to self with special delivery semantics indicating
1587/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001588CodeGen::RValue
1589CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001590 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001591 QualType ResultType,
1592 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001593 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001594 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001595 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001596 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001597 const CodeGen::CallArgList &CallArgs,
1598 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001599 // Create and init a super structure; this is a (receiver, class)
1600 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001601 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001602 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001603 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001604 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001605 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001606 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001607
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001608 // If this is a class message the metaclass is passed as the target.
1609 llvm::Value *Target;
1610 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001611 if (isCategoryImpl) {
1612 // Message sent to 'super' in a class method defined in a category
1613 // implementation requires an odd treatment.
1614 // If we are in a class method, we must retrieve the
1615 // _metaclass_ for the current class, pointed at by
1616 // the class's "isa" pointer. The following assumes that
1617 // isa" is the first ivar in a class (which it must be).
1618 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1619 Target = CGF.Builder.CreateStructGEP(Target, 0);
1620 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001621 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001622 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1623 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1624 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1625 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001626 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001627 }
1628 else if (isCategoryImpl)
1629 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1630 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001631 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1632 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1633 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001634 }
Mike Stump18bb9282009-05-16 07:57:57 +00001635 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1636 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001637 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001638 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001639 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001640 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001641 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001642 return EmitMessageSend(CGF, Return, ResultType,
1643 EmitSelector(CGF.Builder, Sel),
1644 ObjCSuper, ObjCTypes.SuperPtrCTy,
1645 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001646}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001647
1648/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001649CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001650 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001651 QualType ResultType,
1652 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001653 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001654 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001655 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001656 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001657 return EmitMessageSend(CGF, Return, ResultType,
1658 EmitSelector(CGF.Builder, Sel),
1659 Receiver, CGF.getContext().getObjCIdType(),
1660 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001661}
1662
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001663CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001664CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1665 ReturnValueSlot Return,
1666 QualType ResultType,
1667 llvm::Value *Sel,
1668 llvm::Value *Arg0,
1669 QualType Arg0Ty,
1670 bool IsSuper,
1671 const CallArgList &CallArgs,
1672 const ObjCMethodDecl *Method,
1673 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001674 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001675 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001676 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001677 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1678 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001679 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001680
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001681 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001682 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001683 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001684 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001685 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001686
Anders Carlsson280e61f12010-06-21 20:59:55 +00001687 if (Method)
1688 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1689 CGM.getContext().getCanonicalType(ResultType) &&
1690 "Result type mismatch!");
1691
John McCall5880fb82011-05-14 21:12:11 +00001692 NullReturnState nullReturn;
1693
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001694 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001695 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001696 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001697 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001698 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001699 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1700 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1701 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001702 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1703 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1704 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001705 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001706 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001707 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001708 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001709
1710 bool requiresnullCheck = false;
1711 if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1712 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1713 e = Method->param_end(); i != e; ++i) {
1714 const ParmVarDecl *ParamDecl = (*i);
1715 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1716 if (!nullReturn.NullBB)
1717 nullReturn.init(CGF, Arg0);
1718 requiresnullCheck = true;
1719 break;
1720 }
1721 }
1722
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001723 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001724 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001725 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1726 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001727}
1728
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001729static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1730 if (FQT.isObjCGCStrong())
1731 return Qualifiers::Strong;
1732
John McCall31168b02011-06-15 23:02:42 +00001733 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001734 return Qualifiers::Weak;
1735
1736 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1737 return Qualifiers::Strong;
1738
1739 if (const PointerType *PT = FQT->getAs<PointerType>())
1740 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1741
1742 return Qualifiers::GCNone;
1743}
1744
John McCall351762c2011-02-07 10:33:21 +00001745llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1746 const CGBlockInfo &blockInfo) {
1747 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001748 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001749
Douglas Gregor79a91412011-09-13 17:21:33 +00001750 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001751 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001752 return nullPtr;
1753
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001754 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001755 SkipIvars.clear();
1756 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001757 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1758 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001759
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001760 // __isa is the first field in block descriptor and must assume by runtime's
1761 // convention that it is GC'able.
1762 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001763
1764 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1765
1766 // Calculate the basic layout of the block structure.
1767 const llvm::StructLayout *layout =
1768 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1769
1770 // Ignore the optional 'this' capture: C++ objects are not assumed
1771 // to be GC'ed.
1772
1773 // Walk the captured variables.
1774 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1775 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1776 const VarDecl *variable = ci->getVariable();
1777 QualType type = variable->getType();
1778
1779 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1780
1781 // Ignore constant captures.
1782 if (capture.isConstant()) continue;
1783
1784 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1785
1786 // __block variables are passed by their descriptor address.
1787 if (ci->isByRef()) {
1788 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001789 continue;
John McCall351762c2011-02-07 10:33:21 +00001790 }
1791
1792 assert(!type->isArrayType() && "array variable should not be caught");
1793 if (const RecordType *record = type->getAs<RecordType>()) {
1794 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001795 continue;
1796 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001797
John McCall351762c2011-02-07 10:33:21 +00001798 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1799 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1800
1801 if (GCAttr == Qualifiers::Strong)
1802 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1803 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001804 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001805 SkipIvars.push_back(GC_IVAR(fieldOffset,
1806 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001807 }
1808
1809 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001810 return nullPtr;
1811
1812 // Sort on byte position; captures might not be allocated in order,
1813 // and unions can do funny things.
1814 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1815 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001816
1817 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001818 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001819 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1820 printf("\n block variable layout for block: ");
1821 const unsigned char *s = (unsigned char*)BitMap.c_str();
1822 for (unsigned i = 0; i < BitMap.size(); i++)
1823 if (!(s[i] & 0xf0))
1824 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1825 else
1826 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1827 printf("\n");
1828 }
1829
1830 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001831}
1832
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001833llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001834 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001835 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001836 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001837 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1838
Owen Andersonade90fd2009-07-29 18:54:39 +00001839 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001840 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001841}
1842
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001843void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001844 // FIXME: We shouldn't need this, the protocol decl should contain enough
1845 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001846 DefinedProtocols.insert(PD->getIdentifier());
1847
1848 // If we have generated a forward reference to this protocol, emit
1849 // it now. Otherwise do nothing, the protocol objects are lazily
1850 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001851 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001852 GetOrEmitProtocol(PD);
1853}
1854
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001855llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001856 if (DefinedProtocols.count(PD->getIdentifier()))
1857 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001858
Daniel Dunbarc475d422008-10-29 22:36:39 +00001859 return GetOrEmitProtocolRef(PD);
1860}
1861
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001863// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1864struct _objc_protocol {
1865struct _objc_protocol_extension *isa;
1866char *protocol_name;
1867struct _objc_protocol_list *protocol_list;
1868struct _objc__method_prototype_list *instance_methods;
1869struct _objc__method_prototype_list *class_methods
1870};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001872See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001873*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001874llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1875 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1876
1877 // Early exit if a defining object has already been generated.
1878 if (Entry && Entry->hasInitializer())
1879 return Entry;
1880
Douglas Gregora715bff2012-01-01 19:51:50 +00001881 // Use the protocol definition, if there is one.
1882 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1883 PD = Def;
1884
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001885 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001886 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001887 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1888
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001889 // Construct method lists.
1890 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1891 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001892 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001893 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001894 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001895 ObjCMethodDecl *MD = *i;
1896 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001897 if (!C)
1898 return GetOrEmitProtocolRef(PD);
1899
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001900 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1901 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001902 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001903 } else {
1904 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001905 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001906 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001907 }
1908
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001909 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001910 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001911 ObjCMethodDecl *MD = *i;
1912 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001913 if (!C)
1914 return GetOrEmitProtocolRef(PD);
1915
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001916 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1917 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001918 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001919 } else {
1920 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001921 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001922 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001923 }
1924
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001925 MethodTypesExt.insert(MethodTypesExt.end(),
1926 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1927
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001928 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001929 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1930 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001931 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001932 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001933 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001934 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001935 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001936 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001937 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001938 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001939 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001940 ClassMethods)
1941 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001942 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001943 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001944
Daniel Dunbarb036db82008-08-13 03:21:16 +00001945 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001946 // Already created, fix the linkage and update the initializer.
1947 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001948 Entry->setInitializer(Init);
1949 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001950 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001951 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001952 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001953 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001954 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001955 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001956 // FIXME: Is this necessary? Why only for protocol?
1957 Entry->setAlignment(4);
1958 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001959 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001960
1961 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001962}
1963
Daniel Dunbarc475d422008-10-29 22:36:39 +00001964llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001965 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1966
1967 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001968 // We use the initializer as a marker of whether this is a forward
1969 // reference or not. At module finalization we add the empty
1970 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001971 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001972 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001973 llvm::GlobalValue::ExternalLinkage,
1974 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001975 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001976 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001977 // FIXME: Is this necessary? Why only for protocol?
1978 Entry->setAlignment(4);
1979 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001980
Daniel Dunbarb036db82008-08-13 03:21:16 +00001981 return Entry;
1982}
1983
1984/*
1985 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986 uint32_t size;
1987 struct objc_method_description_list *optional_instance_methods;
1988 struct objc_method_description_list *optional_class_methods;
1989 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001990 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001991 };
1992*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001993llvm::Constant *
1994CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1995 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001996 const ConstantVector &OptClassMethods,
1997 const ConstantVector &MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001998 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001999 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002000 llvm::Constant *Values[] = {
2001 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002002 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002003 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002004 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002005 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002006 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002007 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002008 OptClassMethods),
2009 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002010 ObjCTypes),
2011 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2012 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002013 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002014
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002015 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002016 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002017 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002018 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002019
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002020 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002021 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002022
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002023 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002024 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002026 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002027}
2028
2029/*
2030 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002031 struct objc_protocol_list *next;
2032 long count;
2033 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002034 };
2035*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002036llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002037CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002038 ObjCProtocolDecl::protocol_iterator begin,
2039 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002040 std::vector<llvm::Constant*> ProtocolRefs;
2041
Daniel Dunbardec75f82008-08-21 21:57:41 +00002042 for (; begin != end; ++begin)
2043 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002044
2045 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002046 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002047 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002048
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002049 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002050 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002051
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002052 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002053 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002054 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002055 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002056 ProtocolRefs.size() - 1);
2057 Values[2] =
2058 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2059 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002060 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002061
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002062 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002064 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002065 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002066 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002067}
2068
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002069void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
2070 std::vector<llvm::Constant*> &Properties,
2071 const Decl *Container,
2072 const ObjCProtocolDecl *PROTO,
2073 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002074 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2075 E = PROTO->protocol_end(); P != E; ++P)
2076 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2077 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2078 E = PROTO->prop_end(); I != E; ++I) {
2079 const ObjCPropertyDecl *PD = *I;
2080 if (!PropertySet.insert(PD->getIdentifier()))
2081 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002082 llvm::Constant *Prop[] = {
2083 GetPropertyName(PD->getIdentifier()),
2084 GetPropertyTypeString(PD, Container)
2085 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002086 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2087 }
2088}
2089
Daniel Dunbarb036db82008-08-13 03:21:16 +00002090/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002091 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002092 const char * const name;
2093 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002094 };
2095
2096 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002097 uint32_t entsize; // sizeof (struct _objc_property)
2098 uint32_t prop_count;
2099 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002100 };
2101*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002102llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002103 const Decl *Container,
2104 const ObjCContainerDecl *OCD,
2105 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002106 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002107 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002108 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2109 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002110 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002111 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002112 llvm::Constant *Prop[] = {
2113 GetPropertyName(PD->getIdentifier()),
2114 GetPropertyTypeString(PD, Container)
2115 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002116 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002117 Prop));
2118 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002119 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002120 for (ObjCInterfaceDecl::all_protocol_iterator
2121 P = OID->all_referenced_protocol_begin(),
2122 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002123 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2124 ObjCTypes);
2125 }
2126 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2127 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2128 E = CD->protocol_end(); P != E; ++P)
2129 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2130 ObjCTypes);
2131 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002132
2133 // Return null for empty list.
2134 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002135 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002136
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002137 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002138 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002139 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002140 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2141 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002142 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002143 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002144 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002145 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002146
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002147 llvm::GlobalVariable *GV =
2148 CreateMetadataVar(Name, Init,
2149 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002150 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002151 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002152 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002153 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002154}
2155
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002156llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2157 const ConstantVector &MethodTypes,
2158 const ObjCCommonTypesHelper &ObjCTypes) {
2159 // Return null for empty list.
2160 if (MethodTypes.empty())
2161 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2162
2163 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2164 MethodTypes.size());
2165 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2166
2167 llvm::GlobalVariable *GV =
2168 CreateMetadataVar(Name, Init,
2169 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2170 (ObjCABI == 2) ? 8 : 4,
2171 true);
2172 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2173}
2174
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002175/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002176 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002177 int count;
2178 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002179 };
2180*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002181llvm::Constant *
2182CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002183 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002184 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002185 ObjCTypes.SelectorPtrTy),
2186 GetMethodVarType(MD)
2187 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002188 if (!Desc[1])
2189 return 0;
2190
Owen Anderson0e0189d2009-07-27 22:29:56 +00002191 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002192 Desc);
2193}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002194
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002195llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002196 const char *Section,
2197 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002198 // Return null for empty list.
2199 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002200 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002201
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002202 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002203 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002204 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002205 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002206 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002207 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002208
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002209 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002210 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002211 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002212}
2213
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002214/*
2215 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002216 char *category_name;
2217 char *class_name;
2218 struct _objc_method_list *instance_methods;
2219 struct _objc_method_list *class_methods;
2220 struct _objc_protocol_list *protocols;
2221 uint32_t size; // <rdar://4585769>
2222 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002223 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002224*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002225void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002226 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002227
Mike Stump18bb9282009-05-16 07:57:57 +00002228 // FIXME: This is poor design, the OCD should have a pointer to the category
2229 // decl. Additionally, note that Category can be null for the @implementation
2230 // w/o an @interface case. Sema should just create one for us as it does for
2231 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002232 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002233 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002234 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002235
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002236 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002237 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2238 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002239
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002240 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002241 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002242 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002243 // Instance methods should always be defined.
2244 InstanceMethods.push_back(GetMethodConstant(*i));
2245 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002246 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002247 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002248 // Class methods should always be defined.
2249 ClassMethods.push_back(GetMethodConstant(*i));
2250 }
2251
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002252 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002253 Values[0] = GetClassName(OCD->getIdentifier());
2254 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002255 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002257 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002258 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002259 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002260 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002261 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002262 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002263 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002264 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002265 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002266 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002267 Category->protocol_begin(),
2268 Category->protocol_end());
2269 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002270 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002271 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002272 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002273
2274 // If there is no category @interface then there can be no properties.
2275 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002276 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002277 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002278 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002279 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002280 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002281
Owen Anderson0e0189d2009-07-27 22:29:56 +00002282 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002283 Values);
2284
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002285 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002286 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002287 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002288 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002289 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002290 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002291 // method definition entries must be clear for next implementation.
2292 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002293}
2294
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002295// FIXME: Get from somewhere?
2296enum ClassFlags {
2297 eClassFlags_Factory = 0x00001,
2298 eClassFlags_Meta = 0x00002,
2299 // <rdr://5142207>
2300 eClassFlags_HasCXXStructors = 0x02000,
2301 eClassFlags_Hidden = 0x20000,
2302 eClassFlags_ABI2_Hidden = 0x00010,
2303 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2304};
2305
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002306/*
2307 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002308 Class isa;
2309 Class super_class;
2310 const char *name;
2311 long version;
2312 long info;
2313 long instance_size;
2314 struct _objc_ivar_list *ivars;
2315 struct _objc_method_list *methods;
2316 struct _objc_cache *cache;
2317 struct _objc_protocol_list *protocols;
2318 // Objective-C 1.0 extensions (<rdr://4585769>)
2319 const char *ivar_layout;
2320 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002321 };
2322
2323 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002324*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002326 DefinedSymbols.insert(ID->getIdentifier());
2327
Chris Lattner86d7d912008-11-24 03:54:41 +00002328 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002329 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002330 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002331 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002332 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002333 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002334 Interface->all_referenced_protocol_begin(),
2335 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002337 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002338 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002339 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002340 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002341
2342 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002343 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002344 Flags |= eClassFlags_Hidden;
2345
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002346 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002347 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002348 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002349 // Instance methods should always be defined.
2350 InstanceMethods.push_back(GetMethodConstant(*i));
2351 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002352 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002353 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002354 // Class methods should always be defined.
2355 ClassMethods.push_back(GetMethodConstant(*i));
2356 }
2357
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002358 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002359 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002360 ObjCPropertyImplDecl *PID = *i;
2361
2362 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2363 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2364
2365 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2366 if (llvm::Constant *C = GetMethodConstant(MD))
2367 InstanceMethods.push_back(C);
2368 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2369 if (llvm::Constant *C = GetMethodConstant(MD))
2370 InstanceMethods.push_back(C);
2371 }
2372 }
2373
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002374 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002375 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002376 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002377 // Record a reference to the super class.
2378 LazySymbols.insert(Super->getIdentifier());
2379
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002380 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002381 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002382 ObjCTypes.ClassPtrTy);
2383 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002384 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002385 }
2386 Values[ 2] = GetClassName(ID->getIdentifier());
2387 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002388 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2389 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2390 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002391 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002392 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002393 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002394 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002395 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002396 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002397 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002399 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002400 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002401 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002402 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002403 std::string Name("\01L_OBJC_CLASS_");
2404 Name += ClassName;
2405 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2406 // Check for a forward reference.
2407 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2408 if (GV) {
2409 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2410 "Forward metaclass reference has incorrect type.");
2411 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2412 GV->setInitializer(Init);
2413 GV->setSection(Section);
2414 GV->setAlignment(4);
2415 CGM.AddUsedGlobal(GV);
2416 }
2417 else
2418 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002419 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002420 // method definition entries must be clear for next implementation.
2421 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002422}
2423
2424llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2425 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002426 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002427 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002428 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002429
John McCall457a04e2010-10-22 21:05:15 +00002430 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002432
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002433 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002434 // The isa for the metaclass is the root of the hierarchy.
2435 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2436 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2437 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002438 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002439 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002440 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002441 // The super class for the metaclass is emitted as the name of the
2442 // super class. The runtime fixes this up to point to the
2443 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002445 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002446 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002447 ObjCTypes.ClassPtrTy);
2448 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002449 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002450 }
2451 Values[ 2] = GetClassName(ID->getIdentifier());
2452 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002453 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2454 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2455 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002456 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002457 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002458 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002459 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002460 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002462 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002463 Values[ 9] = Protocols;
2464 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002465 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002466 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002467 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002468 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002469 Values);
2470
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002471 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002472 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002473
2474 // Check for a forward reference.
2475 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2476 if (GV) {
2477 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2478 "Forward metaclass reference has incorrect type.");
2479 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2480 GV->setInitializer(Init);
2481 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002482 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002483 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002484 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002485 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002486 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002487 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002488 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002489
2490 return GV;
2491}
2492
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002493llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002494 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002495
Mike Stump18bb9282009-05-16 07:57:57 +00002496 // FIXME: Should we look these up somewhere other than the module. Its a bit
2497 // silly since we only generate these while processing an implementation, so
2498 // exactly one pointer would work if know when we entered/exitted an
2499 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002500
2501 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002502 // Previously, metaclass with internal linkage may have been defined.
2503 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002504 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2505 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002506 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2507 "Forward metaclass reference has incorrect type.");
2508 return GV;
2509 } else {
2510 // Generate as an external reference to keep a consistent
2511 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002512 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002513 llvm::GlobalValue::ExternalLinkage,
2514 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002515 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002516 }
2517}
2518
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002519llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2520 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2521
2522 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2523 true)) {
2524 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2525 "Forward class metadata reference has incorrect type.");
2526 return GV;
2527 } else {
2528 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2529 llvm::GlobalValue::ExternalLinkage,
2530 0,
2531 Name);
2532 }
2533}
2534
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002535/*
2536 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002537 uint32_t size;
2538 const char *weak_ivar_layout;
2539 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002540 };
2541*/
2542llvm::Constant *
2543CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002544 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002545 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002546
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002547 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002548 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002549 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002550 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002551 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002552
2553 // Return null if no extension bits are used.
2554 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002555 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002556
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002557 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002558 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002559 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002560 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002561 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002562}
2563
2564/*
2565 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002566 char *ivar_name;
2567 char *ivar_type;
2568 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002569 };
2570
2571 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002572 int ivar_count;
2573 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002574 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002575*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002576llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002577 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002578 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002579
2580 // When emitting the root class GCC emits ivar entries for the
2581 // actual class structure. It is not clear if we need to follow this
2582 // behavior; for now lets try and get away with not doing it. If so,
2583 // the cleanest solution would be to make up an ObjCInterfaceDecl
2584 // for the class.
2585 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002586 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002587
Jordy Rosea91768e2011-07-22 02:08:32 +00002588 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002589
Jordy Rosea91768e2011-07-22 02:08:32 +00002590 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002591 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002592 // Ignore unnamed bit-fields.
2593 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002594 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002595 llvm::Constant *Ivar[] = {
2596 GetMethodVarName(IVD->getIdentifier()),
2597 GetMethodVarType(IVD),
2598 llvm::ConstantInt::get(ObjCTypes.IntTy,
2599 ComputeIvarBaseOffset(CGM, OID, IVD))
2600 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002601 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002602 }
2603
2604 // Return null for empty list.
2605 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002606 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002607
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002608 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002609 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002610 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002611 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002612 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002613 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002614
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002615 llvm::GlobalVariable *GV;
2616 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002617 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002618 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002619 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002620 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002621 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002622 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002623 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002624 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002625}
2626
2627/*
2628 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002629 SEL method_name;
2630 char *method_types;
2631 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002632 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002633
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002634 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002635 struct objc_method_list *obsolete;
2636 int count;
2637 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002638 };
2639*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002640
2641/// GetMethodConstant - Return a struct objc_method constant for the
2642/// given method if it has been defined. The result is null if the
2643/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002644llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002645 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002646 if (!Fn)
2647 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002648
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002649 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002650 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002651 ObjCTypes.SelectorPtrTy),
2652 GetMethodVarType(MD),
2653 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2654 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002655 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002656}
2657
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002658llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002659 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002660 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002661 // Return null for empty list.
2662 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002663 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002664
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002665 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002666 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002667 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002668 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002669 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002670 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002671 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002672
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002673 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002674 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002675}
2676
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002677llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002678 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002679 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002680 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002681
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002682 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002683 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002684 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002685 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002686 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002687 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002688 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002689 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002690 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002691
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002692 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002693}
2694
Daniel Dunbar30c65362009-03-09 20:09:19 +00002695llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002696CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002697 llvm::Constant *Init,
2698 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002699 unsigned Align,
2700 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002701 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002702 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002703 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002704 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002705 if (Section)
2706 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002707 if (Align)
2708 GV->setAlignment(Align);
2709 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002710 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002711 return GV;
2712}
2713
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002714llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002715 // Abuse this interface function as a place to finalize.
2716 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002717 return NULL;
2718}
2719
Chris Lattnerd4808922009-03-22 21:03:39 +00002720llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002721 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002722}
2723
Chris Lattnerd4808922009-03-22 21:03:39 +00002724llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002725 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002726}
2727
David Chisnall168b80f2010-12-26 22:13:16 +00002728llvm::Constant *CGObjCMac::GetGetStructFunction() {
2729 return ObjCTypes.getCopyStructFn();
2730}
2731llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002732 return ObjCTypes.getCopyStructFn();
2733}
2734
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002735llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2736 return ObjCTypes.getCppAtomicObjectFunction();
2737}
2738
Chris Lattnerd4808922009-03-22 21:03:39 +00002739llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002740 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002741}
2742
John McCallbd309292010-07-06 01:34:17 +00002743void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2744 return EmitTryOrSynchronizedStmt(CGF, S);
2745}
2746
2747void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2748 const ObjCAtSynchronizedStmt &S) {
2749 return EmitTryOrSynchronizedStmt(CGF, S);
2750}
2751
John McCall65bea082010-07-21 06:59:36 +00002752namespace {
John McCallcda666c2010-07-21 07:22:38 +00002753 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002754 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002755 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002756 llvm::Value *CallTryExitVar;
2757 llvm::Value *ExceptionData;
2758 ObjCTypesHelper &ObjCTypes;
2759 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002760 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002761 llvm::Value *CallTryExitVar,
2762 llvm::Value *ExceptionData,
2763 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002764 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002765 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2766
John McCall30317fd2011-07-12 20:27:29 +00002767 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002768 // Check whether we need to call objc_exception_try_exit.
2769 // In optimized code, this branch will always be folded.
2770 llvm::BasicBlock *FinallyCallExit =
2771 CGF.createBasicBlock("finally.call_exit");
2772 llvm::BasicBlock *FinallyNoCallExit =
2773 CGF.createBasicBlock("finally.no_call_exit");
2774 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2775 FinallyCallExit, FinallyNoCallExit);
2776
2777 CGF.EmitBlock(FinallyCallExit);
2778 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2779 ->setDoesNotThrow();
2780
2781 CGF.EmitBlock(FinallyNoCallExit);
2782
2783 if (isa<ObjCAtTryStmt>(S)) {
2784 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002785 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2786 // Save the current cleanup destination in case there's
2787 // control flow inside the finally statement.
2788 llvm::Value *CurCleanupDest =
2789 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2790
John McCall65bea082010-07-21 06:59:36 +00002791 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2792
John McCallcebe0ca2010-08-11 00:16:14 +00002793 if (CGF.HaveInsertPoint()) {
2794 CGF.Builder.CreateStore(CurCleanupDest,
2795 CGF.getNormalCleanupDestSlot());
2796 } else {
2797 // Currently, the end of the cleanup must always exist.
2798 CGF.EnsureInsertPoint();
2799 }
2800 }
John McCall65bea082010-07-21 06:59:36 +00002801 } else {
2802 // Emit objc_sync_exit(expr); as finally's sole statement for
2803 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002804 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002805 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2806 ->setDoesNotThrow();
2807 }
2808 }
2809 };
John McCall42227ed2010-07-31 23:20:56 +00002810
2811 class FragileHazards {
2812 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002813 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002814 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2815
2816 llvm::InlineAsm *ReadHazard;
2817 llvm::InlineAsm *WriteHazard;
2818
2819 llvm::FunctionType *GetAsmFnType();
2820
2821 void collectLocals();
2822 void emitReadHazard(CGBuilderTy &Builder);
2823
2824 public:
2825 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002826
John McCall42227ed2010-07-31 23:20:56 +00002827 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002828 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002829 };
2830}
2831
2832/// Create the fragile-ABI read and write hazards based on the current
2833/// state of the function, which is presumed to be immediately prior
2834/// to a @try block. These hazards are used to maintain correct
2835/// semantics in the face of optimization and the fragile ABI's
2836/// cavalier use of setjmp/longjmp.
2837FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2838 collectLocals();
2839
2840 if (Locals.empty()) return;
2841
2842 // Collect all the blocks in the function.
2843 for (llvm::Function::iterator
2844 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2845 BlocksBeforeTry.insert(&*I);
2846
2847 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2848
2849 // Create a read hazard for the allocas. This inhibits dead-store
2850 // optimizations and forces the values to memory. This hazard is
2851 // inserted before any 'throwing' calls in the protected scope to
2852 // reflect the possibility that the variables might be read from the
2853 // catch block if the call throws.
2854 {
2855 std::string Constraint;
2856 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2857 if (I) Constraint += ',';
2858 Constraint += "*m";
2859 }
2860
2861 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2862 }
2863
2864 // Create a write hazard for the allocas. This inhibits folding
2865 // loads across the hazard. This hazard is inserted at the
2866 // beginning of the catch path to reflect the possibility that the
2867 // variables might have been written within the protected scope.
2868 {
2869 std::string Constraint;
2870 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2871 if (I) Constraint += ',';
2872 Constraint += "=*m";
2873 }
2874
2875 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2876 }
2877}
2878
2879/// Emit a write hazard at the current location.
2880void FragileHazards::emitWriteHazard() {
2881 if (Locals.empty()) return;
2882
Jay Foad5bd375a2011-07-15 08:37:34 +00002883 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002884}
2885
John McCall42227ed2010-07-31 23:20:56 +00002886void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2887 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002888 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002889}
2890
2891/// Emit read hazards in all the protected blocks, i.e. all the blocks
2892/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002893void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002894 if (Locals.empty()) return;
2895
2896 CGBuilderTy Builder(CGF.getLLVMContext());
2897
2898 // Iterate through all blocks, skipping those prior to the try.
2899 for (llvm::Function::iterator
2900 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2901 llvm::BasicBlock &BB = *FI;
2902 if (BlocksBeforeTry.count(&BB)) continue;
2903
2904 // Walk through all the calls in the block.
2905 for (llvm::BasicBlock::iterator
2906 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2907 llvm::Instruction &I = *BI;
2908
2909 // Ignore instructions that aren't non-intrinsic calls.
2910 // These are the only calls that can possibly call longjmp.
2911 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2912 if (isa<llvm::IntrinsicInst>(I))
2913 continue;
2914
2915 // Ignore call sites marked nounwind. This may be questionable,
2916 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2917 llvm::CallSite CS(&I);
2918 if (CS.doesNotThrow()) continue;
2919
John McCall2dd7d442010-08-04 05:59:32 +00002920 // Insert a read hazard before the call. This will ensure that
2921 // any writes to the locals are performed before making the
2922 // call. If the call throws, then this is sufficient to
2923 // guarantee correctness as long as it doesn't also write to any
2924 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002925 Builder.SetInsertPoint(&BB, BI);
2926 emitReadHazard(Builder);
2927 }
2928 }
2929}
2930
2931static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2932 if (V) S.insert(V);
2933}
2934
2935void FragileHazards::collectLocals() {
2936 // Compute a set of allocas to ignore.
2937 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2938 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2939 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002940
2941 // Collect all the allocas currently in the function. This is
2942 // probably way too aggressive.
2943 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2944 for (llvm::BasicBlock::iterator
2945 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2946 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2947 Locals.push_back(&*I);
2948}
2949
2950llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002951 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002952 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2953 tys[i] = Locals[i]->getType();
2954 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002955}
2956
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002957/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002958
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002959 Objective-C setjmp-longjmp (sjlj) Exception Handling
2960 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002961
John McCallbd309292010-07-06 01:34:17 +00002962 A catch buffer is a setjmp buffer plus:
2963 - a pointer to the exception that was caught
2964 - a pointer to the previous exception data buffer
2965 - two pointers of reserved storage
2966 Therefore catch buffers form a stack, with a pointer to the top
2967 of the stack kept in thread-local storage.
2968
2969 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2970 objc_exception_try_exit pops the given catch buffer, which is
2971 required to be the top of the EH stack.
2972 objc_exception_throw pops the top of the EH stack, writes the
2973 thrown exception into the appropriate field, and longjmps
2974 to the setjmp buffer. It crashes the process (with a printf
2975 and an abort()) if there are no catch buffers on the stack.
2976 objc_exception_extract just reads the exception pointer out of the
2977 catch buffer.
2978
2979 There's no reason an implementation couldn't use a light-weight
2980 setjmp here --- something like __builtin_setjmp, but API-compatible
2981 with the heavyweight setjmp. This will be more important if we ever
2982 want to implement correct ObjC/C++ exception interactions for the
2983 fragile ABI.
2984
2985 Note that for this use of setjmp/longjmp to be correct, we may need
2986 to mark some local variables volatile: if a non-volatile local
2987 variable is modified between the setjmp and the longjmp, it has
2988 indeterminate value. For the purposes of LLVM IR, it may be
2989 sufficient to make loads and stores within the @try (to variables
2990 declared outside the @try) volatile. This is necessary for
2991 optimized correctness, but is not currently being done; this is
2992 being tracked as rdar://problem/8160285
2993
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002994 The basic framework for a @try-catch-finally is as follows:
2995 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002996 objc_exception_data d;
2997 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002998 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002999
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003000 objc_exception_try_enter(&d);
3001 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003002 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003003 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003004 // exception path
3005 id _caught = objc_exception_extract(&d);
3006
3007 // enter new try scope for handlers
3008 if (!setjmp(d.jmp_buf)) {
3009 ... match exception and execute catch blocks ...
3010
3011 // fell off end, rethrow.
3012 _rethrow = _caught;
3013 ... jump-through-finally to finally_rethrow ...
3014 } else {
3015 // exception in catch block
3016 _rethrow = objc_exception_extract(&d);
3017 _call_try_exit = false;
3018 ... jump-through-finally to finally_rethrow ...
3019 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003020 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003021 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003022
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003023 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003024 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003025 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003026
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003027 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003028 ... dispatch to finally destination ...
3029
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003030 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003031 objc_exception_throw(_rethrow);
3032
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003033 finally_end:
3034 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003035
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003036 This framework differs slightly from the one gcc uses, in that gcc
3037 uses _rethrow to determine if objc_exception_try_exit should be called
3038 and if the object should be rethrown. This breaks in the face of
3039 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003040
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003041 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003042
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003043 - If there are no catch blocks, then we avoid emitting the second
3044 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003045
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003046 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3047 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003048
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003049 - FIXME: If there is no @finally block we can do a few more
3050 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003051
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003052 Rethrows and Jumps-Through-Finally
3053 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003054
John McCallbd309292010-07-06 01:34:17 +00003055 '@throw;' is supported by pushing the currently-caught exception
3056 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003057
John McCallbd309292010-07-06 01:34:17 +00003058 Branches through the @finally block are handled with an ordinary
3059 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3060 exceptions are not compatible with C++ exceptions, and this is
3061 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003062
John McCallbd309292010-07-06 01:34:17 +00003063 @synchronized(expr) { stmt; } is emitted as if it were:
3064 id synch_value = expr;
3065 objc_sync_enter(synch_value);
3066 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003067*/
3068
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003069void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3070 const Stmt &S) {
3071 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003072
3073 // A destination for the fall-through edges of the catch handlers to
3074 // jump to.
3075 CodeGenFunction::JumpDest FinallyEnd =
3076 CGF.getJumpDestInCurrentScope("finally.end");
3077
3078 // A destination for the rethrow edge of the catch handlers to jump
3079 // to.
3080 CodeGenFunction::JumpDest FinallyRethrow =
3081 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003082
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003083 // For @synchronized, call objc_sync_enter(sync.expr). The
3084 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003085 // @synchronized. We can't avoid a temp here because we need the
3086 // value to be preserved. If the backend ever does liveness
3087 // correctly after setjmp, this will be unnecessary.
3088 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003089 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003090 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003091 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3092 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003093 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3094 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003095
3096 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3097 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003098 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003099
John McCall2dd7d442010-08-04 05:59:32 +00003100 // Allocate memory for the setjmp buffer. This needs to be kept
3101 // live throughout the try and catch blocks.
3102 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3103 "exceptiondata.ptr");
3104
John McCall42227ed2010-07-31 23:20:56 +00003105 // Create the fragile hazards. Note that this will not capture any
3106 // of the allocas required for exception processing, but will
3107 // capture the current basic block (which extends all the way to the
3108 // setjmp call) as "before the @try".
3109 FragileHazards Hazards(CGF);
3110
John McCallbd309292010-07-06 01:34:17 +00003111 // Create a flag indicating whether the cleanup needs to call
3112 // objc_exception_try_exit. This is true except when
3113 // - no catches match and we're branching through the cleanup
3114 // just to rethrow the exception, or
3115 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003116 // The setjmp-safety rule here is that we should always store to this
3117 // variable in a place that dominates the branch through the cleanup
3118 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003119 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003120 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003121
John McCall9916e3f2010-10-04 23:42:51 +00003122 // A slot containing the exception to rethrow. Only needed when we
3123 // have both a @catch and a @finally.
3124 llvm::Value *PropagatingExnVar = 0;
3125
John McCallbd309292010-07-06 01:34:17 +00003126 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003127 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003128 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003129 CallTryExitVar,
3130 ExceptionData,
3131 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // Enter a try block:
3134 // - Call objc_exception_try_enter to push ExceptionData on top of
3135 // the EH stack.
3136 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3137 ->setDoesNotThrow();
3138
3139 // - Call setjmp on the exception data buffer.
3140 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3141 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3142 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003143 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003144 llvm::CallInst *SetJmpResult =
3145 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3146 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003147 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003148
3149 // If setjmp returned 0, enter the protected block; otherwise,
3150 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003151 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3152 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003153 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003154 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3155 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003156
John McCallbd309292010-07-06 01:34:17 +00003157 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003158 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003159 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003160 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003161 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003162
3163 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003164
John McCallbd309292010-07-06 01:34:17 +00003165 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003166 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003167
John McCall42227ed2010-07-31 23:20:56 +00003168 // Don't optimize loads of the in-scope locals across this point.
3169 Hazards.emitWriteHazard();
3170
John McCallbd309292010-07-06 01:34:17 +00003171 // For a @synchronized (or a @try with no catches), just branch
3172 // through the cleanup to the rethrow block.
3173 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3174 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003175 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003176 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003177
3178 // Otherwise, we have to match against the caught exceptions.
3179 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003180 // Retrieve the exception object. We may emit multiple blocks but
3181 // nothing can cross this so the value is already in SSA form.
3182 llvm::CallInst *Caught =
3183 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3184 ExceptionData, "caught");
3185 Caught->setDoesNotThrow();
3186
John McCallbd309292010-07-06 01:34:17 +00003187 // Push the exception to rethrow onto the EH value stack for the
3188 // benefit of any @throws in the handlers.
3189 CGF.ObjCEHValueStack.push_back(Caught);
3190
Douglas Gregor96c79492010-04-23 22:50:49 +00003191 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003192
John McCall2dd7d442010-08-04 05:59:32 +00003193 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003194
John McCall2dd7d442010-08-04 05:59:32 +00003195 llvm::BasicBlock *CatchBlock = 0;
3196 llvm::BasicBlock *CatchHandler = 0;
3197 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003198 // Save the currently-propagating exception before
3199 // objc_exception_try_enter clears the exception slot.
3200 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3201 "propagating_exception");
3202 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3203
John McCall2dd7d442010-08-04 05:59:32 +00003204 // Enter a new exception try block (in case a @catch block
3205 // throws an exception).
3206 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3207 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003208
John McCall2dd7d442010-08-04 05:59:32 +00003209 llvm::CallInst *SetJmpResult =
3210 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3211 "setjmp.result");
3212 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003213 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214
John McCall2dd7d442010-08-04 05:59:32 +00003215 llvm::Value *Threw =
3216 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3217
3218 CatchBlock = CGF.createBasicBlock("catch");
3219 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3220 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3221
3222 CGF.EmitBlock(CatchBlock);
3223 }
3224
3225 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003226
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003227 // Handle catch list. As a special case we check if everything is
3228 // matched and avoid generating code for falling off the end if
3229 // so.
3230 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003231 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3232 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003233
Douglas Gregor46a572b2010-04-26 16:46:50 +00003234 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003235 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003236
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003237 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003238 if (!CatchParam) {
3239 AllMatched = true;
3240 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003241 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003242
John McCallbd309292010-07-06 01:34:17 +00003243 // catch(id e) always matches under this ABI, since only
3244 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003245 // FIXME: For the time being we also match id<X>; this should
3246 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003247 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003248 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003249 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003250
John McCallbd309292010-07-06 01:34:17 +00003251 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003252 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003253 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3254
Anders Carlsson9396a892008-09-11 09:15:33 +00003255 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003256 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003257 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003258
3259 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003260 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003261 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003262
Anders Carlsson9396a892008-09-11 09:15:33 +00003263 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003264
3265 // The scope of the catch variable ends right here.
3266 CatchVarCleanups.ForceCleanup();
3267
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003268 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003269 break;
3270 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003271
Steve Naroff7cae42b2009-07-10 23:34:53 +00003272 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003273 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003274
3275 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003276 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3277 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003278
3279 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003280 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003281
John McCallbd309292010-07-06 01:34:17 +00003282 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003283 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3284 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003285 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003286
John McCallbd309292010-07-06 01:34:17 +00003287 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3288 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003289
3290 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003291 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003292
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003293 // Emit the @catch block.
3294 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003295
3296 // Collect any cleanups for the catch variable. The scope lasts until
3297 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003298 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003299
John McCall1c9c3fd2010-10-15 04:57:14 +00003300 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003301 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003302
John McCallbd309292010-07-06 01:34:17 +00003303 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003304 llvm::Value *Tmp =
3305 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003306 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003307 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003308
Anders Carlsson9396a892008-09-11 09:15:33 +00003309 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003310
3311 // We're done with the catch variable.
3312 CatchVarCleanups.ForceCleanup();
3313
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003314 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003315
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003316 CGF.EmitBlock(NextCatchBlock);
3317 }
3318
John McCallbd309292010-07-06 01:34:17 +00003319 CGF.ObjCEHValueStack.pop_back();
3320
John McCall2dd7d442010-08-04 05:59:32 +00003321 // If nothing wanted anything to do with the caught exception,
3322 // kill the extract call.
3323 if (Caught->use_empty())
3324 Caught->eraseFromParent();
3325
3326 if (!AllMatched)
3327 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3328
3329 if (HasFinally) {
3330 // Emit the exception handler for the @catch blocks.
3331 CGF.EmitBlock(CatchHandler);
3332
3333 // In theory we might now need a write hazard, but actually it's
3334 // unnecessary because there's no local-accessing code between
3335 // the try's write hazard and here.
3336 //Hazards.emitWriteHazard();
3337
John McCall9916e3f2010-10-04 23:42:51 +00003338 // Extract the new exception and save it to the
3339 // propagating-exception slot.
3340 assert(PropagatingExnVar);
3341 llvm::CallInst *NewCaught =
3342 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3343 ExceptionData, "caught");
3344 NewCaught->setDoesNotThrow();
3345 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3346
John McCall2dd7d442010-08-04 05:59:32 +00003347 // Don't pop the catch handler; the throw already did.
3348 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003349 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003350 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003351 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003352
John McCall42227ed2010-07-31 23:20:56 +00003353 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003354 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003355
John McCallbd309292010-07-06 01:34:17 +00003356 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003357 CGF.Builder.restoreIP(TryFallthroughIP);
3358 if (CGF.HaveInsertPoint())
3359 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003360 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003361 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003362
John McCallbd309292010-07-06 01:34:17 +00003363 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003364 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003365 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003366 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003367 // If we have a propagating-exception variable, check it.
3368 llvm::Value *PropagatingExn;
3369 if (PropagatingExnVar) {
3370 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003371
John McCall9916e3f2010-10-04 23:42:51 +00003372 // Otherwise, just look in the buffer for the exception to throw.
3373 } else {
3374 llvm::CallInst *Caught =
3375 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3376 ExceptionData);
3377 Caught->setDoesNotThrow();
3378 PropagatingExn = Caught;
3379 }
3380
3381 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003382 ->setDoesNotThrow();
3383 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003384 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003385
John McCall42227ed2010-07-31 23:20:56 +00003386 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003387}
3388
3389void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003390 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003391 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003392
Anders Carlssone005aa12008-09-09 16:16:55 +00003393 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003394 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003395 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003396 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003397 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003398 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003399 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003400 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003401 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003402
John McCallbd309292010-07-06 01:34:17 +00003403 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3404 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003405 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003406
3407 // Clear the insertion point to indicate we are in unreachable code.
3408 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003409}
3410
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003411/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003412/// object: objc_read_weak (id *src)
3413///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003414llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003415 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003416 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003417 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3418 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3419 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003420 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003421 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003422 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003423 return read_weak;
3424}
3425
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003426/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3427/// objc_assign_weak (id src, id *dst)
3428///
3429void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003430 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003431 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003432 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003433 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003434 assert(Size <= 8 && "does not support size > 8");
3435 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003436 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003437 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3438 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003439 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3440 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003441 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003442 src, dst, "weakassign");
3443 return;
3444}
3445
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003446/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3447/// objc_assign_global (id src, id *dst)
3448///
3449void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003450 llvm::Value *src, llvm::Value *dst,
3451 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003452 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003453 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003454 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003455 assert(Size <= 8 && "does not support size > 8");
3456 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003457 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003458 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3459 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003460 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3461 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003462 if (!threadlocal)
3463 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3464 src, dst, "globalassign");
3465 else
3466 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3467 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003468 return;
3469}
3470
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003471/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003472/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003473///
3474void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003475 llvm::Value *src, llvm::Value *dst,
3476 llvm::Value *ivarOffset) {
3477 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003478 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003479 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003480 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003481 assert(Size <= 8 && "does not support size > 8");
3482 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003483 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003484 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3485 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003486 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3487 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003488 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3489 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003490 return;
3491}
3492
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003493/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3494/// objc_assign_strongCast (id src, id *dst)
3495///
3496void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003497 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003498 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003499 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003500 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003501 assert(Size <= 8 && "does not support size > 8");
3502 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003503 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003504 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3505 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003506 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3507 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003508 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003509 src, dst, "weakassign");
3510 return;
3511}
3512
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003513void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003514 llvm::Value *DestPtr,
3515 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003516 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003517 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3518 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003519 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003520 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003521 return;
3522}
3523
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003524/// EmitObjCValueForIvar - Code Gen for ivar reference.
3525///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003526LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3527 QualType ObjectTy,
3528 llvm::Value *BaseValue,
3529 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003530 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003531 const ObjCInterfaceDecl *ID =
3532 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003533 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3534 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003535}
3536
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003537llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003538 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003539 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003540 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003541 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003542 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3543 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003544}
3545
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003546/* *** Private Interface *** */
3547
3548/// EmitImageInfo - Emit the image info marker used to encode some module
3549/// level information.
3550///
3551/// See: <rdr://4810609&4810587&4810587>
3552/// struct IMAGE_INFO {
3553/// unsigned version;
3554/// unsigned flags;
3555/// };
3556enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003557 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003558 eImageInfo_GarbageCollected = (1 << 1),
3559 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003560 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3561
Daniel Dunbar5e639272010-04-25 20:39:01 +00003562 // A flag indicating that the module has no instances of a @synthesize of a
3563 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003564 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003565};
3566
Daniel Dunbar5e639272010-04-25 20:39:01 +00003567void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003568 unsigned version = 0; // Version is unused?
3569 unsigned flags = 0;
3570
3571 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003572 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003573 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003574 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003575 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003576
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003577 // We never allow @synthesize of a superclass property.
3578 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003579
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003580 // Emitted as int[2];
Chris Lattner3def9ae2012-02-06 22:16:34 +00003581 uint32_t Values[2] = { version, flags };
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003582
3583 const char *Section;
3584 if (ObjCABI == 1)
3585 Section = "__OBJC, __image_info,regular";
3586 else
3587 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003588 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003589 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Chris Lattner3def9ae2012-02-06 22:16:34 +00003590 llvm::ConstantDataArray::get(VMContext, Values),
3591 Section, 0, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003592 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003593}
3594
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003595
3596// struct objc_module {
3597// unsigned long version;
3598// unsigned long size;
3599// const char *name;
3600// Symtab symtab;
3601// };
3602
3603// FIXME: Get from somewhere
3604static const int ModuleVersion = 7;
3605
3606void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003607 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003608
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003609 llvm::Constant *Values[] = {
3610 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3611 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3612 // This used to be the filename, now it is unused. <rdr://4327263>
3613 GetClassName(&CGM.getContext().Idents.get("")),
3614 EmitModuleSymbols()
3615 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003616 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003617 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003618 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003619 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003620}
3621
3622llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003623 unsigned NumClasses = DefinedClasses.size();
3624 unsigned NumCategories = DefinedCategories.size();
3625
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003626 // Return null if no symbols were defined.
3627 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003628 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003629
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003630 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003631 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003632 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003633 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3634 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003635
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003636 // The runtime expects exactly the list of defined classes followed
3637 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00003638 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003639 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003640 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003641 ObjCTypes.Int8PtrTy);
3642 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003643 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003644 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003645 ObjCTypes.Int8PtrTy);
3646
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003647 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003648 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00003649 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003650 Symbols);
3651
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003652 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003653
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003654 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003655 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3656 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003657 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003658 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003659}
3660
John McCall31168b02011-06-15 23:02:42 +00003661llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3662 IdentifierInfo *II) {
3663 LazySymbols.insert(II);
3664
3665 llvm::GlobalVariable *&Entry = ClassReferences[II];
3666
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003667 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003668 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003669 llvm::ConstantExpr::getBitCast(GetClassName(II),
3670 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003671 Entry =
John McCall31168b02011-06-15 23:02:42 +00003672 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3673 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3674 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003675 }
John McCall31168b02011-06-15 23:02:42 +00003676
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003677 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003678}
3679
John McCall31168b02011-06-15 23:02:42 +00003680llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3681 const ObjCInterfaceDecl *ID) {
3682 return EmitClassRefFromId(Builder, ID->getIdentifier());
3683}
3684
3685llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3686 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3687 return EmitClassRefFromId(Builder, II);
3688}
3689
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003690llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3691 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003692 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003693
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003694 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003695 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003696 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003697 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003698 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003699 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3700 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003701 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003702 }
3703
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003704 if (lvalue)
3705 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003706 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003707}
3708
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003709llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003710 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003711
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003712 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003713 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00003714 llvm::ConstantDataArray::getString(VMContext,
3715 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003716 ((ObjCABI == 2) ?
3717 "__TEXT,__objc_classname,cstring_literals" :
3718 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003719 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003720
Owen Anderson170229f2009-07-14 23:10:40 +00003721 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003722}
3723
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003724llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3725 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3726 I = MethodDefinitions.find(MD);
3727 if (I != MethodDefinitions.end())
3728 return I->second;
3729
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003730 return NULL;
3731}
3732
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003733/// GetIvarLayoutName - Returns a unique constant for the given
3734/// ivar layout bitmap.
3735llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003736 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003737 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003738}
3739
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003740void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003741 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003742 bool ForStrongLayout,
3743 bool &HasUnion) {
3744 const RecordDecl *RD = RT->getDecl();
3745 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003746 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003747 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003748 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003749 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003750
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003751 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3752 ForStrongLayout, HasUnion);
3753}
3754
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003755void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003756 const llvm::StructLayout *Layout,
3757 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003758 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003759 unsigned int BytePos, bool ForStrongLayout,
3760 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003761 bool IsUnion = (RD && RD->isUnion());
3762 uint64_t MaxUnionIvarSize = 0;
3763 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003764 const FieldDecl *MaxField = 0;
3765 const FieldDecl *MaxSkippedField = 0;
3766 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003767 uint64_t MaxFieldOffset = 0;
3768 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003769 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003770 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003771
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003772 if (RecFields.empty())
3773 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003774 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3775 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003776 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003777 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003778 FirstFieldDelta =
3779 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3780 }
3781
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003782 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003783 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003784 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003785 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003786 // Note that 'i' here is actually the field index inside RD of Field,
3787 // although this dependency is hidden.
3788 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003789 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003790 } else
John McCall31168b02011-06-15 23:02:42 +00003791 FieldOffset =
3792 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003793
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003794 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003795 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003796 LastFieldBitfieldOrUnnamed = Field;
3797 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003798 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003799 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003800
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003801 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003802 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003803 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003804 if (FQT->isUnionType())
3805 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003806
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003807 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003808 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003809 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003810 continue;
3811 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003812
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003813 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003814 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003815 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003816 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003817 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003818 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003819 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3820 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003821 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003822 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003823 FQT = CArray->getElementType();
3824 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003825
3826 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003827 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003828 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003829 int OldIndex = IvarsInfo.size() - 1;
3830 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003831
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003832 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003833 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003834 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003835
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003836 // Replicate layout information for each array element. Note that
3837 // one element is already done.
3838 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003839 for (int FirstIndex = IvarsInfo.size() - 1,
3840 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003841 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003842 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3843 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3844 IvarsInfo[i].ivar_size));
3845 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3846 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3847 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003848 }
3849 continue;
3850 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003851 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003852 // At this point, we are done with Record/Union and array there of.
3853 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003854 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003855
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003856 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003857 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3858 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003859 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003860 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003861 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003862 MaxUnionIvarSize = UnionIvarSize;
3863 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003864 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003865 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003866 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003867 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003868 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003869 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003870 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003871 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3872 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003873 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003874 // FIXME: Why the asymmetry? We divide by word size in bits on other
3875 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003876 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003877 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003878 MaxSkippedUnionIvarSize = UnionIvarSize;
3879 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003880 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003881 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003882 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003883 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003884 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003885 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003886 }
3887 }
3888 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003889
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003890 if (LastFieldBitfieldOrUnnamed) {
3891 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3892 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003893 uint64_t BitFieldSize
3894 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003895 GC_IVAR skivar;
3896 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3897 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3898 + ((BitFieldSize % ByteSizeInBits) != 0);
3899 SkipIvars.push_back(skivar);
3900 } else {
3901 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3902 // Last field was unnamed. Must update skip info.
3903 unsigned FieldSize
3904 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3905 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3906 FieldSize / ByteSizeInBits));
3907 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003908 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003909
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003910 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003911 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003912 MaxUnionIvarSize));
3913 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003914 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003915 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003916}
3917
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003918/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3919/// the computations and returning the layout bitmap (for ivar or blocks) in
3920/// the given argument BitMap string container. Routine reads
3921/// two containers, IvarsInfo and SkipIvars which are assumed to be
3922/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00003923llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003924 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003925 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003926
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003927 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003928 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003929 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003930 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003931 if (IvarsInfo[0].ivar_bytepos == 0) {
3932 WordsToSkip = 0;
3933 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003934 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003935 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3936 WordsToScan = IvarsInfo[0].ivar_size;
3937 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003938 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003939 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003940 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003941 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003942 // consecutive 'scanned' object pointers.
3943 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003944 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003945 // Skip over 'gc'able object pointer which lay over each other.
3946 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3947 continue;
3948 // Must skip over 1 or more words. We save current skip/scan values
3949 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003950 SKIP_SCAN SkScan;
3951 SkScan.skip = WordsToSkip;
3952 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003953 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003954
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003955 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003956 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3957 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003958 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003959 WordsToSkip = 0;
3960 WordsToScan = IvarsInfo[i].ivar_size;
3961 }
3962 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003963 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003964 SKIP_SCAN SkScan;
3965 SkScan.skip = WordsToSkip;
3966 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003967 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003968 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003969
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003970 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003971 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003972 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003973 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003974 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003975 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003976 IvarsInfo[LastIndex].ivar_bytepos +
3977 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003978 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003979 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003980 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003981 SKIP_SCAN SkScan;
3982 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3983 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003984 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003985 }
3986 }
3987 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3988 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003989 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003990 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003991 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3992 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3993 // 0xM0 followed by 0x0N detected.
3994 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3995 for (int j = i+1; j < SkipScan; j++)
3996 SkipScanIvars[j] = SkipScanIvars[j+1];
3997 --SkipScan;
3998 }
3999 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004000
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004001 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004002 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004003 unsigned char byte;
4004 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4005 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4006 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4007 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004008
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004009 // first skip big.
4010 for (unsigned int ix = 0; ix < skip_big; ix++)
4011 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004012
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004013 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004014 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004015 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004016 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004017 byte |= 0xf;
4018 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004019 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004020 byte |= scan_small;
4021 scan_small = 0;
4022 }
4023 BitMap += byte;
4024 }
4025 // next scan big
4026 for (unsigned int ix = 0; ix < scan_big; ix++)
4027 BitMap += (unsigned char)(0x0f);
4028 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004029 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004030 byte = scan_small;
4031 BitMap += byte;
4032 }
4033 }
4034 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004035 unsigned char zero = 0;
4036 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004037
4038 llvm::GlobalVariable * Entry =
4039 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004040 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004041 ((ObjCABI == 2) ?
4042 "__TEXT,__objc_classname,cstring_literals" :
4043 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004044 1, true);
4045 return getConstantGEP(VMContext, Entry, 0, 0);
4046}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004047
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004048/// BuildIvarLayout - Builds ivar layout bitmap for the class
4049/// implementation for the __strong or __weak case.
4050/// The layout map displays which words in ivar list must be skipped
4051/// and which must be scanned by GC (see below). String is built of bytes.
4052/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4053/// of words to skip and right nibble is count of words to scan. So, each
4054/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4055/// represented by a 0x00 byte which also ends the string.
4056/// 1. when ForStrongLayout is true, following ivars are scanned:
4057/// - id, Class
4058/// - object *
4059/// - __strong anything
4060///
4061/// 2. When ForStrongLayout is false, following ivars are scanned:
4062/// - __weak anything
4063///
4064llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4065 const ObjCImplementationDecl *OMD,
4066 bool ForStrongLayout) {
4067 bool hasUnion = false;
4068
Chris Lattner2192fe52011-07-18 04:24:23 +00004069 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00004070 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00004071 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004072 return llvm::Constant::getNullValue(PtrTy);
4073
Jordy Rosea91768e2011-07-22 02:08:32 +00004074 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4075 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004076 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004077 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004078 IVD; IVD = IVD->getNextIvar())
4079 RecFields.push_back(cast<FieldDecl>(IVD));
4080 }
4081 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004082 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004083 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004084
Jordy Rosea91768e2011-07-22 02:08:32 +00004085 // FIXME: This is not ideal; we shouldn't have to do this copy.
4086 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004087 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004088
4089 if (RecFields.empty())
4090 return llvm::Constant::getNullValue(PtrTy);
4091
4092 SkipIvars.clear();
4093 IvarsInfo.clear();
4094
4095 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4096 if (IvarsInfo.empty())
4097 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004098 // Sort on byte position in case we encounterred a union nested in
4099 // the ivar list.
4100 if (hasUnion && !IvarsInfo.empty())
4101 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4102 if (hasUnion && !SkipIvars.empty())
4103 std::sort(SkipIvars.begin(), SkipIvars.end());
4104
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004105 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004106 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004107
4108 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004109 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004110 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004111 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004112 const unsigned char *s = (unsigned char*)BitMap.c_str();
4113 for (unsigned i = 0; i < BitMap.size(); i++)
4114 if (!(s[i] & 0xf0))
4115 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4116 else
4117 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4118 printf("\n");
4119 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004120 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004121}
4122
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004123llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004124 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4125
Chris Lattner3def9ae2012-02-06 22:16:34 +00004126 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004127 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004128 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004129 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004130 ((ObjCABI == 2) ?
4131 "__TEXT,__objc_methname,cstring_literals" :
4132 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004133 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004134
Owen Anderson170229f2009-07-14 23:10:40 +00004135 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004136}
4137
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004138// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004139llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004140 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4141}
4142
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004143llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004144 std::string TypeStr;
4145 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4146
4147 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004148
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004149 if (!Entry)
4150 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004151 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004152 ((ObjCABI == 2) ?
4153 "__TEXT,__objc_methtype,cstring_literals" :
4154 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004155 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156
Owen Anderson170229f2009-07-14 23:10:40 +00004157 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004158}
4159
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004160llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4161 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004162 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00004163 if (CGM.getContext().getObjCEncodingForMethodDecl(
4164 const_cast<ObjCMethodDecl*>(D),
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004165 TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004166 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004167
4168 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4169
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004170 if (!Entry)
4171 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004172 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004173 ((ObjCABI == 2) ?
4174 "__TEXT,__objc_methtype,cstring_literals" :
4175 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004176 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004177
Owen Anderson170229f2009-07-14 23:10:40 +00004178 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004179}
4180
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004181// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004182llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004183 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004185 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004186 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004187 llvm::ConstantDataArray::getString(VMContext,
4188 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004189 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004190 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004191
Owen Anderson170229f2009-07-14 23:10:40 +00004192 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004193}
4194
4195// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004196// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004197llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004198CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4199 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004200 std::string TypeStr;
4201 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004202 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4203}
4204
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004205void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004206 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004207 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004208 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004209 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004210 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4211 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004212 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004213 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004214 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004215 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004216}
4217
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004218void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004219 EmitModuleInfo();
4220
Daniel Dunbarc475d422008-10-29 22:36:39 +00004221 // Emit the dummy bodies for any protocols which were referenced but
4222 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004223 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004224 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4225 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004226 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004227
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004228 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004229 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004230 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004231 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004232 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004233 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004234 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004235 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004236 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004237 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004238 }
4239
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004240 // Add assembler directives to add lazy undefined symbol references
4241 // for classes which are referenced but not defined. This is
4242 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004243 //
4244 // FIXME: It would be nice if we had an LLVM construct for this.
4245 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004246 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004247 Asm += CGM.getModule().getModuleInlineAsm();
4248 if (!Asm.empty() && Asm.back() != '\n')
4249 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004250
Daniel Dunbard027a922009-09-07 00:20:42 +00004251 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004252 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4253 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004254 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4255 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004256 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004257 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004258 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004259 }
4260
4261 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4262 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4263 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4264 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004265
Daniel Dunbard027a922009-09-07 00:20:42 +00004266 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004267 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004268}
4269
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004270CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004271 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004272 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004273 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004274 ObjCABI = 2;
4275}
4276
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004277/* *** */
4278
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004279ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004280 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4281{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004282 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4283 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004284
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004285 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004286 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004287 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004288 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004289 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004290 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004291
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004292 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004293 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004294 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004295
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004296 // I'm not sure I like this. The implicit coordination is a bit
4297 // gross. We should solve this in a reasonable fashion because this
4298 // is a pretty common task (match some runtime data structure with
4299 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004300
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004301 // FIXME: This is leaked.
4302 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004303
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004304 // struct _objc_super {
4305 // id self;
4306 // Class cls;
4307 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004308 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004309 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004310 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004311 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004312 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004313 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004314 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004315 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004316 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004317
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004318 SuperCTy = Ctx.getTagDeclType(RD);
4319 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004320
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004321 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004322 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4323
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004324 // struct _prop_t {
4325 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004326 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004327 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004328 PropertyTy = llvm::StructType::create("struct._prop_t",
4329 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004330
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004331 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004332 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004333 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004334 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004335 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004336 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004337 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4338 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004339 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004340 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004341
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004342 // struct _objc_method {
4343 // SEL _cmd;
4344 // char *method_type;
4345 // char *_imp;
4346 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004347 MethodTy = llvm::StructType::create("struct._objc_method",
4348 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4349 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004350
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004351 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004352 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004353 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004354
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004355}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004356
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004357ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004358 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004359 // struct _objc_method_description {
4360 // SEL name;
4361 // char *types;
4362 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004363 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004364 llvm::StructType::create("struct._objc_method_description",
4365 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004366
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004367 // struct _objc_method_description_list {
4368 // int count;
4369 // struct _objc_method_description[1];
4370 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004371 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004372 llvm::StructType::create("struct._objc_method_description_list",
4373 IntTy,
4374 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004375
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004376 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004377 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004378 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004379
Daniel Dunbarb036db82008-08-13 03:21:16 +00004380 // Protocol description structures
4381
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004382 // struct _objc_protocol_extension {
4383 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4384 // struct _objc_method_description_list *optional_instance_methods;
4385 // struct _objc_method_description_list *optional_class_methods;
4386 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004387 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004388 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004389 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004390 llvm::StructType::create("struct._objc_protocol_extension",
4391 IntTy, MethodDescriptionListPtrTy,
4392 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004393 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004394
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004395 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004396 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004397
Daniel Dunbarc475d422008-10-29 22:36:39 +00004398 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004399
Chris Lattnera5f58b02011-07-09 17:41:47 +00004400 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004401 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004402
Chris Lattnera5f58b02011-07-09 17:41:47 +00004403 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004404 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004405 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004406 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004407 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004408 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004409
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004410 // struct _objc_protocol {
4411 // struct _objc_protocol_extension *isa;
4412 // char *protocol_name;
4413 // struct _objc_protocol **_objc_protocol_list;
4414 // struct _objc_method_description_list *instance_methods;
4415 // struct _objc_method_description_list *class_methods;
4416 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004417 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4418 llvm::PointerType::getUnqual(ProtocolListTy),
4419 MethodDescriptionListPtrTy,
4420 MethodDescriptionListPtrTy,
4421 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004422
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004423 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004424 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004425
Owen Anderson9793f0e2009-07-29 22:16:19 +00004426 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004427
4428 // Class description structures
4429
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004430 // struct _objc_ivar {
4431 // char *ivar_name;
4432 // char *ivar_type;
4433 // int ivar_offset;
4434 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004435 IvarTy = llvm::StructType::create("struct._objc_ivar",
4436 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004437
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004438 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004439 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004440 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004441 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004442
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004443 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004444 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004445 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004446 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004447
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004448 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004449 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004450 llvm::StructType::create("struct._objc_class_extension",
4451 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004452 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004453
Chris Lattner5ec04a52011-08-12 17:43:31 +00004454 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004455
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004456 // struct _objc_class {
4457 // Class isa;
4458 // Class super_class;
4459 // char *name;
4460 // long version;
4461 // long info;
4462 // long instance_size;
4463 // struct _objc_ivar_list *ivars;
4464 // struct _objc_method_list *methods;
4465 // struct _objc_cache *cache;
4466 // struct _objc_protocol_list *protocols;
4467 // char *ivar_layout;
4468 // struct _objc_class_ext *ext;
4469 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004470 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4471 llvm::PointerType::getUnqual(ClassTy),
4472 Int8PtrTy,
4473 LongTy,
4474 LongTy,
4475 LongTy,
4476 IvarListPtrTy,
4477 MethodListPtrTy,
4478 CachePtrTy,
4479 ProtocolListPtrTy,
4480 Int8PtrTy,
4481 ClassExtensionPtrTy,
4482 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004483
Owen Anderson9793f0e2009-07-29 22:16:19 +00004484 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004485
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004486 // struct _objc_category {
4487 // char *category_name;
4488 // char *class_name;
4489 // struct _objc_method_list *instance_method;
4490 // struct _objc_method_list *class_method;
4491 // uint32_t size; // sizeof(struct _objc_category)
4492 // struct _objc_property_list *instance_properties;// category's @property
4493 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004494 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004495 llvm::StructType::create("struct._objc_category",
4496 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4497 MethodListPtrTy, ProtocolListPtrTy,
4498 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004499
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004500 // Global metadata structures
4501
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004502 // struct _objc_symtab {
4503 // long sel_ref_cnt;
4504 // SEL *refs;
4505 // short cls_def_cnt;
4506 // short cat_def_cnt;
4507 // char *defs[cls_def_cnt + cat_def_cnt];
4508 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004509 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004510 llvm::StructType::create("struct._objc_symtab",
4511 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4512 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004513 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004514
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004515 // struct _objc_module {
4516 // long version;
4517 // long size; // sizeof(struct _objc_module)
4518 // char *name;
4519 // struct _objc_symtab* symtab;
4520 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004521 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004522 llvm::StructType::create("struct._objc_module",
4523 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004524
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525
Mike Stump18bb9282009-05-16 07:57:57 +00004526 // FIXME: This is the size of the setjmp buffer and should be target
4527 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004528 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529
Anders Carlsson9ff22482008-09-09 10:10:21 +00004530 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004531 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004532 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004533
4534 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004535 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004536 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4537 SetJmpBufferSize),
4538 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004539
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004540}
4541
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004542ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004543 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004544 // struct _method_list_t {
4545 // uint32_t entsize; // sizeof(struct _objc_method)
4546 // uint32_t method_count;
4547 // struct _objc_method method_list[method_count];
4548 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004549 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004550 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4551 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004552 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004553 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004554
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004555 // struct _protocol_t {
4556 // id isa; // NULL
4557 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004558 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004559 // const struct method_list_t * const instance_methods;
4560 // const struct method_list_t * const class_methods;
4561 // const struct method_list_t *optionalInstanceMethods;
4562 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004563 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004564 // const uint32_t size; // sizeof(struct _protocol_t)
4565 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004566 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004567 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004568
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004569 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004570 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004571 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Chris Lattnera5f58b02011-07-09 17:41:47 +00004573 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004574 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4575 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4576 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4577 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004578 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4579 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004580
4581 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004582 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004584 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004585 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004586 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004587 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004588 ProtocolListnfABITy->setBody(LongTy,
4589 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4590 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004591
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004592 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004593 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004594
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004595 // struct _ivar_t {
4596 // unsigned long int *offset; // pointer to ivar offset location
4597 // char *name;
4598 // char *type;
4599 // uint32_t alignment;
4600 // uint32_t size;
4601 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004602 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004603 llvm::StructType::create("struct._ivar_t",
4604 llvm::PointerType::getUnqual(LongTy),
4605 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004607 // struct _ivar_list_t {
4608 // uint32 entsize; // sizeof(struct _ivar_t)
4609 // uint32 count;
4610 // struct _iver_t list[count];
4611 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004612 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004613 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4614 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615
Owen Anderson9793f0e2009-07-29 22:16:19 +00004616 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004617
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004618 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004619 // uint32_t const flags;
4620 // uint32_t const instanceStart;
4621 // uint32_t const instanceSize;
4622 // uint32_t const reserved; // only when building for 64bit targets
4623 // const uint8_t * const ivarLayout;
4624 // const char *const name;
4625 // const struct _method_list_t * const baseMethods;
4626 // const struct _objc_protocol_list *const baseProtocols;
4627 // const struct _ivar_list_t *const ivars;
4628 // const uint8_t * const weakIvarLayout;
4629 // const struct _prop_list_t * const properties;
4630 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004631
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004632 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004633 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4634 IntTy, IntTy, IntTy, Int8PtrTy,
4635 Int8PtrTy, MethodListnfABIPtrTy,
4636 ProtocolListnfABIPtrTy,
4637 IvarListnfABIPtrTy,
4638 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004639
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004640 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004641 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004642 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4643 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004644
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004645 // struct _class_t {
4646 // struct _class_t *isa;
4647 // struct _class_t * const superclass;
4648 // void *cache;
4649 // IMP *vtable;
4650 // struct class_ro_t *ro;
4651 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Chris Lattner5ec04a52011-08-12 17:43:31 +00004653 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004654 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4655 llvm::PointerType::getUnqual(ClassnfABITy),
4656 CachePtrTy,
4657 llvm::PointerType::getUnqual(ImpnfABITy),
4658 llvm::PointerType::getUnqual(ClassRonfABITy),
4659 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004660
Fariborz Jahanian71394042009-01-23 23:53:38 +00004661 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004662 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004663
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004664 // struct _category_t {
4665 // const char * const name;
4666 // struct _class_t *const cls;
4667 // const struct _method_list_t * const instance_methods;
4668 // const struct _method_list_t * const class_methods;
4669 // const struct _protocol_list_t * const protocols;
4670 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004671 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004672 CategorynfABITy = llvm::StructType::create("struct._category_t",
4673 Int8PtrTy, ClassnfABIPtrTy,
4674 MethodListnfABIPtrTy,
4675 MethodListnfABIPtrTy,
4676 ProtocolListnfABIPtrTy,
4677 PropertyListPtrTy,
4678 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004679
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004680 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004681 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4682 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004684 // MessageRefTy - LLVM for:
4685 // struct _message_ref_t {
4686 // IMP messenger;
4687 // SEL name;
4688 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004689
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004690 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004691 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004692 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004693 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004694 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004695 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004696 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004697 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004698 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004699 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004700
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004701 MessageRefCTy = Ctx.getTagDeclType(RD);
4702 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4703 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004704
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004705 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004706 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004707
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004708 // SuperMessageRefTy - LLVM for:
4709 // struct _super_message_ref_t {
4710 // SUPER_IMP messenger;
4711 // SEL name;
4712 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004713 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004714 llvm::StructType::create("struct._super_message_ref_t",
4715 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004716
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004717 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004718 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004719
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004720
4721 // struct objc_typeinfo {
4722 // const void** vtable; // objc_ehtype_vtable + 2
4723 // const char* name; // c++ typeinfo string
4724 // Class cls;
4725 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004726 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004727 llvm::StructType::create("struct._objc_typeinfo",
4728 llvm::PointerType::getUnqual(Int8PtrTy),
4729 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004730 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004731}
4732
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004733llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004734 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004735
Fariborz Jahanian71394042009-01-23 23:53:38 +00004736 return NULL;
4737}
4738
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004739void CGObjCNonFragileABIMac::AddModuleClassList(const
4740 std::vector<llvm::GlobalValue*>
4741 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004742 const char *SymbolName,
4743 const char *SectionName) {
4744 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004745
Daniel Dunbar19573e72009-05-15 21:48:48 +00004746 if (!NumClasses)
4747 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004748
Chris Lattner3def9ae2012-02-06 22:16:34 +00004749 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004750 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004751 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004752 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00004753 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004754 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004755 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00004756 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004757
Daniel Dunbar19573e72009-05-15 21:48:48 +00004758 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004759 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004760 llvm::GlobalValue::InternalLinkage,
4761 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004762 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004763 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004764 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004765 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004766}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004767
Fariborz Jahanian71394042009-01-23 23:53:38 +00004768void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4769 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004770
Daniel Dunbar19573e72009-05-15 21:48:48 +00004771 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004772 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004773 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004774 "\01L_OBJC_LABEL_CLASS_$",
4775 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004776
Fariborz Jahanian67260552009-11-17 21:37:35 +00004777 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4778 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4779 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4780 continue;
4781 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004782 }
4783
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004784 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4785 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4786 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4787 continue;
4788 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4789 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004790
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004791 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004792 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4793 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004795 // Build list of all implemented category addresses in array
4796 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004797 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004798 "\01L_OBJC_LABEL_CATEGORY_$",
4799 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004800 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004801 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4802 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004803
Daniel Dunbar5e639272010-04-25 20:39:01 +00004804 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004805}
4806
John McCall9e8bb002011-05-14 03:10:52 +00004807/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4808/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004809/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004810/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004811bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4812 // At various points we've experimented with using vtable-based
4813 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004814 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004815 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004816 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004817 case CodeGenOptions::NonLegacy:
4818 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004819 case CodeGenOptions::Mixed:
4820 break;
4821 }
4822
4823 // If so, see whether this selector is in the white-list of things which must
4824 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004825 if (VTableDispatchMethods.empty()) {
4826 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4827 VTableDispatchMethods.insert(GetNullarySelector("class"));
4828 VTableDispatchMethods.insert(GetNullarySelector("self"));
4829 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4830 VTableDispatchMethods.insert(GetNullarySelector("length"));
4831 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832
John McCall9e8bb002011-05-14 03:10:52 +00004833 // These are vtable-based if GC is disabled.
4834 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004835 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004836 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4837 VTableDispatchMethods.insert(GetNullarySelector("release"));
4838 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4839 }
4840
4841 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4842 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4843 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4844 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4845 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4846 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4847 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4848
4849 // These are vtable-based if GC is enabled.
4850 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004851 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004852 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4853 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4854
4855 // "countByEnumeratingWithState:objects:count"
4856 IdentifierInfo *KeyIdents[] = {
4857 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4858 &CGM.getContext().Idents.get("objects"),
4859 &CGM.getContext().Idents.get("count")
4860 };
4861 VTableDispatchMethods.insert(
4862 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4863 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004864 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004865
John McCall9e8bb002011-05-14 03:10:52 +00004866 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004867}
4868
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004869// Metadata flags
4870enum MetaDataDlags {
4871 CLS = 0x0,
4872 CLS_META = 0x1,
4873 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004874 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004875 CLS_EXCEPTION = 0x20,
4876
4877 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4878 CLS_HAS_IVAR_RELEASER = 0x40,
4879 /// class was compiled with -fobjc-arr
4880 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004881};
4882/// BuildClassRoTInitializer - generate meta-data for:
4883/// struct _class_ro_t {
4884/// uint32_t const flags;
4885/// uint32_t const instanceStart;
4886/// uint32_t const instanceSize;
4887/// uint32_t const reserved; // only when building for 64bit targets
4888/// const uint8_t * const ivarLayout;
4889/// const char *const name;
4890/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004891/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004892/// const struct _ivar_list_t *const ivars;
4893/// const uint8_t * const weakIvarLayout;
4894/// const struct _prop_list_t * const properties;
4895/// }
4896///
4897llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004898 unsigned flags,
4899 unsigned InstanceStart,
4900 unsigned InstanceSize,
4901 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004902 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004903 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004904
4905 if (CGM.getLangOptions().ObjCAutoRefCount)
4906 flags |= CLS_COMPILED_BY_ARC;
4907
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004908 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4909 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4910 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004911 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004912 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4913 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004914 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004915 // const struct _method_list_t * const baseMethods;
4916 std::vector<llvm::Constant*> Methods;
4917 std::string MethodListName("\01l_OBJC_$_");
4918 if (flags & CLS_META) {
4919 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004920 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004921 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004922 // Class methods should always be defined.
4923 Methods.push_back(GetMethodConstant(*i));
4924 }
4925 } else {
4926 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004927 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004928 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004929 // Instance methods should always be defined.
4930 Methods.push_back(GetMethodConstant(*i));
4931 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004932 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004933 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004934 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004935
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004936 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4937 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004938
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004939 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4940 if (llvm::Constant *C = GetMethodConstant(MD))
4941 Methods.push_back(C);
4942 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4943 if (llvm::Constant *C = GetMethodConstant(MD))
4944 Methods.push_back(C);
4945 }
4946 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004947 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004948 Values[ 5] = EmitMethodList(MethodListName,
4949 "__DATA, __objc_const", Methods);
4950
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004951 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4952 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004953 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004954 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004955 OID->all_referenced_protocol_begin(),
4956 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004957
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004958 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004959 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004960 else
4961 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4963 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004964 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004965 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004966 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004967 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4968 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004969 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004970 Values);
4971 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004972 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4973 llvm::GlobalValue::InternalLinkage,
4974 Init,
4975 (flags & CLS_META) ?
4976 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4977 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004978 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004979 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004980 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004981 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004982
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004983}
4984
4985/// BuildClassMetaData - This routine defines that to-level meta-data
4986/// for the given ClassName for:
4987/// struct _class_t {
4988/// struct _class_t *isa;
4989/// struct _class_t * const superclass;
4990/// void *cache;
4991/// IMP *vtable;
4992/// struct class_ro_t *ro;
4993/// }
4994///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004995llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004996 std::string &ClassName,
4997 llvm::Constant *IsAGV,
4998 llvm::Constant *SuperClassGV,
4999 llvm::Constant *ClassRoGV,
5000 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005001 llvm::Constant *Values[] = {
5002 IsAGV,
5003 SuperClassGV,
5004 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5005 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5006 ClassRoGV // &CLASS_RO_GV
5007 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005008 if (!Values[1])
5009 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005010 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005011 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005012 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5013 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005014 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005015 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005016 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005017 if (HiddenVisibility)
5018 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005019 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005020}
5021
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005022bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005023CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005024 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005025}
5026
Daniel Dunbar961202372009-05-03 12:57:56 +00005027void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005028 uint32_t &InstanceStart,
5029 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005030 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005031 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005032
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005033 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005034 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005035
5036 // If there are no fields, the start is the same as the end.
5037 if (!RL.getFieldCount())
5038 InstanceStart = InstanceSize;
5039 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005040 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005041}
5042
Fariborz Jahanian71394042009-01-23 23:53:38 +00005043void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5044 std::string ClassName = ID->getNameAsString();
5045 if (!ObjCEmptyCacheVar) {
5046 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005047 CGM.getModule(),
5048 ObjCTypes.CacheTy,
5049 false,
5050 llvm::GlobalValue::ExternalLinkage,
5051 0,
5052 "_objc_empty_cache");
5053
Fariborz Jahanian71394042009-01-23 23:53:38 +00005054 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055 CGM.getModule(),
5056 ObjCTypes.ImpnfABITy,
5057 false,
5058 llvm::GlobalValue::ExternalLinkage,
5059 0,
5060 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005061 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005062 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005063 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005064 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005065 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005066 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005067 uint32_t InstanceSize = InstanceStart;
5068 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005069 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5070 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005071
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005072 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073
5074 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005075 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005076 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005077 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005078 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005079 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005080 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005081 // class is root
5082 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005083 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005084 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005085 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005086 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005087 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5088 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5089 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005090 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005091 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005092 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005093 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005094 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005095 ObjCMetaClassName +
5096 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005097 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005098 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005099 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005100 }
5101 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5102 InstanceStart,
5103 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005104 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005105 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005106 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5107 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005108 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005109
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005110 // Metadata for the class
5111 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005112 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005113 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005114 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005115 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005116
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005117 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005118 flags |= CLS_EXCEPTION;
5119
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005120 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005121 flags |= CLS_ROOT;
5122 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005123 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005124 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005125 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005126 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005127 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005128 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005129 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005130 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005131 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005132 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005133 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005135 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005136
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005137 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005138 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005139 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5140 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005141 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005142
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005143 // Determine if this class is also "non-lazy".
5144 if (ImplementationIsNonLazy(ID))
5145 DefinedNonLazyClasses.push_back(ClassMD);
5146
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005147 // Force the definition of the EHType if necessary.
5148 if (flags & CLS_EXCEPTION)
5149 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005150 // Make sure method definition entries are all clear for next implementation.
5151 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005152}
5153
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005154/// GenerateProtocolRef - This routine is called to generate code for
5155/// a protocol reference expression; as in:
5156/// @code
5157/// @protocol(Proto1);
5158/// @endcode
5159/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5160/// which will hold address of the protocol meta-data.
5161///
5162llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005163 const ObjCProtocolDecl *PD) {
5164
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005165 // This routine is called for @protocol only. So, we must build definition
5166 // of protocol's meta-data (not a reference to it!)
5167 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168 llvm::Constant *Init =
5169 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005170 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005171
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005172 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005173 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005174
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005175 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5176 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005177 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005178 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005179 CGM.getModule(),
5180 Init->getType(), false,
5181 llvm::GlobalValue::WeakAnyLinkage,
5182 Init,
5183 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005184 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5185 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005186 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005187 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005188}
5189
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005190/// GenerateCategory - Build metadata for a category implementation.
5191/// struct _category_t {
5192/// const char * const name;
5193/// struct _class_t *const cls;
5194/// const struct _method_list_t * const instance_methods;
5195/// const struct _method_list_t * const class_methods;
5196/// const struct _protocol_list_t * const protocols;
5197/// const struct _prop_list_t * const properties;
5198/// }
5199///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005200void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005201 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005202 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005203 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5204 "_$_" + OCD->getNameAsString());
5205 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005206 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005207
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005208 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005209 Values[0] = GetClassName(OCD->getIdentifier());
5210 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005211 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005212 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005213 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5214
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005215 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005216 std::vector<llvm::Constant*> Methods;
5217 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005218 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005219 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005220
5221 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005222 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005223 // Instance methods should always be defined.
5224 Methods.push_back(GetMethodConstant(*i));
5225 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226
5227 Values[2] = EmitMethodList(MethodListName,
5228 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005229 Methods);
5230
5231 MethodListName = Prefix;
5232 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5233 OCD->getNameAsString();
5234 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005235 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005236 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005237 // Class methods should always be defined.
5238 Methods.push_back(GetMethodConstant(*i));
5239 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005240
5241 Values[3] = EmitMethodList(MethodListName,
5242 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005243 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005244 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005245 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005246 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005247 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005248 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5249 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005250 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005251 + Interface->getName() + "_$_"
5252 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005253 Category->protocol_begin(),
5254 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005255 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5256 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005257 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005258 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5259 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005260 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005261
5262 llvm::Constant *Init =
5263 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005264 Values);
5265 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005266 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005267 false,
5268 llvm::GlobalValue::InternalLinkage,
5269 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005270 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005271 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005272 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005273 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005274 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005275 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005276
5277 // Determine if this category is also "non-lazy".
5278 if (ImplementationIsNonLazy(OCD))
5279 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005280 // method definition entries must be clear for next implementation.
5281 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005282}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005283
5284/// GetMethodConstant - Return a struct objc_method constant for the
5285/// given method if it has been defined. The result is null if the
5286/// method has not been defined. The return value has type MethodPtrTy.
5287llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005289 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005290 if (!Fn)
5291 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005292
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005293 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005294 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005295 ObjCTypes.SelectorPtrTy),
5296 GetMethodVarType(MD),
5297 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5298 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005299 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005300}
5301
5302/// EmitMethodList - Build meta-data for method declarations
5303/// struct _method_list_t {
5304/// uint32_t entsize; // sizeof(struct _objc_method)
5305/// uint32_t method_count;
5306/// struct _objc_method method_list[method_count];
5307/// }
5308///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005309llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005310 const char *Section,
5311 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005312 // Return null for empty list.
5313 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005314 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005315
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005316 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005317 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005318 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005319 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005320 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005321 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005322 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005323 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005324 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005325 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005327 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005328 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005329 llvm::GlobalValue::InternalLinkage, Init, Name);
5330 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005331 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005332 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005333 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005334}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005335
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005336/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5337/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005338llvm::GlobalVariable *
5339CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5340 const ObjCIvarDecl *Ivar) {
5341 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005342 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005343 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005344 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005345 CGM.getModule().getGlobalVariable(Name);
5346 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005347 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005348 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005349 false,
5350 llvm::GlobalValue::ExternalLinkage,
5351 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005352 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005353 return IvarOffsetGV;
5354}
5355
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005356llvm::Constant *
5357CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5358 const ObjCIvarDecl *Ivar,
5359 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005360 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005361 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005362 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005363 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005364 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005365
Mike Stump18bb9282009-05-16 07:57:57 +00005366 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5367 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005368 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5369 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005370 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005371 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005372 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005373 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005374 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005375 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005376}
5377
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005378/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005379/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005380/// IvarListnfABIPtrTy.
5381/// struct _ivar_t {
5382/// unsigned long int *offset; // pointer to ivar offset location
5383/// char *name;
5384/// char *type;
5385/// uint32_t alignment;
5386/// uint32_t size;
5387/// }
5388/// struct _ivar_list_t {
5389/// uint32 entsize; // sizeof(struct _ivar_t)
5390/// uint32 count;
5391/// struct _iver_t list[count];
5392/// }
5393///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005394
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005395llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005396 const ObjCImplementationDecl *ID) {
5397
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005398 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005399
Jordy Rosea91768e2011-07-22 02:08:32 +00005400 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005401 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005402
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005403 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005404
Jordy Rosea91768e2011-07-22 02:08:32 +00005405 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005406 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005407 // Ignore unnamed bit-fields.
5408 if (!IVD->getDeclName())
5409 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005410 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005412 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005413 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5414 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005415 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005416 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005417 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005418 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005419 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005420 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005421 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005422 // NOTE. Size of a bitfield does not match gcc's, because of the
5423 // way bitfields are treated special in each. But I am told that
5424 // 'size' for bitfield ivars is ignored by the runtime so it does
5425 // not matter. If it matters, there is enough info to get the
5426 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005427 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005428 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005429 }
5430 // Return null for empty list.
5431 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005432 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005433
5434 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005435 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005436 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5437 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005438 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005439 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005440 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005441 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005442 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5443 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005444 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005445 llvm::GlobalValue::InternalLinkage,
5446 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005447 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005448 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005449 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005450 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005451
Chris Lattnerf56501c2009-07-17 23:57:13 +00005452 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005453 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005454}
5455
5456llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005458 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005459
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005460 if (!Entry) {
5461 // We use the initializer as a marker of whether this is a forward
5462 // reference or not. At module finalization we add the empty
5463 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005464 Entry =
5465 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5466 llvm::GlobalValue::ExternalLinkage,
5467 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005468 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005469 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005470 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005471
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005472 return Entry;
5473}
5474
5475/// GetOrEmitProtocol - Generate the protocol meta-data:
5476/// @code
5477/// struct _protocol_t {
5478/// id isa; // NULL
5479/// const char * const protocol_name;
5480/// const struct _protocol_list_t * protocol_list; // super protocols
5481/// const struct method_list_t * const instance_methods;
5482/// const struct method_list_t * const class_methods;
5483/// const struct method_list_t *optionalInstanceMethods;
5484/// const struct method_list_t *optionalClassMethods;
5485/// const struct _prop_list_t * properties;
5486/// const uint32_t size; // sizeof(struct _protocol_t)
5487/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005488/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489/// }
5490/// @endcode
5491///
5492
5493llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005494 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005495 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005496
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005497 // Early exit if a defining object has already been generated.
5498 if (Entry && Entry->hasInitializer())
5499 return Entry;
5500
Douglas Gregora715bff2012-01-01 19:51:50 +00005501 // Use the protocol definition, if there is one.
5502 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5503 PD = Def;
5504
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005505 // Construct method lists.
5506 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5507 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005508 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005509 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005510 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005511 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005512 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005513 if (!C)
5514 return GetOrEmitProtocolRef(PD);
5515
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005516 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5517 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005518 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005519 } else {
5520 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005521 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005522 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005523 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005524
5525 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005526 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005527 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005528 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005529 if (!C)
5530 return GetOrEmitProtocolRef(PD);
5531
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005532 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5533 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005534 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005535 } else {
5536 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005537 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005538 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005539 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005540
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005541 MethodTypesExt.insert(MethodTypesExt.end(),
5542 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5543
5544 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005545 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005546 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005547 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005548 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5549 PD->protocol_begin(),
5550 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005551
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005552 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005553 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005554 "__DATA, __objc_const",
5555 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005556 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005557 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005558 "__DATA, __objc_const",
5559 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005560 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005561 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005562 "__DATA, __objc_const",
5563 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005564 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005565 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005566 "__DATA, __objc_const",
5567 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005568 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005569 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005570 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005571 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005572 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005573 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005574 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5575 + PD->getName(),
5576 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005577 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005578 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005580 if (Entry) {
5581 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005582 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005583 Entry->setInitializer(Init);
5584 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005585 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005586 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5587 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5588 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005589 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005590 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005591 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005592 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005593 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005594 CGM.AddUsedGlobal(Entry);
5595
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005596 // Use this protocol meta-data to build protocol list table in section
5597 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005598 llvm::GlobalVariable *PTGV =
5599 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5600 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5601 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005602 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005603 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005604 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005605 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005606 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005607 return Entry;
5608}
5609
5610/// EmitProtocolList - Generate protocol list meta-data:
5611/// @code
5612/// struct _protocol_list_t {
5613/// long protocol_count; // Note, this is 32/64 bit
5614/// struct _protocol_t[protocol_count];
5615/// }
5616/// @endcode
5617///
5618llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005619CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005620 ObjCProtocolDecl::protocol_iterator begin,
5621 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005622 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005623
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005624 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005625 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005626 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005627
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005628 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005629 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005630 Name.toVector(TmpName);
5631 llvm::GlobalVariable *GV =
5632 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005633 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005634 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005636 for (; begin != end; ++begin)
5637 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5638
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005639 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005640 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005641 ObjCTypes.ProtocolnfABIPtrTy));
5642
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005643 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005644 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005645 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005646 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005647 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005648 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005649 ProtocolRefs.size()),
5650 ProtocolRefs);
5651
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005652 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005653 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005654 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005655 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005656 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005657 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005658 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005659 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005660 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005661 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005662}
5663
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005664/// GetMethodDescriptionConstant - This routine build following meta-data:
5665/// struct _objc_method {
5666/// SEL _cmd;
5667/// char *method_type;
5668/// char *_imp;
5669/// }
5670
5671llvm::Constant *
5672CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005673 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005674 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005675 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5676 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005677 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005678 if (!Desc[1])
5679 return 0;
5680
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005681 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005682 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005683 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005684}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005685
5686/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5687/// This code gen. amounts to generating code for:
5688/// @code
5689/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5690/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005691///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005692LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005693 CodeGen::CodeGenFunction &CGF,
5694 QualType ObjectTy,
5695 llvm::Value *BaseValue,
5696 const ObjCIvarDecl *Ivar,
5697 unsigned CVRQualifiers) {
5698 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005699 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5700 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005701}
5702
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005703llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005704 CodeGen::CodeGenFunction &CGF,
5705 const ObjCInterfaceDecl *Interface,
5706 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005707 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005708}
5709
John McCall234eac82011-05-13 23:16:18 +00005710static void appendSelectorForMessageRefTable(std::string &buffer,
5711 Selector selector) {
5712 if (selector.isUnarySelector()) {
5713 buffer += selector.getNameForSlot(0);
5714 return;
5715 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005716
John McCall234eac82011-05-13 23:16:18 +00005717 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5718 buffer += selector.getNameForSlot(i);
5719 buffer += '_';
5720 }
5721}
5722
John McCall9e8bb002011-05-14 03:10:52 +00005723/// Emit a "v-table" message send. We emit a weak hidden-visibility
5724/// struct, initially containing the selector pointer and a pointer to
5725/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5726/// load and call the function pointer, passing the address of the
5727/// struct as the second parameter. The runtime determines whether
5728/// the selector is currently emitted using vtable dispatch; if so, it
5729/// substitutes a stub function which simply tail-calls through the
5730/// appropriate vtable slot, and if not, it substitues a stub function
5731/// which tail-calls objc_msgSend. Both stubs adjust the selector
5732/// argument to correctly point to the selector.
5733RValue
5734CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5735 ReturnValueSlot returnSlot,
5736 QualType resultType,
5737 Selector selector,
5738 llvm::Value *arg0,
5739 QualType arg0Type,
5740 bool isSuper,
5741 const CallArgList &formalArgs,
5742 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005743 // Compute the actual arguments.
5744 CallArgList args;
5745
John McCall9e8bb002011-05-14 03:10:52 +00005746 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005747 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005748 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5749 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005750
John McCall9e8bb002011-05-14 03:10:52 +00005751 // Second argument: a pointer to the message ref structure. Leave
5752 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005753 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5754
5755 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5756
5757 const CGFunctionInfo &fnInfo =
5758 CGM.getTypes().getFunctionInfo(resultType, args,
5759 FunctionType::ExtInfo());
5760
John McCall5880fb82011-05-14 21:12:11 +00005761 NullReturnState nullReturn;
5762
John McCall9e8bb002011-05-14 03:10:52 +00005763 // Find the function to call and the mangled name for the message
5764 // ref structure. Using a different mangled name wouldn't actually
5765 // be a problem; it would just be a waste.
5766 //
5767 // The runtime currently never uses vtable dispatch for anything
5768 // except normal, non-super message-sends.
5769 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005770 llvm::Constant *fn = 0;
5771 std::string messageRefName("\01l_");
5772 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005773 if (isSuper) {
5774 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5775 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005776 } else {
John McCall5880fb82011-05-14 21:12:11 +00005777 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005778 fn = ObjCTypes.getMessageSendStretFixupFn();
5779 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005780 }
John McCall234eac82011-05-13 23:16:18 +00005781 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5782 fn = ObjCTypes.getMessageSendFpretFixupFn();
5783 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005784 } else {
John McCall234eac82011-05-13 23:16:18 +00005785 if (isSuper) {
5786 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5787 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005788 } else {
John McCall234eac82011-05-13 23:16:18 +00005789 fn = ObjCTypes.getMessageSendFixupFn();
5790 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005791 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005792 }
John McCall234eac82011-05-13 23:16:18 +00005793 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5794 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005795
John McCall234eac82011-05-13 23:16:18 +00005796 // Append the selector name, except use underscores anywhere we
5797 // would have used colons.
5798 appendSelectorForMessageRefTable(messageRefName, selector);
5799
5800 llvm::GlobalVariable *messageRef
5801 = CGM.getModule().getGlobalVariable(messageRefName);
5802 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005803 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005804 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005805 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005806 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5807 init->getType(),
5808 /*constant*/ false,
5809 llvm::GlobalValue::WeakAnyLinkage,
5810 init,
5811 messageRefName);
5812 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5813 messageRef->setAlignment(16);
5814 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5815 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005816
5817 bool requiresnullCheck = false;
5818 if (CGM.getLangOptions().ObjCAutoRefCount && method)
5819 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5820 e = method->param_end(); i != e; ++i) {
5821 const ParmVarDecl *ParamDecl = (*i);
5822 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5823 if (!nullReturn.NullBB)
5824 nullReturn.init(CGF, arg0);
5825 requiresnullCheck = true;
5826 break;
5827 }
5828 }
5829
John McCall234eac82011-05-13 23:16:18 +00005830 llvm::Value *mref =
5831 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5832
John McCall9e8bb002011-05-14 03:10:52 +00005833 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005834 args[1].RV = RValue::get(mref);
5835
5836 // Load the function to call from the message ref table.
5837 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5838 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5839
5840 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005841 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005842 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5843 callee = CGF.Builder.CreateBitCast(callee,
5844 llvm::PointerType::getUnqual(fnType));
5845
John McCall5880fb82011-05-14 21:12:11 +00005846 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005847 return nullReturn.complete(CGF, result, resultType, formalArgs,
5848 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005849}
5850
5851/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005852CodeGen::RValue
5853CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005854 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005855 QualType ResultType,
5856 Selector Sel,
5857 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005858 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005859 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005860 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005861 return isVTableDispatchedSelector(Sel)
5862 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005863 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005864 false, CallArgs, Method)
5865 : EmitMessageSend(CGF, Return, ResultType,
5866 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005867 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005868 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005869}
5870
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005871llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005872CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005873 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5874
Daniel Dunbara6468342009-03-02 05:18:14 +00005875 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005876 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005877 false, llvm::GlobalValue::ExternalLinkage,
5878 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005879 }
5880
5881 return GV;
5882}
5883
John McCall31168b02011-06-15 23:02:42 +00005884llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5885 IdentifierInfo *II) {
5886 llvm::GlobalVariable *&Entry = ClassReferences[II];
5887
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005888 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005889 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005890 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005891 Entry =
John McCall31168b02011-06-15 23:02:42 +00005892 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5893 false, llvm::GlobalValue::InternalLinkage,
5894 ClassGV,
5895 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005896 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005897 CGM.getTargetData().getABITypeAlignment(
5898 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005899 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005900 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005901 }
John McCall31168b02011-06-15 23:02:42 +00005902
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005903 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005904}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005905
John McCall31168b02011-06-15 23:02:42 +00005906llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5907 const ObjCInterfaceDecl *ID) {
5908 return EmitClassRefFromId(Builder, ID->getIdentifier());
5909}
5910
5911llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5912 CGBuilderTy &Builder) {
5913 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5914 return EmitClassRefFromId(Builder, II);
5915}
5916
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005917llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005918CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005919 const ObjCInterfaceDecl *ID) {
5920 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005921
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005922 if (!Entry) {
5923 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5924 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005925 Entry =
5926 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005927 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005928 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005929 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005930 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005931 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005933 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005934 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005935 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005936
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005937 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005938}
5939
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005940/// EmitMetaClassRef - Return a Value * of the address of _class_t
5941/// meta-data
5942///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5944 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005945 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5946 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005947 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005948
Daniel Dunbar15894b72009-04-07 05:48:37 +00005949 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005950 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005951 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005952 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005953 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005954 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005955 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005956 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005957 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005958 ObjCTypes.ClassnfABIPtrTy));
5959
Daniel Dunbare60aa052009-04-15 19:03:14 +00005960 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005961 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005962
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005963 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005964}
5965
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005966/// GetClass - Return a reference to the class for the given interface
5967/// decl.
5968llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5969 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005970 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005971 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5972 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5973 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5974 }
5975
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005976 return EmitClassRef(Builder, ID);
5977}
5978
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005979/// Generates a message send where the super is the receiver. This is
5980/// a message send to self with special delivery semantics indicating
5981/// which class's method should be called.
5982CodeGen::RValue
5983CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005984 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005985 QualType ResultType,
5986 Selector Sel,
5987 const ObjCInterfaceDecl *Class,
5988 bool isCategoryImpl,
5989 llvm::Value *Receiver,
5990 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005991 const CodeGen::CallArgList &CallArgs,
5992 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005993 // ...
5994 // Create and init a super structure; this is a (receiver, class)
5995 // pair we will pass to objc_msgSendSuper.
5996 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005997 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005998
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005999 llvm::Value *ReceiverAsObject =
6000 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6001 CGF.Builder.CreateStore(ReceiverAsObject,
6002 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006003
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006004 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006005 llvm::Value *Target;
6006 if (IsClassMessage) {
6007 if (isCategoryImpl) {
6008 // Message sent to "super' in a class method defined in
6009 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006010 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006011 Target = CGF.Builder.CreateStructGEP(Target, 0);
6012 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00006013 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006014 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00006015 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006016 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006017
Mike Stump18bb9282009-05-16 07:57:57 +00006018 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6019 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006020 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006021 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6022 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6023 CGF.Builder.CreateStore(Target,
6024 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006025
John McCall9e8bb002011-05-14 03:10:52 +00006026 return (isVTableDispatchedSelector(Sel))
6027 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006028 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006029 true, CallArgs, Method)
6030 : EmitMessageSend(CGF, Return, ResultType,
6031 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006032 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006033 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006034}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006035
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006036llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006037 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006038 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006039
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006040 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006041 llvm::Constant *Casted =
6042 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6043 ObjCTypes.SelectorPtrTy);
6044 Entry =
6045 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6046 llvm::GlobalValue::InternalLinkage,
6047 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006048 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006049 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006050 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006051
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006052 if (lval)
6053 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006054 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6055
6056 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6057 llvm::MDNode::get(VMContext,
6058 ArrayRef<llvm::Value*>()));
6059 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006060}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006061/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006062/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006063///
6064void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006065 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006066 llvm::Value *dst,
6067 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006068 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006069 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006070 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006071 assert(Size <= 8 && "does not support size > 8");
6072 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6073 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006074 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6075 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006076 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6077 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006078 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6079 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006080 return;
6081}
6082
6083/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6084/// objc_assign_strongCast (id src, id *dst)
6085///
6086void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006087 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006088 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006089 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006090 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006091 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006092 assert(Size <= 8 && "does not support size > 8");
6093 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006094 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006095 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6096 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006097 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6098 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006099 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006100 src, dst, "weakassign");
6101 return;
6102}
6103
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006104void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006105 CodeGen::CodeGenFunction &CGF,
6106 llvm::Value *DestPtr,
6107 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006108 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006109 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6110 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006111 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006112 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006113 return;
6114}
6115
Fariborz Jahanian06292952009-02-16 22:52:32 +00006116/// EmitObjCWeakRead - Code gen for loading value of a __weak
6117/// object: objc_read_weak (id *src)
6118///
6119llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006120 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006121 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006122 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006123 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6124 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006125 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006126 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006127 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006128 return read_weak;
6129}
6130
6131/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6132/// objc_assign_weak (id src, id *dst)
6133///
6134void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006135 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006136 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006137 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006138 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006139 assert(Size <= 8 && "does not support size > 8");
6140 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6141 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006142 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6143 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006144 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6145 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006146 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006147 src, dst, "weakassign");
6148 return;
6149}
6150
6151/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6152/// objc_assign_global (id src, id *dst)
6153///
6154void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006155 llvm::Value *src, llvm::Value *dst,
6156 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006157 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006158 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006159 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006160 assert(Size <= 8 && "does not support size > 8");
6161 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6162 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006163 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6164 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006165 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6166 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006167 if (!threadlocal)
6168 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6169 src, dst, "globalassign");
6170 else
6171 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6172 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006173 return;
6174}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006175
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006176void
John McCallbd309292010-07-06 01:34:17 +00006177CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6178 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006179 EmitAtSynchronizedStmt(CGF, S,
6180 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6181 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006182}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006183
John McCall2ca705e2010-07-24 00:37:23 +00006184llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006185CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006186 // There's a particular fixed type info for 'id'.
6187 if (T->isObjCIdType() ||
6188 T->isObjCQualifiedIdType()) {
6189 llvm::Constant *IDEHType =
6190 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6191 if (!IDEHType)
6192 IDEHType =
6193 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6194 false,
6195 llvm::GlobalValue::ExternalLinkage,
6196 0, "OBJC_EHTYPE_id");
6197 return IDEHType;
6198 }
6199
6200 // All other types should be Objective-C interface pointer types.
6201 const ObjCObjectPointerType *PT =
6202 T->getAs<ObjCObjectPointerType>();
6203 assert(PT && "Invalid @catch type.");
6204 const ObjCInterfaceType *IT = PT->getInterfaceType();
6205 assert(IT && "Invalid @catch type.");
6206 return GetInterfaceEHType(IT->getDecl(), false);
6207}
6208
John McCallbd309292010-07-06 01:34:17 +00006209void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6210 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006211 EmitTryCatchStmt(CGF, S,
6212 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6213 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6214 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006215}
6216
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006217/// EmitThrowStmt - Generate code for a throw statement.
6218void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6219 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006220 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006221 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006222 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006223 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006224 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006225 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006226 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006227 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006228 }
6229
John McCall17afe452010-10-16 08:21:07 +00006230 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006231 CGF.Builder.ClearInsertionPoint();
6232}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006233
John McCall2ca705e2010-07-24 00:37:23 +00006234llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006235CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006236 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006237 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006238
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006239 // If we don't need a definition, return the entry if found or check
6240 // if we use an external reference.
6241 if (!ForDefinition) {
6242 if (Entry)
6243 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006244
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006245 // If this type (or a super class) has the __objc_exception__
6246 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006247 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006248 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006249 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006250 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006251 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006252 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006253 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006254 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006255
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006256 // Otherwise we need to either make a new entry or fill in the
6257 // initializer.
6258 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006259 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006260 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006261 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006262 CGM.getModule().getGlobalVariable(VTableName);
6263 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006264 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6265 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006266 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006267 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006268
Chris Lattner5e016ae2010-06-27 07:15:29 +00006269 llvm::Value *VTableIdx =
6270 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006271
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006272 llvm::Constant *Values[] = {
6273 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6274 GetClassName(ID->getIdentifier()),
6275 GetClassGlobal(ClassName)
6276 };
Owen Anderson170229f2009-07-14 23:10:40 +00006277 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006278 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006279
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006280 if (Entry) {
6281 Entry->setInitializer(Init);
6282 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006283 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006284 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006285 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006286 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006287 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006288 }
6289
John McCall457a04e2010-10-22 21:05:15 +00006290 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006291 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006292 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6293 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006294
6295 if (ForDefinition) {
6296 Entry->setSection("__DATA,__objc_const");
6297 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6298 } else {
6299 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6300 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006301
6302 return Entry;
6303}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006304
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006305/* *** */
6306
Daniel Dunbarb036db82008-08-13 03:21:16 +00006307CodeGen::CGObjCRuntime *
6308CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006309 if (CGM.getLangOptions().ObjCNonFragileABI)
6310 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006311 return new CGObjCMac(CGM);
6312}