blob: 84277f6827e86c584488db871f2294f2fe364903 [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;
178 llvm::Type *Int8PtrTy;
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;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000188 /// ProtocolPtrTy - LLVM type for external protocol handles
189 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000190 llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000191
Daniel Dunbarc722b852008-08-30 03:02:31 +0000192 // SuperCTy - clang type for struct objc_super.
193 QualType SuperCTy;
194 // SuperPtrCTy - clang type for struct objc_super *.
195 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000196
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000197 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000198 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000199 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000200 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000201
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000202 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
203 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000204 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000205
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000206 /// PropertyListTy - LLVM type for struct objc_property_list
207 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000208 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000209 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000210 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000211
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000212 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000213 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000214
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000215 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000216 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000217 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000218 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000219
Chris Lattnerce8754e2009-04-22 02:44:54 +0000220 llvm::Constant *getGetPropertyFn() {
221 CodeGen::CodeGenTypes &Types = CGM.getTypes();
222 ASTContext &Ctx = CGM.getContext();
223 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000224 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000225 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
226 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000227 Params.push_back(IdType);
228 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000229 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000230 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000231 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000232 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000233 FunctionType::ExtInfo()),
234 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000235 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
236 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000237
Chris Lattnerce8754e2009-04-22 02:44:54 +0000238 llvm::Constant *getSetPropertyFn() {
239 CodeGen::CodeGenTypes &Types = CGM.getTypes();
240 ASTContext &Ctx = CGM.getContext();
241 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000243 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
244 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000245 Params.push_back(IdType);
246 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000247 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000248 Params.push_back(IdType);
249 Params.push_back(Ctx.BoolTy);
250 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000251 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000252 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000253 FunctionType::ExtInfo()),
254 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000255 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
256 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000257
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000258
259 llvm::Constant *getCopyStructFn() {
260 CodeGen::CodeGenTypes &Types = CGM.getTypes();
261 ASTContext &Ctx = CGM.getContext();
262 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000263 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000264 Params.push_back(Ctx.VoidPtrTy);
265 Params.push_back(Ctx.VoidPtrTy);
266 Params.push_back(Ctx.LongTy);
267 Params.push_back(Ctx.BoolTy);
268 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000269 llvm::FunctionType *FTy =
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000270 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
271 FunctionType::ExtInfo()),
272 false);
273 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
274 }
275
Chris Lattnerce8754e2009-04-22 02:44:54 +0000276 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000277 CodeGen::CodeGenTypes &Types = CGM.getTypes();
278 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000279 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000281 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000282 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000283 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000284 FunctionType::ExtInfo()),
285 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000286 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
287 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000288
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000289 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000290 llvm::Constant *getGcReadWeakFn() {
291 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000292 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000293 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000294 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000295 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000296 }
297
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000298 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000299 llvm::Constant *getGcAssignWeakFn() {
300 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000301 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000302 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000303 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000304 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
305 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000306
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000307 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000308 llvm::Constant *getGcAssignGlobalFn() {
309 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000310 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000311 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000312 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000313 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
314 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000315
Fariborz Jahanian217af242010-07-20 20:30:03 +0000316 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
317 llvm::Constant *getGcAssignThreadLocalFn() {
318 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000319 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000320 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000321 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
323 }
324
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000325 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000326 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000327 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000328 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
329 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000330 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000331 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000332 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
333 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000334
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000335 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
336 llvm::Constant *GcMemmoveCollectableFn() {
337 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000338 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000339 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
341 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000342
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000343 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000344 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000345 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000346 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000347 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000348 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
350 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000351
352 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000353 llvm::Constant *getExceptionThrowFn() {
354 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000355 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000356 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
359 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000360
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000361 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
362 llvm::Constant *getExceptionRethrowFn() {
363 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000364 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000365 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
366 }
367
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000368 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000369 llvm::Constant *getSyncEnterFn() {
370 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000371 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000372 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000373 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000374 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
375 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000376
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000377 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000378 llvm::Constant *getSyncExitFn() {
379 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000380 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000381 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000382 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000385
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
388 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000389
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
392 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000393
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000394 llvm::Constant *getSendStretFn(bool IsSuper) const {
395 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
396 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000397
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000398 llvm::Constant *getSendStretFn2(bool IsSuper) const {
399 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
400 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000401
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000402 llvm::Constant *getSendFpretFn(bool IsSuper) const {
403 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
404 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000405
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000406 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
407 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
408 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000409
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000410 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
411 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
412 }
413
414 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
415 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
416 }
417
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000418 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
419 ~ObjCCommonTypesHelper(){}
420};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000421
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000422/// ObjCTypesHelper - Helper class that encapsulates lazy
423/// construction of varies types used during ObjC generation.
424class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000425public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000426 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000427 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000428 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000429 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000430 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000431 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000432
Daniel Dunbarb036db82008-08-13 03:21:16 +0000433 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000434 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000435 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000436 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000437 /// ProtocolExtensionTy - LLVM type for struct
438 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000439 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000440 /// ProtocolExtensionTy - LLVM type for struct
441 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000442 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000443 /// MethodDescriptionTy - LLVM type for struct
444 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000445 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000446 /// MethodDescriptionListTy - LLVM type for struct
447 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000448 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000449 /// MethodDescriptionListPtrTy - LLVM type for struct
450 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000451 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000452 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000453 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000454 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000455 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000456 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000457 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000458 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000459 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000460 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000461 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000462 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000463 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000464 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000465 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000466 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000467 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000468 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000469 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000470 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000471 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000472 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000473 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000474 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000475 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000476
Anders Carlsson9ff22482008-09-09 10:10:21 +0000477 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000478 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000479
Anders Carlsson9ff22482008-09-09 10:10:21 +0000480 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000482 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000483 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000486 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000487
488 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000489 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000490 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000491 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000492 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000493 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000494 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000495
496 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000497 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000498 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000499 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000500 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000501 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000502 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000503
Anders Carlsson9ff22482008-09-09 10:10:21 +0000504 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000505 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000506 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000508 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000509 "objc_exception_match");
510
Chris Lattnerc6406db2009-04-22 02:26:14 +0000511 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000512
Anders Carlsson9ff22482008-09-09 10:10:21 +0000513 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000514 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000515 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000516 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000517 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
518 params, false),
519 "_setjmp");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000520 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000521
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000522public:
523 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000524 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000525};
526
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000527/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000528/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000529class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000530public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000532 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000533 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000534
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000535 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000536 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000537
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000538 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000539 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000541 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000542 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000543
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000544 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000545 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000546
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000547 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000548 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000549
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000550 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000551 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000552
Fariborz Jahanian71394042009-01-23 23:53:38 +0000553 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000554 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000555
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000556 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000557 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000558
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000559 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000560 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000561
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000562 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000563 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000564
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000565 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000566 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000567
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000568 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000569 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000570
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000571 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000572 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000574 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000576 // MessageRefTy - LLVM for:
577 // struct _message_ref_t {
578 // IMP messenger;
579 // SEL name;
580 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000581 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000582 // MessageRefCTy - clang type for struct _message_ref_t
583 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000584
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000585 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000586 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000587 // MessageRefCPtrTy - clang type for struct _message_ref_t*
588 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000589
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000590 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000591 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000592
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000593 // SuperMessageRefTy - LLVM for:
594 // struct _super_message_ref_t {
595 // SUPER_IMP messenger;
596 // SEL name;
597 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000598 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000599
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000600 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000601 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000602
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000603 llvm::Constant *getMessageSendFixupFn() {
604 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000605 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000606 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000607 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000608 "objc_msgSend_fixup");
609 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000610
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000611 llvm::Constant *getMessageSendFpretFixupFn() {
612 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000613 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000614 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000615 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000616 "objc_msgSend_fpret_fixup");
617 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000618
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000619 llvm::Constant *getMessageSendStretFixupFn() {
620 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000621 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000623 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000624 "objc_msgSend_stret_fixup");
625 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000627 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000628 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000629 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000630 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000631 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000632 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000633 "objc_msgSendSuper2_fixup");
634 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000635
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000636 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000638 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000639 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000640 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000641 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000642 "objc_msgSendSuper2_stret_fixup");
643 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000644
Chris Lattnera7c00b42009-04-22 02:15:23 +0000645 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000646 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000647 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000648
Chris Lattnera7c00b42009-04-22 02:15:23 +0000649 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000650
Chris Lattnera7c00b42009-04-22 02:15:23 +0000651 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000652 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000653 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000654 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000655 "objc_begin_catch");
656 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000657
Chris Lattnera5f58b02011-07-09 17:41:47 +0000658 llvm::StructType *EHTypeTy;
659 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000660
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000661 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
662 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000663};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000665class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000666public:
667 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000668 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000669 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000670 unsigned ivar_bytepos;
671 unsigned ivar_size;
672 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000673 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000674
675 // Allow sorting based on byte pos.
676 bool operator<(const GC_IVAR &b) const {
677 return ivar_bytepos < b.ivar_bytepos;
678 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000679 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000680
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000681 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000682 public:
683 unsigned skip;
684 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000685 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000686 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000687 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000688
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000689protected:
690 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000691 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000692 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000693 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000694
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000695 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000696 SmallVector<GC_IVAR, 16> SkipIvars;
697 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000698
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000699 /// LazySymbols - Symbols to generate a lazy reference for. See
700 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000701 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000702
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000703 /// DefinedSymbols - External symbols which are defined by this
704 /// module. The symbols in this list and LazySymbols are used to add
705 /// special linker symbols which ensure that Objective-C modules are
706 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000707 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000708
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000709 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000710 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000711
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000712 /// MethodVarNames - uniqued method variable names.
713 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000714
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000715 /// DefinedCategoryNames - list of category names in form Class_Category.
716 llvm::SetVector<std::string> DefinedCategoryNames;
717
Daniel Dunbarb036db82008-08-13 03:21:16 +0000718 /// MethodVarTypes - uniqued method type signatures. We have to use
719 /// a StringMap here because have no other unique reference.
720 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000721
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000722 /// MethodDefinitions - map of methods which have been defined in
723 /// this translation unit.
724 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000726 /// PropertyNames - uniqued method variable names.
727 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000729 /// ClassReferences - uniqued class references.
730 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000732 /// SelectorReferences - uniqued selector references.
733 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000734
Daniel Dunbarb036db82008-08-13 03:21:16 +0000735 /// Protocols - Protocols for which an objc_protocol structure has
736 /// been emitted. Forward declarations are handled by creating an
737 /// empty structure whose initializer is filled in when/if defined.
738 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000739
Daniel Dunbarc475d422008-10-29 22:36:39 +0000740 /// DefinedProtocols - Protocols which have actually been
741 /// defined. We should not need this, see FIXME in GenerateProtocol.
742 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000743
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000744 /// DefinedClasses - List of defined classes.
745 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000746
747 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
748 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000750 /// DefinedCategories - List of defined categories.
751 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000753 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
754 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000755
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000756 /// GetNameForMethod - Return a name for the given method.
757 /// \param[out] NameOut - The return value.
758 void GetNameForMethod(const ObjCMethodDecl *OMD,
759 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000760 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000762 /// GetMethodVarName - Return a unique constant for the given
763 /// selector's name. The return value has type char *.
764 llvm::Constant *GetMethodVarName(Selector Sel);
765 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000766
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000767 /// GetMethodVarType - Return a unique constant for the given
768 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000769
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000770 // FIXME: This is a horrible name.
771 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000772 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000773
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000774 /// GetPropertyName - Return a unique constant for the given
775 /// name. The return value has type char *.
776 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000777
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000778 // FIXME: This can be dropped once string functions are unified.
779 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
780 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000781
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000782 /// GetClassName - Return a unique constant for the given selector's
783 /// name. The return value has type char *.
784 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000785
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000786 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
787
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000788 /// BuildIvarLayout - Builds ivar layout bitmap for the class
789 /// implementation for the __strong or __weak case.
790 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000791 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
792 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000793
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000794 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000795
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000796 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797 unsigned int BytePos, bool ForStrongLayout,
798 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000799 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000800 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000801 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +0000802 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000803 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000804 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000805
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000806 /// GetIvarLayoutName - Returns a unique constant for the given
807 /// ivar layout bitmap.
808 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
809 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000810
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000811 /// EmitPropertyList - Emit the given property list. The return
812 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000813 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000814 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000815 const ObjCContainerDecl *OCD,
816 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000817
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000818 /// PushProtocolProperties - Push protocol's property on the input stack.
819 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
820 std::vector<llvm::Constant*> &Properties,
821 const Decl *Container,
822 const ObjCProtocolDecl *PROTO,
823 const ObjCCommonTypesHelper &ObjCTypes);
824
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000825 /// GetProtocolRef - Return a reference to the internal protocol
826 /// description, creating an empty one if it has not been
827 /// defined. The return value has type ProtocolPtrTy.
828 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000829
Daniel Dunbar30c65362009-03-09 20:09:19 +0000830 /// CreateMetadataVar - Create a global variable with internal
831 /// linkage for use by the Objective-C runtime.
832 ///
833 /// This is a convenience wrapper which not only creates the
834 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000835 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000836 ///
837 /// \param Name - The variable name.
838 /// \param Init - The variable initializer; this is also used to
839 /// define the type of the variable.
840 /// \param Section - The section the variable should go into, or 0.
841 /// \param Align - The alignment for the variable, or 0.
842 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000843 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000844 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000845 llvm::Constant *Init,
846 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000847 unsigned Align,
848 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000849
John McCall9e8bb002011-05-14 03:10:52 +0000850 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
851 ReturnValueSlot Return,
852 QualType ResultType,
853 llvm::Value *Sel,
854 llvm::Value *Arg0,
855 QualType Arg0Ty,
856 bool IsSuper,
857 const CallArgList &CallArgs,
858 const ObjCMethodDecl *OMD,
859 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000860
Daniel Dunbar5e639272010-04-25 20:39:01 +0000861 /// EmitImageInfo - Emit the image info marker used to encode some module
862 /// level information.
863 void EmitImageInfo();
864
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000865public:
Owen Andersonae86c192009-07-13 04:10:07 +0000866 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000867 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000868
David Chisnall481e3a82010-01-23 02:40:42 +0000869 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000870
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000871 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
872 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000873
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000874 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000875
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000876 /// GetOrEmitProtocol - Get the protocol object for the given
877 /// declaration, emitting it if necessary. The return value has type
878 /// ProtocolPtrTy.
879 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000880
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000881 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
882 /// object for the given declaration, emitting it if needed. These
883 /// forward references will be filled in with empty bodies if no
884 /// definition is seen. The return value has type ProtocolPtrTy.
885 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000886 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
887 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000888
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000889};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000891class CGObjCMac : public CGObjCCommonMac {
892private:
893 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000894
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000895 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000897 void EmitModuleInfo();
898
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000899 /// EmitModuleSymols - Emit module symbols, the list of defined
900 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000901 llvm::Constant *EmitModuleSymbols();
902
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000903 /// FinishModule - Write out global data structures at the end of
904 /// processing a translation unit.
905 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000906
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000907 /// EmitClassExtension - Generate the class extension structure used
908 /// to store the weak ivar layout and properties. The return value
909 /// has type ClassExtensionPtrTy.
910 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
911
912 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
913 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000914 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000915 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000916
John McCall31168b02011-06-15 23:02:42 +0000917 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
918 IdentifierInfo *II);
919
920 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
921
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000922 /// EmitSuperClassRef - Emits reference to class's main metadata class.
923 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000924
925 /// EmitIvarList - Emit the ivar list for the given
926 /// implementation. If ForClass is true the list of class ivars
927 /// (i.e. metaclass ivars) is emitted, otherwise the list of
928 /// interface ivars will be emitted. The return value has type
929 /// IvarListPtrTy.
930 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000931 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000932
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000933 /// EmitMetaClass - Emit a forward reference to the class structure
934 /// for the metaclass of the given interface. The return value has
935 /// type ClassPtrTy.
936 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
937
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000938 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000939 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000940 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
941 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000942 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000943
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000944 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000945
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000946 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000947
948 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000949 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000950 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000951 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000952 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000953
954 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000955 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000956 /// - TypeName: The name for the type containing the methods.
957 /// - IsProtocol: True iff these methods are for a protocol.
958 /// - ClassMethds: True iff these are class methods.
959 /// - Required: When true, only "required" methods are
960 /// listed. Similarly, when false only "optional" methods are
961 /// listed. For classes this should always be true.
962 /// - begin, end: The method list to output.
963 ///
964 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000965 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000966 const char *Section,
967 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000968
Daniel Dunbarc475d422008-10-29 22:36:39 +0000969 /// GetOrEmitProtocol - Get the protocol object for the given
970 /// declaration, emitting it if necessary. The return value has type
971 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000972 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000973
974 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
975 /// object for the given declaration, emitting it if needed. These
976 /// forward references will be filled in with empty bodies if no
977 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000978 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000979
Daniel Dunbarb036db82008-08-13 03:21:16 +0000980 /// EmitProtocolExtension - Generate the protocol extension
981 /// structure used to store optional instance and class methods, and
982 /// protocol properties. The return value has type
983 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000984 llvm::Constant *
985 EmitProtocolExtension(const ObjCProtocolDecl *PD,
986 const ConstantVector &OptInstanceMethods,
987 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000988
989 /// EmitProtocolList - Generate the list of referenced
990 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000991 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +0000992 ObjCProtocolDecl::protocol_iterator begin,
993 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000994
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000995 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
996 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000997 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
998 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000999
1000public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001001 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001002
Fariborz Jahanian71394042009-01-23 23:53:38 +00001003 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001004
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001005 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001006 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001007 QualType ResultType,
1008 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001009 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001010 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001011 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001012 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001013
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001014 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001015 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001016 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001017 QualType ResultType,
1018 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001019 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001020 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001021 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001022 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001023 const CallArgList &CallArgs,
1024 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001025
Daniel Dunbarcb463852008-11-01 01:53:16 +00001026 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001027 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001028
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001029 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1030 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001031
1032 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1033 /// untyped one.
1034 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1035 const ObjCMethodDecl *Method);
1036
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001037 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001038
Daniel Dunbar92992502008-08-15 22:20:32 +00001039 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001040
Daniel Dunbar92992502008-08-15 22:20:32 +00001041 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001042
Daniel Dunbarcb463852008-11-01 01:53:16 +00001043 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001044 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001045
Chris Lattnerd4808922009-03-22 21:03:39 +00001046 virtual llvm::Constant *GetPropertyGetFunction();
1047 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001048 virtual llvm::Constant *GetGetStructFunction();
1049 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001050 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001051
John McCallbd309292010-07-06 01:34:17 +00001052 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1053 const ObjCAtTryStmt &S);
1054 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1055 const ObjCAtSynchronizedStmt &S);
1056 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001057 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1058 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001059 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001060 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001061 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001062 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001063 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001064 llvm::Value *src, llvm::Value *dest,
1065 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001066 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001067 llvm::Value *src, llvm::Value *dest,
1068 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001069 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001071 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1072 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001073 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001074
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001075 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1076 QualType ObjectTy,
1077 llvm::Value *BaseValue,
1078 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001079 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001080 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001081 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001082 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001083
1084 /// GetClassGlobal - Return the global variable for the Objective-C
1085 /// class of the given name.
1086 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001087 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001088 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001089};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001090
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001091class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001092private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001093 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001094 llvm::GlobalVariable* ObjCEmptyCacheVar;
1095 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001096
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001097 /// SuperClassReferences - uniqued super class references.
1098 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001099
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001100 /// MetaClassReferences - uniqued meta class references.
1101 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001102
1103 /// EHTypeReferences - uniqued class ehtype references.
1104 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001105
John McCall9e8bb002011-05-14 03:10:52 +00001106 /// VTableDispatchMethods - List of methods for which we generate
1107 /// vtable-based message dispatch.
1108 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001109
Fariborz Jahanian67260552009-11-17 21:37:35 +00001110 /// DefinedMetaClasses - List of defined meta-classes.
1111 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1112
John McCall9e8bb002011-05-14 03:10:52 +00001113 /// isVTableDispatchedSelector - Returns true if SEL is a
1114 /// vtable-based selector.
1115 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001116
Fariborz Jahanian71394042009-01-23 23:53:38 +00001117 /// FinishNonFragileABIModule - Write out global data structures at the end of
1118 /// processing a translation unit.
1119 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001120
Daniel Dunbar19573e72009-05-15 21:48:48 +00001121 /// AddModuleClassList - Add the given list of class pointers to the
1122 /// module with the provided symbol and section names.
1123 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1124 const char *SymbolName,
1125 const char *SectionName);
1126
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001127 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1128 unsigned InstanceStart,
1129 unsigned InstanceSize,
1130 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001131 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001132 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001133 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001134 llvm::Constant *ClassRoGV,
1135 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001136
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001137 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001139 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001140
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001141 /// EmitMethodList - Emit the method list for the given
1142 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001143 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001144 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001145 const ConstantVector &Methods);
1146 /// EmitIvarList - Emit the ivar list for the given
1147 /// implementation. If ForClass is true the list of class ivars
1148 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1149 /// interface ivars will be emitted. The return value has type
1150 /// IvarListnfABIPtrTy.
1151 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001152
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001153 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001154 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001155 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001157 /// GetOrEmitProtocol - Get the protocol object for the given
1158 /// declaration, emitting it if necessary. The return value has type
1159 /// ProtocolPtrTy.
1160 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001162 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1163 /// object for the given declaration, emitting it if needed. These
1164 /// forward references will be filled in with empty bodies if no
1165 /// definition is seen. The return value has type ProtocolPtrTy.
1166 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001167
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001168 /// EmitProtocolList - Generate the list of referenced
1169 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001170 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001171 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001172 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001173
John McCall9e8bb002011-05-14 03:10:52 +00001174 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1175 ReturnValueSlot Return,
1176 QualType ResultType,
1177 Selector Sel,
1178 llvm::Value *Receiver,
1179 QualType Arg0Ty,
1180 bool IsSuper,
1181 const CallArgList &CallArgs,
1182 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001183
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001184 /// GetClassGlobal - Return the global variable for the Objective-C
1185 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001186 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001187
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001188 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001189 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001190 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001191 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001192
1193 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1194 IdentifierInfo *II);
1195
1196 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001197
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001198 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1199 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001200 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1201 const ObjCInterfaceDecl *ID);
1202
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001203 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1204 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001205 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001206 const ObjCInterfaceDecl *ID);
1207
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001208 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1209 /// the given ivar.
1210 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001211 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212 const ObjCInterfaceDecl *ID,
1213 const ObjCIvarDecl *Ivar);
1214
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001215 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1216 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001217 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1218 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001219
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001220 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001221 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001222 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001223 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001224
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001225 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001226 return "OBJC_METACLASS_$_";
1227 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001228
Daniel Dunbar15894b72009-04-07 05:48:37 +00001229 const char *getClassSymbolPrefix() const {
1230 return "OBJC_CLASS_$_";
1231 }
1232
Daniel Dunbar961202372009-05-03 12:57:56 +00001233 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001234 uint32_t &InstanceStart,
1235 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001236
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001237 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001238 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001239 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1240 return CGM.getContext().Selectors.getSelector(0, &II);
1241 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001243 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001244 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1245 return CGM.getContext().Selectors.getSelector(1, &II);
1246 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001247
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001248 /// ImplementationIsNonLazy - Check whether the given category or
1249 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001250 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001251
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001252public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001253 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001254 // FIXME. All stubs for now!
1255 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
Fariborz Jahanian71394042009-01-23 23:53:38 +00001257 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001258 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001259 QualType ResultType,
1260 Selector Sel,
1261 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001262 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001263 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001264 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001265
1266 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001267 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001268 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001269 QualType ResultType,
1270 Selector Sel,
1271 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001272 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001273 llvm::Value *Receiver,
1274 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001275 const CallArgList &CallArgs,
1276 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001277
Fariborz Jahanian71394042009-01-23 23:53:38 +00001278 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001279 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001280
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001281 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1282 bool lvalue = false)
1283 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001284
1285 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1286 /// untyped one.
1287 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1288 const ObjCMethodDecl *Method)
1289 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001290
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001291 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292
Fariborz Jahanian71394042009-01-23 23:53:38 +00001293 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001294 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001295 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001296
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001297 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001298
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001300 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001301 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302 virtual llvm::Constant *GetPropertySetFunction() {
1303 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001304 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001305
David Chisnall168b80f2010-12-26 22:13:16 +00001306 virtual llvm::Constant *GetSetStructFunction() {
1307 return ObjCTypes.getCopyStructFn();
1308 }
1309 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001310 return ObjCTypes.getCopyStructFn();
1311 }
1312
Chris Lattnerd4808922009-03-22 21:03:39 +00001313 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001314 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001315 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001316
John McCallbd309292010-07-06 01:34:17 +00001317 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1318 const ObjCAtTryStmt &S);
1319 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1320 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001321 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001322 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001323 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001324 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001325 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001326 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001327 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001328 llvm::Value *src, llvm::Value *dest,
1329 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001330 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001331 llvm::Value *src, llvm::Value *dest,
1332 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001333 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001334 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001335 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1336 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001337 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001338 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1339 QualType ObjectTy,
1340 llvm::Value *BaseValue,
1341 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001342 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001343 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001344 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001345 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001346};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001347
John McCall5880fb82011-05-14 21:12:11 +00001348/// A helper class for performing the null-initialization of a return
1349/// value.
1350struct NullReturnState {
1351 llvm::BasicBlock *NullBB;
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001352 llvm::BasicBlock *callBB;
1353 NullReturnState() : NullBB(0), callBB(0) {}
John McCall5880fb82011-05-14 21:12:11 +00001354
1355 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1356 // Make blocks for the null-init and call edges.
1357 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001358 callBB = CGF.createBasicBlock("msgSend.call");
John McCall5880fb82011-05-14 21:12:11 +00001359
1360 // Check for a null receiver and, if there is one, jump to the
1361 // null-init test.
1362 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1363 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1364
1365 // Otherwise, start performing the call.
1366 CGF.EmitBlock(callBB);
1367 }
1368
1369 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1370 if (!NullBB) return result;
1371
1372 // Finish the call path.
1373 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1374 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1375
1376 // Emit the null-init block and perform the null-initialization there.
1377 CGF.EmitBlock(NullBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001378 if (!resultType->isAnyComplexType()) {
1379 assert(result.isAggregate() && "null init of non-aggregate result?");
1380 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1381 // Jump to the continuation block.
1382 CGF.EmitBlock(contBB);
1383 return result;
1384 }
John McCall5880fb82011-05-14 21:12:11 +00001385
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001386 // _Complex type
1387 // FIXME. Now easy to handle any other scalar type whose result is returned
1388 // in memory due to ABI limitations.
John McCall5880fb82011-05-14 21:12:11 +00001389 CGF.EmitBlock(contBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001390 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1391 llvm::Type *MemberType = CallCV.first->getType();
1392 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1393 // Create phi instruction for scalar complex value.
1394 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1395 PHIReal->addIncoming(ZeroCV, NullBB);
1396 PHIReal->addIncoming(CallCV.first, callBB);
1397 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1398 PHIImag->addIncoming(ZeroCV, NullBB);
1399 PHIImag->addIncoming(CallCV.second, callBB);
1400 return RValue::getComplex(PHIReal, PHIImag);
John McCall5880fb82011-05-14 21:12:11 +00001401 }
1402};
1403
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001404} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001405
1406/* *** Helper Functions *** */
1407
1408/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001409static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001410 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001411 unsigned idx0,
1412 unsigned idx1) {
1413 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001414 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1415 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001416 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001417 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001418}
1419
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001420/// hasObjCExceptionAttribute - Return true if this class or any super
1421/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001422static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001423 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001424 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001425 return true;
1426 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001427 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001428 return false;
1429}
1430
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001431/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001432
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001433CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001434 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001435 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001436 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001437}
1438
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001439/// GetClass - Return a reference to the class for the given interface
1440/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001441llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001442 const ObjCInterfaceDecl *ID) {
1443 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001444}
1445
1446/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001447llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1448 bool lval) {
1449 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001450}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001451llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001452 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001453 return EmitSelector(Builder, Method->getSelector());
1454}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001455
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001456llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001457 if (T->isObjCIdType() ||
1458 T->isObjCQualifiedIdType()) {
1459 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001460 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001461 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001462 if (T->isObjCClassType() ||
1463 T->isObjCQualifiedClassType()) {
1464 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001465 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001466 }
1467 if (T->isObjCObjectPointerType())
1468 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1469
John McCall2ca705e2010-07-24 00:37:23 +00001470 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1471 return 0;
1472}
1473
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001474/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001475/*
1476 struct __builtin_CFString {
1477 const int *isa; // point to __CFConstantStringClassReference
1478 int flags;
1479 const char *str;
1480 long length;
1481 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001482*/
1483
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001484/// or Generate a constant NSString object.
1485/*
1486 struct __builtin_NSString {
1487 const int *isa; // point to __NSConstantStringClassReference
1488 const char *str;
1489 unsigned int length;
1490 };
1491*/
1492
Fariborz Jahanian71394042009-01-23 23:53:38 +00001493llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001494 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001495 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1496 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001497 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001498}
1499
1500/// Generates a message send where the super is the receiver. This is
1501/// a message send to self with special delivery semantics indicating
1502/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001503CodeGen::RValue
1504CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001505 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001506 QualType ResultType,
1507 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001508 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001509 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001510 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001511 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001512 const CodeGen::CallArgList &CallArgs,
1513 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001514 // Create and init a super structure; this is a (receiver, class)
1515 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001516 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001517 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001518 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001519 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001520 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001521 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001522
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001523 // If this is a class message the metaclass is passed as the target.
1524 llvm::Value *Target;
1525 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001526 if (isCategoryImpl) {
1527 // Message sent to 'super' in a class method defined in a category
1528 // implementation requires an odd treatment.
1529 // If we are in a class method, we must retrieve the
1530 // _metaclass_ for the current class, pointed at by
1531 // the class's "isa" pointer. The following assumes that
1532 // isa" is the first ivar in a class (which it must be).
1533 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1534 Target = CGF.Builder.CreateStructGEP(Target, 0);
1535 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001536 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001537 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1538 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1539 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1540 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001541 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001542 }
1543 else if (isCategoryImpl)
1544 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1545 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001546 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1547 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1548 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001549 }
Mike Stump18bb9282009-05-16 07:57:57 +00001550 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1551 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001552 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001553 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001554 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001555 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001556 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001557 return EmitMessageSend(CGF, Return, ResultType,
1558 EmitSelector(CGF.Builder, Sel),
1559 ObjCSuper, ObjCTypes.SuperPtrCTy,
1560 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001561}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001562
1563/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001564CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001565 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001566 QualType ResultType,
1567 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001568 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001569 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001570 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001571 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001572 return EmitMessageSend(CGF, Return, ResultType,
1573 EmitSelector(CGF.Builder, Sel),
1574 Receiver, CGF.getContext().getObjCIdType(),
1575 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001576}
1577
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001578CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001579CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1580 ReturnValueSlot Return,
1581 QualType ResultType,
1582 llvm::Value *Sel,
1583 llvm::Value *Arg0,
1584 QualType Arg0Ty,
1585 bool IsSuper,
1586 const CallArgList &CallArgs,
1587 const ObjCMethodDecl *Method,
1588 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001589 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001590 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001591 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001592 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1593 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001594 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001595
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001596 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001597 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001598 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001599 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001600 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001601
Anders Carlsson280e61f12010-06-21 20:59:55 +00001602 if (Method)
1603 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1604 CGM.getContext().getCanonicalType(ResultType) &&
1605 "Result type mismatch!");
1606
John McCall5880fb82011-05-14 21:12:11 +00001607 NullReturnState nullReturn;
1608
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001609 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001610 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001611 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001612 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001613 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001614 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1615 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1616 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001617 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1618 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1619 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001620 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001621 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001622 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001623 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001624 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001625 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1626 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001627}
1628
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001629static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1630 if (FQT.isObjCGCStrong())
1631 return Qualifiers::Strong;
1632
John McCall31168b02011-06-15 23:02:42 +00001633 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001634 return Qualifiers::Weak;
1635
1636 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1637 return Qualifiers::Strong;
1638
1639 if (const PointerType *PT = FQT->getAs<PointerType>())
1640 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1641
1642 return Qualifiers::GCNone;
1643}
1644
John McCall351762c2011-02-07 10:33:21 +00001645llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1646 const CGBlockInfo &blockInfo) {
1647 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001648 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001649
Douglas Gregor79a91412011-09-13 17:21:33 +00001650 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001651 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001652 return nullPtr;
1653
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001654 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001655 SkipIvars.clear();
1656 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001657 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1658 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001659
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001660 // __isa is the first field in block descriptor and must assume by runtime's
1661 // convention that it is GC'able.
1662 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001663
1664 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1665
1666 // Calculate the basic layout of the block structure.
1667 const llvm::StructLayout *layout =
1668 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1669
1670 // Ignore the optional 'this' capture: C++ objects are not assumed
1671 // to be GC'ed.
1672
1673 // Walk the captured variables.
1674 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1675 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1676 const VarDecl *variable = ci->getVariable();
1677 QualType type = variable->getType();
1678
1679 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1680
1681 // Ignore constant captures.
1682 if (capture.isConstant()) continue;
1683
1684 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1685
1686 // __block variables are passed by their descriptor address.
1687 if (ci->isByRef()) {
1688 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001689 continue;
John McCall351762c2011-02-07 10:33:21 +00001690 }
1691
1692 assert(!type->isArrayType() && "array variable should not be caught");
1693 if (const RecordType *record = type->getAs<RecordType>()) {
1694 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001695 continue;
1696 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001697
John McCall351762c2011-02-07 10:33:21 +00001698 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1699 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1700
1701 if (GCAttr == Qualifiers::Strong)
1702 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1703 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001704 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001705 SkipIvars.push_back(GC_IVAR(fieldOffset,
1706 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001707 }
1708
1709 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001710 return nullPtr;
1711
1712 // Sort on byte position; captures might not be allocated in order,
1713 // and unions can do funny things.
1714 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1715 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001716
1717 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001718 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001719 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1720 printf("\n block variable layout for block: ");
1721 const unsigned char *s = (unsigned char*)BitMap.c_str();
1722 for (unsigned i = 0; i < BitMap.size(); i++)
1723 if (!(s[i] & 0xf0))
1724 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1725 else
1726 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1727 printf("\n");
1728 }
1729
1730 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001731}
1732
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001733llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001734 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001735 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001736 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001737 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1738
Owen Andersonade90fd2009-07-29 18:54:39 +00001739 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001740 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001741}
1742
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001743void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001744 // FIXME: We shouldn't need this, the protocol decl should contain enough
1745 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001746 DefinedProtocols.insert(PD->getIdentifier());
1747
1748 // If we have generated a forward reference to this protocol, emit
1749 // it now. Otherwise do nothing, the protocol objects are lazily
1750 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001751 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001752 GetOrEmitProtocol(PD);
1753}
1754
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001755llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001756 if (DefinedProtocols.count(PD->getIdentifier()))
1757 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001758
Daniel Dunbarc475d422008-10-29 22:36:39 +00001759 return GetOrEmitProtocolRef(PD);
1760}
1761
Daniel Dunbarb036db82008-08-13 03:21:16 +00001762/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001763// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1764struct _objc_protocol {
1765struct _objc_protocol_extension *isa;
1766char *protocol_name;
1767struct _objc_protocol_list *protocol_list;
1768struct _objc__method_prototype_list *instance_methods;
1769struct _objc__method_prototype_list *class_methods
1770};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001771
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001772See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001773*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001774llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1775 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1776
1777 // Early exit if a defining object has already been generated.
1778 if (Entry && Entry->hasInitializer())
1779 return Entry;
1780
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001781 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001782 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001783 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1784
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001785 // Construct method lists.
1786 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1787 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001788 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001789 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001790 ObjCMethodDecl *MD = *i;
1791 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001792 if (!C)
1793 return GetOrEmitProtocolRef(PD);
1794
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001795 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1796 OptInstanceMethods.push_back(C);
1797 } else {
1798 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001799 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001800 }
1801
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001802 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001803 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001804 ObjCMethodDecl *MD = *i;
1805 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001806 if (!C)
1807 return GetOrEmitProtocolRef(PD);
1808
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001809 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1810 OptClassMethods.push_back(C);
1811 } else {
1812 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001813 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001814 }
1815
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001816 llvm::Constant *Values[] = {
1817 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods),
1818 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001819 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001820 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001821 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001822 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001823 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001824 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001825 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001826 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001827 ClassMethods)
1828 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001829 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001830 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001831
Daniel Dunbarb036db82008-08-13 03:21:16 +00001832 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001833 // Already created, fix the linkage and update the initializer.
1834 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001835 Entry->setInitializer(Init);
1836 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001837 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001838 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001839 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001840 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001841 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001842 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843 // FIXME: Is this necessary? Why only for protocol?
1844 Entry->setAlignment(4);
1845 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001846 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001847
1848 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001849}
1850
Daniel Dunbarc475d422008-10-29 22:36:39 +00001851llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001852 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1853
1854 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001855 // We use the initializer as a marker of whether this is a forward
1856 // reference or not. At module finalization we add the empty
1857 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001858 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001859 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001860 llvm::GlobalValue::ExternalLinkage,
1861 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001862 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001863 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001864 // FIXME: Is this necessary? Why only for protocol?
1865 Entry->setAlignment(4);
1866 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001867
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868 return Entry;
1869}
1870
1871/*
1872 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001873 uint32_t size;
1874 struct objc_method_description_list *optional_instance_methods;
1875 struct objc_method_description_list *optional_class_methods;
1876 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001877 };
1878*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001879llvm::Constant *
1880CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1881 const ConstantVector &OptInstanceMethods,
1882 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001883 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001884 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001885 llvm::Constant *Values[] = {
1886 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001888 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001889 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001890 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001891 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001892 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001893 OptClassMethods),
1894 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
1895 ObjCTypes)
1896 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00001897
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001898 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001899 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001900 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001901 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001902
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001903 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001904 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001905
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001906 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001907 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001908 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001909 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001910}
1911
1912/*
1913 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001914 struct objc_protocol_list *next;
1915 long count;
1916 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001917 };
1918*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001919llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001920CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001921 ObjCProtocolDecl::protocol_iterator begin,
1922 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923 std::vector<llvm::Constant*> ProtocolRefs;
1924
Daniel Dunbardec75f82008-08-21 21:57:41 +00001925 for (; begin != end; ++begin)
1926 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001927
1928 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001929 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001930 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001931
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001932 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001933 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001934
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001935 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001936 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001937 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001938 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001939 ProtocolRefs.size() - 1);
1940 Values[2] =
1941 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1942 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001943 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001944
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001945 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001946 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001947 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001948 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001949 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001950}
1951
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001952void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1953 std::vector<llvm::Constant*> &Properties,
1954 const Decl *Container,
1955 const ObjCProtocolDecl *PROTO,
1956 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001957 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1958 E = PROTO->protocol_end(); P != E; ++P)
1959 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1960 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1961 E = PROTO->prop_end(); I != E; ++I) {
1962 const ObjCPropertyDecl *PD = *I;
1963 if (!PropertySet.insert(PD->getIdentifier()))
1964 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001965 llvm::Constant *Prop[] = {
1966 GetPropertyName(PD->getIdentifier()),
1967 GetPropertyTypeString(PD, Container)
1968 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001969 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1970 }
1971}
1972
Daniel Dunbarb036db82008-08-13 03:21:16 +00001973/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001974 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001975 const char * const name;
1976 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001977 };
1978
1979 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001980 uint32_t entsize; // sizeof (struct _objc_property)
1981 uint32_t prop_count;
1982 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001983 };
1984*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001985llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986 const Decl *Container,
1987 const ObjCContainerDecl *OCD,
1988 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001989 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001990 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001991 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1992 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001993 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001994 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001995 llvm::Constant *Prop[] = {
1996 GetPropertyName(PD->getIdentifier()),
1997 GetPropertyTypeString(PD, Container)
1998 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001999 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002000 Prop));
2001 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002002 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002003 for (ObjCInterfaceDecl::all_protocol_iterator
2004 P = OID->all_referenced_protocol_begin(),
2005 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002006 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2007 ObjCTypes);
2008 }
2009 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2010 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2011 E = CD->protocol_end(); P != E; ++P)
2012 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2013 ObjCTypes);
2014 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002015
2016 // Return null for empty list.
2017 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002018 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002019
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002020 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002021 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002022 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002023 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2024 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002026 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002027 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002028 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002029
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002030 llvm::GlobalVariable *GV =
2031 CreateMetadataVar(Name, Init,
2032 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002033 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002034 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002035 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002036 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002037}
2038
2039/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002040 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002041 int count;
2042 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002043 };
2044*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002045llvm::Constant *
2046CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002047 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002048 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002049 ObjCTypes.SelectorPtrTy),
2050 GetMethodVarType(MD)
2051 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002052 if (!Desc[1])
2053 return 0;
2054
Owen Anderson0e0189d2009-07-27 22:29:56 +00002055 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002056 Desc);
2057}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002058
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002059llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002060 const char *Section,
2061 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002062 // Return null for empty list.
2063 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002064 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002065
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002066 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002067 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002068 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002069 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002070 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002071 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002072
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002073 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002074 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002075 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002076}
2077
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002078/*
2079 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002080 char *category_name;
2081 char *class_name;
2082 struct _objc_method_list *instance_methods;
2083 struct _objc_method_list *class_methods;
2084 struct _objc_protocol_list *protocols;
2085 uint32_t size; // <rdar://4585769>
2086 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002087 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002088*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002089void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002090 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002091
Mike Stump18bb9282009-05-16 07:57:57 +00002092 // FIXME: This is poor design, the OCD should have a pointer to the category
2093 // decl. Additionally, note that Category can be null for the @implementation
2094 // w/o an @interface case. Sema should just create one for us as it does for
2095 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002096 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002097 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002098 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002099
2100 llvm::SmallString<256> ExtName;
2101 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2102 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002103
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002104 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002105 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002106 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002107 // Instance methods should always be defined.
2108 InstanceMethods.push_back(GetMethodConstant(*i));
2109 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002110 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002111 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002112 // Class methods should always be defined.
2113 ClassMethods.push_back(GetMethodConstant(*i));
2114 }
2115
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002116 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002117 Values[0] = GetClassName(OCD->getIdentifier());
2118 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002119 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002120 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002121 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002122 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002123 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002124 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002125 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002126 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002127 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002128 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002129 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002130 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002131 Category->protocol_begin(),
2132 Category->protocol_end());
2133 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002134 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002135 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002136 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002137
2138 // If there is no category @interface then there can be no properties.
2139 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002140 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002141 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002142 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002143 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002144 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002145
Owen Anderson0e0189d2009-07-27 22:29:56 +00002146 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002147 Values);
2148
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002149 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002150 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002151 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002152 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002153 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002154 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002155 // method definition entries must be clear for next implementation.
2156 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002157}
2158
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002159// FIXME: Get from somewhere?
2160enum ClassFlags {
2161 eClassFlags_Factory = 0x00001,
2162 eClassFlags_Meta = 0x00002,
2163 // <rdr://5142207>
2164 eClassFlags_HasCXXStructors = 0x02000,
2165 eClassFlags_Hidden = 0x20000,
2166 eClassFlags_ABI2_Hidden = 0x00010,
2167 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2168};
2169
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002170/*
2171 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002172 Class isa;
2173 Class super_class;
2174 const char *name;
2175 long version;
2176 long info;
2177 long instance_size;
2178 struct _objc_ivar_list *ivars;
2179 struct _objc_method_list *methods;
2180 struct _objc_cache *cache;
2181 struct _objc_protocol_list *protocols;
2182 // Objective-C 1.0 extensions (<rdr://4585769>)
2183 const char *ivar_layout;
2184 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002185 };
2186
2187 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002188*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002189void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002190 DefinedSymbols.insert(ID->getIdentifier());
2191
Chris Lattner86d7d912008-11-24 03:54:41 +00002192 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002193 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002194 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002195 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002196 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002197 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002198 Interface->all_referenced_protocol_begin(),
2199 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002200 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002201 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002202 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002203 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002204 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002205
2206 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002207 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208 Flags |= eClassFlags_Hidden;
2209
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002210 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002211 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002212 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002213 // Instance methods should always be defined.
2214 InstanceMethods.push_back(GetMethodConstant(*i));
2215 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002216 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002217 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002218 // Class methods should always be defined.
2219 ClassMethods.push_back(GetMethodConstant(*i));
2220 }
2221
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002222 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002223 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002224 ObjCPropertyImplDecl *PID = *i;
2225
2226 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2227 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2228
2229 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2230 if (llvm::Constant *C = GetMethodConstant(MD))
2231 InstanceMethods.push_back(C);
2232 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2233 if (llvm::Constant *C = GetMethodConstant(MD))
2234 InstanceMethods.push_back(C);
2235 }
2236 }
2237
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002238 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002239 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002240 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002241 // Record a reference to the super class.
2242 LazySymbols.insert(Super->getIdentifier());
2243
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002244 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002245 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002246 ObjCTypes.ClassPtrTy);
2247 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002248 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002249 }
2250 Values[ 2] = GetClassName(ID->getIdentifier());
2251 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002252 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2253 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2254 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002255 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002257 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002258 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002259 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002260 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002261 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002262 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002263 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002264 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002265 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002266 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002267 std::string Name("\01L_OBJC_CLASS_");
2268 Name += ClassName;
2269 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2270 // Check for a forward reference.
2271 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2272 if (GV) {
2273 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2274 "Forward metaclass reference has incorrect type.");
2275 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2276 GV->setInitializer(Init);
2277 GV->setSection(Section);
2278 GV->setAlignment(4);
2279 CGM.AddUsedGlobal(GV);
2280 }
2281 else
2282 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002284 // method definition entries must be clear for next implementation.
2285 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002286}
2287
2288llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2289 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002290 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002291 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002292 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002293
John McCall457a04e2010-10-22 21:05:15 +00002294 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002295 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002296
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002297 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002298 // The isa for the metaclass is the root of the hierarchy.
2299 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2300 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2301 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002302 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002303 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002304 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002305 // The super class for the metaclass is emitted as the name of the
2306 // super class. The runtime fixes this up to point to the
2307 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002309 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002310 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002311 ObjCTypes.ClassPtrTy);
2312 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002313 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314 }
2315 Values[ 2] = GetClassName(ID->getIdentifier());
2316 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002317 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2318 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2319 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002320 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002321 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002322 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002323 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002324 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002325 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002326 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002327 Values[ 9] = Protocols;
2328 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002329 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002330 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002331 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002332 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002333 Values);
2334
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002335 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002336 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002337
2338 // Check for a forward reference.
2339 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2340 if (GV) {
2341 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2342 "Forward metaclass reference has incorrect type.");
2343 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2344 GV->setInitializer(Init);
2345 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002346 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002347 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002348 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002349 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002350 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002351 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002352 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002353
2354 return GV;
2355}
2356
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002357llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002358 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002359
Mike Stump18bb9282009-05-16 07:57:57 +00002360 // FIXME: Should we look these up somewhere other than the module. Its a bit
2361 // silly since we only generate these while processing an implementation, so
2362 // exactly one pointer would work if know when we entered/exitted an
2363 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002364
2365 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002366 // Previously, metaclass with internal linkage may have been defined.
2367 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002368 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2369 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002370 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2371 "Forward metaclass reference has incorrect type.");
2372 return GV;
2373 } else {
2374 // Generate as an external reference to keep a consistent
2375 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002376 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002377 llvm::GlobalValue::ExternalLinkage,
2378 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002379 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002380 }
2381}
2382
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002383llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2384 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2385
2386 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2387 true)) {
2388 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2389 "Forward class metadata reference has incorrect type.");
2390 return GV;
2391 } else {
2392 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2393 llvm::GlobalValue::ExternalLinkage,
2394 0,
2395 Name);
2396 }
2397}
2398
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002399/*
2400 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002401 uint32_t size;
2402 const char *weak_ivar_layout;
2403 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002404 };
2405*/
2406llvm::Constant *
2407CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002408 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002409 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002410
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002411 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002412 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002413 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002414 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002415 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002416
2417 // Return null if no extension bits are used.
2418 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002419 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002421 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002422 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002423 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002424 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002425 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002426}
2427
2428/*
2429 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002430 char *ivar_name;
2431 char *ivar_type;
2432 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002433 };
2434
2435 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002436 int ivar_count;
2437 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002438 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002439*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002440llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002441 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002442 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443
2444 // When emitting the root class GCC emits ivar entries for the
2445 // actual class structure. It is not clear if we need to follow this
2446 // behavior; for now lets try and get away with not doing it. If so,
2447 // the cleanest solution would be to make up an ObjCInterfaceDecl
2448 // for the class.
2449 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002450 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002451
Jordy Rosea91768e2011-07-22 02:08:32 +00002452 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002453
Jordy Rosea91768e2011-07-22 02:08:32 +00002454 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002455 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002456 // Ignore unnamed bit-fields.
2457 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002458 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002459 llvm::Constant *Ivar[] = {
2460 GetMethodVarName(IVD->getIdentifier()),
2461 GetMethodVarType(IVD),
2462 llvm::ConstantInt::get(ObjCTypes.IntTy,
2463 ComputeIvarBaseOffset(CGM, OID, IVD))
2464 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002465 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002466 }
2467
2468 // Return null for empty list.
2469 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002470 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002471
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002472 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002473 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002474 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002475 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002476 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002477 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002478
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002479 llvm::GlobalVariable *GV;
2480 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002481 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002482 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002483 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002484 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002485 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002486 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002487 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002488 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002489}
2490
2491/*
2492 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002493 SEL method_name;
2494 char *method_types;
2495 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002496 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002497
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002498 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002499 struct objc_method_list *obsolete;
2500 int count;
2501 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002502 };
2503*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002504
2505/// GetMethodConstant - Return a struct objc_method constant for the
2506/// given method if it has been defined. The result is null if the
2507/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002508llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002509 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002510 if (!Fn)
2511 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002512
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002513 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002514 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002515 ObjCTypes.SelectorPtrTy),
2516 GetMethodVarType(MD),
2517 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2518 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002519 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002520}
2521
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002522llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002523 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002524 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002525 // Return null for empty list.
2526 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002527 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002528
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002529 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002530 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002531 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002532 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002533 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002534 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002535 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002536
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002537 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002538 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002539}
2540
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002541llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002542 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002543 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002544 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002545
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002546 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002547 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002548 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002549 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002550 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002551 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002552 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002553 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002554 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002555
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002556 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002557}
2558
Daniel Dunbar30c65362009-03-09 20:09:19 +00002559llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002560CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002561 llvm::Constant *Init,
2562 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002563 unsigned Align,
2564 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002565 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002566 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002567 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002568 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002569 if (Section)
2570 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002571 if (Align)
2572 GV->setAlignment(Align);
2573 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002574 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002575 return GV;
2576}
2577
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002578llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002579 // Abuse this interface function as a place to finalize.
2580 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002581 return NULL;
2582}
2583
Chris Lattnerd4808922009-03-22 21:03:39 +00002584llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002585 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002586}
2587
Chris Lattnerd4808922009-03-22 21:03:39 +00002588llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002589 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002590}
2591
David Chisnall168b80f2010-12-26 22:13:16 +00002592llvm::Constant *CGObjCMac::GetGetStructFunction() {
2593 return ObjCTypes.getCopyStructFn();
2594}
2595llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002596 return ObjCTypes.getCopyStructFn();
2597}
2598
Chris Lattnerd4808922009-03-22 21:03:39 +00002599llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002600 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002601}
2602
John McCallbd309292010-07-06 01:34:17 +00002603void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2604 return EmitTryOrSynchronizedStmt(CGF, S);
2605}
2606
2607void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2608 const ObjCAtSynchronizedStmt &S) {
2609 return EmitTryOrSynchronizedStmt(CGF, S);
2610}
2611
John McCall65bea082010-07-21 06:59:36 +00002612namespace {
John McCallcda666c2010-07-21 07:22:38 +00002613 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002614 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002615 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002616 llvm::Value *CallTryExitVar;
2617 llvm::Value *ExceptionData;
2618 ObjCTypesHelper &ObjCTypes;
2619 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002620 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002621 llvm::Value *CallTryExitVar,
2622 llvm::Value *ExceptionData,
2623 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002624 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002625 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2626
John McCall30317fd2011-07-12 20:27:29 +00002627 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002628 // Check whether we need to call objc_exception_try_exit.
2629 // In optimized code, this branch will always be folded.
2630 llvm::BasicBlock *FinallyCallExit =
2631 CGF.createBasicBlock("finally.call_exit");
2632 llvm::BasicBlock *FinallyNoCallExit =
2633 CGF.createBasicBlock("finally.no_call_exit");
2634 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2635 FinallyCallExit, FinallyNoCallExit);
2636
2637 CGF.EmitBlock(FinallyCallExit);
2638 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2639 ->setDoesNotThrow();
2640
2641 CGF.EmitBlock(FinallyNoCallExit);
2642
2643 if (isa<ObjCAtTryStmt>(S)) {
2644 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002645 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2646 // Save the current cleanup destination in case there's
2647 // control flow inside the finally statement.
2648 llvm::Value *CurCleanupDest =
2649 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2650
John McCall65bea082010-07-21 06:59:36 +00002651 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2652
John McCallcebe0ca2010-08-11 00:16:14 +00002653 if (CGF.HaveInsertPoint()) {
2654 CGF.Builder.CreateStore(CurCleanupDest,
2655 CGF.getNormalCleanupDestSlot());
2656 } else {
2657 // Currently, the end of the cleanup must always exist.
2658 CGF.EnsureInsertPoint();
2659 }
2660 }
John McCall65bea082010-07-21 06:59:36 +00002661 } else {
2662 // Emit objc_sync_exit(expr); as finally's sole statement for
2663 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002664 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002665 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2666 ->setDoesNotThrow();
2667 }
2668 }
2669 };
John McCall42227ed2010-07-31 23:20:56 +00002670
2671 class FragileHazards {
2672 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002673 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002674 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2675
2676 llvm::InlineAsm *ReadHazard;
2677 llvm::InlineAsm *WriteHazard;
2678
2679 llvm::FunctionType *GetAsmFnType();
2680
2681 void collectLocals();
2682 void emitReadHazard(CGBuilderTy &Builder);
2683
2684 public:
2685 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002686
John McCall42227ed2010-07-31 23:20:56 +00002687 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002688 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002689 };
2690}
2691
2692/// Create the fragile-ABI read and write hazards based on the current
2693/// state of the function, which is presumed to be immediately prior
2694/// to a @try block. These hazards are used to maintain correct
2695/// semantics in the face of optimization and the fragile ABI's
2696/// cavalier use of setjmp/longjmp.
2697FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2698 collectLocals();
2699
2700 if (Locals.empty()) return;
2701
2702 // Collect all the blocks in the function.
2703 for (llvm::Function::iterator
2704 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2705 BlocksBeforeTry.insert(&*I);
2706
2707 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2708
2709 // Create a read hazard for the allocas. This inhibits dead-store
2710 // optimizations and forces the values to memory. This hazard is
2711 // inserted before any 'throwing' calls in the protected scope to
2712 // reflect the possibility that the variables might be read from the
2713 // catch block if the call throws.
2714 {
2715 std::string Constraint;
2716 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2717 if (I) Constraint += ',';
2718 Constraint += "*m";
2719 }
2720
2721 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2722 }
2723
2724 // Create a write hazard for the allocas. This inhibits folding
2725 // loads across the hazard. This hazard is inserted at the
2726 // beginning of the catch path to reflect the possibility that the
2727 // variables might have been written within the protected scope.
2728 {
2729 std::string Constraint;
2730 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2731 if (I) Constraint += ',';
2732 Constraint += "=*m";
2733 }
2734
2735 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2736 }
2737}
2738
2739/// Emit a write hazard at the current location.
2740void FragileHazards::emitWriteHazard() {
2741 if (Locals.empty()) return;
2742
Jay Foad5bd375a2011-07-15 08:37:34 +00002743 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002744}
2745
John McCall42227ed2010-07-31 23:20:56 +00002746void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2747 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002748 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002749}
2750
2751/// Emit read hazards in all the protected blocks, i.e. all the blocks
2752/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002753void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002754 if (Locals.empty()) return;
2755
2756 CGBuilderTy Builder(CGF.getLLVMContext());
2757
2758 // Iterate through all blocks, skipping those prior to the try.
2759 for (llvm::Function::iterator
2760 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2761 llvm::BasicBlock &BB = *FI;
2762 if (BlocksBeforeTry.count(&BB)) continue;
2763
2764 // Walk through all the calls in the block.
2765 for (llvm::BasicBlock::iterator
2766 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2767 llvm::Instruction &I = *BI;
2768
2769 // Ignore instructions that aren't non-intrinsic calls.
2770 // These are the only calls that can possibly call longjmp.
2771 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2772 if (isa<llvm::IntrinsicInst>(I))
2773 continue;
2774
2775 // Ignore call sites marked nounwind. This may be questionable,
2776 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2777 llvm::CallSite CS(&I);
2778 if (CS.doesNotThrow()) continue;
2779
John McCall2dd7d442010-08-04 05:59:32 +00002780 // Insert a read hazard before the call. This will ensure that
2781 // any writes to the locals are performed before making the
2782 // call. If the call throws, then this is sufficient to
2783 // guarantee correctness as long as it doesn't also write to any
2784 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002785 Builder.SetInsertPoint(&BB, BI);
2786 emitReadHazard(Builder);
2787 }
2788 }
2789}
2790
2791static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2792 if (V) S.insert(V);
2793}
2794
2795void FragileHazards::collectLocals() {
2796 // Compute a set of allocas to ignore.
2797 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2798 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2799 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002800
2801 // Collect all the allocas currently in the function. This is
2802 // probably way too aggressive.
2803 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2804 for (llvm::BasicBlock::iterator
2805 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2806 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2807 Locals.push_back(&*I);
2808}
2809
2810llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002811 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002812 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2813 tys[i] = Locals[i]->getType();
2814 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002815}
2816
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002817/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002818
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002819 Objective-C setjmp-longjmp (sjlj) Exception Handling
2820 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002821
John McCallbd309292010-07-06 01:34:17 +00002822 A catch buffer is a setjmp buffer plus:
2823 - a pointer to the exception that was caught
2824 - a pointer to the previous exception data buffer
2825 - two pointers of reserved storage
2826 Therefore catch buffers form a stack, with a pointer to the top
2827 of the stack kept in thread-local storage.
2828
2829 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2830 objc_exception_try_exit pops the given catch buffer, which is
2831 required to be the top of the EH stack.
2832 objc_exception_throw pops the top of the EH stack, writes the
2833 thrown exception into the appropriate field, and longjmps
2834 to the setjmp buffer. It crashes the process (with a printf
2835 and an abort()) if there are no catch buffers on the stack.
2836 objc_exception_extract just reads the exception pointer out of the
2837 catch buffer.
2838
2839 There's no reason an implementation couldn't use a light-weight
2840 setjmp here --- something like __builtin_setjmp, but API-compatible
2841 with the heavyweight setjmp. This will be more important if we ever
2842 want to implement correct ObjC/C++ exception interactions for the
2843 fragile ABI.
2844
2845 Note that for this use of setjmp/longjmp to be correct, we may need
2846 to mark some local variables volatile: if a non-volatile local
2847 variable is modified between the setjmp and the longjmp, it has
2848 indeterminate value. For the purposes of LLVM IR, it may be
2849 sufficient to make loads and stores within the @try (to variables
2850 declared outside the @try) volatile. This is necessary for
2851 optimized correctness, but is not currently being done; this is
2852 being tracked as rdar://problem/8160285
2853
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002854 The basic framework for a @try-catch-finally is as follows:
2855 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002856 objc_exception_data d;
2857 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002858 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002859
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002860 objc_exception_try_enter(&d);
2861 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002862 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002863 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002864 // exception path
2865 id _caught = objc_exception_extract(&d);
2866
2867 // enter new try scope for handlers
2868 if (!setjmp(d.jmp_buf)) {
2869 ... match exception and execute catch blocks ...
2870
2871 // fell off end, rethrow.
2872 _rethrow = _caught;
2873 ... jump-through-finally to finally_rethrow ...
2874 } else {
2875 // exception in catch block
2876 _rethrow = objc_exception_extract(&d);
2877 _call_try_exit = false;
2878 ... jump-through-finally to finally_rethrow ...
2879 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002880 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002881 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002882
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002883 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002884 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002885 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002886
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002887 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002888 ... dispatch to finally destination ...
2889
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002891 objc_exception_throw(_rethrow);
2892
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002893 finally_end:
2894 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002895
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002896 This framework differs slightly from the one gcc uses, in that gcc
2897 uses _rethrow to determine if objc_exception_try_exit should be called
2898 and if the object should be rethrown. This breaks in the face of
2899 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002900
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002901 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002902
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002903 - If there are no catch blocks, then we avoid emitting the second
2904 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002905
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002906 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2907 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002908
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002909 - FIXME: If there is no @finally block we can do a few more
2910 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002911
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002912 Rethrows and Jumps-Through-Finally
2913 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002914
John McCallbd309292010-07-06 01:34:17 +00002915 '@throw;' is supported by pushing the currently-caught exception
2916 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002917
John McCallbd309292010-07-06 01:34:17 +00002918 Branches through the @finally block are handled with an ordinary
2919 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2920 exceptions are not compatible with C++ exceptions, and this is
2921 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002922
John McCallbd309292010-07-06 01:34:17 +00002923 @synchronized(expr) { stmt; } is emitted as if it were:
2924 id synch_value = expr;
2925 objc_sync_enter(synch_value);
2926 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002927*/
2928
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002929void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2930 const Stmt &S) {
2931 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002932
2933 // A destination for the fall-through edges of the catch handlers to
2934 // jump to.
2935 CodeGenFunction::JumpDest FinallyEnd =
2936 CGF.getJumpDestInCurrentScope("finally.end");
2937
2938 // A destination for the rethrow edge of the catch handlers to jump
2939 // to.
2940 CodeGenFunction::JumpDest FinallyRethrow =
2941 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002942
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002943 // For @synchronized, call objc_sync_enter(sync.expr). The
2944 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002945 // @synchronized. We can't avoid a temp here because we need the
2946 // value to be preserved. If the backend ever does liveness
2947 // correctly after setjmp, this will be unnecessary.
2948 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002949 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002950 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002951 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2952 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002953 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2954 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002955
2956 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2957 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002958 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002959
John McCall2dd7d442010-08-04 05:59:32 +00002960 // Allocate memory for the setjmp buffer. This needs to be kept
2961 // live throughout the try and catch blocks.
2962 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2963 "exceptiondata.ptr");
2964
John McCall42227ed2010-07-31 23:20:56 +00002965 // Create the fragile hazards. Note that this will not capture any
2966 // of the allocas required for exception processing, but will
2967 // capture the current basic block (which extends all the way to the
2968 // setjmp call) as "before the @try".
2969 FragileHazards Hazards(CGF);
2970
John McCallbd309292010-07-06 01:34:17 +00002971 // Create a flag indicating whether the cleanup needs to call
2972 // objc_exception_try_exit. This is true except when
2973 // - no catches match and we're branching through the cleanup
2974 // just to rethrow the exception, or
2975 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002976 // The setjmp-safety rule here is that we should always store to this
2977 // variable in a place that dominates the branch through the cleanup
2978 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002979 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002980 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002981
John McCall9916e3f2010-10-04 23:42:51 +00002982 // A slot containing the exception to rethrow. Only needed when we
2983 // have both a @catch and a @finally.
2984 llvm::Value *PropagatingExnVar = 0;
2985
John McCallbd309292010-07-06 01:34:17 +00002986 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002987 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002988 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002989 CallTryExitVar,
2990 ExceptionData,
2991 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002992
2993 // Enter a try block:
2994 // - Call objc_exception_try_enter to push ExceptionData on top of
2995 // the EH stack.
2996 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2997 ->setDoesNotThrow();
2998
2999 // - Call setjmp on the exception data buffer.
3000 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3001 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3002 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003003 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003004 llvm::CallInst *SetJmpResult =
3005 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3006 SetJmpResult->setDoesNotThrow();
3007
3008 // If setjmp returned 0, enter the protected block; otherwise,
3009 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003010 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3011 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003012 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003013 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3014 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003015
John McCallbd309292010-07-06 01:34:17 +00003016 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003017 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003018 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003019 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003020 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003021
3022 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003023
John McCallbd309292010-07-06 01:34:17 +00003024 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003025 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003026
John McCall42227ed2010-07-31 23:20:56 +00003027 // Don't optimize loads of the in-scope locals across this point.
3028 Hazards.emitWriteHazard();
3029
John McCallbd309292010-07-06 01:34:17 +00003030 // For a @synchronized (or a @try with no catches), just branch
3031 // through the cleanup to the rethrow block.
3032 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3033 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003034 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003035 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003036
3037 // Otherwise, we have to match against the caught exceptions.
3038 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003039 // Retrieve the exception object. We may emit multiple blocks but
3040 // nothing can cross this so the value is already in SSA form.
3041 llvm::CallInst *Caught =
3042 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3043 ExceptionData, "caught");
3044 Caught->setDoesNotThrow();
3045
John McCallbd309292010-07-06 01:34:17 +00003046 // Push the exception to rethrow onto the EH value stack for the
3047 // benefit of any @throws in the handlers.
3048 CGF.ObjCEHValueStack.push_back(Caught);
3049
Douglas Gregor96c79492010-04-23 22:50:49 +00003050 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003051
John McCall2dd7d442010-08-04 05:59:32 +00003052 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003053
John McCall2dd7d442010-08-04 05:59:32 +00003054 llvm::BasicBlock *CatchBlock = 0;
3055 llvm::BasicBlock *CatchHandler = 0;
3056 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003057 // Save the currently-propagating exception before
3058 // objc_exception_try_enter clears the exception slot.
3059 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3060 "propagating_exception");
3061 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3062
John McCall2dd7d442010-08-04 05:59:32 +00003063 // Enter a new exception try block (in case a @catch block
3064 // throws an exception).
3065 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3066 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003067
John McCall2dd7d442010-08-04 05:59:32 +00003068 llvm::CallInst *SetJmpResult =
3069 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3070 "setjmp.result");
3071 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003072
John McCall2dd7d442010-08-04 05:59:32 +00003073 llvm::Value *Threw =
3074 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3075
3076 CatchBlock = CGF.createBasicBlock("catch");
3077 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3078 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3079
3080 CGF.EmitBlock(CatchBlock);
3081 }
3082
3083 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003084
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003085 // Handle catch list. As a special case we check if everything is
3086 // matched and avoid generating code for falling off the end if
3087 // so.
3088 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003089 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3090 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003091
Douglas Gregor46a572b2010-04-26 16:46:50 +00003092 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003093 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003094
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003095 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003096 if (!CatchParam) {
3097 AllMatched = true;
3098 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003099 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100
John McCallbd309292010-07-06 01:34:17 +00003101 // catch(id e) always matches under this ABI, since only
3102 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003103 // FIXME: For the time being we also match id<X>; this should
3104 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003105 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003106 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003107 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003108
John McCallbd309292010-07-06 01:34:17 +00003109 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003110 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003111 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3112
Anders Carlsson9396a892008-09-11 09:15:33 +00003113 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003114 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003115 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003116
3117 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003118 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003119 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003120
Anders Carlsson9396a892008-09-11 09:15:33 +00003121 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003122
3123 // The scope of the catch variable ends right here.
3124 CatchVarCleanups.ForceCleanup();
3125
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003126 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003127 break;
3128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003129
Steve Naroff7cae42b2009-07-10 23:34:53 +00003130 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003131 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003132
3133 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003134 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3135 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003136
3137 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003138 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003139
John McCallbd309292010-07-06 01:34:17 +00003140 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003141 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3142 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003143 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003144
John McCallbd309292010-07-06 01:34:17 +00003145 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3146 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003147
3148 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003149 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003150
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003151 // Emit the @catch block.
3152 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003153
3154 // Collect any cleanups for the catch variable. The scope lasts until
3155 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003156 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003157
John McCall1c9c3fd2010-10-15 04:57:14 +00003158 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003159 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003160
John McCallbd309292010-07-06 01:34:17 +00003161 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003162 llvm::Value *Tmp =
3163 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003164 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003165 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003166
Anders Carlsson9396a892008-09-11 09:15:33 +00003167 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003168
3169 // We're done with the catch variable.
3170 CatchVarCleanups.ForceCleanup();
3171
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003172 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003173
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003174 CGF.EmitBlock(NextCatchBlock);
3175 }
3176
John McCallbd309292010-07-06 01:34:17 +00003177 CGF.ObjCEHValueStack.pop_back();
3178
John McCall2dd7d442010-08-04 05:59:32 +00003179 // If nothing wanted anything to do with the caught exception,
3180 // kill the extract call.
3181 if (Caught->use_empty())
3182 Caught->eraseFromParent();
3183
3184 if (!AllMatched)
3185 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3186
3187 if (HasFinally) {
3188 // Emit the exception handler for the @catch blocks.
3189 CGF.EmitBlock(CatchHandler);
3190
3191 // In theory we might now need a write hazard, but actually it's
3192 // unnecessary because there's no local-accessing code between
3193 // the try's write hazard and here.
3194 //Hazards.emitWriteHazard();
3195
John McCall9916e3f2010-10-04 23:42:51 +00003196 // Extract the new exception and save it to the
3197 // propagating-exception slot.
3198 assert(PropagatingExnVar);
3199 llvm::CallInst *NewCaught =
3200 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3201 ExceptionData, "caught");
3202 NewCaught->setDoesNotThrow();
3203 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3204
John McCall2dd7d442010-08-04 05:59:32 +00003205 // Don't pop the catch handler; the throw already did.
3206 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003207 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003208 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003209 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003210
John McCall42227ed2010-07-31 23:20:56 +00003211 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003212 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003213
John McCallbd309292010-07-06 01:34:17 +00003214 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003215 CGF.Builder.restoreIP(TryFallthroughIP);
3216 if (CGF.HaveInsertPoint())
3217 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003218 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003219 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003220
John McCallbd309292010-07-06 01:34:17 +00003221 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003222 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003223 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003224 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003225 // If we have a propagating-exception variable, check it.
3226 llvm::Value *PropagatingExn;
3227 if (PropagatingExnVar) {
3228 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003229
John McCall9916e3f2010-10-04 23:42:51 +00003230 // Otherwise, just look in the buffer for the exception to throw.
3231 } else {
3232 llvm::CallInst *Caught =
3233 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3234 ExceptionData);
3235 Caught->setDoesNotThrow();
3236 PropagatingExn = Caught;
3237 }
3238
3239 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003240 ->setDoesNotThrow();
3241 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003242 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003243
John McCall42227ed2010-07-31 23:20:56 +00003244 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003245}
3246
3247void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003248 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003249 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003250
Anders Carlssone005aa12008-09-09 16:16:55 +00003251 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003252 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003253 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003254 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003255 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003256 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003257 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003258 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003259 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003260
John McCallbd309292010-07-06 01:34:17 +00003261 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3262 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003263 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003264
3265 // Clear the insertion point to indicate we are in unreachable code.
3266 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003267}
3268
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003269/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003270/// object: objc_read_weak (id *src)
3271///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003272llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003273 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003274 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003275 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3276 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3277 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003278 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003279 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003280 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003281 return read_weak;
3282}
3283
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003284/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3285/// objc_assign_weak (id src, id *dst)
3286///
3287void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003288 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003289 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003290 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003291 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003292 assert(Size <= 8 && "does not support size > 8");
3293 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003294 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003295 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3296 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003297 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3298 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003299 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003300 src, dst, "weakassign");
3301 return;
3302}
3303
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003304/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3305/// objc_assign_global (id src, id *dst)
3306///
3307void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003308 llvm::Value *src, llvm::Value *dst,
3309 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003310 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003311 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003312 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003313 assert(Size <= 8 && "does not support size > 8");
3314 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003315 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003316 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3317 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003318 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3319 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003320 if (!threadlocal)
3321 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3322 src, dst, "globalassign");
3323 else
3324 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3325 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003326 return;
3327}
3328
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003329/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003330/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003331///
3332void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003333 llvm::Value *src, llvm::Value *dst,
3334 llvm::Value *ivarOffset) {
3335 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003336 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003337 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003338 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003339 assert(Size <= 8 && "does not support size > 8");
3340 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003341 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003342 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3343 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003344 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3345 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003346 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3347 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003348 return;
3349}
3350
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003351/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3352/// objc_assign_strongCast (id src, id *dst)
3353///
3354void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003355 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003356 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003357 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003358 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003359 assert(Size <= 8 && "does not support size > 8");
3360 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003361 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003362 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3363 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003364 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3365 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003366 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003367 src, dst, "weakassign");
3368 return;
3369}
3370
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003371void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003372 llvm::Value *DestPtr,
3373 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003374 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003375 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3376 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003377 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003378 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003379 return;
3380}
3381
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003382/// EmitObjCValueForIvar - Code Gen for ivar reference.
3383///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003384LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3385 QualType ObjectTy,
3386 llvm::Value *BaseValue,
3387 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003388 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003389 const ObjCInterfaceDecl *ID =
3390 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003391 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3392 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003393}
3394
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003395llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003396 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003397 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003398 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003399 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003400 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3401 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003402}
3403
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003404/* *** Private Interface *** */
3405
3406/// EmitImageInfo - Emit the image info marker used to encode some module
3407/// level information.
3408///
3409/// See: <rdr://4810609&4810587&4810587>
3410/// struct IMAGE_INFO {
3411/// unsigned version;
3412/// unsigned flags;
3413/// };
3414enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003415 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003416 eImageInfo_GarbageCollected = (1 << 1),
3417 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003418 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3419
Daniel Dunbar5e639272010-04-25 20:39:01 +00003420 // A flag indicating that the module has no instances of a @synthesize of a
3421 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003422 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003423};
3424
Daniel Dunbar5e639272010-04-25 20:39:01 +00003425void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003426 unsigned version = 0; // Version is unused?
3427 unsigned flags = 0;
3428
3429 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003430 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003431 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003432 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003433 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003434
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003435 // We never allow @synthesize of a superclass property.
3436 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003437
Chris Lattner2192fe52011-07-18 04:24:23 +00003438 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner5e016ae2010-06-27 07:15:29 +00003439
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003440 // Emitted as int[2];
3441 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003442 llvm::ConstantInt::get(Int32Ty, version),
3443 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003444 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003445 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003446
3447 const char *Section;
3448 if (ObjCABI == 1)
3449 Section = "__OBJC, __image_info,regular";
3450 else
3451 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003452 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003453 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003454 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003455 Section,
3456 0,
3457 true);
3458 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003459}
3460
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003461
3462// struct objc_module {
3463// unsigned long version;
3464// unsigned long size;
3465// const char *name;
3466// Symtab symtab;
3467// };
3468
3469// FIXME: Get from somewhere
3470static const int ModuleVersion = 7;
3471
3472void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003473 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003474
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003475 llvm::Constant *Values[] = {
3476 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3477 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3478 // This used to be the filename, now it is unused. <rdr://4327263>
3479 GetClassName(&CGM.getContext().Idents.get("")),
3480 EmitModuleSymbols()
3481 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003482 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003483 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003484 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003485 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003486}
3487
3488llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003489 unsigned NumClasses = DefinedClasses.size();
3490 unsigned NumCategories = DefinedCategories.size();
3491
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003492 // Return null if no symbols were defined.
3493 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003494 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003495
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003496 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003497 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003498 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003499 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3500 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003501
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003502 // The runtime expects exactly the list of defined classes followed
3503 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003504 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003505 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003506 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003507 ObjCTypes.Int8PtrTy);
3508 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003509 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003510 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003511 ObjCTypes.Int8PtrTy);
3512
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003513 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003514 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003515 NumClasses + NumCategories),
3516 Symbols);
3517
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003518 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003519
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003520 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003521 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3522 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003523 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003524 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003525}
3526
John McCall31168b02011-06-15 23:02:42 +00003527llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3528 IdentifierInfo *II) {
3529 LazySymbols.insert(II);
3530
3531 llvm::GlobalVariable *&Entry = ClassReferences[II];
3532
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003533 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003534 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003535 llvm::ConstantExpr::getBitCast(GetClassName(II),
3536 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003537 Entry =
John McCall31168b02011-06-15 23:02:42 +00003538 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3539 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3540 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003541 }
John McCall31168b02011-06-15 23:02:42 +00003542
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003543 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003544}
3545
John McCall31168b02011-06-15 23:02:42 +00003546llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3547 const ObjCInterfaceDecl *ID) {
3548 return EmitClassRefFromId(Builder, ID->getIdentifier());
3549}
3550
3551llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3552 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3553 return EmitClassRefFromId(Builder, II);
3554}
3555
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003556llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3557 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003558 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003560 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003562 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003563 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003565 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3566 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003567 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003568 }
3569
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003570 if (lvalue)
3571 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003572 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003573}
3574
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003575llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003576 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003577
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003578 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003579 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003580 llvm::ConstantArray::get(VMContext,
3581 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003582 ((ObjCABI == 2) ?
3583 "__TEXT,__objc_classname,cstring_literals" :
3584 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003585 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003586
Owen Anderson170229f2009-07-14 23:10:40 +00003587 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003588}
3589
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003590llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3591 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3592 I = MethodDefinitions.find(MD);
3593 if (I != MethodDefinitions.end())
3594 return I->second;
3595
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003596 return NULL;
3597}
3598
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003599/// GetIvarLayoutName - Returns a unique constant for the given
3600/// ivar layout bitmap.
3601llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003602 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003603 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003604}
3605
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003606void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003607 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003608 bool ForStrongLayout,
3609 bool &HasUnion) {
3610 const RecordDecl *RD = RT->getDecl();
3611 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003612 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003613 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003614 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003615 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003616
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003617 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3618 ForStrongLayout, HasUnion);
3619}
3620
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003621void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003622 const llvm::StructLayout *Layout,
3623 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003624 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003625 unsigned int BytePos, bool ForStrongLayout,
3626 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003627 bool IsUnion = (RD && RD->isUnion());
3628 uint64_t MaxUnionIvarSize = 0;
3629 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003630 const FieldDecl *MaxField = 0;
3631 const FieldDecl *MaxSkippedField = 0;
3632 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003633 uint64_t MaxFieldOffset = 0;
3634 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003635 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003636 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003638 if (RecFields.empty())
3639 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003640 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3641 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003642 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003643 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003644 FirstFieldDelta =
3645 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3646 }
3647
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003648 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003649 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003650 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003651 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003652 // Note that 'i' here is actually the field index inside RD of Field,
3653 // although this dependency is hidden.
3654 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003655 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003656 } else
John McCall31168b02011-06-15 23:02:42 +00003657 FieldOffset =
3658 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003659
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003660 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003661 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003662 LastFieldBitfieldOrUnnamed = Field;
3663 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003664 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003665 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003666
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003667 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003668 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003669 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003670 if (FQT->isUnionType())
3671 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003672
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003673 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003674 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003675 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003676 continue;
3677 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003678
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003679 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003680 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003681 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003682 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003683 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003684 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003685 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3686 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003687 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003688 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003689 FQT = CArray->getElementType();
3690 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003691
3692 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003693 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003694 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003695 int OldIndex = IvarsInfo.size() - 1;
3696 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003697
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003698 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003699 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003700 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003701
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003702 // Replicate layout information for each array element. Note that
3703 // one element is already done.
3704 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003705 for (int FirstIndex = IvarsInfo.size() - 1,
3706 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003707 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003708 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3709 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3710 IvarsInfo[i].ivar_size));
3711 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3712 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3713 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003714 }
3715 continue;
3716 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003717 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003718 // At this point, we are done with Record/Union and array there of.
3719 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003720 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003721
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003722 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003723 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3724 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003725 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003726 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003727 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003728 MaxUnionIvarSize = UnionIvarSize;
3729 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003730 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003731 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003732 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003733 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003734 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003735 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003736 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003737 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3738 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003739 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003740 // FIXME: Why the asymmetry? We divide by word size in bits on other
3741 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003742 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003743 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003744 MaxSkippedUnionIvarSize = UnionIvarSize;
3745 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003746 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003747 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003748 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003749 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003750 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003751 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003752 }
3753 }
3754 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003755
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003756 if (LastFieldBitfieldOrUnnamed) {
3757 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3758 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003759 uint64_t BitFieldSize
3760 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003761 GC_IVAR skivar;
3762 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3763 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3764 + ((BitFieldSize % ByteSizeInBits) != 0);
3765 SkipIvars.push_back(skivar);
3766 } else {
3767 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3768 // Last field was unnamed. Must update skip info.
3769 unsigned FieldSize
3770 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3771 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3772 FieldSize / ByteSizeInBits));
3773 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003774 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003775
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003776 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003777 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003778 MaxUnionIvarSize));
3779 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003780 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003781 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003782}
3783
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003784/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3785/// the computations and returning the layout bitmap (for ivar or blocks) in
3786/// the given argument BitMap string container. Routine reads
3787/// two containers, IvarsInfo and SkipIvars which are assumed to be
3788/// filled already by the caller.
3789llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003790 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003791 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003792
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003793 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003794 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003795 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003796 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003797 if (IvarsInfo[0].ivar_bytepos == 0) {
3798 WordsToSkip = 0;
3799 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003800 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003801 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3802 WordsToScan = IvarsInfo[0].ivar_size;
3803 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003804 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003805 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003806 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003807 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003808 // consecutive 'scanned' object pointers.
3809 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003810 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003811 // Skip over 'gc'able object pointer which lay over each other.
3812 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3813 continue;
3814 // Must skip over 1 or more words. We save current skip/scan values
3815 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003816 SKIP_SCAN SkScan;
3817 SkScan.skip = WordsToSkip;
3818 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003819 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003820
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003821 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003822 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3823 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003824 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003825 WordsToSkip = 0;
3826 WordsToScan = IvarsInfo[i].ivar_size;
3827 }
3828 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003829 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003830 SKIP_SCAN SkScan;
3831 SkScan.skip = WordsToSkip;
3832 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003833 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003834 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003835
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003836 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003837 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003838 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003839 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003840 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003841 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003842 IvarsInfo[LastIndex].ivar_bytepos +
3843 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003844 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003845 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003846 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003847 SKIP_SCAN SkScan;
3848 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3849 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003850 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003851 }
3852 }
3853 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3854 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003855 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003856 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003857 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3858 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3859 // 0xM0 followed by 0x0N detected.
3860 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3861 for (int j = i+1; j < SkipScan; j++)
3862 SkipScanIvars[j] = SkipScanIvars[j+1];
3863 --SkipScan;
3864 }
3865 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003866
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003867 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003868 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003869 unsigned char byte;
3870 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3871 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3872 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3873 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003874
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003875 // first skip big.
3876 for (unsigned int ix = 0; ix < skip_big; ix++)
3877 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003878
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003879 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003880 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003881 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003882 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003883 byte |= 0xf;
3884 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003885 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003886 byte |= scan_small;
3887 scan_small = 0;
3888 }
3889 BitMap += byte;
3890 }
3891 // next scan big
3892 for (unsigned int ix = 0; ix < scan_big; ix++)
3893 BitMap += (unsigned char)(0x0f);
3894 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003895 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003896 byte = scan_small;
3897 BitMap += byte;
3898 }
3899 }
3900 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003901 unsigned char zero = 0;
3902 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003903
3904 llvm::GlobalVariable * Entry =
3905 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3906 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003907 ((ObjCABI == 2) ?
3908 "__TEXT,__objc_classname,cstring_literals" :
3909 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003910 1, true);
3911 return getConstantGEP(VMContext, Entry, 0, 0);
3912}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003913
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003914/// BuildIvarLayout - Builds ivar layout bitmap for the class
3915/// implementation for the __strong or __weak case.
3916/// The layout map displays which words in ivar list must be skipped
3917/// and which must be scanned by GC (see below). String is built of bytes.
3918/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3919/// of words to skip and right nibble is count of words to scan. So, each
3920/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3921/// represented by a 0x00 byte which also ends the string.
3922/// 1. when ForStrongLayout is true, following ivars are scanned:
3923/// - id, Class
3924/// - object *
3925/// - __strong anything
3926///
3927/// 2. When ForStrongLayout is false, following ivars are scanned:
3928/// - __weak anything
3929///
3930llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3931 const ObjCImplementationDecl *OMD,
3932 bool ForStrongLayout) {
3933 bool hasUnion = false;
3934
Chris Lattner2192fe52011-07-18 04:24:23 +00003935 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00003936 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00003937 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003938 return llvm::Constant::getNullValue(PtrTy);
3939
Jordy Rosea91768e2011-07-22 02:08:32 +00003940 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3941 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003942 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003943 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003944 IVD; IVD = IVD->getNextIvar())
3945 RecFields.push_back(cast<FieldDecl>(IVD));
3946 }
3947 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00003948 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00003949 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003950
Jordy Rosea91768e2011-07-22 02:08:32 +00003951 // FIXME: This is not ideal; we shouldn't have to do this copy.
3952 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003953 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003954
3955 if (RecFields.empty())
3956 return llvm::Constant::getNullValue(PtrTy);
3957
3958 SkipIvars.clear();
3959 IvarsInfo.clear();
3960
3961 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3962 if (IvarsInfo.empty())
3963 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003964 // Sort on byte position in case we encounterred a union nested in
3965 // the ivar list.
3966 if (hasUnion && !IvarsInfo.empty())
3967 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3968 if (hasUnion && !SkipIvars.empty())
3969 std::sort(SkipIvars.begin(), SkipIvars.end());
3970
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003971 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003972 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003973
3974 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003975 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003976 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003977 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003978 const unsigned char *s = (unsigned char*)BitMap.c_str();
3979 for (unsigned i = 0; i < BitMap.size(); i++)
3980 if (!(s[i] & 0xf0))
3981 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3982 else
3983 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3984 printf("\n");
3985 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003986 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003987}
3988
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003989llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003990 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3991
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003992 // FIXME: Avoid std::string copying.
3993 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003994 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003995 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003996 ((ObjCABI == 2) ?
3997 "__TEXT,__objc_methname,cstring_literals" :
3998 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003999 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004000
Owen Anderson170229f2009-07-14 23:10:40 +00004001 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004002}
4003
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004004// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004005llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004006 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4007}
4008
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004009llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004010 std::string TypeStr;
4011 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4012
4013 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004014
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004015 if (!Entry)
4016 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004017 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004018 ((ObjCABI == 2) ?
4019 "__TEXT,__objc_methtype,cstring_literals" :
4020 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004021 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004022
Owen Anderson170229f2009-07-14 23:10:40 +00004023 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004024}
4025
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004026llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004027 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00004028 if (CGM.getContext().getObjCEncodingForMethodDecl(
4029 const_cast<ObjCMethodDecl*>(D),
4030 TypeStr))
4031 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004032
4033 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4034
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004035 if (!Entry)
4036 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004037 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004038 ((ObjCABI == 2) ?
4039 "__TEXT,__objc_methtype,cstring_literals" :
4040 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004041 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004042
Owen Anderson170229f2009-07-14 23:10:40 +00004043 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004044}
4045
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004046// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004047llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004048 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004049
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004050 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004051 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004052 llvm::ConstantArray::get(VMContext,
4053 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004054 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004055 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004056
Owen Anderson170229f2009-07-14 23:10:40 +00004057 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004058}
4059
4060// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004061// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004062llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004063CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4064 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004065 std::string TypeStr;
4066 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004067 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4068}
4069
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004070void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004071 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004072 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004073 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004074 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004075 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4076 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004077 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004078 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004079 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004080 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004081}
4082
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004083void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004084 EmitModuleInfo();
4085
Daniel Dunbarc475d422008-10-29 22:36:39 +00004086 // Emit the dummy bodies for any protocols which were referenced but
4087 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004088 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004089 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4090 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004091 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004092
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004093 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004094 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004095 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004096 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004097 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004098 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004099 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004100 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004101 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004102 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004103 }
4104
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004105 // Add assembler directives to add lazy undefined symbol references
4106 // for classes which are referenced but not defined. This is
4107 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004108 //
4109 // FIXME: It would be nice if we had an LLVM construct for this.
4110 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4111 llvm::SmallString<256> Asm;
4112 Asm += CGM.getModule().getModuleInlineAsm();
4113 if (!Asm.empty() && Asm.back() != '\n')
4114 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004115
Daniel Dunbard027a922009-09-07 00:20:42 +00004116 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004117 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4118 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004119 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4120 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004121 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004122 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004123 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004124 }
4125
4126 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4127 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4128 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4129 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004130
Daniel Dunbard027a922009-09-07 00:20:42 +00004131 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004132 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004133}
4134
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004136 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004137 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004138 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004139 ObjCABI = 2;
4140}
4141
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004142/* *** */
4143
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004144ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004145 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004146 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4147 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004149 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004150 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004151 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004152 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004153 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004154
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004155 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004156 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004157 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004158
Mike Stump18bb9282009-05-16 07:57:57 +00004159 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4160 // comes out a bit cleaner.
Chris Lattner2192fe52011-07-18 04:24:23 +00004161 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004162 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004163
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004164 // I'm not sure I like this. The implicit coordination is a bit
4165 // gross. We should solve this in a reasonable fashion because this
4166 // is a pretty common task (match some runtime data structure with
4167 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004168
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004169 // FIXME: This is leaked.
4170 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004171
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004172 // struct _objc_super {
4173 // id self;
4174 // Class cls;
4175 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004176 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004177 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004178 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004179 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004180 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004181 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004182 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004183 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004184 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004185
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004186 SuperCTy = Ctx.getTagDeclType(RD);
4187 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004188
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004189 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004190 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4191
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004192 // struct _prop_t {
4193 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004194 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004195 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004196 PropertyTy = llvm::StructType::create("struct._prop_t",
4197 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004198
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004199 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004200 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004201 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004202 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004203 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004204 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004205 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4206 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004207 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004208 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004209
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004210 // struct _objc_method {
4211 // SEL _cmd;
4212 // char *method_type;
4213 // char *_imp;
4214 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004215 MethodTy = llvm::StructType::create("struct._objc_method",
4216 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4217 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004218
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004219 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004220 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004221 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004222
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004223}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004224
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004225ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004226 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004227 // struct _objc_method_description {
4228 // SEL name;
4229 // char *types;
4230 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004231 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004232 llvm::StructType::create("struct._objc_method_description",
4233 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004234
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004235 // struct _objc_method_description_list {
4236 // int count;
4237 // struct _objc_method_description[1];
4238 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004239 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004240 llvm::StructType::create("struct._objc_method_description_list",
4241 IntTy,
4242 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004243
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004244 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004245 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004246 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004247
Daniel Dunbarb036db82008-08-13 03:21:16 +00004248 // Protocol description structures
4249
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004250 // struct _objc_protocol_extension {
4251 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4252 // struct _objc_method_description_list *optional_instance_methods;
4253 // struct _objc_method_description_list *optional_class_methods;
4254 // struct _objc_property_list *instance_properties;
4255 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004256 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004257 llvm::StructType::create("struct._objc_protocol_extension",
4258 IntTy, MethodDescriptionListPtrTy,
4259 MethodDescriptionListPtrTy, PropertyListPtrTy,
4260 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004261
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004262 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004263 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004264
Daniel Dunbarc475d422008-10-29 22:36:39 +00004265 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004266
Chris Lattnera5f58b02011-07-09 17:41:47 +00004267 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004268 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004269
Chris Lattnera5f58b02011-07-09 17:41:47 +00004270 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004271 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004272 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004273 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004274 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004275 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004276
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004277 // struct _objc_protocol {
4278 // struct _objc_protocol_extension *isa;
4279 // char *protocol_name;
4280 // struct _objc_protocol **_objc_protocol_list;
4281 // struct _objc_method_description_list *instance_methods;
4282 // struct _objc_method_description_list *class_methods;
4283 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004284 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4285 llvm::PointerType::getUnqual(ProtocolListTy),
4286 MethodDescriptionListPtrTy,
4287 MethodDescriptionListPtrTy,
4288 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004289
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004290 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004291 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004292
Owen Anderson9793f0e2009-07-29 22:16:19 +00004293 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004294
4295 // Class description structures
4296
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004297 // struct _objc_ivar {
4298 // char *ivar_name;
4299 // char *ivar_type;
4300 // int ivar_offset;
4301 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004302 IvarTy = llvm::StructType::create("struct._objc_ivar",
4303 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004304
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004305 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004306 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004307 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004308 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004309
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004310 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004311 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004312 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004313 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004314
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004315 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004316 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004317 llvm::StructType::create("struct._objc_class_extension",
4318 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004319 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004320
Chris Lattner5ec04a52011-08-12 17:43:31 +00004321 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004322
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004323 // struct _objc_class {
4324 // Class isa;
4325 // Class super_class;
4326 // char *name;
4327 // long version;
4328 // long info;
4329 // long instance_size;
4330 // struct _objc_ivar_list *ivars;
4331 // struct _objc_method_list *methods;
4332 // struct _objc_cache *cache;
4333 // struct _objc_protocol_list *protocols;
4334 // char *ivar_layout;
4335 // struct _objc_class_ext *ext;
4336 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004337 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4338 llvm::PointerType::getUnqual(ClassTy),
4339 Int8PtrTy,
4340 LongTy,
4341 LongTy,
4342 LongTy,
4343 IvarListPtrTy,
4344 MethodListPtrTy,
4345 CachePtrTy,
4346 ProtocolListPtrTy,
4347 Int8PtrTy,
4348 ClassExtensionPtrTy,
4349 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004350
Owen Anderson9793f0e2009-07-29 22:16:19 +00004351 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004352
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004353 // struct _objc_category {
4354 // char *category_name;
4355 // char *class_name;
4356 // struct _objc_method_list *instance_method;
4357 // struct _objc_method_list *class_method;
4358 // uint32_t size; // sizeof(struct _objc_category)
4359 // struct _objc_property_list *instance_properties;// category's @property
4360 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004361 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004362 llvm::StructType::create("struct._objc_category",
4363 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4364 MethodListPtrTy, ProtocolListPtrTy,
4365 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004366
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004367 // Global metadata structures
4368
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004369 // struct _objc_symtab {
4370 // long sel_ref_cnt;
4371 // SEL *refs;
4372 // short cls_def_cnt;
4373 // short cat_def_cnt;
4374 // char *defs[cls_def_cnt + cat_def_cnt];
4375 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004376 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004377 llvm::StructType::create("struct._objc_symtab",
4378 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4379 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004380 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004381
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004382 // struct _objc_module {
4383 // long version;
4384 // long size; // sizeof(struct _objc_module)
4385 // char *name;
4386 // struct _objc_symtab* symtab;
4387 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004389 llvm::StructType::create("struct._objc_module",
4390 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004391
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004392
Mike Stump18bb9282009-05-16 07:57:57 +00004393 // FIXME: This is the size of the setjmp buffer and should be target
4394 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004395 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004396
Anders Carlsson9ff22482008-09-09 10:10:21 +00004397 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004398 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004399 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004400
4401 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004402 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004403 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4404 SetJmpBufferSize),
4405 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004406
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004407}
4408
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004409ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004410 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004411 // struct _method_list_t {
4412 // uint32_t entsize; // sizeof(struct _objc_method)
4413 // uint32_t method_count;
4414 // struct _objc_method method_list[method_count];
4415 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004416 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004417 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4418 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004419 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004420 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004421
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004422 // struct _protocol_t {
4423 // id isa; // NULL
4424 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004425 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004426 // const struct method_list_t * const instance_methods;
4427 // const struct method_list_t * const class_methods;
4428 // const struct method_list_t *optionalInstanceMethods;
4429 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004430 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004431 // const uint32_t size; // sizeof(struct _protocol_t)
4432 // const uint32_t flags; // = 0
4433 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004434
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004435 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004436 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004437 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004438
Chris Lattnera5f58b02011-07-09 17:41:47 +00004439 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004440 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4441 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4442 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4443 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4444 PropertyListPtrTy, IntTy, IntTy, NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004445
4446 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004447 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004449 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004450 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004451 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004452 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004453 ProtocolListnfABITy->setBody(LongTy,
4454 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4455 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004456
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004457 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004458 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004459
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004460 // struct _ivar_t {
4461 // unsigned long int *offset; // pointer to ivar offset location
4462 // char *name;
4463 // char *type;
4464 // uint32_t alignment;
4465 // uint32_t size;
4466 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004467 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004468 llvm::StructType::create("struct._ivar_t",
4469 llvm::PointerType::getUnqual(LongTy),
4470 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004471
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004472 // struct _ivar_list_t {
4473 // uint32 entsize; // sizeof(struct _ivar_t)
4474 // uint32 count;
4475 // struct _iver_t list[count];
4476 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004477 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004478 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4479 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004480
Owen Anderson9793f0e2009-07-29 22:16:19 +00004481 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004482
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004483 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004484 // uint32_t const flags;
4485 // uint32_t const instanceStart;
4486 // uint32_t const instanceSize;
4487 // uint32_t const reserved; // only when building for 64bit targets
4488 // const uint8_t * const ivarLayout;
4489 // const char *const name;
4490 // const struct _method_list_t * const baseMethods;
4491 // const struct _objc_protocol_list *const baseProtocols;
4492 // const struct _ivar_list_t *const ivars;
4493 // const uint8_t * const weakIvarLayout;
4494 // const struct _prop_list_t * const properties;
4495 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004496
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004497 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004498 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4499 IntTy, IntTy, IntTy, Int8PtrTy,
4500 Int8PtrTy, MethodListnfABIPtrTy,
4501 ProtocolListnfABIPtrTy,
4502 IvarListnfABIPtrTy,
4503 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004504
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004505 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004506 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004507 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4508 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004509
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004510 // struct _class_t {
4511 // struct _class_t *isa;
4512 // struct _class_t * const superclass;
4513 // void *cache;
4514 // IMP *vtable;
4515 // struct class_ro_t *ro;
4516 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517
Chris Lattner5ec04a52011-08-12 17:43:31 +00004518 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004519 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4520 llvm::PointerType::getUnqual(ClassnfABITy),
4521 CachePtrTy,
4522 llvm::PointerType::getUnqual(ImpnfABITy),
4523 llvm::PointerType::getUnqual(ClassRonfABITy),
4524 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004525
Fariborz Jahanian71394042009-01-23 23:53:38 +00004526 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004527 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004528
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004529 // struct _category_t {
4530 // const char * const name;
4531 // struct _class_t *const cls;
4532 // const struct _method_list_t * const instance_methods;
4533 // const struct _method_list_t * const class_methods;
4534 // const struct _protocol_list_t * const protocols;
4535 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004536 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004537 CategorynfABITy = llvm::StructType::create("struct._category_t",
4538 Int8PtrTy, ClassnfABIPtrTy,
4539 MethodListnfABIPtrTy,
4540 MethodListnfABIPtrTy,
4541 ProtocolListnfABIPtrTy,
4542 PropertyListPtrTy,
4543 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004544
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004545 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004546 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4547 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004549 // MessageRefTy - LLVM for:
4550 // struct _message_ref_t {
4551 // IMP messenger;
4552 // SEL name;
4553 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004554
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004555 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004556 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004557 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004558 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004559 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004560 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004561 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004562 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004563 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004564 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004565
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004566 MessageRefCTy = Ctx.getTagDeclType(RD);
4567 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4568 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004569
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004570 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004571 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004572
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004573 // SuperMessageRefTy - LLVM for:
4574 // struct _super_message_ref_t {
4575 // SUPER_IMP messenger;
4576 // SEL name;
4577 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004578 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004579 llvm::StructType::create("struct._super_message_ref_t",
4580 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004581
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004582 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004584
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004585
4586 // struct objc_typeinfo {
4587 // const void** vtable; // objc_ehtype_vtable + 2
4588 // const char* name; // c++ typeinfo string
4589 // Class cls;
4590 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004591 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004592 llvm::StructType::create("struct._objc_typeinfo",
4593 llvm::PointerType::getUnqual(Int8PtrTy),
4594 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004595 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004596}
4597
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004598llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004599 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004600
Fariborz Jahanian71394042009-01-23 23:53:38 +00004601 return NULL;
4602}
4603
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004604void CGObjCNonFragileABIMac::AddModuleClassList(const
4605 std::vector<llvm::GlobalValue*>
4606 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004607 const char *SymbolName,
4608 const char *SectionName) {
4609 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004610
Daniel Dunbar19573e72009-05-15 21:48:48 +00004611 if (!NumClasses)
4612 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004613
Daniel Dunbar19573e72009-05-15 21:48:48 +00004614 std::vector<llvm::Constant*> Symbols(NumClasses);
4615 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004616 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004617 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004619 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004620 NumClasses),
4621 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004622
Daniel Dunbar19573e72009-05-15 21:48:48 +00004623 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004624 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 llvm::GlobalValue::InternalLinkage,
4626 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004627 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004628 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004629 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004630 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004631}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004632
Fariborz Jahanian71394042009-01-23 23:53:38 +00004633void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4634 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004635
Daniel Dunbar19573e72009-05-15 21:48:48 +00004636 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004637 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004638 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004639 "\01L_OBJC_LABEL_CLASS_$",
4640 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004641
Fariborz Jahanian67260552009-11-17 21:37:35 +00004642 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4643 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4644 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4645 continue;
4646 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004647 }
4648
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004649 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4650 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4651 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4652 continue;
4653 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4654 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004655
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004656 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004657 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4658 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004659
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004660 // Build list of all implemented category addresses in array
4661 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004662 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004663 "\01L_OBJC_LABEL_CATEGORY_$",
4664 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004665 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004666 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4667 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004668
Daniel Dunbar5e639272010-04-25 20:39:01 +00004669 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004670}
4671
John McCall9e8bb002011-05-14 03:10:52 +00004672/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4673/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004674/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004675/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004676bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4677 // At various points we've experimented with using vtable-based
4678 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004679 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4680 default:
John McCall9e8bb002011-05-14 03:10:52 +00004681 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004682 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004683 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004684 case CodeGenOptions::NonLegacy:
4685 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004686 case CodeGenOptions::Mixed:
4687 break;
4688 }
4689
4690 // If so, see whether this selector is in the white-list of things which must
4691 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004692 if (VTableDispatchMethods.empty()) {
4693 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4694 VTableDispatchMethods.insert(GetNullarySelector("class"));
4695 VTableDispatchMethods.insert(GetNullarySelector("self"));
4696 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4697 VTableDispatchMethods.insert(GetNullarySelector("length"));
4698 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004699
John McCall9e8bb002011-05-14 03:10:52 +00004700 // These are vtable-based if GC is disabled.
4701 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004702 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004703 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4704 VTableDispatchMethods.insert(GetNullarySelector("release"));
4705 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4706 }
4707
4708 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4709 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4710 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4711 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4712 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4713 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4714 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4715
4716 // These are vtable-based if GC is enabled.
4717 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004718 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004719 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4720 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4721
4722 // "countByEnumeratingWithState:objects:count"
4723 IdentifierInfo *KeyIdents[] = {
4724 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4725 &CGM.getContext().Idents.get("objects"),
4726 &CGM.getContext().Idents.get("count")
4727 };
4728 VTableDispatchMethods.insert(
4729 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4730 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004731 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004732
John McCall9e8bb002011-05-14 03:10:52 +00004733 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004734}
4735
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004736// Metadata flags
4737enum MetaDataDlags {
4738 CLS = 0x0,
4739 CLS_META = 0x1,
4740 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004741 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004742 CLS_EXCEPTION = 0x20,
4743
4744 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4745 CLS_HAS_IVAR_RELEASER = 0x40,
4746 /// class was compiled with -fobjc-arr
4747 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004748};
4749/// BuildClassRoTInitializer - generate meta-data for:
4750/// struct _class_ro_t {
4751/// uint32_t const flags;
4752/// uint32_t const instanceStart;
4753/// uint32_t const instanceSize;
4754/// uint32_t const reserved; // only when building for 64bit targets
4755/// const uint8_t * const ivarLayout;
4756/// const char *const name;
4757/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004758/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004759/// const struct _ivar_list_t *const ivars;
4760/// const uint8_t * const weakIvarLayout;
4761/// const struct _prop_list_t * const properties;
4762/// }
4763///
4764llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004765 unsigned flags,
4766 unsigned InstanceStart,
4767 unsigned InstanceSize,
4768 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004769 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004770 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004771
4772 if (CGM.getLangOptions().ObjCAutoRefCount)
4773 flags |= CLS_COMPILED_BY_ARC;
4774
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004775 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4776 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4777 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004778 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004779 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4780 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004781 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004782 // const struct _method_list_t * const baseMethods;
4783 std::vector<llvm::Constant*> Methods;
4784 std::string MethodListName("\01l_OBJC_$_");
4785 if (flags & CLS_META) {
4786 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004787 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004788 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004789 // Class methods should always be defined.
4790 Methods.push_back(GetMethodConstant(*i));
4791 }
4792 } else {
4793 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004795 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004796 // Instance methods should always be defined.
4797 Methods.push_back(GetMethodConstant(*i));
4798 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004800 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004801 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004803 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4804 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004805
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004806 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4807 if (llvm::Constant *C = GetMethodConstant(MD))
4808 Methods.push_back(C);
4809 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4810 if (llvm::Constant *C = GetMethodConstant(MD))
4811 Methods.push_back(C);
4812 }
4813 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004814 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004815 Values[ 5] = EmitMethodList(MethodListName,
4816 "__DATA, __objc_const", Methods);
4817
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004818 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4819 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004820 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004821 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004822 OID->all_referenced_protocol_begin(),
4823 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004824
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004825 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004826 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004827 else
4828 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004829 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4830 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004831 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004832 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004833 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004834 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4835 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004836 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004837 Values);
4838 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004839 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4840 llvm::GlobalValue::InternalLinkage,
4841 Init,
4842 (flags & CLS_META) ?
4843 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4844 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004845 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004846 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004847 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004848 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004849
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004850}
4851
4852/// BuildClassMetaData - This routine defines that to-level meta-data
4853/// for the given ClassName for:
4854/// struct _class_t {
4855/// struct _class_t *isa;
4856/// struct _class_t * const superclass;
4857/// void *cache;
4858/// IMP *vtable;
4859/// struct class_ro_t *ro;
4860/// }
4861///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004862llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004863 std::string &ClassName,
4864 llvm::Constant *IsAGV,
4865 llvm::Constant *SuperClassGV,
4866 llvm::Constant *ClassRoGV,
4867 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004868 llvm::Constant *Values[] = {
4869 IsAGV,
4870 SuperClassGV,
4871 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4872 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4873 ClassRoGV // &CLASS_RO_GV
4874 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004875 if (!Values[1])
4876 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004877 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004878 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004879 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4880 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004881 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004882 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004883 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004884 if (HiddenVisibility)
4885 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004886 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004887}
4888
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004889bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004890CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004891 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004892}
4893
Daniel Dunbar961202372009-05-03 12:57:56 +00004894void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004895 uint32_t &InstanceStart,
4896 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004897 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004898 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004899
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004900 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004901 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004902
4903 // If there are no fields, the start is the same as the end.
4904 if (!RL.getFieldCount())
4905 InstanceStart = InstanceSize;
4906 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004907 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004908}
4909
Fariborz Jahanian71394042009-01-23 23:53:38 +00004910void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4911 std::string ClassName = ID->getNameAsString();
4912 if (!ObjCEmptyCacheVar) {
4913 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004914 CGM.getModule(),
4915 ObjCTypes.CacheTy,
4916 false,
4917 llvm::GlobalValue::ExternalLinkage,
4918 0,
4919 "_objc_empty_cache");
4920
Fariborz Jahanian71394042009-01-23 23:53:38 +00004921 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004922 CGM.getModule(),
4923 ObjCTypes.ImpnfABITy,
4924 false,
4925 llvm::GlobalValue::ExternalLinkage,
4926 0,
4927 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004928 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004930 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004931 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004932 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004933 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004934 uint32_t InstanceSize = InstanceStart;
4935 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004936 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4937 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004938
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004939 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004940
4941 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004942 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004943 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004944 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004945 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004946 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004947 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004948 // class is root
4949 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004950 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004951 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004952 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004953 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004954 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4955 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4956 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004957 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004958 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004959 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004960 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004961 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004962 ObjCMetaClassName +
4963 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004964 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004965 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004966 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004967 }
4968 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4969 InstanceStart,
4970 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004971 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004972 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004973 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4974 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004975 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004976
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004977 // Metadata for the class
4978 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004979 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004980 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004981 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004982 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004983
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004984 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004985 flags |= CLS_EXCEPTION;
4986
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004987 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004988 flags |= CLS_ROOT;
4989 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004990 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004991 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004992 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004993 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004994 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004995 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004996 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004997 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004998 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004999 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005000 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005001 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005002 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005003
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005004 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005005 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005006 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5007 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005008 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005009
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005010 // Determine if this class is also "non-lazy".
5011 if (ImplementationIsNonLazy(ID))
5012 DefinedNonLazyClasses.push_back(ClassMD);
5013
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005014 // Force the definition of the EHType if necessary.
5015 if (flags & CLS_EXCEPTION)
5016 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005017 // Make sure method definition entries are all clear for next implementation.
5018 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005019}
5020
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005021/// GenerateProtocolRef - This routine is called to generate code for
5022/// a protocol reference expression; as in:
5023/// @code
5024/// @protocol(Proto1);
5025/// @endcode
5026/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5027/// which will hold address of the protocol meta-data.
5028///
5029llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005030 const ObjCProtocolDecl *PD) {
5031
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005032 // This routine is called for @protocol only. So, we must build definition
5033 // of protocol's meta-data (not a reference to it!)
5034 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005035 llvm::Constant *Init =
5036 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5037 ObjCTypes.ExternalProtocolPtrTy);
5038
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005039 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005040 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005041
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005042 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5043 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005044 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005045 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005046 CGM.getModule(),
5047 Init->getType(), false,
5048 llvm::GlobalValue::WeakAnyLinkage,
5049 Init,
5050 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005051 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5052 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005053 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005054 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005055}
5056
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005057/// GenerateCategory - Build metadata for a category implementation.
5058/// struct _category_t {
5059/// const char * const name;
5060/// struct _class_t *const cls;
5061/// const struct _method_list_t * const instance_methods;
5062/// const struct _method_list_t * const class_methods;
5063/// const struct _protocol_list_t * const protocols;
5064/// const struct _prop_list_t * const properties;
5065/// }
5066///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005067void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005068 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005069 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005070 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5071 "_$_" + OCD->getNameAsString());
5072 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005073 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005074
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005075 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005076 Values[0] = GetClassName(OCD->getIdentifier());
5077 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005078 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005079 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005080 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5081
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005082 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005083 std::vector<llvm::Constant*> Methods;
5084 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005085 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005086 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087
5088 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005089 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005090 // Instance methods should always be defined.
5091 Methods.push_back(GetMethodConstant(*i));
5092 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093
5094 Values[2] = EmitMethodList(MethodListName,
5095 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005096 Methods);
5097
5098 MethodListName = Prefix;
5099 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5100 OCD->getNameAsString();
5101 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005102 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005103 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005104 // Class methods should always be defined.
5105 Methods.push_back(GetMethodConstant(*i));
5106 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005107
5108 Values[3] = EmitMethodList(MethodListName,
5109 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005110 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005111 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005112 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005113 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005114 llvm::SmallString<256> ExtName;
5115 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5116 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005117 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005118 + Interface->getName() + "_$_"
5119 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005120 Category->protocol_begin(),
5121 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005122 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5123 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005124 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005125 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5126 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005127 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005128
5129 llvm::Constant *Init =
5130 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005131 Values);
5132 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005133 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005134 false,
5135 llvm::GlobalValue::InternalLinkage,
5136 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005137 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005138 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005139 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005140 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005141 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005142 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005143
5144 // Determine if this category is also "non-lazy".
5145 if (ImplementationIsNonLazy(OCD))
5146 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005147 // method definition entries must be clear for next implementation.
5148 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005149}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005150
5151/// GetMethodConstant - Return a struct objc_method constant for the
5152/// given method if it has been defined. The result is null if the
5153/// method has not been defined. The return value has type MethodPtrTy.
5154llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005155 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005156 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005157 if (!Fn)
5158 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005159
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005160 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005161 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005162 ObjCTypes.SelectorPtrTy),
5163 GetMethodVarType(MD),
5164 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5165 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005166 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005167}
5168
5169/// EmitMethodList - Build meta-data for method declarations
5170/// struct _method_list_t {
5171/// uint32_t entsize; // sizeof(struct _objc_method)
5172/// uint32_t method_count;
5173/// struct _objc_method method_list[method_count];
5174/// }
5175///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005176llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005177 const char *Section,
5178 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005179 // Return null for empty list.
5180 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005181 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005182
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005183 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005184 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005185 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005186 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005187 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005188 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005189 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005190 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005191 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005192 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005193
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005194 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005195 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005196 llvm::GlobalValue::InternalLinkage, Init, Name);
5197 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005198 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005199 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005200 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005201}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005202
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005203/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5204/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005205llvm::GlobalVariable *
5206CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5207 const ObjCIvarDecl *Ivar) {
5208 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005209 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005210 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005212 CGM.getModule().getGlobalVariable(Name);
5213 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005214 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005215 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005216 false,
5217 llvm::GlobalValue::ExternalLinkage,
5218 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005219 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005220 return IvarOffsetGV;
5221}
5222
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005223llvm::Constant *
5224CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5225 const ObjCIvarDecl *Ivar,
5226 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005227 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005228 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005229 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005230 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005231 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005232
Mike Stump18bb9282009-05-16 07:57:57 +00005233 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5234 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005235 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5236 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005237 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005238 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005239 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005240 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005241 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005242 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005243}
5244
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005245/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005246/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005247/// IvarListnfABIPtrTy.
5248/// struct _ivar_t {
5249/// unsigned long int *offset; // pointer to ivar offset location
5250/// char *name;
5251/// char *type;
5252/// uint32_t alignment;
5253/// uint32_t size;
5254/// }
5255/// struct _ivar_list_t {
5256/// uint32 entsize; // sizeof(struct _ivar_t)
5257/// uint32 count;
5258/// struct _iver_t list[count];
5259/// }
5260///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005261
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005262llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005263 const ObjCImplementationDecl *ID) {
5264
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005265 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005266
Jordy Rosea91768e2011-07-22 02:08:32 +00005267 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005268 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005269
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005270 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005271
Jordy Rosea91768e2011-07-22 02:08:32 +00005272 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005273 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005274 // Ignore unnamed bit-fields.
5275 if (!IVD->getDeclName())
5276 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005277 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005278 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005279 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005280 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5281 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005282 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005283 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005284 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005285 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005286 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005287 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005288 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005289 // NOTE. Size of a bitfield does not match gcc's, because of the
5290 // way bitfields are treated special in each. But I am told that
5291 // 'size' for bitfield ivars is ignored by the runtime so it does
5292 // not matter. If it matters, there is enough info to get the
5293 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005294 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005295 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005296 }
5297 // Return null for empty list.
5298 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005299 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005300
5301 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005302 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005303 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5304 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005305 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005306 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005307 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005308 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005309 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5310 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005311 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005312 llvm::GlobalValue::InternalLinkage,
5313 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005314 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005315 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005316 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005317 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005318
Chris Lattnerf56501c2009-07-17 23:57:13 +00005319 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005320 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005321}
5322
5323llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005324 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005325 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005327 if (!Entry) {
5328 // We use the initializer as a marker of whether this is a forward
5329 // reference or not. At module finalization we add the empty
5330 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005331 Entry =
5332 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5333 llvm::GlobalValue::ExternalLinkage,
5334 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005335 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005336 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005337 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005338
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005339 return Entry;
5340}
5341
5342/// GetOrEmitProtocol - Generate the protocol meta-data:
5343/// @code
5344/// struct _protocol_t {
5345/// id isa; // NULL
5346/// const char * const protocol_name;
5347/// const struct _protocol_list_t * protocol_list; // super protocols
5348/// const struct method_list_t * const instance_methods;
5349/// const struct method_list_t * const class_methods;
5350/// const struct method_list_t *optionalInstanceMethods;
5351/// const struct method_list_t *optionalClassMethods;
5352/// const struct _prop_list_t * properties;
5353/// const uint32_t size; // sizeof(struct _protocol_t)
5354/// const uint32_t flags; // = 0
5355/// }
5356/// @endcode
5357///
5358
5359llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005360 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005361 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005362
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005363 // Early exit if a defining object has already been generated.
5364 if (Entry && Entry->hasInitializer())
5365 return Entry;
5366
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005367 // Construct method lists.
5368 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5369 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005371 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005372 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005373 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005374 if (!C)
5375 return GetOrEmitProtocolRef(PD);
5376
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005377 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5378 OptInstanceMethods.push_back(C);
5379 } else {
5380 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005381 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005382 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005383
5384 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005385 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005386 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005387 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005388 if (!C)
5389 return GetOrEmitProtocolRef(PD);
5390
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5392 OptClassMethods.push_back(C);
5393 } else {
5394 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005395 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005396 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005397
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005398 llvm::Constant *Values[10];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005399 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005400 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005401 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005402 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5403 PD->protocol_begin(),
5404 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005405
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005406 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005407 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005408 "__DATA, __objc_const",
5409 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005410 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005411 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005412 "__DATA, __objc_const",
5413 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005414 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005415 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005416 "__DATA, __objc_const",
5417 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005418 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005419 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005420 "__DATA, __objc_const",
5421 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005422 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005423 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005424 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005425 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005426 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005427 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005428 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005429 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005430
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005431 if (Entry) {
5432 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005433 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005434 Entry->setInitializer(Init);
5435 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005436 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005437 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5438 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5439 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005440 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005441 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005442 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005443 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005444 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005445 CGM.AddUsedGlobal(Entry);
5446
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005447 // Use this protocol meta-data to build protocol list table in section
5448 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005449 llvm::GlobalVariable *PTGV =
5450 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5451 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5452 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005453 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005454 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005455 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005456 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005457 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005458 return Entry;
5459}
5460
5461/// EmitProtocolList - Generate protocol list meta-data:
5462/// @code
5463/// struct _protocol_list_t {
5464/// long protocol_count; // Note, this is 32/64 bit
5465/// struct _protocol_t[protocol_count];
5466/// }
5467/// @endcode
5468///
5469llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005470CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005471 ObjCProtocolDecl::protocol_iterator begin,
5472 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005473 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005474
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005475 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005476 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005477 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005479 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005480 llvm::SmallString<256> TmpName;
5481 Name.toVector(TmpName);
5482 llvm::GlobalVariable *GV =
5483 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005484 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005485 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005486
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005487 for (; begin != end; ++begin)
5488 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5489
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005490 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005491 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005492 ObjCTypes.ProtocolnfABIPtrTy));
5493
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005494 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005495 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005496 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005497 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005498 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005499 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005500 ProtocolRefs.size()),
5501 ProtocolRefs);
5502
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005503 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005504 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005505 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005506 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005507 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005508 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005509 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005510 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005511 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005512 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005513}
5514
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005515/// GetMethodDescriptionConstant - This routine build following meta-data:
5516/// struct _objc_method {
5517/// SEL _cmd;
5518/// char *method_type;
5519/// char *_imp;
5520/// }
5521
5522llvm::Constant *
5523CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005524 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005525 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005526 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5527 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005528 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005529 if (!Desc[1])
5530 return 0;
5531
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005532 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005533 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005534 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005535}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005536
5537/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5538/// This code gen. amounts to generating code for:
5539/// @code
5540/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5541/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005542///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005543LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005544 CodeGen::CodeGenFunction &CGF,
5545 QualType ObjectTy,
5546 llvm::Value *BaseValue,
5547 const ObjCIvarDecl *Ivar,
5548 unsigned CVRQualifiers) {
5549 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005550 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5551 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005552}
5553
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005554llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555 CodeGen::CodeGenFunction &CGF,
5556 const ObjCInterfaceDecl *Interface,
5557 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005558 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005559}
5560
John McCall234eac82011-05-13 23:16:18 +00005561static void appendSelectorForMessageRefTable(std::string &buffer,
5562 Selector selector) {
5563 if (selector.isUnarySelector()) {
5564 buffer += selector.getNameForSlot(0);
5565 return;
5566 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005567
John McCall234eac82011-05-13 23:16:18 +00005568 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5569 buffer += selector.getNameForSlot(i);
5570 buffer += '_';
5571 }
5572}
5573
John McCall9e8bb002011-05-14 03:10:52 +00005574/// Emit a "v-table" message send. We emit a weak hidden-visibility
5575/// struct, initially containing the selector pointer and a pointer to
5576/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5577/// load and call the function pointer, passing the address of the
5578/// struct as the second parameter. The runtime determines whether
5579/// the selector is currently emitted using vtable dispatch; if so, it
5580/// substitutes a stub function which simply tail-calls through the
5581/// appropriate vtable slot, and if not, it substitues a stub function
5582/// which tail-calls objc_msgSend. Both stubs adjust the selector
5583/// argument to correctly point to the selector.
5584RValue
5585CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5586 ReturnValueSlot returnSlot,
5587 QualType resultType,
5588 Selector selector,
5589 llvm::Value *arg0,
5590 QualType arg0Type,
5591 bool isSuper,
5592 const CallArgList &formalArgs,
5593 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005594 // Compute the actual arguments.
5595 CallArgList args;
5596
John McCall9e8bb002011-05-14 03:10:52 +00005597 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005598 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005599 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5600 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005601
John McCall9e8bb002011-05-14 03:10:52 +00005602 // Second argument: a pointer to the message ref structure. Leave
5603 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005604 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5605
5606 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5607
5608 const CGFunctionInfo &fnInfo =
5609 CGM.getTypes().getFunctionInfo(resultType, args,
5610 FunctionType::ExtInfo());
5611
John McCall5880fb82011-05-14 21:12:11 +00005612 NullReturnState nullReturn;
5613
John McCall9e8bb002011-05-14 03:10:52 +00005614 // Find the function to call and the mangled name for the message
5615 // ref structure. Using a different mangled name wouldn't actually
5616 // be a problem; it would just be a waste.
5617 //
5618 // The runtime currently never uses vtable dispatch for anything
5619 // except normal, non-super message-sends.
5620 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005621 llvm::Constant *fn = 0;
5622 std::string messageRefName("\01l_");
5623 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005624 if (isSuper) {
5625 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5626 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005627 } else {
John McCall5880fb82011-05-14 21:12:11 +00005628 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005629 fn = ObjCTypes.getMessageSendStretFixupFn();
5630 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005631 }
John McCall234eac82011-05-13 23:16:18 +00005632 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5633 fn = ObjCTypes.getMessageSendFpretFixupFn();
5634 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005635 } else {
John McCall234eac82011-05-13 23:16:18 +00005636 if (isSuper) {
5637 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5638 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005639 } else {
John McCall234eac82011-05-13 23:16:18 +00005640 fn = ObjCTypes.getMessageSendFixupFn();
5641 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005642 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005643 }
John McCall234eac82011-05-13 23:16:18 +00005644 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5645 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005646
John McCall234eac82011-05-13 23:16:18 +00005647 // Append the selector name, except use underscores anywhere we
5648 // would have used colons.
5649 appendSelectorForMessageRefTable(messageRefName, selector);
5650
5651 llvm::GlobalVariable *messageRef
5652 = CGM.getModule().getGlobalVariable(messageRefName);
5653 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005654 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005655 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005656 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005657 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5658 init->getType(),
5659 /*constant*/ false,
5660 llvm::GlobalValue::WeakAnyLinkage,
5661 init,
5662 messageRefName);
5663 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5664 messageRef->setAlignment(16);
5665 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5666 }
5667 llvm::Value *mref =
5668 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5669
John McCall9e8bb002011-05-14 03:10:52 +00005670 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005671 args[1].RV = RValue::get(mref);
5672
5673 // Load the function to call from the message ref table.
5674 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5675 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5676
5677 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005678 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005679 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5680 callee = CGF.Builder.CreateBitCast(callee,
5681 llvm::PointerType::getUnqual(fnType));
5682
John McCall5880fb82011-05-14 21:12:11 +00005683 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5684 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005685}
5686
5687/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005688CodeGen::RValue
5689CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005690 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005691 QualType ResultType,
5692 Selector Sel,
5693 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005694 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005695 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005696 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005697 return isVTableDispatchedSelector(Sel)
5698 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005700 false, CallArgs, Method)
5701 : EmitMessageSend(CGF, Return, ResultType,
5702 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005704 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005705}
5706
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005707llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005708CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005709 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5710
Daniel Dunbara6468342009-03-02 05:18:14 +00005711 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005712 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005713 false, llvm::GlobalValue::ExternalLinkage,
5714 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005715 }
5716
5717 return GV;
5718}
5719
John McCall31168b02011-06-15 23:02:42 +00005720llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5721 IdentifierInfo *II) {
5722 llvm::GlobalVariable *&Entry = ClassReferences[II];
5723
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005724 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005725 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005726 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005727 Entry =
John McCall31168b02011-06-15 23:02:42 +00005728 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5729 false, llvm::GlobalValue::InternalLinkage,
5730 ClassGV,
5731 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005732 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005733 CGM.getTargetData().getABITypeAlignment(
5734 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005735 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005736 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005737 }
John McCall31168b02011-06-15 23:02:42 +00005738
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005739 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005740}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005741
John McCall31168b02011-06-15 23:02:42 +00005742llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5743 const ObjCInterfaceDecl *ID) {
5744 return EmitClassRefFromId(Builder, ID->getIdentifier());
5745}
5746
5747llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5748 CGBuilderTy &Builder) {
5749 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5750 return EmitClassRefFromId(Builder, II);
5751}
5752
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005753llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005754CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005755 const ObjCInterfaceDecl *ID) {
5756 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005757
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005758 if (!Entry) {
5759 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5760 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761 Entry =
5762 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005763 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005764 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005765 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005766 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005767 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005768 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005769 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005770 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005771 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005772
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005773 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005774}
5775
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005776/// EmitMetaClassRef - Return a Value * of the address of _class_t
5777/// meta-data
5778///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005779llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5780 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005781 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5782 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005783 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005784
Daniel Dunbar15894b72009-04-07 05:48:37 +00005785 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005786 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005787 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005788 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005789 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005790 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005791 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005792 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005793 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005794 ObjCTypes.ClassnfABIPtrTy));
5795
Daniel Dunbare60aa052009-04-15 19:03:14 +00005796 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005797 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005798
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005799 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005800}
5801
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005802/// GetClass - Return a reference to the class for the given interface
5803/// decl.
5804llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5805 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005806 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005807 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5808 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5809 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5810 }
5811
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005812 return EmitClassRef(Builder, ID);
5813}
5814
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005815/// Generates a message send where the super is the receiver. This is
5816/// a message send to self with special delivery semantics indicating
5817/// which class's method should be called.
5818CodeGen::RValue
5819CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005820 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005821 QualType ResultType,
5822 Selector Sel,
5823 const ObjCInterfaceDecl *Class,
5824 bool isCategoryImpl,
5825 llvm::Value *Receiver,
5826 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005827 const CodeGen::CallArgList &CallArgs,
5828 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005829 // ...
5830 // Create and init a super structure; this is a (receiver, class)
5831 // pair we will pass to objc_msgSendSuper.
5832 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005833 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005834
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005835 llvm::Value *ReceiverAsObject =
5836 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5837 CGF.Builder.CreateStore(ReceiverAsObject,
5838 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005839
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005840 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005841 llvm::Value *Target;
5842 if (IsClassMessage) {
5843 if (isCategoryImpl) {
5844 // Message sent to "super' in a class method defined in
5845 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005846 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005847 Target = CGF.Builder.CreateStructGEP(Target, 0);
5848 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005849 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005850 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005851 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005852 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005853
Mike Stump18bb9282009-05-16 07:57:57 +00005854 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5855 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00005856 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005857 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5858 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5859 CGF.Builder.CreateStore(Target,
5860 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005861
John McCall9e8bb002011-05-14 03:10:52 +00005862 return (isVTableDispatchedSelector(Sel))
5863 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005864 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005865 true, CallArgs, Method)
5866 : EmitMessageSend(CGF, Return, ResultType,
5867 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005868 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005869 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005870}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005871
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005872llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005873 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005874 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005876 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005877 llvm::Constant *Casted =
5878 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5879 ObjCTypes.SelectorPtrTy);
5880 Entry =
5881 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5882 llvm::GlobalValue::InternalLinkage,
5883 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005884 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005885 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005886 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005887
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005888 if (lval)
5889 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005890 return Builder.CreateLoad(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005891}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005892/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005893/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005894///
5895void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005896 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005897 llvm::Value *dst,
5898 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005899 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005900 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005901 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005902 assert(Size <= 8 && "does not support size > 8");
5903 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5904 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005905 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5906 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005907 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5908 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005909 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5910 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005911 return;
5912}
5913
5914/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5915/// objc_assign_strongCast (id src, id *dst)
5916///
5917void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005918 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005919 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005920 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005921 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005922 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005923 assert(Size <= 8 && "does not support size > 8");
5924 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005925 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005926 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5927 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005928 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5929 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005930 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005931 src, dst, "weakassign");
5932 return;
5933}
5934
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005935void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005936 CodeGen::CodeGenFunction &CGF,
5937 llvm::Value *DestPtr,
5938 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005939 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005940 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5941 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005942 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005943 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005944 return;
5945}
5946
Fariborz Jahanian06292952009-02-16 22:52:32 +00005947/// EmitObjCWeakRead - Code gen for loading value of a __weak
5948/// object: objc_read_weak (id *src)
5949///
5950llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005951 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005952 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005953 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005954 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5955 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005956 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005957 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005958 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005959 return read_weak;
5960}
5961
5962/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5963/// objc_assign_weak (id src, id *dst)
5964///
5965void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005966 llvm::Value *src, llvm::Value *dst) {
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);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005977 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005978 src, dst, "weakassign");
5979 return;
5980}
5981
5982/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5983/// objc_assign_global (id src, id *dst)
5984///
5985void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005986 llvm::Value *src, llvm::Value *dst,
5987 bool threadlocal) {
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)
5993 : 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);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005998 if (!threadlocal)
5999 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6000 src, dst, "globalassign");
6001 else
6002 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6003 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006004 return;
6005}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006006
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006007void
John McCallbd309292010-07-06 01:34:17 +00006008CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6009 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006010 EmitAtSynchronizedStmt(CGF, S,
6011 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6012 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006013}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006014
John McCall2ca705e2010-07-24 00:37:23 +00006015llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006016CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006017 // There's a particular fixed type info for 'id'.
6018 if (T->isObjCIdType() ||
6019 T->isObjCQualifiedIdType()) {
6020 llvm::Constant *IDEHType =
6021 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6022 if (!IDEHType)
6023 IDEHType =
6024 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6025 false,
6026 llvm::GlobalValue::ExternalLinkage,
6027 0, "OBJC_EHTYPE_id");
6028 return IDEHType;
6029 }
6030
6031 // All other types should be Objective-C interface pointer types.
6032 const ObjCObjectPointerType *PT =
6033 T->getAs<ObjCObjectPointerType>();
6034 assert(PT && "Invalid @catch type.");
6035 const ObjCInterfaceType *IT = PT->getInterfaceType();
6036 assert(IT && "Invalid @catch type.");
6037 return GetInterfaceEHType(IT->getDecl(), false);
6038}
6039
John McCallbd309292010-07-06 01:34:17 +00006040void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6041 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006042 EmitTryCatchStmt(CGF, S,
6043 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6044 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6045 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006046}
6047
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006048/// EmitThrowStmt - Generate code for a throw statement.
6049void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6050 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006051 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006052 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006053 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006054 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006055 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006056 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006057 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006058 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006059 }
6060
John McCall17afe452010-10-16 08:21:07 +00006061 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006062 CGF.Builder.ClearInsertionPoint();
6063}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006064
John McCall2ca705e2010-07-24 00:37:23 +00006065llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006066CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006067 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006068 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006069
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006070 // If we don't need a definition, return the entry if found or check
6071 // if we use an external reference.
6072 if (!ForDefinition) {
6073 if (Entry)
6074 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006075
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006076 // If this type (or a super class) has the __objc_exception__
6077 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006078 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006079 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006080 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006081 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006082 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006083 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006084 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006085 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006086
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006087 // Otherwise we need to either make a new entry or fill in the
6088 // initializer.
6089 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006090 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006091 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006092 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006093 CGM.getModule().getGlobalVariable(VTableName);
6094 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006095 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6096 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006097 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006098 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006099
Chris Lattner5e016ae2010-06-27 07:15:29 +00006100 llvm::Value *VTableIdx =
6101 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006102
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006103 llvm::Constant *Values[] = {
6104 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6105 GetClassName(ID->getIdentifier()),
6106 GetClassGlobal(ClassName)
6107 };
Owen Anderson170229f2009-07-14 23:10:40 +00006108 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006109 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006110
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006111 if (Entry) {
6112 Entry->setInitializer(Init);
6113 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006114 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006115 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006116 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006117 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006118 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006119 }
6120
John McCall457a04e2010-10-22 21:05:15 +00006121 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006122 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006123 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6124 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006125
6126 if (ForDefinition) {
6127 Entry->setSection("__DATA,__objc_const");
6128 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6129 } else {
6130 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6131 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006132
6133 return Entry;
6134}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006135
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006136/* *** */
6137
Daniel Dunbarb036db82008-08-13 03:21:16 +00006138CodeGen::CGObjCRuntime *
6139CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006140 if (CGM.getLangOptions().ObjCNonFragileABI)
6141 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006142 return new CGObjCMac(CGM);
6143}