blob: 8bd96382c9e5211169262eeb72d0c9618cc028de [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
Daniel Dunbarcb463852008-11-01 01:53:16 +00001087 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001088 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089
Chris Lattnerd4808922009-03-22 21:03:39 +00001090 virtual llvm::Constant *GetPropertyGetFunction();
1091 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001092 virtual llvm::Constant *GetGetStructFunction();
1093 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001094 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001095 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001096
John McCallbd309292010-07-06 01:34:17 +00001097 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1098 const ObjCAtTryStmt &S);
1099 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1100 const ObjCAtSynchronizedStmt &S);
1101 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001102 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1103 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001104 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001105 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001106 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001107 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001108 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001109 llvm::Value *src, llvm::Value *dest,
1110 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001111 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001112 llvm::Value *src, llvm::Value *dest,
1113 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001114 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1115 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001116 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1117 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001118 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001119
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001120 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1121 QualType ObjectTy,
1122 llvm::Value *BaseValue,
1123 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001124 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001125 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001126 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001127 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001128
1129 /// GetClassGlobal - Return the global variable for the Objective-C
1130 /// class of the given name.
1131 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001132 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001133 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001134};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001135
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001136class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001137private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001138 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001139 llvm::GlobalVariable* ObjCEmptyCacheVar;
1140 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001141
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001142 /// SuperClassReferences - uniqued super class references.
1143 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001144
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001145 /// MetaClassReferences - uniqued meta class references.
1146 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001147
1148 /// EHTypeReferences - uniqued class ehtype references.
1149 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001150
John McCall9e8bb002011-05-14 03:10:52 +00001151 /// VTableDispatchMethods - List of methods for which we generate
1152 /// vtable-based message dispatch.
1153 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001154
Fariborz Jahanian67260552009-11-17 21:37:35 +00001155 /// DefinedMetaClasses - List of defined meta-classes.
1156 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1157
John McCall9e8bb002011-05-14 03:10:52 +00001158 /// isVTableDispatchedSelector - Returns true if SEL is a
1159 /// vtable-based selector.
1160 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161
Fariborz Jahanian71394042009-01-23 23:53:38 +00001162 /// FinishNonFragileABIModule - Write out global data structures at the end of
1163 /// processing a translation unit.
1164 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001165
Daniel Dunbar19573e72009-05-15 21:48:48 +00001166 /// AddModuleClassList - Add the given list of class pointers to the
1167 /// module with the provided symbol and section names.
1168 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1169 const char *SymbolName,
1170 const char *SectionName);
1171
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001172 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1173 unsigned InstanceStart,
1174 unsigned InstanceSize,
1175 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001176 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001177 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001178 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001179 llvm::Constant *ClassRoGV,
1180 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001182 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001183
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001184 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001185
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001186 /// EmitMethodList - Emit the method list for the given
1187 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001188 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001189 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001190 const ConstantVector &Methods);
1191 /// EmitIvarList - Emit the ivar list for the given
1192 /// implementation. If ForClass is true the list of class ivars
1193 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1194 /// interface ivars will be emitted. The return value has type
1195 /// IvarListnfABIPtrTy.
1196 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001197
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001198 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001199 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001200 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001201
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001202 /// GetOrEmitProtocol - Get the protocol object for the given
1203 /// declaration, emitting it if necessary. The return value has type
1204 /// ProtocolPtrTy.
1205 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001206
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001207 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1208 /// object for the given declaration, emitting it if needed. These
1209 /// forward references will be filled in with empty bodies if no
1210 /// definition is seen. The return value has type ProtocolPtrTy.
1211 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001213 /// EmitProtocolList - Generate the list of referenced
1214 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001215 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001216 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001217 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218
John McCall9e8bb002011-05-14 03:10:52 +00001219 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1220 ReturnValueSlot Return,
1221 QualType ResultType,
1222 Selector Sel,
1223 llvm::Value *Receiver,
1224 QualType Arg0Ty,
1225 bool IsSuper,
1226 const CallArgList &CallArgs,
1227 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001228
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001229 /// GetClassGlobal - Return the global variable for the Objective-C
1230 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001231 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001232
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001233 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001234 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001235 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001236 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001237
1238 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1239 IdentifierInfo *II);
1240
1241 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001243 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1244 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001245 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1246 const ObjCInterfaceDecl *ID);
1247
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001248 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1249 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001250 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001251 const ObjCInterfaceDecl *ID);
1252
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001253 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1254 /// the given ivar.
1255 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001256 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001257 const ObjCInterfaceDecl *ID,
1258 const ObjCIvarDecl *Ivar);
1259
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001260 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1261 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001262 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1263 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001264
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001265 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001266 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001267 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001268 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001269
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001270 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001271 return "OBJC_METACLASS_$_";
1272 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001273
Daniel Dunbar15894b72009-04-07 05:48:37 +00001274 const char *getClassSymbolPrefix() const {
1275 return "OBJC_CLASS_$_";
1276 }
1277
Daniel Dunbar961202372009-05-03 12:57:56 +00001278 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001279 uint32_t &InstanceStart,
1280 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001281
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001282 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001283 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001284 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1285 return CGM.getContext().Selectors.getSelector(0, &II);
1286 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001287
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001288 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001289 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1290 return CGM.getContext().Selectors.getSelector(1, &II);
1291 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001292
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001293 /// ImplementationIsNonLazy - Check whether the given category or
1294 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001295 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001296
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001297public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001298 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001299 // FIXME. All stubs for now!
1300 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001301
Fariborz Jahanian71394042009-01-23 23:53:38 +00001302 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001303 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001304 QualType ResultType,
1305 Selector Sel,
1306 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001307 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001308 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001309 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310
1311 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001312 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001313 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001314 QualType ResultType,
1315 Selector Sel,
1316 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001317 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001318 llvm::Value *Receiver,
1319 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001320 const CallArgList &CallArgs,
1321 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322
Fariborz Jahanian71394042009-01-23 23:53:38 +00001323 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001324 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001325
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001326 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1327 bool lvalue = false)
1328 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001329
1330 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1331 /// untyped one.
1332 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1333 const ObjCMethodDecl *Method)
1334 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001336 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001337
Fariborz Jahanian71394042009-01-23 23:53:38 +00001338 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001339 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001340 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001341
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001342 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001343
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001344 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001345 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001346 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001347 virtual llvm::Constant *GetPropertySetFunction() {
1348 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001349 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001350
David Chisnall168b80f2010-12-26 22:13:16 +00001351 virtual llvm::Constant *GetSetStructFunction() {
1352 return ObjCTypes.getCopyStructFn();
1353 }
1354 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001355 return ObjCTypes.getCopyStructFn();
1356 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001357 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1358 return ObjCTypes.getCppAtomicObjectFunction();
1359 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001360
Chris Lattnerd4808922009-03-22 21:03:39 +00001361 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001362 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001363 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001364
John McCallbd309292010-07-06 01:34:17 +00001365 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1366 const ObjCAtTryStmt &S);
1367 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1368 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001369 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001370 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001371 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001372 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001373 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001374 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001375 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001376 llvm::Value *src, llvm::Value *dest,
1377 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001378 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001379 llvm::Value *src, llvm::Value *dest,
1380 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001381 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001382 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001383 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1384 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001385 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001386 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1387 QualType ObjectTy,
1388 llvm::Value *BaseValue,
1389 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001390 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001391 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001392 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001393 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001394};
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001395} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001396
1397/* *** Helper Functions *** */
1398
1399/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001400static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001401 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001402 unsigned idx0,
1403 unsigned idx1) {
1404 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001405 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1406 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001407 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001408 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001409}
1410
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001411/// hasObjCExceptionAttribute - Return true if this class or any super
1412/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001413static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001414 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001415 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001416 return true;
1417 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001418 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001419 return false;
1420}
1421
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001422/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001423
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001424CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001425 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001426 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001427 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001428}
1429
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001430/// GetClass - Return a reference to the class for the given interface
1431/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001432llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001433 const ObjCInterfaceDecl *ID) {
1434 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001435}
1436
1437/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001438llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1439 bool lval) {
1440 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001441}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001442llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001443 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001444 return EmitSelector(Builder, Method->getSelector());
1445}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001446
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001447llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001448 if (T->isObjCIdType() ||
1449 T->isObjCQualifiedIdType()) {
1450 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001451 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001452 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001453 if (T->isObjCClassType() ||
1454 T->isObjCQualifiedClassType()) {
1455 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001456 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001457 }
1458 if (T->isObjCObjectPointerType())
1459 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1460
John McCall2ca705e2010-07-24 00:37:23 +00001461 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001462}
1463
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001464/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001465/*
1466 struct __builtin_CFString {
1467 const int *isa; // point to __CFConstantStringClassReference
1468 int flags;
1469 const char *str;
1470 long length;
1471 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001472*/
1473
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001474/// or Generate a constant NSString object.
1475/*
1476 struct __builtin_NSString {
1477 const int *isa; // point to __NSConstantStringClassReference
1478 const char *str;
1479 unsigned int length;
1480 };
1481*/
1482
Fariborz Jahanian71394042009-01-23 23:53:38 +00001483llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001484 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001485 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1486 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001487 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001488}
1489
1490/// Generates a message send where the super is the receiver. This is
1491/// a message send to self with special delivery semantics indicating
1492/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001493CodeGen::RValue
1494CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001495 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001496 QualType ResultType,
1497 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001498 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001499 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001500 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001501 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001502 const CodeGen::CallArgList &CallArgs,
1503 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001504 // Create and init a super structure; this is a (receiver, class)
1505 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001506 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001507 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001508 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001509 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001510 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001511 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001512
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001513 // If this is a class message the metaclass is passed as the target.
1514 llvm::Value *Target;
1515 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001516 if (isCategoryImpl) {
1517 // Message sent to 'super' in a class method defined in a category
1518 // implementation requires an odd treatment.
1519 // If we are in a class method, we must retrieve the
1520 // _metaclass_ for the current class, pointed at by
1521 // the class's "isa" pointer. The following assumes that
1522 // isa" is the first ivar in a class (which it must be).
1523 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1524 Target = CGF.Builder.CreateStructGEP(Target, 0);
1525 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001526 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001527 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1528 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1529 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1530 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001531 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001532 }
1533 else if (isCategoryImpl)
1534 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1535 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001536 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1537 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1538 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001539 }
Mike Stump18bb9282009-05-16 07:57:57 +00001540 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1541 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001542 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001543 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001544 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001545 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001546 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001547 return EmitMessageSend(CGF, Return, ResultType,
1548 EmitSelector(CGF.Builder, Sel),
1549 ObjCSuper, ObjCTypes.SuperPtrCTy,
1550 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001551}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001552
1553/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001554CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001555 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001556 QualType ResultType,
1557 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001558 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001559 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001560 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001561 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001562 return EmitMessageSend(CGF, Return, ResultType,
1563 EmitSelector(CGF.Builder, Sel),
1564 Receiver, CGF.getContext().getObjCIdType(),
1565 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001566}
1567
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001568CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001569CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1570 ReturnValueSlot Return,
1571 QualType ResultType,
1572 llvm::Value *Sel,
1573 llvm::Value *Arg0,
1574 QualType Arg0Ty,
1575 bool IsSuper,
1576 const CallArgList &CallArgs,
1577 const ObjCMethodDecl *Method,
1578 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001579 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001580 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001581 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001582 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1583 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001584 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001585
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001586 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001587 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001588 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001589 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001590 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001591
Anders Carlsson280e61f12010-06-21 20:59:55 +00001592 if (Method)
1593 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1594 CGM.getContext().getCanonicalType(ResultType) &&
1595 "Result type mismatch!");
1596
John McCall5880fb82011-05-14 21:12:11 +00001597 NullReturnState nullReturn;
1598
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001599 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001600 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian326efeb2012-01-28 18:46:31 +00001601 if (!IsSuper) {
1602 bool nullCheckAlreadyDone = false;
1603 // We have already done this computation once and flag could have been
1604 // passed down. But such cases are extremely rare and we do this lazily,
1605 // instead of absorbing cost of passing down a flag for all cases.
1606 if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1607 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1608 e = Method->param_end(); i != e; ++i) {
1609 if ((*i)->hasAttr<NSConsumedAttr>()) {
1610 nullCheckAlreadyDone = true;
1611 break;
1612 }
1613 }
1614 if (!nullCheckAlreadyDone)
1615 nullReturn.init(CGF, Arg0);
1616 }
1617
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001618 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001619 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001620 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1621 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1622 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001623 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1624 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1625 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001626 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001627 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001628 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001629 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001630 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001631 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1632 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001633}
1634
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001635static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1636 if (FQT.isObjCGCStrong())
1637 return Qualifiers::Strong;
1638
John McCall31168b02011-06-15 23:02:42 +00001639 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001640 return Qualifiers::Weak;
1641
1642 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1643 return Qualifiers::Strong;
1644
1645 if (const PointerType *PT = FQT->getAs<PointerType>())
1646 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1647
1648 return Qualifiers::GCNone;
1649}
1650
John McCall351762c2011-02-07 10:33:21 +00001651llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1652 const CGBlockInfo &blockInfo) {
1653 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001654 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001655
Douglas Gregor79a91412011-09-13 17:21:33 +00001656 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001657 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001658 return nullPtr;
1659
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001660 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001661 SkipIvars.clear();
1662 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001663 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1664 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001665
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001666 // __isa is the first field in block descriptor and must assume by runtime's
1667 // convention that it is GC'able.
1668 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001669
1670 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1671
1672 // Calculate the basic layout of the block structure.
1673 const llvm::StructLayout *layout =
1674 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1675
1676 // Ignore the optional 'this' capture: C++ objects are not assumed
1677 // to be GC'ed.
1678
1679 // Walk the captured variables.
1680 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1681 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1682 const VarDecl *variable = ci->getVariable();
1683 QualType type = variable->getType();
1684
1685 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1686
1687 // Ignore constant captures.
1688 if (capture.isConstant()) continue;
1689
1690 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1691
1692 // __block variables are passed by their descriptor address.
1693 if (ci->isByRef()) {
1694 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001695 continue;
John McCall351762c2011-02-07 10:33:21 +00001696 }
1697
1698 assert(!type->isArrayType() && "array variable should not be caught");
1699 if (const RecordType *record = type->getAs<RecordType>()) {
1700 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001701 continue;
1702 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001703
John McCall351762c2011-02-07 10:33:21 +00001704 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1705 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1706
1707 if (GCAttr == Qualifiers::Strong)
1708 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1709 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001710 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001711 SkipIvars.push_back(GC_IVAR(fieldOffset,
1712 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001713 }
1714
1715 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001716 return nullPtr;
1717
1718 // Sort on byte position; captures might not be allocated in order,
1719 // and unions can do funny things.
1720 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1721 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001722
1723 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001724 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001725 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1726 printf("\n block variable layout for block: ");
1727 const unsigned char *s = (unsigned char*)BitMap.c_str();
1728 for (unsigned i = 0; i < BitMap.size(); i++)
1729 if (!(s[i] & 0xf0))
1730 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1731 else
1732 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1733 printf("\n");
1734 }
1735
1736 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001737}
1738
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001739llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001740 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001741 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001742 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001743 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1744
Owen Andersonade90fd2009-07-29 18:54:39 +00001745 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001746 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001747}
1748
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001749void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001750 // FIXME: We shouldn't need this, the protocol decl should contain enough
1751 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 DefinedProtocols.insert(PD->getIdentifier());
1753
1754 // If we have generated a forward reference to this protocol, emit
1755 // it now. Otherwise do nothing, the protocol objects are lazily
1756 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001757 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001758 GetOrEmitProtocol(PD);
1759}
1760
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001761llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001762 if (DefinedProtocols.count(PD->getIdentifier()))
1763 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001764
Daniel Dunbarc475d422008-10-29 22:36:39 +00001765 return GetOrEmitProtocolRef(PD);
1766}
1767
Daniel Dunbarb036db82008-08-13 03:21:16 +00001768/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001769// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1770struct _objc_protocol {
1771struct _objc_protocol_extension *isa;
1772char *protocol_name;
1773struct _objc_protocol_list *protocol_list;
1774struct _objc__method_prototype_list *instance_methods;
1775struct _objc__method_prototype_list *class_methods
1776};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001777
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001778See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001779*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001780llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1781 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1782
1783 // Early exit if a defining object has already been generated.
1784 if (Entry && Entry->hasInitializer())
1785 return Entry;
1786
Douglas Gregora715bff2012-01-01 19:51:50 +00001787 // Use the protocol definition, if there is one.
1788 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1789 PD = Def;
1790
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001791 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001792 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001793 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1794
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001795 // Construct method lists.
1796 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1797 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001798 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001799 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001800 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001801 ObjCMethodDecl *MD = *i;
1802 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001803 if (!C)
1804 return GetOrEmitProtocolRef(PD);
1805
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001806 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1807 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001808 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001809 } else {
1810 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001811 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001812 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001813 }
1814
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001815 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001816 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001817 ObjCMethodDecl *MD = *i;
1818 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001819 if (!C)
1820 return GetOrEmitProtocolRef(PD);
1821
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001822 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1823 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001824 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001825 } else {
1826 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001827 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001828 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001829 }
1830
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001831 MethodTypesExt.insert(MethodTypesExt.end(),
1832 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1833
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001834 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001835 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1836 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001837 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001838 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001839 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001840 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001841 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001842 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001843 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001844 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001845 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001846 ClassMethods)
1847 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001848 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001849 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001850
Daniel Dunbarb036db82008-08-13 03:21:16 +00001851 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001852 // Already created, fix the linkage and update the initializer.
1853 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854 Entry->setInitializer(Init);
1855 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001856 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001857 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001860 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862 // FIXME: Is this necessary? Why only for protocol?
1863 Entry->setAlignment(4);
1864 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001865 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001866
1867 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868}
1869
Daniel Dunbarc475d422008-10-29 22:36:39 +00001870llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1872
1873 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001874 // We use the initializer as a marker of whether this is a forward
1875 // reference or not. At module finalization we add the empty
1876 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001877 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001878 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001879 llvm::GlobalValue::ExternalLinkage,
1880 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001881 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001882 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001883 // FIXME: Is this necessary? Why only for protocol?
1884 Entry->setAlignment(4);
1885 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001886
Daniel Dunbarb036db82008-08-13 03:21:16 +00001887 return Entry;
1888}
1889
1890/*
1891 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001892 uint32_t size;
1893 struct objc_method_description_list *optional_instance_methods;
1894 struct objc_method_description_list *optional_class_methods;
1895 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001896 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001897 };
1898*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001899llvm::Constant *
1900CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1901 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001902 const ConstantVector &OptClassMethods,
1903 const ConstantVector &MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001904 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001905 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001906 llvm::Constant *Values[] = {
1907 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001908 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001909 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001910 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001911 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001912 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001913 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001914 OptClassMethods),
1915 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001916 ObjCTypes),
1917 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
1918 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001919 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00001920
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001921 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001922 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001923 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001924 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001925
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001926 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001927 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001928
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001929 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001930 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001932 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001933}
1934
1935/*
1936 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001937 struct objc_protocol_list *next;
1938 long count;
1939 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001940 };
1941*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001942llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001943CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001944 ObjCProtocolDecl::protocol_iterator begin,
1945 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001946 std::vector<llvm::Constant*> ProtocolRefs;
1947
Daniel Dunbardec75f82008-08-21 21:57:41 +00001948 for (; begin != end; ++begin)
1949 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001950
1951 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001952 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001953 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001954
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001955 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001956 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001957
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001958 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001959 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001960 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001961 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001962 ProtocolRefs.size() - 1);
1963 Values[2] =
1964 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1965 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001966 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001968 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001969 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001970 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001971 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001972 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001973}
1974
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001975void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1976 std::vector<llvm::Constant*> &Properties,
1977 const Decl *Container,
1978 const ObjCProtocolDecl *PROTO,
1979 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001980 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1981 E = PROTO->protocol_end(); P != E; ++P)
1982 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1983 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1984 E = PROTO->prop_end(); I != E; ++I) {
1985 const ObjCPropertyDecl *PD = *I;
1986 if (!PropertySet.insert(PD->getIdentifier()))
1987 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001988 llvm::Constant *Prop[] = {
1989 GetPropertyName(PD->getIdentifier()),
1990 GetPropertyTypeString(PD, Container)
1991 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001992 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1993 }
1994}
1995
Daniel Dunbarb036db82008-08-13 03:21:16 +00001996/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001997 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001998 const char * const name;
1999 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002000 };
2001
2002 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002003 uint32_t entsize; // sizeof (struct _objc_property)
2004 uint32_t prop_count;
2005 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002006 };
2007*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002008llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002009 const Decl *Container,
2010 const ObjCContainerDecl *OCD,
2011 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002012 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002013 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002014 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2015 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002016 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002017 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002018 llvm::Constant *Prop[] = {
2019 GetPropertyName(PD->getIdentifier()),
2020 GetPropertyTypeString(PD, Container)
2021 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002022 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002023 Prop));
2024 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002025 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002026 for (ObjCInterfaceDecl::all_protocol_iterator
2027 P = OID->all_referenced_protocol_begin(),
2028 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002029 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2030 ObjCTypes);
2031 }
2032 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2033 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2034 E = CD->protocol_end(); P != E; ++P)
2035 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2036 ObjCTypes);
2037 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002038
2039 // Return null for empty list.
2040 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002041 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002042
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002043 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002044 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002045 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002046 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2047 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002048 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002049 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002050 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002051 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002052
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002053 llvm::GlobalVariable *GV =
2054 CreateMetadataVar(Name, Init,
2055 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002056 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002057 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002058 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002059 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002060}
2061
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002062llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2063 const ConstantVector &MethodTypes,
2064 const ObjCCommonTypesHelper &ObjCTypes) {
2065 // Return null for empty list.
2066 if (MethodTypes.empty())
2067 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2068
2069 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2070 MethodTypes.size());
2071 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2072
2073 llvm::GlobalVariable *GV =
2074 CreateMetadataVar(Name, Init,
2075 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2076 (ObjCABI == 2) ? 8 : 4,
2077 true);
2078 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2079}
2080
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002081/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002082 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002083 int count;
2084 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002085 };
2086*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002087llvm::Constant *
2088CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002089 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002090 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002091 ObjCTypes.SelectorPtrTy),
2092 GetMethodVarType(MD)
2093 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002094 if (!Desc[1])
2095 return 0;
2096
Owen Anderson0e0189d2009-07-27 22:29:56 +00002097 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002098 Desc);
2099}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002100
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002101llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002102 const char *Section,
2103 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002104 // Return null for empty list.
2105 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002106 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002107
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002108 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002109 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002110 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002111 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002112 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002113 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002114
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002115 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002116 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002117 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002118}
2119
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002120/*
2121 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002122 char *category_name;
2123 char *class_name;
2124 struct _objc_method_list *instance_methods;
2125 struct _objc_method_list *class_methods;
2126 struct _objc_protocol_list *protocols;
2127 uint32_t size; // <rdar://4585769>
2128 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002129 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002131void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002132 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002133
Mike Stump18bb9282009-05-16 07:57:57 +00002134 // FIXME: This is poor design, the OCD should have a pointer to the category
2135 // decl. Additionally, note that Category can be null for the @implementation
2136 // w/o an @interface case. Sema should just create one for us as it does for
2137 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002138 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002140 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002141
2142 llvm::SmallString<256> ExtName;
2143 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2144 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002145
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002146 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002147 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002148 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002149 // Instance methods should always be defined.
2150 InstanceMethods.push_back(GetMethodConstant(*i));
2151 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002152 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002153 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002154 // Class methods should always be defined.
2155 ClassMethods.push_back(GetMethodConstant(*i));
2156 }
2157
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002158 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002159 Values[0] = GetClassName(OCD->getIdentifier());
2160 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002161 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002162 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002163 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002164 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002165 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002166 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002167 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002168 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002169 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002170 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002171 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002172 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002173 Category->protocol_begin(),
2174 Category->protocol_end());
2175 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002176 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002177 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002178 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002179
2180 // If there is no category @interface then there can be no properties.
2181 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002182 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002183 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002184 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002185 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002186 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002187
Owen Anderson0e0189d2009-07-27 22:29:56 +00002188 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002189 Values);
2190
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002191 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002192 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002193 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002194 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002195 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002196 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002197 // method definition entries must be clear for next implementation.
2198 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002199}
2200
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002201// FIXME: Get from somewhere?
2202enum ClassFlags {
2203 eClassFlags_Factory = 0x00001,
2204 eClassFlags_Meta = 0x00002,
2205 // <rdr://5142207>
2206 eClassFlags_HasCXXStructors = 0x02000,
2207 eClassFlags_Hidden = 0x20000,
2208 eClassFlags_ABI2_Hidden = 0x00010,
2209 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2210};
2211
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002212/*
2213 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002214 Class isa;
2215 Class super_class;
2216 const char *name;
2217 long version;
2218 long info;
2219 long instance_size;
2220 struct _objc_ivar_list *ivars;
2221 struct _objc_method_list *methods;
2222 struct _objc_cache *cache;
2223 struct _objc_protocol_list *protocols;
2224 // Objective-C 1.0 extensions (<rdr://4585769>)
2225 const char *ivar_layout;
2226 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002227 };
2228
2229 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002230*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002231void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002232 DefinedSymbols.insert(ID->getIdentifier());
2233
Chris Lattner86d7d912008-11-24 03:54:41 +00002234 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002235 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002236 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002237 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002238 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002239 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002240 Interface->all_referenced_protocol_begin(),
2241 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002242 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002243 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002244 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002245 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002246 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002247
2248 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002249 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250 Flags |= eClassFlags_Hidden;
2251
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002252 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002253 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002254 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002255 // Instance methods should always be defined.
2256 InstanceMethods.push_back(GetMethodConstant(*i));
2257 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002258 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002259 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002260 // Class methods should always be defined.
2261 ClassMethods.push_back(GetMethodConstant(*i));
2262 }
2263
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002264 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002265 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002266 ObjCPropertyImplDecl *PID = *i;
2267
2268 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2269 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2270
2271 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2272 if (llvm::Constant *C = GetMethodConstant(MD))
2273 InstanceMethods.push_back(C);
2274 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2275 if (llvm::Constant *C = GetMethodConstant(MD))
2276 InstanceMethods.push_back(C);
2277 }
2278 }
2279
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002280 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002281 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002282 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002283 // Record a reference to the super class.
2284 LazySymbols.insert(Super->getIdentifier());
2285
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002286 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002287 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002288 ObjCTypes.ClassPtrTy);
2289 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002290 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002291 }
2292 Values[ 2] = GetClassName(ID->getIdentifier());
2293 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002294 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2295 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2296 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002297 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002298 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002299 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002300 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002301 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002302 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002303 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002304 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002305 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002306 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002307 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002309 std::string Name("\01L_OBJC_CLASS_");
2310 Name += ClassName;
2311 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2312 // Check for a forward reference.
2313 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2314 if (GV) {
2315 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2316 "Forward metaclass reference has incorrect type.");
2317 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2318 GV->setInitializer(Init);
2319 GV->setSection(Section);
2320 GV->setAlignment(4);
2321 CGM.AddUsedGlobal(GV);
2322 }
2323 else
2324 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002326 // method definition entries must be clear for next implementation.
2327 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002328}
2329
2330llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2331 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002332 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002333 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002334 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002335
John McCall457a04e2010-10-22 21:05:15 +00002336 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002337 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002338
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002339 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340 // The isa for the metaclass is the root of the hierarchy.
2341 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2342 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2343 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002344 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002345 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002346 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002347 // The super class for the metaclass is emitted as the name of the
2348 // super class. The runtime fixes this up to point to the
2349 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002350 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002351 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002352 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002353 ObjCTypes.ClassPtrTy);
2354 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002355 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356 }
2357 Values[ 2] = GetClassName(ID->getIdentifier());
2358 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002359 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2360 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2361 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002362 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002363 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002364 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002365 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002366 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002367 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002368 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002369 Values[ 9] = Protocols;
2370 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002371 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002372 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002373 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002374 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002375 Values);
2376
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002377 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002378 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002379
2380 // Check for a forward reference.
2381 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2382 if (GV) {
2383 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2384 "Forward metaclass reference has incorrect type.");
2385 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2386 GV->setInitializer(Init);
2387 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002388 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002389 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002390 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002391 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002392 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002393 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002394 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002395
2396 return GV;
2397}
2398
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002399llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002400 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002401
Mike Stump18bb9282009-05-16 07:57:57 +00002402 // FIXME: Should we look these up somewhere other than the module. Its a bit
2403 // silly since we only generate these while processing an implementation, so
2404 // exactly one pointer would work if know when we entered/exitted an
2405 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002406
2407 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002408 // Previously, metaclass with internal linkage may have been defined.
2409 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002410 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2411 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002412 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2413 "Forward metaclass reference has incorrect type.");
2414 return GV;
2415 } else {
2416 // Generate as an external reference to keep a consistent
2417 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002418 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002419 llvm::GlobalValue::ExternalLinkage,
2420 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002421 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002422 }
2423}
2424
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002425llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2426 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2427
2428 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2429 true)) {
2430 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2431 "Forward class metadata reference has incorrect type.");
2432 return GV;
2433 } else {
2434 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2435 llvm::GlobalValue::ExternalLinkage,
2436 0,
2437 Name);
2438 }
2439}
2440
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002441/*
2442 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002443 uint32_t size;
2444 const char *weak_ivar_layout;
2445 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002446 };
2447*/
2448llvm::Constant *
2449CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002450 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002451 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002452
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002453 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002454 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002455 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002456 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002457 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002458
2459 // Return null if no extension bits are used.
2460 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002461 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002462
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002463 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002464 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002465 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002466 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002467 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002468}
2469
2470/*
2471 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002472 char *ivar_name;
2473 char *ivar_type;
2474 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002475 };
2476
2477 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002478 int ivar_count;
2479 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002480 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002481*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002482llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002483 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002484 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485
2486 // When emitting the root class GCC emits ivar entries for the
2487 // actual class structure. It is not clear if we need to follow this
2488 // behavior; for now lets try and get away with not doing it. If so,
2489 // the cleanest solution would be to make up an ObjCInterfaceDecl
2490 // for the class.
2491 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002492 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002493
Jordy Rosea91768e2011-07-22 02:08:32 +00002494 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002495
Jordy Rosea91768e2011-07-22 02:08:32 +00002496 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002497 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002498 // Ignore unnamed bit-fields.
2499 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002500 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002501 llvm::Constant *Ivar[] = {
2502 GetMethodVarName(IVD->getIdentifier()),
2503 GetMethodVarType(IVD),
2504 llvm::ConstantInt::get(ObjCTypes.IntTy,
2505 ComputeIvarBaseOffset(CGM, OID, IVD))
2506 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002507 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002508 }
2509
2510 // Return null for empty list.
2511 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002512 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002513
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002514 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002515 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002516 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002517 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002518 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002519 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002520
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002521 llvm::GlobalVariable *GV;
2522 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002523 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002524 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002525 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002526 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002527 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002528 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002529 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002530 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002531}
2532
2533/*
2534 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002535 SEL method_name;
2536 char *method_types;
2537 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002538 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002539
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002540 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002541 struct objc_method_list *obsolete;
2542 int count;
2543 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002544 };
2545*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002546
2547/// GetMethodConstant - Return a struct objc_method constant for the
2548/// given method if it has been defined. The result is null if the
2549/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002550llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002551 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002552 if (!Fn)
2553 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002554
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002555 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002556 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002557 ObjCTypes.SelectorPtrTy),
2558 GetMethodVarType(MD),
2559 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2560 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002561 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002562}
2563
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002564llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002565 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002566 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002567 // Return null for empty list.
2568 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002569 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002570
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002571 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002572 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002573 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002574 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002575 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002576 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002577 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002578
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002579 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002580 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002581}
2582
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002583llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002584 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002585 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002586 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002587
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002588 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002589 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002590 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002591 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002592 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002593 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002594 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002595 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002596 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002597
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002598 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002599}
2600
Daniel Dunbar30c65362009-03-09 20:09:19 +00002601llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002602CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002603 llvm::Constant *Init,
2604 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002605 unsigned Align,
2606 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002607 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002608 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002609 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002610 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002611 if (Section)
2612 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002613 if (Align)
2614 GV->setAlignment(Align);
2615 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002616 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002617 return GV;
2618}
2619
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002620llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002621 // Abuse this interface function as a place to finalize.
2622 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002623 return NULL;
2624}
2625
Chris Lattnerd4808922009-03-22 21:03:39 +00002626llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002627 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002628}
2629
Chris Lattnerd4808922009-03-22 21:03:39 +00002630llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002631 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002632}
2633
David Chisnall168b80f2010-12-26 22:13:16 +00002634llvm::Constant *CGObjCMac::GetGetStructFunction() {
2635 return ObjCTypes.getCopyStructFn();
2636}
2637llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002638 return ObjCTypes.getCopyStructFn();
2639}
2640
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002641llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2642 return ObjCTypes.getCppAtomicObjectFunction();
2643}
2644
Chris Lattnerd4808922009-03-22 21:03:39 +00002645llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002646 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002647}
2648
John McCallbd309292010-07-06 01:34:17 +00002649void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2650 return EmitTryOrSynchronizedStmt(CGF, S);
2651}
2652
2653void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2654 const ObjCAtSynchronizedStmt &S) {
2655 return EmitTryOrSynchronizedStmt(CGF, S);
2656}
2657
John McCall65bea082010-07-21 06:59:36 +00002658namespace {
John McCallcda666c2010-07-21 07:22:38 +00002659 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002660 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002661 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002662 llvm::Value *CallTryExitVar;
2663 llvm::Value *ExceptionData;
2664 ObjCTypesHelper &ObjCTypes;
2665 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002666 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002667 llvm::Value *CallTryExitVar,
2668 llvm::Value *ExceptionData,
2669 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002670 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002671 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2672
John McCall30317fd2011-07-12 20:27:29 +00002673 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002674 // Check whether we need to call objc_exception_try_exit.
2675 // In optimized code, this branch will always be folded.
2676 llvm::BasicBlock *FinallyCallExit =
2677 CGF.createBasicBlock("finally.call_exit");
2678 llvm::BasicBlock *FinallyNoCallExit =
2679 CGF.createBasicBlock("finally.no_call_exit");
2680 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2681 FinallyCallExit, FinallyNoCallExit);
2682
2683 CGF.EmitBlock(FinallyCallExit);
2684 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2685 ->setDoesNotThrow();
2686
2687 CGF.EmitBlock(FinallyNoCallExit);
2688
2689 if (isa<ObjCAtTryStmt>(S)) {
2690 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002691 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2692 // Save the current cleanup destination in case there's
2693 // control flow inside the finally statement.
2694 llvm::Value *CurCleanupDest =
2695 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2696
John McCall65bea082010-07-21 06:59:36 +00002697 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2698
John McCallcebe0ca2010-08-11 00:16:14 +00002699 if (CGF.HaveInsertPoint()) {
2700 CGF.Builder.CreateStore(CurCleanupDest,
2701 CGF.getNormalCleanupDestSlot());
2702 } else {
2703 // Currently, the end of the cleanup must always exist.
2704 CGF.EnsureInsertPoint();
2705 }
2706 }
John McCall65bea082010-07-21 06:59:36 +00002707 } else {
2708 // Emit objc_sync_exit(expr); as finally's sole statement for
2709 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002710 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002711 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2712 ->setDoesNotThrow();
2713 }
2714 }
2715 };
John McCall42227ed2010-07-31 23:20:56 +00002716
2717 class FragileHazards {
2718 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002719 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002720 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2721
2722 llvm::InlineAsm *ReadHazard;
2723 llvm::InlineAsm *WriteHazard;
2724
2725 llvm::FunctionType *GetAsmFnType();
2726
2727 void collectLocals();
2728 void emitReadHazard(CGBuilderTy &Builder);
2729
2730 public:
2731 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002732
John McCall42227ed2010-07-31 23:20:56 +00002733 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002734 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002735 };
2736}
2737
2738/// Create the fragile-ABI read and write hazards based on the current
2739/// state of the function, which is presumed to be immediately prior
2740/// to a @try block. These hazards are used to maintain correct
2741/// semantics in the face of optimization and the fragile ABI's
2742/// cavalier use of setjmp/longjmp.
2743FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2744 collectLocals();
2745
2746 if (Locals.empty()) return;
2747
2748 // Collect all the blocks in the function.
2749 for (llvm::Function::iterator
2750 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2751 BlocksBeforeTry.insert(&*I);
2752
2753 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2754
2755 // Create a read hazard for the allocas. This inhibits dead-store
2756 // optimizations and forces the values to memory. This hazard is
2757 // inserted before any 'throwing' calls in the protected scope to
2758 // reflect the possibility that the variables might be read from the
2759 // catch block if the call throws.
2760 {
2761 std::string Constraint;
2762 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2763 if (I) Constraint += ',';
2764 Constraint += "*m";
2765 }
2766
2767 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2768 }
2769
2770 // Create a write hazard for the allocas. This inhibits folding
2771 // loads across the hazard. This hazard is inserted at the
2772 // beginning of the catch path to reflect the possibility that the
2773 // variables might have been written within the protected scope.
2774 {
2775 std::string Constraint;
2776 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2777 if (I) Constraint += ',';
2778 Constraint += "=*m";
2779 }
2780
2781 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2782 }
2783}
2784
2785/// Emit a write hazard at the current location.
2786void FragileHazards::emitWriteHazard() {
2787 if (Locals.empty()) return;
2788
Jay Foad5bd375a2011-07-15 08:37:34 +00002789 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002790}
2791
John McCall42227ed2010-07-31 23:20:56 +00002792void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2793 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002794 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002795}
2796
2797/// Emit read hazards in all the protected blocks, i.e. all the blocks
2798/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002799void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002800 if (Locals.empty()) return;
2801
2802 CGBuilderTy Builder(CGF.getLLVMContext());
2803
2804 // Iterate through all blocks, skipping those prior to the try.
2805 for (llvm::Function::iterator
2806 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2807 llvm::BasicBlock &BB = *FI;
2808 if (BlocksBeforeTry.count(&BB)) continue;
2809
2810 // Walk through all the calls in the block.
2811 for (llvm::BasicBlock::iterator
2812 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2813 llvm::Instruction &I = *BI;
2814
2815 // Ignore instructions that aren't non-intrinsic calls.
2816 // These are the only calls that can possibly call longjmp.
2817 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2818 if (isa<llvm::IntrinsicInst>(I))
2819 continue;
2820
2821 // Ignore call sites marked nounwind. This may be questionable,
2822 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2823 llvm::CallSite CS(&I);
2824 if (CS.doesNotThrow()) continue;
2825
John McCall2dd7d442010-08-04 05:59:32 +00002826 // Insert a read hazard before the call. This will ensure that
2827 // any writes to the locals are performed before making the
2828 // call. If the call throws, then this is sufficient to
2829 // guarantee correctness as long as it doesn't also write to any
2830 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002831 Builder.SetInsertPoint(&BB, BI);
2832 emitReadHazard(Builder);
2833 }
2834 }
2835}
2836
2837static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2838 if (V) S.insert(V);
2839}
2840
2841void FragileHazards::collectLocals() {
2842 // Compute a set of allocas to ignore.
2843 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2844 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2845 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002846
2847 // Collect all the allocas currently in the function. This is
2848 // probably way too aggressive.
2849 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2850 for (llvm::BasicBlock::iterator
2851 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2852 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2853 Locals.push_back(&*I);
2854}
2855
2856llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002857 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002858 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2859 tys[i] = Locals[i]->getType();
2860 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002861}
2862
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002863/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002864
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002865 Objective-C setjmp-longjmp (sjlj) Exception Handling
2866 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002867
John McCallbd309292010-07-06 01:34:17 +00002868 A catch buffer is a setjmp buffer plus:
2869 - a pointer to the exception that was caught
2870 - a pointer to the previous exception data buffer
2871 - two pointers of reserved storage
2872 Therefore catch buffers form a stack, with a pointer to the top
2873 of the stack kept in thread-local storage.
2874
2875 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2876 objc_exception_try_exit pops the given catch buffer, which is
2877 required to be the top of the EH stack.
2878 objc_exception_throw pops the top of the EH stack, writes the
2879 thrown exception into the appropriate field, and longjmps
2880 to the setjmp buffer. It crashes the process (with a printf
2881 and an abort()) if there are no catch buffers on the stack.
2882 objc_exception_extract just reads the exception pointer out of the
2883 catch buffer.
2884
2885 There's no reason an implementation couldn't use a light-weight
2886 setjmp here --- something like __builtin_setjmp, but API-compatible
2887 with the heavyweight setjmp. This will be more important if we ever
2888 want to implement correct ObjC/C++ exception interactions for the
2889 fragile ABI.
2890
2891 Note that for this use of setjmp/longjmp to be correct, we may need
2892 to mark some local variables volatile: if a non-volatile local
2893 variable is modified between the setjmp and the longjmp, it has
2894 indeterminate value. For the purposes of LLVM IR, it may be
2895 sufficient to make loads and stores within the @try (to variables
2896 declared outside the @try) volatile. This is necessary for
2897 optimized correctness, but is not currently being done; this is
2898 being tracked as rdar://problem/8160285
2899
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002900 The basic framework for a @try-catch-finally is as follows:
2901 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002902 objc_exception_data d;
2903 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002904 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002905
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002906 objc_exception_try_enter(&d);
2907 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002908 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002909 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002910 // exception path
2911 id _caught = objc_exception_extract(&d);
2912
2913 // enter new try scope for handlers
2914 if (!setjmp(d.jmp_buf)) {
2915 ... match exception and execute catch blocks ...
2916
2917 // fell off end, rethrow.
2918 _rethrow = _caught;
2919 ... jump-through-finally to finally_rethrow ...
2920 } else {
2921 // exception in catch block
2922 _rethrow = objc_exception_extract(&d);
2923 _call_try_exit = false;
2924 ... jump-through-finally to finally_rethrow ...
2925 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002926 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002927 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002928
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002929 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002930 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002932
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002933 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002934 ... dispatch to finally destination ...
2935
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002936 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002937 objc_exception_throw(_rethrow);
2938
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002939 finally_end:
2940 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002941
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002942 This framework differs slightly from the one gcc uses, in that gcc
2943 uses _rethrow to determine if objc_exception_try_exit should be called
2944 and if the object should be rethrown. This breaks in the face of
2945 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002946
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002947 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002948
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002949 - If there are no catch blocks, then we avoid emitting the second
2950 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002951
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002952 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2953 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002954
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002955 - FIXME: If there is no @finally block we can do a few more
2956 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002957
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002958 Rethrows and Jumps-Through-Finally
2959 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002960
John McCallbd309292010-07-06 01:34:17 +00002961 '@throw;' is supported by pushing the currently-caught exception
2962 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002963
John McCallbd309292010-07-06 01:34:17 +00002964 Branches through the @finally block are handled with an ordinary
2965 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2966 exceptions are not compatible with C++ exceptions, and this is
2967 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002968
John McCallbd309292010-07-06 01:34:17 +00002969 @synchronized(expr) { stmt; } is emitted as if it were:
2970 id synch_value = expr;
2971 objc_sync_enter(synch_value);
2972 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002973*/
2974
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002975void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2976 const Stmt &S) {
2977 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002978
2979 // A destination for the fall-through edges of the catch handlers to
2980 // jump to.
2981 CodeGenFunction::JumpDest FinallyEnd =
2982 CGF.getJumpDestInCurrentScope("finally.end");
2983
2984 // A destination for the rethrow edge of the catch handlers to jump
2985 // to.
2986 CodeGenFunction::JumpDest FinallyRethrow =
2987 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002988
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002989 // For @synchronized, call objc_sync_enter(sync.expr). The
2990 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002991 // @synchronized. We can't avoid a temp here because we need the
2992 // value to be preserved. If the backend ever does liveness
2993 // correctly after setjmp, this will be unnecessary.
2994 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002995 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002996 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002997 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2998 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002999 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3000 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003001
3002 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3003 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003004 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003005
John McCall2dd7d442010-08-04 05:59:32 +00003006 // Allocate memory for the setjmp buffer. This needs to be kept
3007 // live throughout the try and catch blocks.
3008 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3009 "exceptiondata.ptr");
3010
John McCall42227ed2010-07-31 23:20:56 +00003011 // Create the fragile hazards. Note that this will not capture any
3012 // of the allocas required for exception processing, but will
3013 // capture the current basic block (which extends all the way to the
3014 // setjmp call) as "before the @try".
3015 FragileHazards Hazards(CGF);
3016
John McCallbd309292010-07-06 01:34:17 +00003017 // Create a flag indicating whether the cleanup needs to call
3018 // objc_exception_try_exit. This is true except when
3019 // - no catches match and we're branching through the cleanup
3020 // just to rethrow the exception, or
3021 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003022 // The setjmp-safety rule here is that we should always store to this
3023 // variable in a place that dominates the branch through the cleanup
3024 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003025 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003026 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003027
John McCall9916e3f2010-10-04 23:42:51 +00003028 // A slot containing the exception to rethrow. Only needed when we
3029 // have both a @catch and a @finally.
3030 llvm::Value *PropagatingExnVar = 0;
3031
John McCallbd309292010-07-06 01:34:17 +00003032 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003033 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003034 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003035 CallTryExitVar,
3036 ExceptionData,
3037 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003038
3039 // Enter a try block:
3040 // - Call objc_exception_try_enter to push ExceptionData on top of
3041 // the EH stack.
3042 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3043 ->setDoesNotThrow();
3044
3045 // - Call setjmp on the exception data buffer.
3046 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3047 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3048 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003049 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003050 llvm::CallInst *SetJmpResult =
3051 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3052 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003053 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003054
3055 // If setjmp returned 0, enter the protected block; otherwise,
3056 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003057 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3058 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003059 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003060 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3061 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003062
John McCallbd309292010-07-06 01:34:17 +00003063 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003064 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003065 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003066 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003067 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003068
3069 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003070
John McCallbd309292010-07-06 01:34:17 +00003071 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003072 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003073
John McCall42227ed2010-07-31 23:20:56 +00003074 // Don't optimize loads of the in-scope locals across this point.
3075 Hazards.emitWriteHazard();
3076
John McCallbd309292010-07-06 01:34:17 +00003077 // For a @synchronized (or a @try with no catches), just branch
3078 // through the cleanup to the rethrow block.
3079 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3080 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003081 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003082 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003083
3084 // Otherwise, we have to match against the caught exceptions.
3085 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003086 // Retrieve the exception object. We may emit multiple blocks but
3087 // nothing can cross this so the value is already in SSA form.
3088 llvm::CallInst *Caught =
3089 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3090 ExceptionData, "caught");
3091 Caught->setDoesNotThrow();
3092
John McCallbd309292010-07-06 01:34:17 +00003093 // Push the exception to rethrow onto the EH value stack for the
3094 // benefit of any @throws in the handlers.
3095 CGF.ObjCEHValueStack.push_back(Caught);
3096
Douglas Gregor96c79492010-04-23 22:50:49 +00003097 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003098
John McCall2dd7d442010-08-04 05:59:32 +00003099 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003100
John McCall2dd7d442010-08-04 05:59:32 +00003101 llvm::BasicBlock *CatchBlock = 0;
3102 llvm::BasicBlock *CatchHandler = 0;
3103 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003104 // Save the currently-propagating exception before
3105 // objc_exception_try_enter clears the exception slot.
3106 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3107 "propagating_exception");
3108 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3109
John McCall2dd7d442010-08-04 05:59:32 +00003110 // Enter a new exception try block (in case a @catch block
3111 // throws an exception).
3112 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3113 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003114
John McCall2dd7d442010-08-04 05:59:32 +00003115 llvm::CallInst *SetJmpResult =
3116 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3117 "setjmp.result");
3118 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003119 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003120
John McCall2dd7d442010-08-04 05:59:32 +00003121 llvm::Value *Threw =
3122 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3123
3124 CatchBlock = CGF.createBasicBlock("catch");
3125 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3126 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3127
3128 CGF.EmitBlock(CatchBlock);
3129 }
3130
3131 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003132
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003133 // Handle catch list. As a special case we check if everything is
3134 // matched and avoid generating code for falling off the end if
3135 // so.
3136 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003137 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3138 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003139
Douglas Gregor46a572b2010-04-26 16:46:50 +00003140 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003141 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003142
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003143 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003144 if (!CatchParam) {
3145 AllMatched = true;
3146 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003147 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003148
John McCallbd309292010-07-06 01:34:17 +00003149 // catch(id e) always matches under this ABI, since only
3150 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003151 // FIXME: For the time being we also match id<X>; this should
3152 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003153 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003154 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003155 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003156
John McCallbd309292010-07-06 01:34:17 +00003157 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003158 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003159 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3160
Anders Carlsson9396a892008-09-11 09:15:33 +00003161 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003162 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003163 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003164
3165 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003166 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003167 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003168
Anders Carlsson9396a892008-09-11 09:15:33 +00003169 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003170
3171 // The scope of the catch variable ends right here.
3172 CatchVarCleanups.ForceCleanup();
3173
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003174 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003175 break;
3176 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003177
Steve Naroff7cae42b2009-07-10 23:34:53 +00003178 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003179 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003180
3181 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003182 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3183 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003184
3185 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003186 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003187
John McCallbd309292010-07-06 01:34:17 +00003188 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003189 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3190 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003191 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003192
John McCallbd309292010-07-06 01:34:17 +00003193 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3194 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003195
3196 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003197 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003198
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003199 // Emit the @catch block.
3200 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003201
3202 // Collect any cleanups for the catch variable. The scope lasts until
3203 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003204 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003205
John McCall1c9c3fd2010-10-15 04:57:14 +00003206 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003207 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003208
John McCallbd309292010-07-06 01:34:17 +00003209 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003210 llvm::Value *Tmp =
3211 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003212 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003213 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214
Anders Carlsson9396a892008-09-11 09:15:33 +00003215 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003216
3217 // We're done with the catch variable.
3218 CatchVarCleanups.ForceCleanup();
3219
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003220 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003221
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003222 CGF.EmitBlock(NextCatchBlock);
3223 }
3224
John McCallbd309292010-07-06 01:34:17 +00003225 CGF.ObjCEHValueStack.pop_back();
3226
John McCall2dd7d442010-08-04 05:59:32 +00003227 // If nothing wanted anything to do with the caught exception,
3228 // kill the extract call.
3229 if (Caught->use_empty())
3230 Caught->eraseFromParent();
3231
3232 if (!AllMatched)
3233 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3234
3235 if (HasFinally) {
3236 // Emit the exception handler for the @catch blocks.
3237 CGF.EmitBlock(CatchHandler);
3238
3239 // In theory we might now need a write hazard, but actually it's
3240 // unnecessary because there's no local-accessing code between
3241 // the try's write hazard and here.
3242 //Hazards.emitWriteHazard();
3243
John McCall9916e3f2010-10-04 23:42:51 +00003244 // Extract the new exception and save it to the
3245 // propagating-exception slot.
3246 assert(PropagatingExnVar);
3247 llvm::CallInst *NewCaught =
3248 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3249 ExceptionData, "caught");
3250 NewCaught->setDoesNotThrow();
3251 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3252
John McCall2dd7d442010-08-04 05:59:32 +00003253 // Don't pop the catch handler; the throw already did.
3254 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003255 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003256 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003257 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003258
John McCall42227ed2010-07-31 23:20:56 +00003259 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003260 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003261
John McCallbd309292010-07-06 01:34:17 +00003262 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003263 CGF.Builder.restoreIP(TryFallthroughIP);
3264 if (CGF.HaveInsertPoint())
3265 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003266 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003267 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003268
John McCallbd309292010-07-06 01:34:17 +00003269 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003270 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003271 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003272 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003273 // If we have a propagating-exception variable, check it.
3274 llvm::Value *PropagatingExn;
3275 if (PropagatingExnVar) {
3276 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003277
John McCall9916e3f2010-10-04 23:42:51 +00003278 // Otherwise, just look in the buffer for the exception to throw.
3279 } else {
3280 llvm::CallInst *Caught =
3281 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3282 ExceptionData);
3283 Caught->setDoesNotThrow();
3284 PropagatingExn = Caught;
3285 }
3286
3287 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003288 ->setDoesNotThrow();
3289 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003290 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003291
John McCall42227ed2010-07-31 23:20:56 +00003292 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003293}
3294
3295void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003296 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003297 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003298
Anders Carlssone005aa12008-09-09 16:16:55 +00003299 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003300 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003301 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003302 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003303 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003304 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003305 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003306 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003307 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003308
John McCallbd309292010-07-06 01:34:17 +00003309 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3310 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003311 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003312
3313 // Clear the insertion point to indicate we are in unreachable code.
3314 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003315}
3316
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003317/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003318/// object: objc_read_weak (id *src)
3319///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003320llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003321 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003322 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003323 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3324 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3325 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003326 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003327 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003328 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003329 return read_weak;
3330}
3331
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003332/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3333/// objc_assign_weak (id src, id *dst)
3334///
3335void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003336 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003337 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003338 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003339 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003340 assert(Size <= 8 && "does not support size > 8");
3341 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003342 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003343 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3344 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003345 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3346 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003347 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003348 src, dst, "weakassign");
3349 return;
3350}
3351
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003352/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3353/// objc_assign_global (id src, id *dst)
3354///
3355void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003356 llvm::Value *src, llvm::Value *dst,
3357 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003358 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003359 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003360 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003361 assert(Size <= 8 && "does not support size > 8");
3362 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003363 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003364 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3365 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003366 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3367 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003368 if (!threadlocal)
3369 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3370 src, dst, "globalassign");
3371 else
3372 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3373 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003374 return;
3375}
3376
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003377/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003378/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003379///
3380void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003381 llvm::Value *src, llvm::Value *dst,
3382 llvm::Value *ivarOffset) {
3383 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003384 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003385 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003386 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003387 assert(Size <= 8 && "does not support size > 8");
3388 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003390 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3391 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003392 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3393 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003394 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3395 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003396 return;
3397}
3398
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003399/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3400/// objc_assign_strongCast (id src, id *dst)
3401///
3402void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003403 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003404 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003405 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003406 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003407 assert(Size <= 8 && "does not support size > 8");
3408 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003409 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003410 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3411 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003412 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3413 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003414 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003415 src, dst, "weakassign");
3416 return;
3417}
3418
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003419void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003420 llvm::Value *DestPtr,
3421 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003422 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003423 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3424 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003425 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003426 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003427 return;
3428}
3429
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003430/// EmitObjCValueForIvar - Code Gen for ivar reference.
3431///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003432LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3433 QualType ObjectTy,
3434 llvm::Value *BaseValue,
3435 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003436 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003437 const ObjCInterfaceDecl *ID =
3438 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003439 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3440 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003441}
3442
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003443llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003444 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003445 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003446 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003447 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003448 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3449 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003450}
3451
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003452/* *** Private Interface *** */
3453
3454/// EmitImageInfo - Emit the image info marker used to encode some module
3455/// level information.
3456///
3457/// See: <rdr://4810609&4810587&4810587>
3458/// struct IMAGE_INFO {
3459/// unsigned version;
3460/// unsigned flags;
3461/// };
3462enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003463 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003464 eImageInfo_GarbageCollected = (1 << 1),
3465 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003466 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3467
Daniel Dunbar5e639272010-04-25 20:39:01 +00003468 // A flag indicating that the module has no instances of a @synthesize of a
3469 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003470 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003471};
3472
Daniel Dunbar5e639272010-04-25 20:39:01 +00003473void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003474 unsigned version = 0; // Version is unused?
3475 unsigned flags = 0;
3476
3477 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003478 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003479 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003480 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003481 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003482
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003483 // We never allow @synthesize of a superclass property.
3484 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003485
Chris Lattner2192fe52011-07-18 04:24:23 +00003486 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner5e016ae2010-06-27 07:15:29 +00003487
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003488 // Emitted as int[2];
3489 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003490 llvm::ConstantInt::get(Int32Ty, version),
3491 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003492 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003493 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003494
3495 const char *Section;
3496 if (ObjCABI == 1)
3497 Section = "__OBJC, __image_info,regular";
3498 else
3499 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003500 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003501 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003502 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003503 Section,
3504 0,
3505 true);
3506 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003507}
3508
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003509
3510// struct objc_module {
3511// unsigned long version;
3512// unsigned long size;
3513// const char *name;
3514// Symtab symtab;
3515// };
3516
3517// FIXME: Get from somewhere
3518static const int ModuleVersion = 7;
3519
3520void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003521 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003522
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003523 llvm::Constant *Values[] = {
3524 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3525 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3526 // This used to be the filename, now it is unused. <rdr://4327263>
3527 GetClassName(&CGM.getContext().Idents.get("")),
3528 EmitModuleSymbols()
3529 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003530 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003531 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003532 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003533 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003534}
3535
3536llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003537 unsigned NumClasses = DefinedClasses.size();
3538 unsigned NumCategories = DefinedCategories.size();
3539
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003540 // Return null if no symbols were defined.
3541 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003542 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003543
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003544 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003545 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003546 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003547 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3548 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003549
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003550 // The runtime expects exactly the list of defined classes followed
3551 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003552 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003553 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003554 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003555 ObjCTypes.Int8PtrTy);
3556 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003557 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003558 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003559 ObjCTypes.Int8PtrTy);
3560
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003562 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003563 NumClasses + NumCategories),
3564 Symbols);
3565
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003566 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003567
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003568 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003569 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3570 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003571 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003572 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003573}
3574
John McCall31168b02011-06-15 23:02:42 +00003575llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3576 IdentifierInfo *II) {
3577 LazySymbols.insert(II);
3578
3579 llvm::GlobalVariable *&Entry = ClassReferences[II];
3580
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003581 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003583 llvm::ConstantExpr::getBitCast(GetClassName(II),
3584 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003585 Entry =
John McCall31168b02011-06-15 23:02:42 +00003586 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3587 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3588 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003589 }
John McCall31168b02011-06-15 23:02:42 +00003590
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003591 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003592}
3593
John McCall31168b02011-06-15 23:02:42 +00003594llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3595 const ObjCInterfaceDecl *ID) {
3596 return EmitClassRefFromId(Builder, ID->getIdentifier());
3597}
3598
3599llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3600 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3601 return EmitClassRefFromId(Builder, II);
3602}
3603
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003604llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3605 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003606 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003607
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003608 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003609 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003610 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003611 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003612 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003613 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3614 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003615 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003616 }
3617
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003618 if (lvalue)
3619 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003620 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003621}
3622
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003623llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003624 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003625
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003626 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003627 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003628 llvm::ConstantArray::get(VMContext,
3629 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003630 ((ObjCABI == 2) ?
3631 "__TEXT,__objc_classname,cstring_literals" :
3632 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003633 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003634
Owen Anderson170229f2009-07-14 23:10:40 +00003635 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003636}
3637
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003638llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3639 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3640 I = MethodDefinitions.find(MD);
3641 if (I != MethodDefinitions.end())
3642 return I->second;
3643
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003644 return NULL;
3645}
3646
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003647/// GetIvarLayoutName - Returns a unique constant for the given
3648/// ivar layout bitmap.
3649llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003650 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003651 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003652}
3653
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003654void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003655 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003656 bool ForStrongLayout,
3657 bool &HasUnion) {
3658 const RecordDecl *RD = RT->getDecl();
3659 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003660 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003661 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003663 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003664
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003665 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3666 ForStrongLayout, HasUnion);
3667}
3668
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003669void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003670 const llvm::StructLayout *Layout,
3671 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003672 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003673 unsigned int BytePos, bool ForStrongLayout,
3674 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003675 bool IsUnion = (RD && RD->isUnion());
3676 uint64_t MaxUnionIvarSize = 0;
3677 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003678 const FieldDecl *MaxField = 0;
3679 const FieldDecl *MaxSkippedField = 0;
3680 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003681 uint64_t MaxFieldOffset = 0;
3682 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003683 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003684 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003685
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003686 if (RecFields.empty())
3687 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003688 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3689 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003690 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003691 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003692 FirstFieldDelta =
3693 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3694 }
3695
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003696 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003697 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003698 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003699 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003700 // Note that 'i' here is actually the field index inside RD of Field,
3701 // although this dependency is hidden.
3702 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003703 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003704 } else
John McCall31168b02011-06-15 23:02:42 +00003705 FieldOffset =
3706 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003707
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003708 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003709 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003710 LastFieldBitfieldOrUnnamed = Field;
3711 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003712 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003713 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003714
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003715 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003716 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003717 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003718 if (FQT->isUnionType())
3719 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003720
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003721 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003722 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003723 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003724 continue;
3725 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003726
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003727 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003728 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003729 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003730 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003731 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003732 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003733 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3734 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003735 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003736 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003737 FQT = CArray->getElementType();
3738 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003739
3740 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003741 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003742 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003743 int OldIndex = IvarsInfo.size() - 1;
3744 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003745
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003746 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003747 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003748 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003749
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003750 // Replicate layout information for each array element. Note that
3751 // one element is already done.
3752 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003753 for (int FirstIndex = IvarsInfo.size() - 1,
3754 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003755 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003756 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3757 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3758 IvarsInfo[i].ivar_size));
3759 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3760 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3761 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003762 }
3763 continue;
3764 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003765 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003766 // At this point, we are done with Record/Union and array there of.
3767 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003768 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003769
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003770 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003771 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3772 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003773 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003774 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003775 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003776 MaxUnionIvarSize = UnionIvarSize;
3777 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003778 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003779 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003780 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003781 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003782 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003783 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003784 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003785 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3786 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003787 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003788 // FIXME: Why the asymmetry? We divide by word size in bits on other
3789 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003790 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003791 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003792 MaxSkippedUnionIvarSize = UnionIvarSize;
3793 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003794 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003795 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003796 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003797 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003798 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003799 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003800 }
3801 }
3802 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003803
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003804 if (LastFieldBitfieldOrUnnamed) {
3805 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3806 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003807 uint64_t BitFieldSize
3808 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003809 GC_IVAR skivar;
3810 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3811 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3812 + ((BitFieldSize % ByteSizeInBits) != 0);
3813 SkipIvars.push_back(skivar);
3814 } else {
3815 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3816 // Last field was unnamed. Must update skip info.
3817 unsigned FieldSize
3818 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3819 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3820 FieldSize / ByteSizeInBits));
3821 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003822 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003823
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003824 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003825 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003826 MaxUnionIvarSize));
3827 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003828 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003829 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003830}
3831
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003832/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3833/// the computations and returning the layout bitmap (for ivar or blocks) in
3834/// the given argument BitMap string container. Routine reads
3835/// two containers, IvarsInfo and SkipIvars which are assumed to be
3836/// filled already by the caller.
3837llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003838 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003839 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003840
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003841 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003842 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003843 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003844 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003845 if (IvarsInfo[0].ivar_bytepos == 0) {
3846 WordsToSkip = 0;
3847 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003848 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003849 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3850 WordsToScan = IvarsInfo[0].ivar_size;
3851 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003852 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003853 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003854 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003855 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003856 // consecutive 'scanned' object pointers.
3857 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003858 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003859 // Skip over 'gc'able object pointer which lay over each other.
3860 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3861 continue;
3862 // Must skip over 1 or more words. We save current skip/scan values
3863 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003864 SKIP_SCAN SkScan;
3865 SkScan.skip = WordsToSkip;
3866 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003867 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003868
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003869 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003870 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3871 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003872 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003873 WordsToSkip = 0;
3874 WordsToScan = IvarsInfo[i].ivar_size;
3875 }
3876 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003877 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003878 SKIP_SCAN SkScan;
3879 SkScan.skip = WordsToSkip;
3880 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003881 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003882 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003883
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003884 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003885 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003886 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003887 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003888 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003889 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003890 IvarsInfo[LastIndex].ivar_bytepos +
3891 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003892 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003893 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003894 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003895 SKIP_SCAN SkScan;
3896 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3897 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003898 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003899 }
3900 }
3901 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3902 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003903 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003904 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003905 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3906 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3907 // 0xM0 followed by 0x0N detected.
3908 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3909 for (int j = i+1; j < SkipScan; j++)
3910 SkipScanIvars[j] = SkipScanIvars[j+1];
3911 --SkipScan;
3912 }
3913 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003914
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003915 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003916 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003917 unsigned char byte;
3918 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3919 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3920 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3921 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003922
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003923 // first skip big.
3924 for (unsigned int ix = 0; ix < skip_big; ix++)
3925 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003926
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003927 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003928 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003929 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003930 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003931 byte |= 0xf;
3932 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003933 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003934 byte |= scan_small;
3935 scan_small = 0;
3936 }
3937 BitMap += byte;
3938 }
3939 // next scan big
3940 for (unsigned int ix = 0; ix < scan_big; ix++)
3941 BitMap += (unsigned char)(0x0f);
3942 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003943 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003944 byte = scan_small;
3945 BitMap += byte;
3946 }
3947 }
3948 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003949 unsigned char zero = 0;
3950 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003951
3952 llvm::GlobalVariable * Entry =
3953 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3954 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003955 ((ObjCABI == 2) ?
3956 "__TEXT,__objc_classname,cstring_literals" :
3957 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003958 1, true);
3959 return getConstantGEP(VMContext, Entry, 0, 0);
3960}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003961
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003962/// BuildIvarLayout - Builds ivar layout bitmap for the class
3963/// implementation for the __strong or __weak case.
3964/// The layout map displays which words in ivar list must be skipped
3965/// and which must be scanned by GC (see below). String is built of bytes.
3966/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3967/// of words to skip and right nibble is count of words to scan. So, each
3968/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3969/// represented by a 0x00 byte which also ends the string.
3970/// 1. when ForStrongLayout is true, following ivars are scanned:
3971/// - id, Class
3972/// - object *
3973/// - __strong anything
3974///
3975/// 2. When ForStrongLayout is false, following ivars are scanned:
3976/// - __weak anything
3977///
3978llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3979 const ObjCImplementationDecl *OMD,
3980 bool ForStrongLayout) {
3981 bool hasUnion = false;
3982
Chris Lattner2192fe52011-07-18 04:24:23 +00003983 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00003984 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00003985 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003986 return llvm::Constant::getNullValue(PtrTy);
3987
Jordy Rosea91768e2011-07-22 02:08:32 +00003988 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3989 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003990 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003991 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003992 IVD; IVD = IVD->getNextIvar())
3993 RecFields.push_back(cast<FieldDecl>(IVD));
3994 }
3995 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00003996 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00003997 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003998
Jordy Rosea91768e2011-07-22 02:08:32 +00003999 // FIXME: This is not ideal; we shouldn't have to do this copy.
4000 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004001 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004002
4003 if (RecFields.empty())
4004 return llvm::Constant::getNullValue(PtrTy);
4005
4006 SkipIvars.clear();
4007 IvarsInfo.clear();
4008
4009 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4010 if (IvarsInfo.empty())
4011 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004012 // Sort on byte position in case we encounterred a union nested in
4013 // the ivar list.
4014 if (hasUnion && !IvarsInfo.empty())
4015 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4016 if (hasUnion && !SkipIvars.empty())
4017 std::sort(SkipIvars.begin(), SkipIvars.end());
4018
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004019 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004020 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004021
4022 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004023 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004024 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004025 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004026 const unsigned char *s = (unsigned char*)BitMap.c_str();
4027 for (unsigned i = 0; i < BitMap.size(); i++)
4028 if (!(s[i] & 0xf0))
4029 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4030 else
4031 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4032 printf("\n");
4033 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004034 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004035}
4036
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004037llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004038 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4039
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004040 // FIXME: Avoid std::string copying.
4041 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004042 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00004043 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004044 ((ObjCABI == 2) ?
4045 "__TEXT,__objc_methname,cstring_literals" :
4046 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004047 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004048
Owen Anderson170229f2009-07-14 23:10:40 +00004049 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004050}
4051
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004052// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004053llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004054 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4055}
4056
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004057llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004058 std::string TypeStr;
4059 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4060
4061 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004062
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004063 if (!Entry)
4064 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004065 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004066 ((ObjCABI == 2) ?
4067 "__TEXT,__objc_methtype,cstring_literals" :
4068 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004069 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004070
Owen Anderson170229f2009-07-14 23:10:40 +00004071 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004072}
4073
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004074llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4075 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004076 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00004077 if (CGM.getContext().getObjCEncodingForMethodDecl(
4078 const_cast<ObjCMethodDecl*>(D),
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004079 TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004080 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004081
4082 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4083
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004084 if (!Entry)
4085 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004086 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004087 ((ObjCABI == 2) ?
4088 "__TEXT,__objc_methtype,cstring_literals" :
4089 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004090 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004091
Owen Anderson170229f2009-07-14 23:10:40 +00004092 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004093}
4094
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004095// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004096llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004097 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004098
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004099 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004100 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004101 llvm::ConstantArray::get(VMContext,
4102 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004103 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004104 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004105
Owen Anderson170229f2009-07-14 23:10:40 +00004106 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004107}
4108
4109// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004110// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004111llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004112CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4113 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004114 std::string TypeStr;
4115 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004116 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4117}
4118
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004119void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004120 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004121 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004122 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004123 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004124 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4125 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004126 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004127 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004128 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004129 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004130}
4131
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004132void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004133 EmitModuleInfo();
4134
Daniel Dunbarc475d422008-10-29 22:36:39 +00004135 // Emit the dummy bodies for any protocols which were referenced but
4136 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004137 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004138 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4139 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004140 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004141
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004142 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004143 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004144 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004145 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004146 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004147 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004148 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004149 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004150 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004151 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004152 }
4153
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004154 // Add assembler directives to add lazy undefined symbol references
4155 // for classes which are referenced but not defined. This is
4156 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004157 //
4158 // FIXME: It would be nice if we had an LLVM construct for this.
4159 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4160 llvm::SmallString<256> Asm;
4161 Asm += CGM.getModule().getModuleInlineAsm();
4162 if (!Asm.empty() && Asm.back() != '\n')
4163 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004164
Daniel Dunbard027a922009-09-07 00:20:42 +00004165 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004166 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4167 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004168 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4169 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004170 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004171 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004172 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004173 }
4174
4175 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4176 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4177 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4178 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004179
Daniel Dunbard027a922009-09-07 00:20:42 +00004180 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004181 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004182}
4183
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004185 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004186 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004187 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004188 ObjCABI = 2;
4189}
4190
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004191/* *** */
4192
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004193ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004194 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4195{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4197 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004198
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004199 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004200 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004201 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004202 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004203 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004204 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004205
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004206 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004207 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004208 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004209
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004210 // I'm not sure I like this. The implicit coordination is a bit
4211 // gross. We should solve this in a reasonable fashion because this
4212 // is a pretty common task (match some runtime data structure with
4213 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004214
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004215 // FIXME: This is leaked.
4216 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004217
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004218 // struct _objc_super {
4219 // id self;
4220 // Class cls;
4221 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004222 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004223 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004224 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004225 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004226 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004227 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004228 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004229 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004230 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004231
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004232 SuperCTy = Ctx.getTagDeclType(RD);
4233 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004235 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004236 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4237
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004238 // struct _prop_t {
4239 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004240 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004241 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004242 PropertyTy = llvm::StructType::create("struct._prop_t",
4243 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004244
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004245 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004246 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004247 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004248 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004249 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004250 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004251 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4252 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004253 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004254 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004255
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004256 // struct _objc_method {
4257 // SEL _cmd;
4258 // char *method_type;
4259 // char *_imp;
4260 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004261 MethodTy = llvm::StructType::create("struct._objc_method",
4262 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4263 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004264
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004265 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004266 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004267 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004268
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004269}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004270
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004271ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004272 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004273 // struct _objc_method_description {
4274 // SEL name;
4275 // char *types;
4276 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004277 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004278 llvm::StructType::create("struct._objc_method_description",
4279 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004280
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004281 // struct _objc_method_description_list {
4282 // int count;
4283 // struct _objc_method_description[1];
4284 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004285 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004286 llvm::StructType::create("struct._objc_method_description_list",
4287 IntTy,
4288 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004289
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004290 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004291 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004292 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004293
Daniel Dunbarb036db82008-08-13 03:21:16 +00004294 // Protocol description structures
4295
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004296 // struct _objc_protocol_extension {
4297 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4298 // struct _objc_method_description_list *optional_instance_methods;
4299 // struct _objc_method_description_list *optional_class_methods;
4300 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004301 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004302 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004303 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004304 llvm::StructType::create("struct._objc_protocol_extension",
4305 IntTy, MethodDescriptionListPtrTy,
4306 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004307 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004308
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004309 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004310 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004311
Daniel Dunbarc475d422008-10-29 22:36:39 +00004312 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004313
Chris Lattnera5f58b02011-07-09 17:41:47 +00004314 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004315 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004316
Chris Lattnera5f58b02011-07-09 17:41:47 +00004317 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004318 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004319 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004320 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004321 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004322 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004323
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004324 // struct _objc_protocol {
4325 // struct _objc_protocol_extension *isa;
4326 // char *protocol_name;
4327 // struct _objc_protocol **_objc_protocol_list;
4328 // struct _objc_method_description_list *instance_methods;
4329 // struct _objc_method_description_list *class_methods;
4330 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004331 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4332 llvm::PointerType::getUnqual(ProtocolListTy),
4333 MethodDescriptionListPtrTy,
4334 MethodDescriptionListPtrTy,
4335 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004336
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004337 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004338 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004339
Owen Anderson9793f0e2009-07-29 22:16:19 +00004340 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004341
4342 // Class description structures
4343
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004344 // struct _objc_ivar {
4345 // char *ivar_name;
4346 // char *ivar_type;
4347 // int ivar_offset;
4348 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004349 IvarTy = llvm::StructType::create("struct._objc_ivar",
4350 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004351
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004352 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004353 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004354 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004355 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004356
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004357 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004358 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004359 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004360 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004361
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004362 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004363 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004364 llvm::StructType::create("struct._objc_class_extension",
4365 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004366 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004367
Chris Lattner5ec04a52011-08-12 17:43:31 +00004368 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004369
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004370 // struct _objc_class {
4371 // Class isa;
4372 // Class super_class;
4373 // char *name;
4374 // long version;
4375 // long info;
4376 // long instance_size;
4377 // struct _objc_ivar_list *ivars;
4378 // struct _objc_method_list *methods;
4379 // struct _objc_cache *cache;
4380 // struct _objc_protocol_list *protocols;
4381 // char *ivar_layout;
4382 // struct _objc_class_ext *ext;
4383 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004384 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4385 llvm::PointerType::getUnqual(ClassTy),
4386 Int8PtrTy,
4387 LongTy,
4388 LongTy,
4389 LongTy,
4390 IvarListPtrTy,
4391 MethodListPtrTy,
4392 CachePtrTy,
4393 ProtocolListPtrTy,
4394 Int8PtrTy,
4395 ClassExtensionPtrTy,
4396 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004397
Owen Anderson9793f0e2009-07-29 22:16:19 +00004398 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004399
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004400 // struct _objc_category {
4401 // char *category_name;
4402 // char *class_name;
4403 // struct _objc_method_list *instance_method;
4404 // struct _objc_method_list *class_method;
4405 // uint32_t size; // sizeof(struct _objc_category)
4406 // struct _objc_property_list *instance_properties;// category's @property
4407 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004408 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004409 llvm::StructType::create("struct._objc_category",
4410 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4411 MethodListPtrTy, ProtocolListPtrTy,
4412 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004413
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004414 // Global metadata structures
4415
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004416 // struct _objc_symtab {
4417 // long sel_ref_cnt;
4418 // SEL *refs;
4419 // short cls_def_cnt;
4420 // short cat_def_cnt;
4421 // char *defs[cls_def_cnt + cat_def_cnt];
4422 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004423 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004424 llvm::StructType::create("struct._objc_symtab",
4425 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4426 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004427 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004428
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004429 // struct _objc_module {
4430 // long version;
4431 // long size; // sizeof(struct _objc_module)
4432 // char *name;
4433 // struct _objc_symtab* symtab;
4434 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004435 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004436 llvm::StructType::create("struct._objc_module",
4437 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004438
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439
Mike Stump18bb9282009-05-16 07:57:57 +00004440 // FIXME: This is the size of the setjmp buffer and should be target
4441 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004442 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004443
Anders Carlsson9ff22482008-09-09 10:10:21 +00004444 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004445 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004446 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004447
4448 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004449 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004450 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4451 SetJmpBufferSize),
4452 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004453
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004454}
4455
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004456ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004457 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004458 // struct _method_list_t {
4459 // uint32_t entsize; // sizeof(struct _objc_method)
4460 // uint32_t method_count;
4461 // struct _objc_method method_list[method_count];
4462 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004463 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004464 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4465 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004466 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004467 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004468
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004469 // struct _protocol_t {
4470 // id isa; // NULL
4471 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004472 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004473 // const struct method_list_t * const instance_methods;
4474 // const struct method_list_t * const class_methods;
4475 // const struct method_list_t *optionalInstanceMethods;
4476 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004477 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004478 // const uint32_t size; // sizeof(struct _protocol_t)
4479 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004480 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004481 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004482
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004483 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004484 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004485 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004486
Chris Lattnera5f58b02011-07-09 17:41:47 +00004487 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004488 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4489 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4490 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4491 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004492 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4493 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004494
4495 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004496 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004497
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004498 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004499 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004500 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004501 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004502 ProtocolListnfABITy->setBody(LongTy,
4503 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4504 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004505
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004506 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004507 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004508
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004509 // struct _ivar_t {
4510 // unsigned long int *offset; // pointer to ivar offset location
4511 // char *name;
4512 // char *type;
4513 // uint32_t alignment;
4514 // uint32_t size;
4515 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004516 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004517 llvm::StructType::create("struct._ivar_t",
4518 llvm::PointerType::getUnqual(LongTy),
4519 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004520
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004521 // struct _ivar_list_t {
4522 // uint32 entsize; // sizeof(struct _ivar_t)
4523 // uint32 count;
4524 // struct _iver_t list[count];
4525 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004526 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004527 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4528 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529
Owen Anderson9793f0e2009-07-29 22:16:19 +00004530 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004531
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004532 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004533 // uint32_t const flags;
4534 // uint32_t const instanceStart;
4535 // uint32_t const instanceSize;
4536 // uint32_t const reserved; // only when building for 64bit targets
4537 // const uint8_t * const ivarLayout;
4538 // const char *const name;
4539 // const struct _method_list_t * const baseMethods;
4540 // const struct _objc_protocol_list *const baseProtocols;
4541 // const struct _ivar_list_t *const ivars;
4542 // const uint8_t * const weakIvarLayout;
4543 // const struct _prop_list_t * const properties;
4544 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004545
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004546 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004547 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4548 IntTy, IntTy, IntTy, Int8PtrTy,
4549 Int8PtrTy, MethodListnfABIPtrTy,
4550 ProtocolListnfABIPtrTy,
4551 IvarListnfABIPtrTy,
4552 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004553
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004554 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004555 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004556 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4557 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004558
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004559 // struct _class_t {
4560 // struct _class_t *isa;
4561 // struct _class_t * const superclass;
4562 // void *cache;
4563 // IMP *vtable;
4564 // struct class_ro_t *ro;
4565 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004566
Chris Lattner5ec04a52011-08-12 17:43:31 +00004567 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004568 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4569 llvm::PointerType::getUnqual(ClassnfABITy),
4570 CachePtrTy,
4571 llvm::PointerType::getUnqual(ImpnfABITy),
4572 llvm::PointerType::getUnqual(ClassRonfABITy),
4573 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004574
Fariborz Jahanian71394042009-01-23 23:53:38 +00004575 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004576 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004577
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004578 // struct _category_t {
4579 // const char * const name;
4580 // struct _class_t *const cls;
4581 // const struct _method_list_t * const instance_methods;
4582 // const struct _method_list_t * const class_methods;
4583 // const struct _protocol_list_t * const protocols;
4584 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004585 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004586 CategorynfABITy = llvm::StructType::create("struct._category_t",
4587 Int8PtrTy, ClassnfABIPtrTy,
4588 MethodListnfABIPtrTy,
4589 MethodListnfABIPtrTy,
4590 ProtocolListnfABIPtrTy,
4591 PropertyListPtrTy,
4592 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004593
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004594 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004595 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4596 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004597
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004598 // MessageRefTy - LLVM for:
4599 // struct _message_ref_t {
4600 // IMP messenger;
4601 // SEL name;
4602 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004604 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004605 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004606 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004607 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004608 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004609 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004610 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004611 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004612 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004613 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004614
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004615 MessageRefCTy = Ctx.getTagDeclType(RD);
4616 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4617 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004619 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004620 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004621
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004622 // SuperMessageRefTy - LLVM for:
4623 // struct _super_message_ref_t {
4624 // SUPER_IMP messenger;
4625 // SEL name;
4626 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004627 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004628 llvm::StructType::create("struct._super_message_ref_t",
4629 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004630
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004631 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004632 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004633
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004634
4635 // struct objc_typeinfo {
4636 // const void** vtable; // objc_ehtype_vtable + 2
4637 // const char* name; // c++ typeinfo string
4638 // Class cls;
4639 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004640 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004641 llvm::StructType::create("struct._objc_typeinfo",
4642 llvm::PointerType::getUnqual(Int8PtrTy),
4643 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004644 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004645}
4646
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004647llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004648 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004649
Fariborz Jahanian71394042009-01-23 23:53:38 +00004650 return NULL;
4651}
4652
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653void CGObjCNonFragileABIMac::AddModuleClassList(const
4654 std::vector<llvm::GlobalValue*>
4655 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004656 const char *SymbolName,
4657 const char *SectionName) {
4658 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004659
Daniel Dunbar19573e72009-05-15 21:48:48 +00004660 if (!NumClasses)
4661 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004662
Daniel Dunbar19573e72009-05-15 21:48:48 +00004663 std::vector<llvm::Constant*> Symbols(NumClasses);
4664 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004665 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004666 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004667 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004668 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004669 NumClasses),
4670 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004671
Daniel Dunbar19573e72009-05-15 21:48:48 +00004672 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004673 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004674 llvm::GlobalValue::InternalLinkage,
4675 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004676 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004677 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004678 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004679 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004680}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004681
Fariborz Jahanian71394042009-01-23 23:53:38 +00004682void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4683 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684
Daniel Dunbar19573e72009-05-15 21:48:48 +00004685 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004686 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004687 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004688 "\01L_OBJC_LABEL_CLASS_$",
4689 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004690
Fariborz Jahanian67260552009-11-17 21:37:35 +00004691 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4692 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4693 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4694 continue;
4695 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004696 }
4697
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004698 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4699 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4700 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4701 continue;
4702 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4703 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004704
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004705 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004706 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4707 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004708
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004709 // Build list of all implemented category addresses in array
4710 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004711 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004712 "\01L_OBJC_LABEL_CATEGORY_$",
4713 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004714 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004715 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4716 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004717
Daniel Dunbar5e639272010-04-25 20:39:01 +00004718 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004719}
4720
John McCall9e8bb002011-05-14 03:10:52 +00004721/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4722/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004723/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004724/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004725bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4726 // At various points we've experimented with using vtable-based
4727 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004728 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004729 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004730 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004731 case CodeGenOptions::NonLegacy:
4732 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004733 case CodeGenOptions::Mixed:
4734 break;
4735 }
4736
4737 // If so, see whether this selector is in the white-list of things which must
4738 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004739 if (VTableDispatchMethods.empty()) {
4740 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4741 VTableDispatchMethods.insert(GetNullarySelector("class"));
4742 VTableDispatchMethods.insert(GetNullarySelector("self"));
4743 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4744 VTableDispatchMethods.insert(GetNullarySelector("length"));
4745 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004746
John McCall9e8bb002011-05-14 03:10:52 +00004747 // These are vtable-based if GC is disabled.
4748 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004749 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004750 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4751 VTableDispatchMethods.insert(GetNullarySelector("release"));
4752 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4753 }
4754
4755 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4756 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4757 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4758 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4759 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4760 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4761 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4762
4763 // These are vtable-based if GC is enabled.
4764 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004765 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004766 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4767 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4768
4769 // "countByEnumeratingWithState:objects:count"
4770 IdentifierInfo *KeyIdents[] = {
4771 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4772 &CGM.getContext().Idents.get("objects"),
4773 &CGM.getContext().Idents.get("count")
4774 };
4775 VTableDispatchMethods.insert(
4776 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4777 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004778 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004779
John McCall9e8bb002011-05-14 03:10:52 +00004780 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004781}
4782
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004783// Metadata flags
4784enum MetaDataDlags {
4785 CLS = 0x0,
4786 CLS_META = 0x1,
4787 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004788 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004789 CLS_EXCEPTION = 0x20,
4790
4791 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4792 CLS_HAS_IVAR_RELEASER = 0x40,
4793 /// class was compiled with -fobjc-arr
4794 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004795};
4796/// BuildClassRoTInitializer - generate meta-data for:
4797/// struct _class_ro_t {
4798/// uint32_t const flags;
4799/// uint32_t const instanceStart;
4800/// uint32_t const instanceSize;
4801/// uint32_t const reserved; // only when building for 64bit targets
4802/// const uint8_t * const ivarLayout;
4803/// const char *const name;
4804/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004805/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004806/// const struct _ivar_list_t *const ivars;
4807/// const uint8_t * const weakIvarLayout;
4808/// const struct _prop_list_t * const properties;
4809/// }
4810///
4811llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004812 unsigned flags,
4813 unsigned InstanceStart,
4814 unsigned InstanceSize,
4815 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004816 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004817 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004818
4819 if (CGM.getLangOptions().ObjCAutoRefCount)
4820 flags |= CLS_COMPILED_BY_ARC;
4821
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004822 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4823 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4824 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004825 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004826 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4827 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004828 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004829 // const struct _method_list_t * const baseMethods;
4830 std::vector<llvm::Constant*> Methods;
4831 std::string MethodListName("\01l_OBJC_$_");
4832 if (flags & CLS_META) {
4833 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004834 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004835 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004836 // Class methods should always be defined.
4837 Methods.push_back(GetMethodConstant(*i));
4838 }
4839 } else {
4840 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004841 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004842 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004843 // Instance methods should always be defined.
4844 Methods.push_back(GetMethodConstant(*i));
4845 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004846 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004847 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004848 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004849
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004850 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4851 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004852
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004853 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4854 if (llvm::Constant *C = GetMethodConstant(MD))
4855 Methods.push_back(C);
4856 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4857 if (llvm::Constant *C = GetMethodConstant(MD))
4858 Methods.push_back(C);
4859 }
4860 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004861 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004862 Values[ 5] = EmitMethodList(MethodListName,
4863 "__DATA, __objc_const", Methods);
4864
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004865 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4866 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004867 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004868 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004869 OID->all_referenced_protocol_begin(),
4870 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004871
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004872 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004873 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004874 else
4875 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4877 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004878 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004879 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004880 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004881 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4882 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004883 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004884 Values);
4885 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004886 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4887 llvm::GlobalValue::InternalLinkage,
4888 Init,
4889 (flags & CLS_META) ?
4890 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4891 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004892 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004893 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004894 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004895 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004896
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004897}
4898
4899/// BuildClassMetaData - This routine defines that to-level meta-data
4900/// for the given ClassName for:
4901/// struct _class_t {
4902/// struct _class_t *isa;
4903/// struct _class_t * const superclass;
4904/// void *cache;
4905/// IMP *vtable;
4906/// struct class_ro_t *ro;
4907/// }
4908///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004909llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004910 std::string &ClassName,
4911 llvm::Constant *IsAGV,
4912 llvm::Constant *SuperClassGV,
4913 llvm::Constant *ClassRoGV,
4914 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004915 llvm::Constant *Values[] = {
4916 IsAGV,
4917 SuperClassGV,
4918 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4919 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4920 ClassRoGV // &CLASS_RO_GV
4921 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004922 if (!Values[1])
4923 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004924 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004925 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004926 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4927 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004928 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004929 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004930 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004931 if (HiddenVisibility)
4932 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004933 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004934}
4935
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004936bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004937CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004938 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004939}
4940
Daniel Dunbar961202372009-05-03 12:57:56 +00004941void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004942 uint32_t &InstanceStart,
4943 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004944 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004945 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004946
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004947 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004948 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004949
4950 // If there are no fields, the start is the same as the end.
4951 if (!RL.getFieldCount())
4952 InstanceStart = InstanceSize;
4953 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004954 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004955}
4956
Fariborz Jahanian71394042009-01-23 23:53:38 +00004957void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4958 std::string ClassName = ID->getNameAsString();
4959 if (!ObjCEmptyCacheVar) {
4960 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004961 CGM.getModule(),
4962 ObjCTypes.CacheTy,
4963 false,
4964 llvm::GlobalValue::ExternalLinkage,
4965 0,
4966 "_objc_empty_cache");
4967
Fariborz Jahanian71394042009-01-23 23:53:38 +00004968 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004969 CGM.getModule(),
4970 ObjCTypes.ImpnfABITy,
4971 false,
4972 llvm::GlobalValue::ExternalLinkage,
4973 0,
4974 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004975 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004977 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004978 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004979 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004980 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004981 uint32_t InstanceSize = InstanceStart;
4982 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004983 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4984 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004985
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004986 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004987
4988 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004989 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004990 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004991 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004992 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004993 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004994 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004995 // class is root
4996 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004997 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004998 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004999 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005000 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005001 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5002 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5003 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005004 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005005 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005006 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005007 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005008 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005009 ObjCMetaClassName +
5010 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005011 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005012 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005013 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005014 }
5015 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5016 InstanceStart,
5017 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005018 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005019 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005020 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5021 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005022 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005023
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005024 // Metadata for the class
5025 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005026 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005027 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005028 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005029 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005030
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005031 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005032 flags |= CLS_EXCEPTION;
5033
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005034 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005035 flags |= CLS_ROOT;
5036 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005037 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005038 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005039 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005040 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005041 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005042 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005043 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005044 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005045 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005046 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005047 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005048 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005049 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005050
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005051 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005052 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005053 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5054 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005055 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005056
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005057 // Determine if this class is also "non-lazy".
5058 if (ImplementationIsNonLazy(ID))
5059 DefinedNonLazyClasses.push_back(ClassMD);
5060
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005061 // Force the definition of the EHType if necessary.
5062 if (flags & CLS_EXCEPTION)
5063 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005064 // Make sure method definition entries are all clear for next implementation.
5065 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005066}
5067
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005068/// GenerateProtocolRef - This routine is called to generate code for
5069/// a protocol reference expression; as in:
5070/// @code
5071/// @protocol(Proto1);
5072/// @endcode
5073/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5074/// which will hold address of the protocol meta-data.
5075///
5076llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005077 const ObjCProtocolDecl *PD) {
5078
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005079 // This routine is called for @protocol only. So, we must build definition
5080 // of protocol's meta-data (not a reference to it!)
5081 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082 llvm::Constant *Init =
5083 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005084 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005085
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005086 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005087 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005088
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005089 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5090 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005091 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005092 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093 CGM.getModule(),
5094 Init->getType(), false,
5095 llvm::GlobalValue::WeakAnyLinkage,
5096 Init,
5097 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005098 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5099 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005100 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005101 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005102}
5103
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005104/// GenerateCategory - Build metadata for a category implementation.
5105/// struct _category_t {
5106/// const char * const name;
5107/// struct _class_t *const cls;
5108/// const struct _method_list_t * const instance_methods;
5109/// const struct _method_list_t * const class_methods;
5110/// const struct _protocol_list_t * const protocols;
5111/// const struct _prop_list_t * const properties;
5112/// }
5113///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005114void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005115 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005116 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005117 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5118 "_$_" + OCD->getNameAsString());
5119 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005120 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005121
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005122 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005123 Values[0] = GetClassName(OCD->getIdentifier());
5124 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005125 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005126 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005127 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5128
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005129 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005130 std::vector<llvm::Constant*> Methods;
5131 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005132 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005133 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134
5135 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005136 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005137 // Instance methods should always be defined.
5138 Methods.push_back(GetMethodConstant(*i));
5139 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005140
5141 Values[2] = EmitMethodList(MethodListName,
5142 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005143 Methods);
5144
5145 MethodListName = Prefix;
5146 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5147 OCD->getNameAsString();
5148 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005149 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005150 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005151 // Class methods should always be defined.
5152 Methods.push_back(GetMethodConstant(*i));
5153 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154
5155 Values[3] = EmitMethodList(MethodListName,
5156 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005157 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005158 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005159 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005160 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005161 llvm::SmallString<256> ExtName;
5162 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5163 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005164 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005165 + Interface->getName() + "_$_"
5166 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005167 Category->protocol_begin(),
5168 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005169 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5170 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005171 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005172 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5173 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005174 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005175
5176 llvm::Constant *Init =
5177 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005178 Values);
5179 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005180 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005181 false,
5182 llvm::GlobalValue::InternalLinkage,
5183 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005184 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005185 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005186 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005187 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005188 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005189 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005190
5191 // Determine if this category is also "non-lazy".
5192 if (ImplementationIsNonLazy(OCD))
5193 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005194 // method definition entries must be clear for next implementation.
5195 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005196}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005197
5198/// GetMethodConstant - Return a struct objc_method constant for the
5199/// given method if it has been defined. The result is null if the
5200/// method has not been defined. The return value has type MethodPtrTy.
5201llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005202 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005203 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005204 if (!Fn)
5205 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005206
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005207 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005208 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005209 ObjCTypes.SelectorPtrTy),
5210 GetMethodVarType(MD),
5211 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5212 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005213 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005214}
5215
5216/// EmitMethodList - Build meta-data for method declarations
5217/// struct _method_list_t {
5218/// uint32_t entsize; // sizeof(struct _objc_method)
5219/// uint32_t method_count;
5220/// struct _objc_method method_list[method_count];
5221/// }
5222///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005223llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005224 const char *Section,
5225 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005226 // Return null for empty list.
5227 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005228 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005229
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005230 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005231 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005232 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005233 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005234 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005235 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005236 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005237 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005238 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005239 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005240
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005241 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005242 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005243 llvm::GlobalValue::InternalLinkage, Init, Name);
5244 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005245 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005246 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005247 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005248}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005249
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005250/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5251/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005252llvm::GlobalVariable *
5253CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5254 const ObjCIvarDecl *Ivar) {
5255 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005256 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005257 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005258 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005259 CGM.getModule().getGlobalVariable(Name);
5260 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005261 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005262 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005263 false,
5264 llvm::GlobalValue::ExternalLinkage,
5265 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005266 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005267 return IvarOffsetGV;
5268}
5269
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005270llvm::Constant *
5271CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5272 const ObjCIvarDecl *Ivar,
5273 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005274 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005275 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005276 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005277 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005278 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005279
Mike Stump18bb9282009-05-16 07:57:57 +00005280 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5281 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005282 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5283 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005284 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005285 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005286 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005287 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005288 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005289 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005290}
5291
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005292/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005293/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005294/// IvarListnfABIPtrTy.
5295/// struct _ivar_t {
5296/// unsigned long int *offset; // pointer to ivar offset location
5297/// char *name;
5298/// char *type;
5299/// uint32_t alignment;
5300/// uint32_t size;
5301/// }
5302/// struct _ivar_list_t {
5303/// uint32 entsize; // sizeof(struct _ivar_t)
5304/// uint32 count;
5305/// struct _iver_t list[count];
5306/// }
5307///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005308
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005309llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005310 const ObjCImplementationDecl *ID) {
5311
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005312 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005313
Jordy Rosea91768e2011-07-22 02:08:32 +00005314 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005315 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005316
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005317 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005318
Jordy Rosea91768e2011-07-22 02:08:32 +00005319 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005320 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005321 // Ignore unnamed bit-fields.
5322 if (!IVD->getDeclName())
5323 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005324 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005325 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005326 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005327 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5328 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005329 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005330 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005331 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005332 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005333 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005334 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005335 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005336 // NOTE. Size of a bitfield does not match gcc's, because of the
5337 // way bitfields are treated special in each. But I am told that
5338 // 'size' for bitfield ivars is ignored by the runtime so it does
5339 // not matter. If it matters, there is enough info to get the
5340 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005341 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005342 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005343 }
5344 // Return null for empty list.
5345 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005346 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005347
5348 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005349 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005350 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5351 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005352 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005353 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005354 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005355 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005356 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5357 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005358 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005359 llvm::GlobalValue::InternalLinkage,
5360 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005361 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005362 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005363 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005364 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005365
Chris Lattnerf56501c2009-07-17 23:57:13 +00005366 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005367 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005368}
5369
5370llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005371 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005372 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005373
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005374 if (!Entry) {
5375 // We use the initializer as a marker of whether this is a forward
5376 // reference or not. At module finalization we add the empty
5377 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378 Entry =
5379 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5380 llvm::GlobalValue::ExternalLinkage,
5381 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005382 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005385
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005386 return Entry;
5387}
5388
5389/// GetOrEmitProtocol - Generate the protocol meta-data:
5390/// @code
5391/// struct _protocol_t {
5392/// id isa; // NULL
5393/// const char * const protocol_name;
5394/// const struct _protocol_list_t * protocol_list; // super protocols
5395/// const struct method_list_t * const instance_methods;
5396/// const struct method_list_t * const class_methods;
5397/// const struct method_list_t *optionalInstanceMethods;
5398/// const struct method_list_t *optionalClassMethods;
5399/// const struct _prop_list_t * properties;
5400/// const uint32_t size; // sizeof(struct _protocol_t)
5401/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005402/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005403/// }
5404/// @endcode
5405///
5406
5407llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005408 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005409 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005410
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005411 // Early exit if a defining object has already been generated.
5412 if (Entry && Entry->hasInitializer())
5413 return Entry;
5414
Douglas Gregora715bff2012-01-01 19:51:50 +00005415 // Use the protocol definition, if there is one.
5416 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5417 PD = Def;
5418
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005419 // Construct method lists.
5420 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5421 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005422 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005423 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005424 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005425 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005426 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005427 if (!C)
5428 return GetOrEmitProtocolRef(PD);
5429
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005430 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5431 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005432 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 } else {
5434 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005435 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005436 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005437 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005438
5439 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005440 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005441 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005442 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005443 if (!C)
5444 return GetOrEmitProtocolRef(PD);
5445
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005446 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5447 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005448 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005449 } else {
5450 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005451 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005453 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005454
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005455 MethodTypesExt.insert(MethodTypesExt.end(),
5456 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5457
5458 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005459 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005460 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005461 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005462 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5463 PD->protocol_begin(),
5464 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005465
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005466 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005467 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005468 "__DATA, __objc_const",
5469 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005470 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005471 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005472 "__DATA, __objc_const",
5473 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005474 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005475 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005476 "__DATA, __objc_const",
5477 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005479 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005480 "__DATA, __objc_const",
5481 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005482 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005483 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005484 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005485 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005486 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005487 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005488 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5489 + PD->getName(),
5490 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005491 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005492 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005493
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005494 if (Entry) {
5495 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005496 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005497 Entry->setInitializer(Init);
5498 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005499 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005500 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5501 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5502 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005503 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005504 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005505 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005506 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005507 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005508 CGM.AddUsedGlobal(Entry);
5509
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005510 // Use this protocol meta-data to build protocol list table in section
5511 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005512 llvm::GlobalVariable *PTGV =
5513 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5514 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5515 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005516 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005517 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005518 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005519 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005520 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005521 return Entry;
5522}
5523
5524/// EmitProtocolList - Generate protocol list meta-data:
5525/// @code
5526/// struct _protocol_list_t {
5527/// long protocol_count; // Note, this is 32/64 bit
5528/// struct _protocol_t[protocol_count];
5529/// }
5530/// @endcode
5531///
5532llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005533CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005534 ObjCProtocolDecl::protocol_iterator begin,
5535 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005536 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005537
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005538 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005539 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005540 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005541
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005542 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005543 llvm::SmallString<256> TmpName;
5544 Name.toVector(TmpName);
5545 llvm::GlobalVariable *GV =
5546 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005547 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005548 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005549
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005550 for (; begin != end; ++begin)
5551 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5552
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005553 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005554 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555 ObjCTypes.ProtocolnfABIPtrTy));
5556
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005557 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005558 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005559 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005560 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005561 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005562 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005563 ProtocolRefs.size()),
5564 ProtocolRefs);
5565
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005566 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005567 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005568 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005569 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005570 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005571 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005572 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005573 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005574 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005575 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005576}
5577
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005578/// GetMethodDescriptionConstant - This routine build following meta-data:
5579/// struct _objc_method {
5580/// SEL _cmd;
5581/// char *method_type;
5582/// char *_imp;
5583/// }
5584
5585llvm::Constant *
5586CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005587 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005588 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005589 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5590 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005591 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005592 if (!Desc[1])
5593 return 0;
5594
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005595 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005596 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005597 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005598}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005599
5600/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5601/// This code gen. amounts to generating code for:
5602/// @code
5603/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5604/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005605///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005606LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005607 CodeGen::CodeGenFunction &CGF,
5608 QualType ObjectTy,
5609 llvm::Value *BaseValue,
5610 const ObjCIvarDecl *Ivar,
5611 unsigned CVRQualifiers) {
5612 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005613 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5614 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005615}
5616
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005617llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005618 CodeGen::CodeGenFunction &CGF,
5619 const ObjCInterfaceDecl *Interface,
5620 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005621 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005622}
5623
John McCall234eac82011-05-13 23:16:18 +00005624static void appendSelectorForMessageRefTable(std::string &buffer,
5625 Selector selector) {
5626 if (selector.isUnarySelector()) {
5627 buffer += selector.getNameForSlot(0);
5628 return;
5629 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005630
John McCall234eac82011-05-13 23:16:18 +00005631 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5632 buffer += selector.getNameForSlot(i);
5633 buffer += '_';
5634 }
5635}
5636
John McCall9e8bb002011-05-14 03:10:52 +00005637/// Emit a "v-table" message send. We emit a weak hidden-visibility
5638/// struct, initially containing the selector pointer and a pointer to
5639/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5640/// load and call the function pointer, passing the address of the
5641/// struct as the second parameter. The runtime determines whether
5642/// the selector is currently emitted using vtable dispatch; if so, it
5643/// substitutes a stub function which simply tail-calls through the
5644/// appropriate vtable slot, and if not, it substitues a stub function
5645/// which tail-calls objc_msgSend. Both stubs adjust the selector
5646/// argument to correctly point to the selector.
5647RValue
5648CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5649 ReturnValueSlot returnSlot,
5650 QualType resultType,
5651 Selector selector,
5652 llvm::Value *arg0,
5653 QualType arg0Type,
5654 bool isSuper,
5655 const CallArgList &formalArgs,
5656 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005657 // Compute the actual arguments.
5658 CallArgList args;
5659
John McCall9e8bb002011-05-14 03:10:52 +00005660 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005661 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005662 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5663 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005664
John McCall9e8bb002011-05-14 03:10:52 +00005665 // Second argument: a pointer to the message ref structure. Leave
5666 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005667 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5668
5669 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5670
5671 const CGFunctionInfo &fnInfo =
5672 CGM.getTypes().getFunctionInfo(resultType, args,
5673 FunctionType::ExtInfo());
5674
John McCall5880fb82011-05-14 21:12:11 +00005675 NullReturnState nullReturn;
5676
John McCall9e8bb002011-05-14 03:10:52 +00005677 // Find the function to call and the mangled name for the message
5678 // ref structure. Using a different mangled name wouldn't actually
5679 // be a problem; it would just be a waste.
5680 //
5681 // The runtime currently never uses vtable dispatch for anything
5682 // except normal, non-super message-sends.
5683 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005684 llvm::Constant *fn = 0;
5685 std::string messageRefName("\01l_");
5686 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005687 if (isSuper) {
5688 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5689 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005690 } else {
John McCall5880fb82011-05-14 21:12:11 +00005691 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005692 fn = ObjCTypes.getMessageSendStretFixupFn();
5693 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005694 }
John McCall234eac82011-05-13 23:16:18 +00005695 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5696 fn = ObjCTypes.getMessageSendFpretFixupFn();
5697 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005698 } else {
John McCall234eac82011-05-13 23:16:18 +00005699 if (isSuper) {
5700 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5701 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005702 } else {
John McCall234eac82011-05-13 23:16:18 +00005703 fn = ObjCTypes.getMessageSendFixupFn();
5704 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005705 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005706 }
John McCall234eac82011-05-13 23:16:18 +00005707 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5708 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005709
John McCall234eac82011-05-13 23:16:18 +00005710 // Append the selector name, except use underscores anywhere we
5711 // would have used colons.
5712 appendSelectorForMessageRefTable(messageRefName, selector);
5713
5714 llvm::GlobalVariable *messageRef
5715 = CGM.getModule().getGlobalVariable(messageRefName);
5716 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005717 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005718 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005719 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005720 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5721 init->getType(),
5722 /*constant*/ false,
5723 llvm::GlobalValue::WeakAnyLinkage,
5724 init,
5725 messageRefName);
5726 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5727 messageRef->setAlignment(16);
5728 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5729 }
5730 llvm::Value *mref =
5731 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5732
John McCall9e8bb002011-05-14 03:10:52 +00005733 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005734 args[1].RV = RValue::get(mref);
5735
5736 // Load the function to call from the message ref table.
5737 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5738 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5739
5740 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005741 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005742 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5743 callee = CGF.Builder.CreateBitCast(callee,
5744 llvm::PointerType::getUnqual(fnType));
5745
John McCall5880fb82011-05-14 21:12:11 +00005746 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5747 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005748}
5749
5750/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005751CodeGen::RValue
5752CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005753 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005754 QualType ResultType,
5755 Selector Sel,
5756 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005757 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005758 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005759 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005760 return isVTableDispatchedSelector(Sel)
5761 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005762 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005763 false, CallArgs, Method)
5764 : EmitMessageSend(CGF, Return, ResultType,
5765 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005766 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005767 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005768}
5769
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005770llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005771CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005772 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5773
Daniel Dunbara6468342009-03-02 05:18:14 +00005774 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005775 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005776 false, llvm::GlobalValue::ExternalLinkage,
5777 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005778 }
5779
5780 return GV;
5781}
5782
John McCall31168b02011-06-15 23:02:42 +00005783llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5784 IdentifierInfo *II) {
5785 llvm::GlobalVariable *&Entry = ClassReferences[II];
5786
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005787 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005788 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005789 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005790 Entry =
John McCall31168b02011-06-15 23:02:42 +00005791 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5792 false, llvm::GlobalValue::InternalLinkage,
5793 ClassGV,
5794 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005795 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005796 CGM.getTargetData().getABITypeAlignment(
5797 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005798 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005799 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005800 }
John McCall31168b02011-06-15 23:02:42 +00005801
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005802 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005803}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005804
John McCall31168b02011-06-15 23:02:42 +00005805llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5806 const ObjCInterfaceDecl *ID) {
5807 return EmitClassRefFromId(Builder, ID->getIdentifier());
5808}
5809
5810llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5811 CGBuilderTy &Builder) {
5812 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5813 return EmitClassRefFromId(Builder, II);
5814}
5815
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005816llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005817CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005818 const ObjCInterfaceDecl *ID) {
5819 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005820
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005821 if (!Entry) {
5822 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5823 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005824 Entry =
5825 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005826 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005827 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005828 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005829 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005830 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005831 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005832 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005833 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005834 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005835
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005836 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005837}
5838
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005839/// EmitMetaClassRef - Return a Value * of the address of _class_t
5840/// meta-data
5841///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005842llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5843 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005844 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5845 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005846 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005847
Daniel Dunbar15894b72009-04-07 05:48:37 +00005848 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005849 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005850 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005851 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005852 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005853 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005854 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005855 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005856 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005857 ObjCTypes.ClassnfABIPtrTy));
5858
Daniel Dunbare60aa052009-04-15 19:03:14 +00005859 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005860 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005861
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005862 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005863}
5864
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005865/// GetClass - Return a reference to the class for the given interface
5866/// decl.
5867llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5868 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005869 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005870 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5871 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5872 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5873 }
5874
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005875 return EmitClassRef(Builder, ID);
5876}
5877
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005878/// Generates a message send where the super is the receiver. This is
5879/// a message send to self with special delivery semantics indicating
5880/// which class's method should be called.
5881CodeGen::RValue
5882CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005883 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005884 QualType ResultType,
5885 Selector Sel,
5886 const ObjCInterfaceDecl *Class,
5887 bool isCategoryImpl,
5888 llvm::Value *Receiver,
5889 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005890 const CodeGen::CallArgList &CallArgs,
5891 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005892 // ...
5893 // Create and init a super structure; this is a (receiver, class)
5894 // pair we will pass to objc_msgSendSuper.
5895 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005896 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005897
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005898 llvm::Value *ReceiverAsObject =
5899 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5900 CGF.Builder.CreateStore(ReceiverAsObject,
5901 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005902
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005903 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005904 llvm::Value *Target;
5905 if (IsClassMessage) {
5906 if (isCategoryImpl) {
5907 // Message sent to "super' in a class method defined in
5908 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005909 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005910 Target = CGF.Builder.CreateStructGEP(Target, 0);
5911 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005912 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005913 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005914 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005915 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005916
Mike Stump18bb9282009-05-16 07:57:57 +00005917 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5918 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00005919 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005920 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5921 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5922 CGF.Builder.CreateStore(Target,
5923 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005924
John McCall9e8bb002011-05-14 03:10:52 +00005925 return (isVTableDispatchedSelector(Sel))
5926 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005927 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005928 true, CallArgs, Method)
5929 : EmitMessageSend(CGF, Return, ResultType,
5930 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005931 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005932 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005933}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005934
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005935llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005936 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005937 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005938
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005939 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005940 llvm::Constant *Casted =
5941 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5942 ObjCTypes.SelectorPtrTy);
5943 Entry =
5944 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5945 llvm::GlobalValue::InternalLinkage,
5946 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005947 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005948 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005949 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005950
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005951 if (lval)
5952 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00005953 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
5954
5955 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5956 llvm::MDNode::get(VMContext,
5957 ArrayRef<llvm::Value*>()));
5958 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005959}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005960/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005961/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005962///
5963void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005964 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005965 llvm::Value *dst,
5966 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005967 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005968 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005969 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005970 assert(Size <= 8 && "does not support size > 8");
5971 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5972 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005973 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5974 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005975 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5976 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005977 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5978 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005979 return;
5980}
5981
5982/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5983/// objc_assign_strongCast (id src, id *dst)
5984///
5985void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005986 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005987 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005988 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005989 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005990 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005991 assert(Size <= 8 && "does not support size > 8");
5992 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005993 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005994 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5995 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005996 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5997 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005998 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005999 src, dst, "weakassign");
6000 return;
6001}
6002
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006003void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006004 CodeGen::CodeGenFunction &CGF,
6005 llvm::Value *DestPtr,
6006 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006007 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006008 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6009 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006010 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006011 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006012 return;
6013}
6014
Fariborz Jahanian06292952009-02-16 22:52:32 +00006015/// EmitObjCWeakRead - Code gen for loading value of a __weak
6016/// object: objc_read_weak (id *src)
6017///
6018llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006019 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006020 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006021 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006022 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6023 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006024 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006025 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006026 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006027 return read_weak;
6028}
6029
6030/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6031/// objc_assign_weak (id src, id *dst)
6032///
6033void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006034 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006035 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006036 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006037 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006038 assert(Size <= 8 && "does not support size > 8");
6039 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6040 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006041 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6042 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006043 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6044 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006045 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006046 src, dst, "weakassign");
6047 return;
6048}
6049
6050/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6051/// objc_assign_global (id src, id *dst)
6052///
6053void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006054 llvm::Value *src, llvm::Value *dst,
6055 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006056 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006057 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006058 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006059 assert(Size <= 8 && "does not support size > 8");
6060 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6061 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006062 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6063 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006064 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6065 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006066 if (!threadlocal)
6067 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6068 src, dst, "globalassign");
6069 else
6070 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6071 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006072 return;
6073}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006074
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006075void
John McCallbd309292010-07-06 01:34:17 +00006076CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6077 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006078 EmitAtSynchronizedStmt(CGF, S,
6079 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6080 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006081}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006082
John McCall2ca705e2010-07-24 00:37:23 +00006083llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006084CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006085 // There's a particular fixed type info for 'id'.
6086 if (T->isObjCIdType() ||
6087 T->isObjCQualifiedIdType()) {
6088 llvm::Constant *IDEHType =
6089 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6090 if (!IDEHType)
6091 IDEHType =
6092 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6093 false,
6094 llvm::GlobalValue::ExternalLinkage,
6095 0, "OBJC_EHTYPE_id");
6096 return IDEHType;
6097 }
6098
6099 // All other types should be Objective-C interface pointer types.
6100 const ObjCObjectPointerType *PT =
6101 T->getAs<ObjCObjectPointerType>();
6102 assert(PT && "Invalid @catch type.");
6103 const ObjCInterfaceType *IT = PT->getInterfaceType();
6104 assert(IT && "Invalid @catch type.");
6105 return GetInterfaceEHType(IT->getDecl(), false);
6106}
6107
John McCallbd309292010-07-06 01:34:17 +00006108void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6109 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006110 EmitTryCatchStmt(CGF, S,
6111 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6112 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6113 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006114}
6115
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006116/// EmitThrowStmt - Generate code for a throw statement.
6117void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6118 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006119 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006120 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006121 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006122 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006123 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006124 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006125 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006126 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006127 }
6128
John McCall17afe452010-10-16 08:21:07 +00006129 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006130 CGF.Builder.ClearInsertionPoint();
6131}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006132
John McCall2ca705e2010-07-24 00:37:23 +00006133llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006134CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006135 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006136 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006137
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006138 // If we don't need a definition, return the entry if found or check
6139 // if we use an external reference.
6140 if (!ForDefinition) {
6141 if (Entry)
6142 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006143
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006144 // If this type (or a super class) has the __objc_exception__
6145 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006146 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006147 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006148 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006149 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006150 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006151 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006152 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006153 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006154
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006155 // Otherwise we need to either make a new entry or fill in the
6156 // initializer.
6157 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006158 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006159 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006160 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006161 CGM.getModule().getGlobalVariable(VTableName);
6162 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006163 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6164 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006165 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006166 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006167
Chris Lattner5e016ae2010-06-27 07:15:29 +00006168 llvm::Value *VTableIdx =
6169 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006170
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006171 llvm::Constant *Values[] = {
6172 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6173 GetClassName(ID->getIdentifier()),
6174 GetClassGlobal(ClassName)
6175 };
Owen Anderson170229f2009-07-14 23:10:40 +00006176 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006177 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006178
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006179 if (Entry) {
6180 Entry->setInitializer(Init);
6181 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006182 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006183 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006184 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006185 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006186 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006187 }
6188
John McCall457a04e2010-10-22 21:05:15 +00006189 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006190 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006191 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6192 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006193
6194 if (ForDefinition) {
6195 Entry->setSection("__DATA,__objc_const");
6196 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6197 } else {
6198 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6199 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006200
6201 return Entry;
6202}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006203
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006204/* *** */
6205
Daniel Dunbarb036db82008-08-13 03:21:16 +00006206CodeGen::CGObjCRuntime *
6207CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006208 if (CGM.getLangOptions().ObjCNonFragileABI)
6209 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006210 return new CGObjCMac(CGM);
6211}