blob: a782db76f68066028c09282f00c33c2492d06085 [file] [log] [blame]
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbar303e2c22008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045
Daniel Dunbar303e2c22008-08-11 02:45:11 +000046namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000047
Daniel Dunbar59e476b2009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000049
Daniel Dunbar59e476b2009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000052
Fariborz Jahanian279eda62009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000056
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000057private:
John McCall9dc0db22011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCall31168b02011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000073 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000074
John McCall9dc0db22011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000085
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000087
John McCall9dc0db22011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall9dc0db22011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000099
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000101
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000102 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
103 ///
104 /// The messenger used when the return value is returned in two values on the
105 /// x87 floating point stack; without a special entrypoint, the nil case
106 /// would be unbalanced. Only used on 64-bit X86.
107 llvm::Constant *getMessageSendFp2retFn() const {
108 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
109 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
110 llvm::Type *resultType =
111 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
112
113 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
114 params, true),
115 "objc_msgSend_fp2ret");
116 }
117
John McCall9dc0db22011-05-15 01:53:33 +0000118 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
119 ///
120 /// The messenger used for super calls, which have different dispatch
121 /// semantics. The class passed is the superclass of the current
122 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000123 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000124 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000126 params, true),
127 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000129
John McCall9dc0db22011-05-15 01:53:33 +0000130 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
131 ///
132 /// A slightly different messenger used for super calls. The class
133 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000134 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000135 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000136 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000137 params, true),
138 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000139 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000140
John McCall9dc0db22011-05-15 01:53:33 +0000141 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
142 /// SEL op, ...)
143 ///
144 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000145 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000146 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000147 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000148 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000149 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000150 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000151
John McCall9dc0db22011-05-15 01:53:33 +0000152 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
153 /// SEL op, ...)
154 ///
155 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000157 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000158 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000159 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000161 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000162
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000163 llvm::Constant *getMessageSendSuperFpretFn() const {
164 // There is no objc_msgSendSuper_fpret? How can that work?
165 return getMessageSendSuperFn();
166 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000167
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000168 llvm::Constant *getMessageSendSuperFpretFn2() const {
169 // There is no objc_msgSendSuper_fpret? How can that work?
170 return getMessageSendSuperFn2();
171 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000173protected:
174 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000176public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000177 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000178 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000179
Daniel Dunbar5d715592008-08-12 05:28:47 +0000180 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000181 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000182
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000183 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000184 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000186 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000187 llvm::Type *SelectorPtrTy;
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),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000519 "_setjmp",
520 llvm::Attribute::ReturnsTwice);
Chris Lattnerc6406db2009-04-22 02:26:14 +0000521 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000522
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000523public:
524 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000525 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000526};
527
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000528/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000529/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000530class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000531public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000532
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000533 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000534 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000535
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000536 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000537 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000538
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000539 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000540 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000541
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000542 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000543 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000544
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000545 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000546 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000547
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000548 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000549 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000550
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000551 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000552 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000553
Fariborz Jahanian71394042009-01-23 23:53:38 +0000554 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000555 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000556
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000557 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000558 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000559
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000560 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000561 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000562
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000563 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000564 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000566 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000567 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000569 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000570 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000571
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000572 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000573 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000574
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000575 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000576
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000577 // MessageRefTy - LLVM for:
578 // struct _message_ref_t {
579 // IMP messenger;
580 // SEL name;
581 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000582 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000583 // MessageRefCTy - clang type for struct _message_ref_t
584 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000585
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000586 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000587 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000588 // MessageRefCPtrTy - clang type for struct _message_ref_t*
589 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000591 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000592 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000593
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000594 // SuperMessageRefTy - LLVM for:
595 // struct _super_message_ref_t {
596 // SUPER_IMP messenger;
597 // SEL name;
598 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000599 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000600
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000601 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000602 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000603
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000604 llvm::Constant *getMessageSendFixupFn() {
605 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000606 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000608 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000609 "objc_msgSend_fixup");
610 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendFpretFixupFn() {
613 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000614 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000615 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000616 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000617 "objc_msgSend_fpret_fixup");
618 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000620 llvm::Constant *getMessageSendStretFixupFn() {
621 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000622 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000623 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000624 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000625 "objc_msgSend_stret_fixup");
626 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000627
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000628 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000629 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000630 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000631 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000632 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000633 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000634 "objc_msgSendSuper2_fixup");
635 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000636
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000637 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000639 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000640 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000641 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000642 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000643 "objc_msgSendSuper2_stret_fixup");
644 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000645
Chris Lattnera7c00b42009-04-22 02:15:23 +0000646 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000647 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000648 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Chris Lattnera7c00b42009-04-22 02:15:23 +0000650 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000651
Chris Lattnera7c00b42009-04-22 02:15:23 +0000652 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000653 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000655 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000656 "objc_begin_catch");
657 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000658
Chris Lattnera5f58b02011-07-09 17:41:47 +0000659 llvm::StructType *EHTypeTy;
660 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000661
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000662 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
663 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000664};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000665
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000666class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000667public:
668 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000669 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000670 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000671 unsigned ivar_bytepos;
672 unsigned ivar_size;
673 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000674 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000675
676 // Allow sorting based on byte pos.
677 bool operator<(const GC_IVAR &b) const {
678 return ivar_bytepos < b.ivar_bytepos;
679 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000680 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000681
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000682 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000683 public:
684 unsigned skip;
685 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000686 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000687 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000688 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000689
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000690protected:
691 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000692 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000693 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000694 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000696 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000697 SmallVector<GC_IVAR, 16> SkipIvars;
698 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000699
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000700 /// LazySymbols - Symbols to generate a lazy reference for. See
701 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000702 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000703
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000704 /// DefinedSymbols - External symbols which are defined by this
705 /// module. The symbols in this list and LazySymbols are used to add
706 /// special linker symbols which ensure that Objective-C modules are
707 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000708 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000709
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000710 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000711 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000713 /// MethodVarNames - uniqued method variable names.
714 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000715
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000716 /// DefinedCategoryNames - list of category names in form Class_Category.
717 llvm::SetVector<std::string> DefinedCategoryNames;
718
Daniel Dunbarb036db82008-08-13 03:21:16 +0000719 /// MethodVarTypes - uniqued method type signatures. We have to use
720 /// a StringMap here because have no other unique reference.
721 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000722
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000723 /// MethodDefinitions - map of methods which have been defined in
724 /// this translation unit.
725 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000726
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000727 /// PropertyNames - uniqued method variable names.
728 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000729
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000730 /// ClassReferences - uniqued class references.
731 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000732
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000733 /// SelectorReferences - uniqued selector references.
734 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000735
Daniel Dunbarb036db82008-08-13 03:21:16 +0000736 /// Protocols - Protocols for which an objc_protocol structure has
737 /// been emitted. Forward declarations are handled by creating an
738 /// empty structure whose initializer is filled in when/if defined.
739 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000740
Daniel Dunbarc475d422008-10-29 22:36:39 +0000741 /// DefinedProtocols - Protocols which have actually been
742 /// defined. We should not need this, see FIXME in GenerateProtocol.
743 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000744
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000745 /// DefinedClasses - List of defined classes.
746 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000747
748 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
749 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000751 /// DefinedCategories - List of defined categories.
752 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000753
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000754 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
755 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000756
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000757 /// GetNameForMethod - Return a name for the given method.
758 /// \param[out] NameOut - The return value.
759 void GetNameForMethod(const ObjCMethodDecl *OMD,
760 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000761 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000762
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000763 /// GetMethodVarName - Return a unique constant for the given
764 /// selector's name. The return value has type char *.
765 llvm::Constant *GetMethodVarName(Selector Sel);
766 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000767
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000768 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000769 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000771 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000772 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
773 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000774 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000775
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000776 /// GetPropertyName - Return a unique constant for the given
777 /// name. The return value has type char *.
778 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000780 // FIXME: This can be dropped once string functions are unified.
781 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
782 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000783
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000784 /// GetClassName - Return a unique constant for the given selector's
785 /// name. The return value has type char *.
786 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000787
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000788 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
789
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000790 /// BuildIvarLayout - Builds ivar layout bitmap for the class
791 /// implementation for the __strong or __weak case.
792 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000793 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
794 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000795
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000796 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000798 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000799 unsigned int BytePos, bool ForStrongLayout,
800 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000801 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000802 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000803 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +0000804 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000805 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000806 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000807
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000808 /// GetIvarLayoutName - Returns a unique constant for the given
809 /// ivar layout bitmap.
810 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
811 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000812
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000813 /// EmitPropertyList - Emit the given property list. The return
814 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000815 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000816 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000817 const ObjCContainerDecl *OCD,
818 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000819
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000820 /// EmitProtocolMethodTypes - Generate the array of extended method type
821 /// strings. The return value has type Int8PtrPtrTy.
822 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
823 const ConstantVector &MethodTypes,
824 const ObjCCommonTypesHelper &ObjCTypes);
825
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000826 /// PushProtocolProperties - Push protocol's property on the input stack.
827 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
828 std::vector<llvm::Constant*> &Properties,
829 const Decl *Container,
830 const ObjCProtocolDecl *PROTO,
831 const ObjCCommonTypesHelper &ObjCTypes);
832
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000833 /// GetProtocolRef - Return a reference to the internal protocol
834 /// description, creating an empty one if it has not been
835 /// defined. The return value has type ProtocolPtrTy.
836 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000837
Daniel Dunbar30c65362009-03-09 20:09:19 +0000838 /// CreateMetadataVar - Create a global variable with internal
839 /// linkage for use by the Objective-C runtime.
840 ///
841 /// This is a convenience wrapper which not only creates the
842 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000843 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000844 ///
845 /// \param Name - The variable name.
846 /// \param Init - The variable initializer; this is also used to
847 /// define the type of the variable.
848 /// \param Section - The section the variable should go into, or 0.
849 /// \param Align - The alignment for the variable, or 0.
850 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000851 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000852 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000853 llvm::Constant *Init,
854 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000855 unsigned Align,
856 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000857
John McCall9e8bb002011-05-14 03:10:52 +0000858 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
859 ReturnValueSlot Return,
860 QualType ResultType,
861 llvm::Value *Sel,
862 llvm::Value *Arg0,
863 QualType Arg0Ty,
864 bool IsSuper,
865 const CallArgList &CallArgs,
866 const ObjCMethodDecl *OMD,
867 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000868
Daniel Dunbar5e639272010-04-25 20:39:01 +0000869 /// EmitImageInfo - Emit the image info marker used to encode some module
870 /// level information.
871 void EmitImageInfo();
872
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000873public:
Owen Andersonae86c192009-07-13 04:10:07 +0000874 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000875 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000876
David Chisnall481e3a82010-01-23 02:40:42 +0000877 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000878
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000879 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
880 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000882 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000883
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000884 /// GetOrEmitProtocol - Get the protocol object for the given
885 /// declaration, emitting it if necessary. The return value has type
886 /// ProtocolPtrTy.
887 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000888
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000889 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
890 /// object for the given declaration, emitting it if needed. These
891 /// forward references will be filled in with empty bodies if no
892 /// definition is seen. The return value has type ProtocolPtrTy.
893 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000894 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
895 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000896
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000897};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000898
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000899class CGObjCMac : public CGObjCCommonMac {
900private:
901 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000902
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000903 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000904 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000905 void EmitModuleInfo();
906
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000907 /// EmitModuleSymols - Emit module symbols, the list of defined
908 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000909 llvm::Constant *EmitModuleSymbols();
910
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000911 /// FinishModule - Write out global data structures at the end of
912 /// processing a translation unit.
913 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000914
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000915 /// EmitClassExtension - Generate the class extension structure used
916 /// to store the weak ivar layout and properties. The return value
917 /// has type ClassExtensionPtrTy.
918 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
919
920 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
921 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000922 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000923 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000924
John McCall31168b02011-06-15 23:02:42 +0000925 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
926 IdentifierInfo *II);
927
928 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
929
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000930 /// EmitSuperClassRef - Emits reference to class's main metadata class.
931 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000932
933 /// EmitIvarList - Emit the ivar list for the given
934 /// implementation. If ForClass is true the list of class ivars
935 /// (i.e. metaclass ivars) is emitted, otherwise the list of
936 /// interface ivars will be emitted. The return value has type
937 /// IvarListPtrTy.
938 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000939 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000940
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000941 /// EmitMetaClass - Emit a forward reference to the class structure
942 /// for the metaclass of the given interface. The return value has
943 /// type ClassPtrTy.
944 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
945
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000946 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000947 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000948 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
949 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000950 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000951
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000952 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000953
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000954 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000955
956 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000957 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000958 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000959 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000960 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000961
962 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000963 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000964 /// - TypeName: The name for the type containing the methods.
965 /// - IsProtocol: True iff these methods are for a protocol.
966 /// - ClassMethds: True iff these are class methods.
967 /// - Required: When true, only "required" methods are
968 /// listed. Similarly, when false only "optional" methods are
969 /// listed. For classes this should always be true.
970 /// - begin, end: The method list to output.
971 ///
972 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000973 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000974 const char *Section,
975 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000976
Daniel Dunbarc475d422008-10-29 22:36:39 +0000977 /// GetOrEmitProtocol - Get the protocol object for the given
978 /// declaration, emitting it if necessary. The return value has type
979 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000980 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000981
982 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
983 /// object for the given declaration, emitting it if needed. These
984 /// forward references will be filled in with empty bodies if no
985 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000986 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000987
Daniel Dunbarb036db82008-08-13 03:21:16 +0000988 /// EmitProtocolExtension - Generate the protocol extension
989 /// structure used to store optional instance and class methods, and
990 /// protocol properties. The return value has type
991 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000992 llvm::Constant *
993 EmitProtocolExtension(const ObjCProtocolDecl *PD,
994 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000995 const ConstantVector &OptClassMethods,
996 const ConstantVector &MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000997
998 /// EmitProtocolList - Generate the list of referenced
999 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001000 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001001 ObjCProtocolDecl::protocol_iterator begin,
1002 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001003
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001004 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1005 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001006 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1007 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001008
1009public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001010 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001011
Fariborz Jahanian71394042009-01-23 23:53:38 +00001012 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001013
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001014 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001015 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001016 QualType ResultType,
1017 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001018 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001019 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001020 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001021 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001022
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001023 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001024 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001025 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001026 QualType ResultType,
1027 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001028 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001029 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001030 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001031 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001032 const CallArgList &CallArgs,
1033 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001034
Daniel Dunbarcb463852008-11-01 01:53:16 +00001035 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001036 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001037
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001038 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1039 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001040
1041 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1042 /// untyped one.
1043 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1044 const ObjCMethodDecl *Method);
1045
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001046 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001047
Daniel Dunbar92992502008-08-15 22:20:32 +00001048 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001049
Daniel Dunbar92992502008-08-15 22:20:32 +00001050 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001051
Daniel Dunbarcb463852008-11-01 01:53:16 +00001052 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001053 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001054
Chris Lattnerd4808922009-03-22 21:03:39 +00001055 virtual llvm::Constant *GetPropertyGetFunction();
1056 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001057 virtual llvm::Constant *GetGetStructFunction();
1058 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001059 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001060
John McCallbd309292010-07-06 01:34:17 +00001061 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1062 const ObjCAtTryStmt &S);
1063 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1064 const ObjCAtSynchronizedStmt &S);
1065 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001066 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1067 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001068 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001069 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001070 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001071 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001072 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001073 llvm::Value *src, llvm::Value *dest,
1074 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001075 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001076 llvm::Value *src, llvm::Value *dest,
1077 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001078 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1079 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001080 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1081 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001082 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001083
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001084 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1085 QualType ObjectTy,
1086 llvm::Value *BaseValue,
1087 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001088 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001089 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001090 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001091 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001092
1093 /// GetClassGlobal - Return the global variable for the Objective-C
1094 /// class of the given name.
1095 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001096 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001097 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001098};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001099
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001100class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001101private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001102 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001103 llvm::GlobalVariable* ObjCEmptyCacheVar;
1104 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001105
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001106 /// SuperClassReferences - uniqued super class references.
1107 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001108
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001109 /// MetaClassReferences - uniqued meta class references.
1110 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001111
1112 /// EHTypeReferences - uniqued class ehtype references.
1113 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001114
John McCall9e8bb002011-05-14 03:10:52 +00001115 /// VTableDispatchMethods - List of methods for which we generate
1116 /// vtable-based message dispatch.
1117 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001118
Fariborz Jahanian67260552009-11-17 21:37:35 +00001119 /// DefinedMetaClasses - List of defined meta-classes.
1120 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1121
John McCall9e8bb002011-05-14 03:10:52 +00001122 /// isVTableDispatchedSelector - Returns true if SEL is a
1123 /// vtable-based selector.
1124 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001125
Fariborz Jahanian71394042009-01-23 23:53:38 +00001126 /// FinishNonFragileABIModule - Write out global data structures at the end of
1127 /// processing a translation unit.
1128 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001129
Daniel Dunbar19573e72009-05-15 21:48:48 +00001130 /// AddModuleClassList - Add the given list of class pointers to the
1131 /// module with the provided symbol and section names.
1132 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1133 const char *SymbolName,
1134 const char *SectionName);
1135
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001136 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1137 unsigned InstanceStart,
1138 unsigned InstanceSize,
1139 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001140 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001141 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001142 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001143 llvm::Constant *ClassRoGV,
1144 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001145
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001146 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001147
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001148 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001149
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001150 /// EmitMethodList - Emit the method list for the given
1151 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001152 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001153 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001154 const ConstantVector &Methods);
1155 /// EmitIvarList - Emit the ivar list for the given
1156 /// implementation. If ForClass is true the list of class ivars
1157 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1158 /// interface ivars will be emitted. The return value has type
1159 /// IvarListnfABIPtrTy.
1160 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001162 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001163 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001164 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001165
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001166 /// GetOrEmitProtocol - Get the protocol object for the given
1167 /// declaration, emitting it if necessary. The return value has type
1168 /// ProtocolPtrTy.
1169 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001170
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001171 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1172 /// object for the given declaration, emitting it if needed. These
1173 /// forward references will be filled in with empty bodies if no
1174 /// definition is seen. The return value has type ProtocolPtrTy.
1175 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001176
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001177 /// EmitProtocolList - Generate the list of referenced
1178 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001179 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001180 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001181 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182
John McCall9e8bb002011-05-14 03:10:52 +00001183 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1184 ReturnValueSlot Return,
1185 QualType ResultType,
1186 Selector Sel,
1187 llvm::Value *Receiver,
1188 QualType Arg0Ty,
1189 bool IsSuper,
1190 const CallArgList &CallArgs,
1191 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001192
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001193 /// GetClassGlobal - Return the global variable for the Objective-C
1194 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001195 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001196
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001197 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001198 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001200 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001201
1202 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1203 IdentifierInfo *II);
1204
1205 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001206
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001207 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1208 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1210 const ObjCInterfaceDecl *ID);
1211
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001212 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1213 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001214 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001215 const ObjCInterfaceDecl *ID);
1216
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001217 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1218 /// the given ivar.
1219 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001220 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001221 const ObjCInterfaceDecl *ID,
1222 const ObjCIvarDecl *Ivar);
1223
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001224 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1225 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001226 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1227 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001228
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001229 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001230 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001231 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001232 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001233
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001235 return "OBJC_METACLASS_$_";
1236 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001237
Daniel Dunbar15894b72009-04-07 05:48:37 +00001238 const char *getClassSymbolPrefix() const {
1239 return "OBJC_CLASS_$_";
1240 }
1241
Daniel Dunbar961202372009-05-03 12:57:56 +00001242 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001243 uint32_t &InstanceStart,
1244 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001245
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001246 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001247 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001248 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1249 return CGM.getContext().Selectors.getSelector(0, &II);
1250 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001251
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001252 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001253 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1254 return CGM.getContext().Selectors.getSelector(1, &II);
1255 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001256
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001257 /// ImplementationIsNonLazy - Check whether the given category or
1258 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001259 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001260
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001261public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001262 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001263 // FIXME. All stubs for now!
1264 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001265
Fariborz Jahanian71394042009-01-23 23:53:38 +00001266 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001267 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001268 QualType ResultType,
1269 Selector Sel,
1270 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001271 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001272 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001273 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001274
1275 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001276 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001277 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001278 QualType ResultType,
1279 Selector Sel,
1280 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001281 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001282 llvm::Value *Receiver,
1283 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001284 const CallArgList &CallArgs,
1285 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001286
Fariborz Jahanian71394042009-01-23 23:53:38 +00001287 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001288 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001289
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001290 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1291 bool lvalue = false)
1292 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001293
1294 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1295 /// untyped one.
1296 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1297 const ObjCMethodDecl *Method)
1298 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001300 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001301
Fariborz Jahanian71394042009-01-23 23:53:38 +00001302 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001303 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001304 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001306 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001307
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001309 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001310 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001311 virtual llvm::Constant *GetPropertySetFunction() {
1312 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001313 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001314
David Chisnall168b80f2010-12-26 22:13:16 +00001315 virtual llvm::Constant *GetSetStructFunction() {
1316 return ObjCTypes.getCopyStructFn();
1317 }
1318 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001319 return ObjCTypes.getCopyStructFn();
1320 }
1321
Chris Lattnerd4808922009-03-22 21:03:39 +00001322 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001323 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001324 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001325
John McCallbd309292010-07-06 01:34:17 +00001326 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1327 const ObjCAtTryStmt &S);
1328 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1329 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001330 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001331 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001332 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001333 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001334 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001335 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001336 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001337 llvm::Value *src, llvm::Value *dest,
1338 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001339 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001340 llvm::Value *src, llvm::Value *dest,
1341 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001342 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001343 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001344 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1345 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001346 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001347 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1348 QualType ObjectTy,
1349 llvm::Value *BaseValue,
1350 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001351 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001352 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001353 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001354 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001355};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356
John McCall5880fb82011-05-14 21:12:11 +00001357/// A helper class for performing the null-initialization of a return
1358/// value.
1359struct NullReturnState {
1360 llvm::BasicBlock *NullBB;
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001361 llvm::BasicBlock *callBB;
1362 NullReturnState() : NullBB(0), callBB(0) {}
John McCall5880fb82011-05-14 21:12:11 +00001363
1364 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1365 // Make blocks for the null-init and call edges.
1366 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001367 callBB = CGF.createBasicBlock("msgSend.call");
John McCall5880fb82011-05-14 21:12:11 +00001368
1369 // Check for a null receiver and, if there is one, jump to the
1370 // null-init test.
1371 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1372 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1373
1374 // Otherwise, start performing the call.
1375 CGF.EmitBlock(callBB);
1376 }
1377
1378 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1379 if (!NullBB) return result;
1380
1381 // Finish the call path.
1382 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1383 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1384
1385 // Emit the null-init block and perform the null-initialization there.
1386 CGF.EmitBlock(NullBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001387 if (!resultType->isAnyComplexType()) {
1388 assert(result.isAggregate() && "null init of non-aggregate result?");
1389 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1390 // Jump to the continuation block.
1391 CGF.EmitBlock(contBB);
1392 return result;
1393 }
John McCall5880fb82011-05-14 21:12:11 +00001394
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001395 // _Complex type
1396 // FIXME. Now easy to handle any other scalar type whose result is returned
1397 // in memory due to ABI limitations.
John McCall5880fb82011-05-14 21:12:11 +00001398 CGF.EmitBlock(contBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001399 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1400 llvm::Type *MemberType = CallCV.first->getType();
1401 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1402 // Create phi instruction for scalar complex value.
1403 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1404 PHIReal->addIncoming(ZeroCV, NullBB);
1405 PHIReal->addIncoming(CallCV.first, callBB);
1406 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1407 PHIImag->addIncoming(ZeroCV, NullBB);
1408 PHIImag->addIncoming(CallCV.second, callBB);
1409 return RValue::getComplex(PHIReal, PHIImag);
John McCall5880fb82011-05-14 21:12:11 +00001410 }
1411};
1412
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001413} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001414
1415/* *** Helper Functions *** */
1416
1417/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001418static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001419 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001420 unsigned idx0,
1421 unsigned idx1) {
1422 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001423 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1424 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001425 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001426 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001427}
1428
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001429/// hasObjCExceptionAttribute - Return true if this class or any super
1430/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001431static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001432 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001433 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001434 return true;
1435 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001436 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001437 return false;
1438}
1439
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001440/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001441
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001442CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001443 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001444 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001445 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001446}
1447
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001448/// GetClass - Return a reference to the class for the given interface
1449/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001450llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001451 const ObjCInterfaceDecl *ID) {
1452 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001453}
1454
1455/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001456llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1457 bool lval) {
1458 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001459}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001460llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001461 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001462 return EmitSelector(Builder, Method->getSelector());
1463}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001464
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001465llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001466 if (T->isObjCIdType() ||
1467 T->isObjCQualifiedIdType()) {
1468 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001469 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001470 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001471 if (T->isObjCClassType() ||
1472 T->isObjCQualifiedClassType()) {
1473 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001474 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001475 }
1476 if (T->isObjCObjectPointerType())
1477 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1478
John McCall2ca705e2010-07-24 00:37:23 +00001479 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1480 return 0;
1481}
1482
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001483/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001484/*
1485 struct __builtin_CFString {
1486 const int *isa; // point to __CFConstantStringClassReference
1487 int flags;
1488 const char *str;
1489 long length;
1490 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001491*/
1492
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001493/// or Generate a constant NSString object.
1494/*
1495 struct __builtin_NSString {
1496 const int *isa; // point to __NSConstantStringClassReference
1497 const char *str;
1498 unsigned int length;
1499 };
1500*/
1501
Fariborz Jahanian71394042009-01-23 23:53:38 +00001502llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001503 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001504 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1505 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001506 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001507}
1508
1509/// Generates a message send where the super is the receiver. This is
1510/// a message send to self with special delivery semantics indicating
1511/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001512CodeGen::RValue
1513CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001514 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001515 QualType ResultType,
1516 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001517 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001518 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001519 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001520 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001521 const CodeGen::CallArgList &CallArgs,
1522 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001523 // Create and init a super structure; this is a (receiver, class)
1524 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001525 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001526 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001527 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001528 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001529 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001530 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001531
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001532 // If this is a class message the metaclass is passed as the target.
1533 llvm::Value *Target;
1534 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001535 if (isCategoryImpl) {
1536 // Message sent to 'super' in a class method defined in a category
1537 // implementation requires an odd treatment.
1538 // If we are in a class method, we must retrieve the
1539 // _metaclass_ for the current class, pointed at by
1540 // the class's "isa" pointer. The following assumes that
1541 // isa" is the first ivar in a class (which it must be).
1542 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1543 Target = CGF.Builder.CreateStructGEP(Target, 0);
1544 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001545 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001546 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1547 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1548 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1549 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001550 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001551 }
1552 else if (isCategoryImpl)
1553 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1554 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001555 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1556 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1557 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001558 }
Mike Stump18bb9282009-05-16 07:57:57 +00001559 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1560 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001561 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001562 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001563 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001564 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001565 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001566 return EmitMessageSend(CGF, Return, ResultType,
1567 EmitSelector(CGF.Builder, Sel),
1568 ObjCSuper, ObjCTypes.SuperPtrCTy,
1569 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001570}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001571
1572/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001573CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001574 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001575 QualType ResultType,
1576 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001577 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001578 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001579 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001580 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001581 return EmitMessageSend(CGF, Return, ResultType,
1582 EmitSelector(CGF.Builder, Sel),
1583 Receiver, CGF.getContext().getObjCIdType(),
1584 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001585}
1586
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001587CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001588CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1589 ReturnValueSlot Return,
1590 QualType ResultType,
1591 llvm::Value *Sel,
1592 llvm::Value *Arg0,
1593 QualType Arg0Ty,
1594 bool IsSuper,
1595 const CallArgList &CallArgs,
1596 const ObjCMethodDecl *Method,
1597 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001598 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001599 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001600 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001601 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1602 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001603 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001604
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001605 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001606 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001607 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001608 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001609 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001610
Anders Carlsson280e61f12010-06-21 20:59:55 +00001611 if (Method)
1612 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1613 CGM.getContext().getCanonicalType(ResultType) &&
1614 "Result type mismatch!");
1615
John McCall5880fb82011-05-14 21:12:11 +00001616 NullReturnState nullReturn;
1617
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001618 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001619 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001620 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001621 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001622 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001623 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1624 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1625 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001626 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1627 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1628 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001629 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001630 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001631 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001632 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001633 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001634 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1635 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001636}
1637
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001638static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1639 if (FQT.isObjCGCStrong())
1640 return Qualifiers::Strong;
1641
John McCall31168b02011-06-15 23:02:42 +00001642 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001643 return Qualifiers::Weak;
1644
1645 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1646 return Qualifiers::Strong;
1647
1648 if (const PointerType *PT = FQT->getAs<PointerType>())
1649 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1650
1651 return Qualifiers::GCNone;
1652}
1653
John McCall351762c2011-02-07 10:33:21 +00001654llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1655 const CGBlockInfo &blockInfo) {
1656 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001657 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001658
Douglas Gregor79a91412011-09-13 17:21:33 +00001659 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001660 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001661 return nullPtr;
1662
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001663 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001664 SkipIvars.clear();
1665 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001666 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1667 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001668
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001669 // __isa is the first field in block descriptor and must assume by runtime's
1670 // convention that it is GC'able.
1671 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001672
1673 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1674
1675 // Calculate the basic layout of the block structure.
1676 const llvm::StructLayout *layout =
1677 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1678
1679 // Ignore the optional 'this' capture: C++ objects are not assumed
1680 // to be GC'ed.
1681
1682 // Walk the captured variables.
1683 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1684 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1685 const VarDecl *variable = ci->getVariable();
1686 QualType type = variable->getType();
1687
1688 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1689
1690 // Ignore constant captures.
1691 if (capture.isConstant()) continue;
1692
1693 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1694
1695 // __block variables are passed by their descriptor address.
1696 if (ci->isByRef()) {
1697 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001698 continue;
John McCall351762c2011-02-07 10:33:21 +00001699 }
1700
1701 assert(!type->isArrayType() && "array variable should not be caught");
1702 if (const RecordType *record = type->getAs<RecordType>()) {
1703 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001704 continue;
1705 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001706
John McCall351762c2011-02-07 10:33:21 +00001707 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1708 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1709
1710 if (GCAttr == Qualifiers::Strong)
1711 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1712 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001713 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001714 SkipIvars.push_back(GC_IVAR(fieldOffset,
1715 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001716 }
1717
1718 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001719 return nullPtr;
1720
1721 // Sort on byte position; captures might not be allocated in order,
1722 // and unions can do funny things.
1723 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1724 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001725
1726 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001727 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001728 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1729 printf("\n block variable layout for block: ");
1730 const unsigned char *s = (unsigned char*)BitMap.c_str();
1731 for (unsigned i = 0; i < BitMap.size(); i++)
1732 if (!(s[i] & 0xf0))
1733 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1734 else
1735 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1736 printf("\n");
1737 }
1738
1739 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001740}
1741
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001742llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001743 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001744 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001745 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001746 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1747
Owen Andersonade90fd2009-07-29 18:54:39 +00001748 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001749 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001750}
1751
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001752void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001753 // FIXME: We shouldn't need this, the protocol decl should contain enough
1754 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001755 DefinedProtocols.insert(PD->getIdentifier());
1756
1757 // If we have generated a forward reference to this protocol, emit
1758 // it now. Otherwise do nothing, the protocol objects are lazily
1759 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001760 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001761 GetOrEmitProtocol(PD);
1762}
1763
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001764llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001765 if (DefinedProtocols.count(PD->getIdentifier()))
1766 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001767
Daniel Dunbarc475d422008-10-29 22:36:39 +00001768 return GetOrEmitProtocolRef(PD);
1769}
1770
Daniel Dunbarb036db82008-08-13 03:21:16 +00001771/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001772// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1773struct _objc_protocol {
1774struct _objc_protocol_extension *isa;
1775char *protocol_name;
1776struct _objc_protocol_list *protocol_list;
1777struct _objc__method_prototype_list *instance_methods;
1778struct _objc__method_prototype_list *class_methods
1779};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001780
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001781See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001782*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001783llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1784 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1785
1786 // Early exit if a defining object has already been generated.
1787 if (Entry && Entry->hasInitializer())
1788 return Entry;
1789
Douglas Gregora715bff2012-01-01 19:51:50 +00001790 // Use the protocol definition, if there is one.
1791 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1792 PD = Def;
1793
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001794 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001795 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001796 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1797
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001798 // Construct method lists.
1799 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1800 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001801 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001802 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001803 i = PD->instmeth_begin(), e = PD->instmeth_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 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001811 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001812 } else {
1813 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001814 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001815 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001816 }
1817
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001818 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001819 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001820 ObjCMethodDecl *MD = *i;
1821 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001822 if (!C)
1823 return GetOrEmitProtocolRef(PD);
1824
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001825 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1826 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001827 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001828 } else {
1829 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001830 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001831 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001832 }
1833
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001834 MethodTypesExt.insert(MethodTypesExt.end(),
1835 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1836
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001837 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001838 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1839 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001840 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001841 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001842 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001843 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001844 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001845 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001846 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001847 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001848 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001849 ClassMethods)
1850 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001851 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001852 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001853
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001855 // Already created, fix the linkage and update the initializer.
1856 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857 Entry->setInitializer(Init);
1858 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001860 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001862 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001863 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001864 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001865 // FIXME: Is this necessary? Why only for protocol?
1866 Entry->setAlignment(4);
1867 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001868 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001869
1870 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001871}
1872
Daniel Dunbarc475d422008-10-29 22:36:39 +00001873llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001874 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1875
1876 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001877 // We use the initializer as a marker of whether this is a forward
1878 // reference or not. At module finalization we add the empty
1879 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001880 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001881 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001882 llvm::GlobalValue::ExternalLinkage,
1883 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001884 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001885 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001886 // FIXME: Is this necessary? Why only for protocol?
1887 Entry->setAlignment(4);
1888 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001889
Daniel Dunbarb036db82008-08-13 03:21:16 +00001890 return Entry;
1891}
1892
1893/*
1894 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001895 uint32_t size;
1896 struct objc_method_description_list *optional_instance_methods;
1897 struct objc_method_description_list *optional_class_methods;
1898 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001899 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001900 };
1901*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001902llvm::Constant *
1903CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1904 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001905 const ConstantVector &OptClassMethods,
1906 const ConstantVector &MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001907 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001908 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001909 llvm::Constant *Values[] = {
1910 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001911 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001912 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001913 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001914 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001915 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001916 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001917 OptClassMethods),
1918 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001919 ObjCTypes),
1920 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
1921 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001922 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001924 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001925 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001926 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001927 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001928
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001929 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001930 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001931
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001932 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001933 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001934 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001935 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001936}
1937
1938/*
1939 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940 struct objc_protocol_list *next;
1941 long count;
1942 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001943 };
1944*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001945llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001946CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001947 ObjCProtocolDecl::protocol_iterator begin,
1948 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001949 std::vector<llvm::Constant*> ProtocolRefs;
1950
Daniel Dunbardec75f82008-08-21 21:57:41 +00001951 for (; begin != end; ++begin)
1952 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001953
1954 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001955 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001956 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001957
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001958 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001959 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001960
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001961 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001962 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001963 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001964 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001965 ProtocolRefs.size() - 1);
1966 Values[2] =
1967 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1968 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001969 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001970
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001971 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001972 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001973 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001974 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001975 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001976}
1977
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001978void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1979 std::vector<llvm::Constant*> &Properties,
1980 const Decl *Container,
1981 const ObjCProtocolDecl *PROTO,
1982 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001983 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1984 E = PROTO->protocol_end(); P != E; ++P)
1985 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1986 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1987 E = PROTO->prop_end(); I != E; ++I) {
1988 const ObjCPropertyDecl *PD = *I;
1989 if (!PropertySet.insert(PD->getIdentifier()))
1990 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001991 llvm::Constant *Prop[] = {
1992 GetPropertyName(PD->getIdentifier()),
1993 GetPropertyTypeString(PD, Container)
1994 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001995 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1996 }
1997}
1998
Daniel Dunbarb036db82008-08-13 03:21:16 +00001999/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002000 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002001 const char * const name;
2002 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002003 };
2004
2005 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002006 uint32_t entsize; // sizeof (struct _objc_property)
2007 uint32_t prop_count;
2008 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002009 };
2010*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002011llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002012 const Decl *Container,
2013 const ObjCContainerDecl *OCD,
2014 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002015 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002016 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002017 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2018 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002019 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002020 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002021 llvm::Constant *Prop[] = {
2022 GetPropertyName(PD->getIdentifier()),
2023 GetPropertyTypeString(PD, Container)
2024 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002025 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002026 Prop));
2027 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002028 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002029 for (ObjCInterfaceDecl::all_protocol_iterator
2030 P = OID->all_referenced_protocol_begin(),
2031 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002032 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2033 ObjCTypes);
2034 }
2035 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2036 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2037 E = CD->protocol_end(); P != E; ++P)
2038 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2039 ObjCTypes);
2040 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002041
2042 // Return null for empty list.
2043 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002044 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002045
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002046 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002047 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002048 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002049 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2050 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002051 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002052 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002053 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002054 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002055
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002056 llvm::GlobalVariable *GV =
2057 CreateMetadataVar(Name, Init,
2058 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002059 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002060 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002061 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002062 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002063}
2064
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002065llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2066 const ConstantVector &MethodTypes,
2067 const ObjCCommonTypesHelper &ObjCTypes) {
2068 // Return null for empty list.
2069 if (MethodTypes.empty())
2070 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2071
2072 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2073 MethodTypes.size());
2074 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2075
2076 llvm::GlobalVariable *GV =
2077 CreateMetadataVar(Name, Init,
2078 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2079 (ObjCABI == 2) ? 8 : 4,
2080 true);
2081 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2082}
2083
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002084/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002085 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002086 int count;
2087 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002088 };
2089*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002090llvm::Constant *
2091CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002092 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002093 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002094 ObjCTypes.SelectorPtrTy),
2095 GetMethodVarType(MD)
2096 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002097 if (!Desc[1])
2098 return 0;
2099
Owen Anderson0e0189d2009-07-27 22:29:56 +00002100 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002101 Desc);
2102}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002103
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002104llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002105 const char *Section,
2106 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002107 // Return null for empty list.
2108 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002109 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002110
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002111 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002112 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002113 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002114 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002115 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002116 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002117
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002118 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002119 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002120 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002121}
2122
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002123/*
2124 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002125 char *category_name;
2126 char *class_name;
2127 struct _objc_method_list *instance_methods;
2128 struct _objc_method_list *class_methods;
2129 struct _objc_protocol_list *protocols;
2130 uint32_t size; // <rdar://4585769>
2131 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002132 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002133*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002134void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002135 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002136
Mike Stump18bb9282009-05-16 07:57:57 +00002137 // FIXME: This is poor design, the OCD should have a pointer to the category
2138 // decl. Additionally, note that Category can be null for the @implementation
2139 // w/o an @interface case. Sema should just create one for us as it does for
2140 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002141 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002142 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002143 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002144
2145 llvm::SmallString<256> ExtName;
2146 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2147 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002148
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002149 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002150 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002151 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002152 // Instance methods should always be defined.
2153 InstanceMethods.push_back(GetMethodConstant(*i));
2154 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002155 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002156 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002157 // Class methods should always be defined.
2158 ClassMethods.push_back(GetMethodConstant(*i));
2159 }
2160
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002161 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002162 Values[0] = GetClassName(OCD->getIdentifier());
2163 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002164 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002165 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002166 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002167 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002168 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002169 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002170 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002171 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002172 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002173 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002174 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002175 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002176 Category->protocol_begin(),
2177 Category->protocol_end());
2178 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002179 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002180 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002181 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002182
2183 // If there is no category @interface then there can be no properties.
2184 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002185 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002186 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002187 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002188 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002189 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002190
Owen Anderson0e0189d2009-07-27 22:29:56 +00002191 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002192 Values);
2193
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002194 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002195 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002196 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002197 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002198 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002199 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002200 // method definition entries must be clear for next implementation.
2201 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002202}
2203
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002204// FIXME: Get from somewhere?
2205enum ClassFlags {
2206 eClassFlags_Factory = 0x00001,
2207 eClassFlags_Meta = 0x00002,
2208 // <rdr://5142207>
2209 eClassFlags_HasCXXStructors = 0x02000,
2210 eClassFlags_Hidden = 0x20000,
2211 eClassFlags_ABI2_Hidden = 0x00010,
2212 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2213};
2214
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002215/*
2216 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002217 Class isa;
2218 Class super_class;
2219 const char *name;
2220 long version;
2221 long info;
2222 long instance_size;
2223 struct _objc_ivar_list *ivars;
2224 struct _objc_method_list *methods;
2225 struct _objc_cache *cache;
2226 struct _objc_protocol_list *protocols;
2227 // Objective-C 1.0 extensions (<rdr://4585769>)
2228 const char *ivar_layout;
2229 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002230 };
2231
2232 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002233*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002234void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002235 DefinedSymbols.insert(ID->getIdentifier());
2236
Chris Lattner86d7d912008-11-24 03:54:41 +00002237 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002239 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002240 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002241 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002242 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002243 Interface->all_referenced_protocol_begin(),
2244 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002245 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002246 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002247 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002248 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002249 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250
2251 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002252 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 Flags |= eClassFlags_Hidden;
2254
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002255 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002257 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002258 // Instance methods should always be defined.
2259 InstanceMethods.push_back(GetMethodConstant(*i));
2260 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002261 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002262 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002263 // Class methods should always be defined.
2264 ClassMethods.push_back(GetMethodConstant(*i));
2265 }
2266
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002267 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002268 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002269 ObjCPropertyImplDecl *PID = *i;
2270
2271 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2272 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2273
2274 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2275 if (llvm::Constant *C = GetMethodConstant(MD))
2276 InstanceMethods.push_back(C);
2277 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2278 if (llvm::Constant *C = GetMethodConstant(MD))
2279 InstanceMethods.push_back(C);
2280 }
2281 }
2282
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002283 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002284 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002285 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002286 // Record a reference to the super class.
2287 LazySymbols.insert(Super->getIdentifier());
2288
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002289 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002290 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002291 ObjCTypes.ClassPtrTy);
2292 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002293 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002294 }
2295 Values[ 2] = GetClassName(ID->getIdentifier());
2296 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002297 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2298 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2299 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002300 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002301 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002302 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002303 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002304 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002305 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002306 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002307 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002308 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002309 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002310 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002311 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002312 std::string Name("\01L_OBJC_CLASS_");
2313 Name += ClassName;
2314 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2315 // Check for a forward reference.
2316 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2317 if (GV) {
2318 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2319 "Forward metaclass reference has incorrect type.");
2320 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2321 GV->setInitializer(Init);
2322 GV->setSection(Section);
2323 GV->setAlignment(4);
2324 CGM.AddUsedGlobal(GV);
2325 }
2326 else
2327 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002328 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002329 // method definition entries must be clear for next implementation.
2330 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002331}
2332
2333llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2334 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002335 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002336 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002337 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002338
John McCall457a04e2010-10-22 21:05:15 +00002339 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002341
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002342 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002343 // The isa for the metaclass is the root of the hierarchy.
2344 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2345 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2346 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002347 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002348 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002349 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002350 // The super class for the metaclass is emitted as the name of the
2351 // super class. The runtime fixes this up to point to the
2352 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002353 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002354 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002355 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356 ObjCTypes.ClassPtrTy);
2357 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002358 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002359 }
2360 Values[ 2] = GetClassName(ID->getIdentifier());
2361 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002362 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2363 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2364 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002365 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002366 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002367 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002368 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002369 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002370 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002371 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002372 Values[ 9] = Protocols;
2373 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002374 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002375 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002376 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002377 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002378 Values);
2379
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002380 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002381 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002382
2383 // Check for a forward reference.
2384 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2385 if (GV) {
2386 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2387 "Forward metaclass reference has incorrect type.");
2388 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2389 GV->setInitializer(Init);
2390 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002391 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002392 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002393 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002394 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002395 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002396 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002397 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398
2399 return GV;
2400}
2401
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002402llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002403 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002404
Mike Stump18bb9282009-05-16 07:57:57 +00002405 // FIXME: Should we look these up somewhere other than the module. Its a bit
2406 // silly since we only generate these while processing an implementation, so
2407 // exactly one pointer would work if know when we entered/exitted an
2408 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002409
2410 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002411 // Previously, metaclass with internal linkage may have been defined.
2412 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002413 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2414 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002415 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2416 "Forward metaclass reference has incorrect type.");
2417 return GV;
2418 } else {
2419 // Generate as an external reference to keep a consistent
2420 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002421 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002422 llvm::GlobalValue::ExternalLinkage,
2423 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002424 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002425 }
2426}
2427
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002428llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2429 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2430
2431 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2432 true)) {
2433 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2434 "Forward class metadata reference has incorrect type.");
2435 return GV;
2436 } else {
2437 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2438 llvm::GlobalValue::ExternalLinkage,
2439 0,
2440 Name);
2441 }
2442}
2443
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444/*
2445 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002446 uint32_t size;
2447 const char *weak_ivar_layout;
2448 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002449 };
2450*/
2451llvm::Constant *
2452CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002453 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002454 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002456 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002457 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002458 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002459 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002460 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461
2462 // Return null if no extension bits are used.
2463 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002464 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002465
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002466 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002467 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002468 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002469 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002470 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002471}
2472
2473/*
2474 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002475 char *ivar_name;
2476 char *ivar_type;
2477 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002478 };
2479
2480 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002481 int ivar_count;
2482 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002483 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002484*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002486 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002487 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002488
2489 // When emitting the root class GCC emits ivar entries for the
2490 // actual class structure. It is not clear if we need to follow this
2491 // behavior; for now lets try and get away with not doing it. If so,
2492 // the cleanest solution would be to make up an ObjCInterfaceDecl
2493 // for the class.
2494 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002495 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002496
Jordy Rosea91768e2011-07-22 02:08:32 +00002497 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002498
Jordy Rosea91768e2011-07-22 02:08:32 +00002499 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002500 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002501 // Ignore unnamed bit-fields.
2502 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002503 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002504 llvm::Constant *Ivar[] = {
2505 GetMethodVarName(IVD->getIdentifier()),
2506 GetMethodVarType(IVD),
2507 llvm::ConstantInt::get(ObjCTypes.IntTy,
2508 ComputeIvarBaseOffset(CGM, OID, IVD))
2509 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002510 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002511 }
2512
2513 // Return null for empty list.
2514 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002515 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002516
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002517 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002518 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002519 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002520 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002521 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002522 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002523
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002524 llvm::GlobalVariable *GV;
2525 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002526 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002527 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002528 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002529 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002530 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002531 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002532 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002533 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002534}
2535
2536/*
2537 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002538 SEL method_name;
2539 char *method_types;
2540 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002541 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002542
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002543 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002544 struct objc_method_list *obsolete;
2545 int count;
2546 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002547 };
2548*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002549
2550/// GetMethodConstant - Return a struct objc_method constant for the
2551/// given method if it has been defined. The result is null if the
2552/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002553llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002554 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002555 if (!Fn)
2556 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002557
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002558 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002559 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002560 ObjCTypes.SelectorPtrTy),
2561 GetMethodVarType(MD),
2562 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2563 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002564 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002565}
2566
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002567llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002568 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002569 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002570 // Return null for empty list.
2571 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002572 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002573
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002574 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002575 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002576 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002577 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002578 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002579 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002580 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002581
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002582 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002583 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002584}
2585
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002586llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002587 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002588 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002589 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002590
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002591 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002592 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002593 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002594 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002595 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002596 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002597 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002598 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002599 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002600
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002601 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002602}
2603
Daniel Dunbar30c65362009-03-09 20:09:19 +00002604llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002605CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002606 llvm::Constant *Init,
2607 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002608 unsigned Align,
2609 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002610 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002611 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002612 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002613 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002614 if (Section)
2615 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002616 if (Align)
2617 GV->setAlignment(Align);
2618 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002619 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002620 return GV;
2621}
2622
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002623llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002624 // Abuse this interface function as a place to finalize.
2625 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002626 return NULL;
2627}
2628
Chris Lattnerd4808922009-03-22 21:03:39 +00002629llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002630 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002631}
2632
Chris Lattnerd4808922009-03-22 21:03:39 +00002633llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002634 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002635}
2636
David Chisnall168b80f2010-12-26 22:13:16 +00002637llvm::Constant *CGObjCMac::GetGetStructFunction() {
2638 return ObjCTypes.getCopyStructFn();
2639}
2640llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002641 return ObjCTypes.getCopyStructFn();
2642}
2643
Chris Lattnerd4808922009-03-22 21:03:39 +00002644llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002645 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002646}
2647
John McCallbd309292010-07-06 01:34:17 +00002648void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2649 return EmitTryOrSynchronizedStmt(CGF, S);
2650}
2651
2652void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2653 const ObjCAtSynchronizedStmt &S) {
2654 return EmitTryOrSynchronizedStmt(CGF, S);
2655}
2656
John McCall65bea082010-07-21 06:59:36 +00002657namespace {
John McCallcda666c2010-07-21 07:22:38 +00002658 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002659 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002660 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002661 llvm::Value *CallTryExitVar;
2662 llvm::Value *ExceptionData;
2663 ObjCTypesHelper &ObjCTypes;
2664 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002665 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002666 llvm::Value *CallTryExitVar,
2667 llvm::Value *ExceptionData,
2668 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002669 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002670 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2671
John McCall30317fd2011-07-12 20:27:29 +00002672 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002673 // Check whether we need to call objc_exception_try_exit.
2674 // In optimized code, this branch will always be folded.
2675 llvm::BasicBlock *FinallyCallExit =
2676 CGF.createBasicBlock("finally.call_exit");
2677 llvm::BasicBlock *FinallyNoCallExit =
2678 CGF.createBasicBlock("finally.no_call_exit");
2679 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2680 FinallyCallExit, FinallyNoCallExit);
2681
2682 CGF.EmitBlock(FinallyCallExit);
2683 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2684 ->setDoesNotThrow();
2685
2686 CGF.EmitBlock(FinallyNoCallExit);
2687
2688 if (isa<ObjCAtTryStmt>(S)) {
2689 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002690 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2691 // Save the current cleanup destination in case there's
2692 // control flow inside the finally statement.
2693 llvm::Value *CurCleanupDest =
2694 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2695
John McCall65bea082010-07-21 06:59:36 +00002696 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2697
John McCallcebe0ca2010-08-11 00:16:14 +00002698 if (CGF.HaveInsertPoint()) {
2699 CGF.Builder.CreateStore(CurCleanupDest,
2700 CGF.getNormalCleanupDestSlot());
2701 } else {
2702 // Currently, the end of the cleanup must always exist.
2703 CGF.EnsureInsertPoint();
2704 }
2705 }
John McCall65bea082010-07-21 06:59:36 +00002706 } else {
2707 // Emit objc_sync_exit(expr); as finally's sole statement for
2708 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002709 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002710 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2711 ->setDoesNotThrow();
2712 }
2713 }
2714 };
John McCall42227ed2010-07-31 23:20:56 +00002715
2716 class FragileHazards {
2717 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002718 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002719 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2720
2721 llvm::InlineAsm *ReadHazard;
2722 llvm::InlineAsm *WriteHazard;
2723
2724 llvm::FunctionType *GetAsmFnType();
2725
2726 void collectLocals();
2727 void emitReadHazard(CGBuilderTy &Builder);
2728
2729 public:
2730 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002731
John McCall42227ed2010-07-31 23:20:56 +00002732 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002733 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002734 };
2735}
2736
2737/// Create the fragile-ABI read and write hazards based on the current
2738/// state of the function, which is presumed to be immediately prior
2739/// to a @try block. These hazards are used to maintain correct
2740/// semantics in the face of optimization and the fragile ABI's
2741/// cavalier use of setjmp/longjmp.
2742FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2743 collectLocals();
2744
2745 if (Locals.empty()) return;
2746
2747 // Collect all the blocks in the function.
2748 for (llvm::Function::iterator
2749 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2750 BlocksBeforeTry.insert(&*I);
2751
2752 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2753
2754 // Create a read hazard for the allocas. This inhibits dead-store
2755 // optimizations and forces the values to memory. This hazard is
2756 // inserted before any 'throwing' calls in the protected scope to
2757 // reflect the possibility that the variables might be read from the
2758 // catch block if the call throws.
2759 {
2760 std::string Constraint;
2761 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2762 if (I) Constraint += ',';
2763 Constraint += "*m";
2764 }
2765
2766 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2767 }
2768
2769 // Create a write hazard for the allocas. This inhibits folding
2770 // loads across the hazard. This hazard is inserted at the
2771 // beginning of the catch path to reflect the possibility that the
2772 // variables might have been written within the protected scope.
2773 {
2774 std::string Constraint;
2775 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2776 if (I) Constraint += ',';
2777 Constraint += "=*m";
2778 }
2779
2780 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2781 }
2782}
2783
2784/// Emit a write hazard at the current location.
2785void FragileHazards::emitWriteHazard() {
2786 if (Locals.empty()) return;
2787
Jay Foad5bd375a2011-07-15 08:37:34 +00002788 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002789}
2790
John McCall42227ed2010-07-31 23:20:56 +00002791void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2792 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002793 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002794}
2795
2796/// Emit read hazards in all the protected blocks, i.e. all the blocks
2797/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002798void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002799 if (Locals.empty()) return;
2800
2801 CGBuilderTy Builder(CGF.getLLVMContext());
2802
2803 // Iterate through all blocks, skipping those prior to the try.
2804 for (llvm::Function::iterator
2805 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2806 llvm::BasicBlock &BB = *FI;
2807 if (BlocksBeforeTry.count(&BB)) continue;
2808
2809 // Walk through all the calls in the block.
2810 for (llvm::BasicBlock::iterator
2811 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2812 llvm::Instruction &I = *BI;
2813
2814 // Ignore instructions that aren't non-intrinsic calls.
2815 // These are the only calls that can possibly call longjmp.
2816 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2817 if (isa<llvm::IntrinsicInst>(I))
2818 continue;
2819
2820 // Ignore call sites marked nounwind. This may be questionable,
2821 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2822 llvm::CallSite CS(&I);
2823 if (CS.doesNotThrow()) continue;
2824
John McCall2dd7d442010-08-04 05:59:32 +00002825 // Insert a read hazard before the call. This will ensure that
2826 // any writes to the locals are performed before making the
2827 // call. If the call throws, then this is sufficient to
2828 // guarantee correctness as long as it doesn't also write to any
2829 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002830 Builder.SetInsertPoint(&BB, BI);
2831 emitReadHazard(Builder);
2832 }
2833 }
2834}
2835
2836static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2837 if (V) S.insert(V);
2838}
2839
2840void FragileHazards::collectLocals() {
2841 // Compute a set of allocas to ignore.
2842 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2843 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2844 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002845
2846 // Collect all the allocas currently in the function. This is
2847 // probably way too aggressive.
2848 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2849 for (llvm::BasicBlock::iterator
2850 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2851 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2852 Locals.push_back(&*I);
2853}
2854
2855llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002856 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002857 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2858 tys[i] = Locals[i]->getType();
2859 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002860}
2861
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002862/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002863
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002864 Objective-C setjmp-longjmp (sjlj) Exception Handling
2865 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002866
John McCallbd309292010-07-06 01:34:17 +00002867 A catch buffer is a setjmp buffer plus:
2868 - a pointer to the exception that was caught
2869 - a pointer to the previous exception data buffer
2870 - two pointers of reserved storage
2871 Therefore catch buffers form a stack, with a pointer to the top
2872 of the stack kept in thread-local storage.
2873
2874 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2875 objc_exception_try_exit pops the given catch buffer, which is
2876 required to be the top of the EH stack.
2877 objc_exception_throw pops the top of the EH stack, writes the
2878 thrown exception into the appropriate field, and longjmps
2879 to the setjmp buffer. It crashes the process (with a printf
2880 and an abort()) if there are no catch buffers on the stack.
2881 objc_exception_extract just reads the exception pointer out of the
2882 catch buffer.
2883
2884 There's no reason an implementation couldn't use a light-weight
2885 setjmp here --- something like __builtin_setjmp, but API-compatible
2886 with the heavyweight setjmp. This will be more important if we ever
2887 want to implement correct ObjC/C++ exception interactions for the
2888 fragile ABI.
2889
2890 Note that for this use of setjmp/longjmp to be correct, we may need
2891 to mark some local variables volatile: if a non-volatile local
2892 variable is modified between the setjmp and the longjmp, it has
2893 indeterminate value. For the purposes of LLVM IR, it may be
2894 sufficient to make loads and stores within the @try (to variables
2895 declared outside the @try) volatile. This is necessary for
2896 optimized correctness, but is not currently being done; this is
2897 being tracked as rdar://problem/8160285
2898
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002899 The basic framework for a @try-catch-finally is as follows:
2900 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002901 objc_exception_data d;
2902 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002903 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002904
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002905 objc_exception_try_enter(&d);
2906 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002907 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002908 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002909 // exception path
2910 id _caught = objc_exception_extract(&d);
2911
2912 // enter new try scope for handlers
2913 if (!setjmp(d.jmp_buf)) {
2914 ... match exception and execute catch blocks ...
2915
2916 // fell off end, rethrow.
2917 _rethrow = _caught;
2918 ... jump-through-finally to finally_rethrow ...
2919 } else {
2920 // exception in catch block
2921 _rethrow = objc_exception_extract(&d);
2922 _call_try_exit = false;
2923 ... jump-through-finally to finally_rethrow ...
2924 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002925 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002926 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002927
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002928 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002929 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002930 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002931
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002932 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002933 ... dispatch to finally destination ...
2934
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002935 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002936 objc_exception_throw(_rethrow);
2937
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002938 finally_end:
2939 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002940
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002941 This framework differs slightly from the one gcc uses, in that gcc
2942 uses _rethrow to determine if objc_exception_try_exit should be called
2943 and if the object should be rethrown. This breaks in the face of
2944 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002945
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002946 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002947
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002948 - If there are no catch blocks, then we avoid emitting the second
2949 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002950
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002951 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2952 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002953
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002954 - FIXME: If there is no @finally block we can do a few more
2955 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002956
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002957 Rethrows and Jumps-Through-Finally
2958 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002959
John McCallbd309292010-07-06 01:34:17 +00002960 '@throw;' is supported by pushing the currently-caught exception
2961 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002962
John McCallbd309292010-07-06 01:34:17 +00002963 Branches through the @finally block are handled with an ordinary
2964 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2965 exceptions are not compatible with C++ exceptions, and this is
2966 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002967
John McCallbd309292010-07-06 01:34:17 +00002968 @synchronized(expr) { stmt; } is emitted as if it were:
2969 id synch_value = expr;
2970 objc_sync_enter(synch_value);
2971 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002972*/
2973
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002974void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2975 const Stmt &S) {
2976 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002977
2978 // A destination for the fall-through edges of the catch handlers to
2979 // jump to.
2980 CodeGenFunction::JumpDest FinallyEnd =
2981 CGF.getJumpDestInCurrentScope("finally.end");
2982
2983 // A destination for the rethrow edge of the catch handlers to jump
2984 // to.
2985 CodeGenFunction::JumpDest FinallyRethrow =
2986 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002987
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002988 // For @synchronized, call objc_sync_enter(sync.expr). The
2989 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002990 // @synchronized. We can't avoid a temp here because we need the
2991 // value to be preserved. If the backend ever does liveness
2992 // correctly after setjmp, this will be unnecessary.
2993 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002994 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002995 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002996 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2997 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002998 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2999 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003000
3001 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3002 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003003 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003004
John McCall2dd7d442010-08-04 05:59:32 +00003005 // Allocate memory for the setjmp buffer. This needs to be kept
3006 // live throughout the try and catch blocks.
3007 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3008 "exceptiondata.ptr");
3009
John McCall42227ed2010-07-31 23:20:56 +00003010 // Create the fragile hazards. Note that this will not capture any
3011 // of the allocas required for exception processing, but will
3012 // capture the current basic block (which extends all the way to the
3013 // setjmp call) as "before the @try".
3014 FragileHazards Hazards(CGF);
3015
John McCallbd309292010-07-06 01:34:17 +00003016 // Create a flag indicating whether the cleanup needs to call
3017 // objc_exception_try_exit. This is true except when
3018 // - no catches match and we're branching through the cleanup
3019 // just to rethrow the exception, or
3020 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003021 // The setjmp-safety rule here is that we should always store to this
3022 // variable in a place that dominates the branch through the cleanup
3023 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003024 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003025 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003026
John McCall9916e3f2010-10-04 23:42:51 +00003027 // A slot containing the exception to rethrow. Only needed when we
3028 // have both a @catch and a @finally.
3029 llvm::Value *PropagatingExnVar = 0;
3030
John McCallbd309292010-07-06 01:34:17 +00003031 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003032 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003033 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003034 CallTryExitVar,
3035 ExceptionData,
3036 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003037
3038 // Enter a try block:
3039 // - Call objc_exception_try_enter to push ExceptionData on top of
3040 // the EH stack.
3041 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3042 ->setDoesNotThrow();
3043
3044 // - Call setjmp on the exception data buffer.
3045 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3046 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3047 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003048 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003049 llvm::CallInst *SetJmpResult =
3050 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3051 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003052 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003053
3054 // If setjmp returned 0, enter the protected block; otherwise,
3055 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003056 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3057 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003058 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003059 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3060 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003061
John McCallbd309292010-07-06 01:34:17 +00003062 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003063 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003064 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003065 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003066 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003067
3068 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003069
John McCallbd309292010-07-06 01:34:17 +00003070 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003071 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003072
John McCall42227ed2010-07-31 23:20:56 +00003073 // Don't optimize loads of the in-scope locals across this point.
3074 Hazards.emitWriteHazard();
3075
John McCallbd309292010-07-06 01:34:17 +00003076 // For a @synchronized (or a @try with no catches), just branch
3077 // through the cleanup to the rethrow block.
3078 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3079 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003080 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003081 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003082
3083 // Otherwise, we have to match against the caught exceptions.
3084 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003085 // Retrieve the exception object. We may emit multiple blocks but
3086 // nothing can cross this so the value is already in SSA form.
3087 llvm::CallInst *Caught =
3088 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3089 ExceptionData, "caught");
3090 Caught->setDoesNotThrow();
3091
John McCallbd309292010-07-06 01:34:17 +00003092 // Push the exception to rethrow onto the EH value stack for the
3093 // benefit of any @throws in the handlers.
3094 CGF.ObjCEHValueStack.push_back(Caught);
3095
Douglas Gregor96c79492010-04-23 22:50:49 +00003096 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003097
John McCall2dd7d442010-08-04 05:59:32 +00003098 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003099
John McCall2dd7d442010-08-04 05:59:32 +00003100 llvm::BasicBlock *CatchBlock = 0;
3101 llvm::BasicBlock *CatchHandler = 0;
3102 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003103 // Save the currently-propagating exception before
3104 // objc_exception_try_enter clears the exception slot.
3105 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3106 "propagating_exception");
3107 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3108
John McCall2dd7d442010-08-04 05:59:32 +00003109 // Enter a new exception try block (in case a @catch block
3110 // throws an exception).
3111 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3112 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003113
John McCall2dd7d442010-08-04 05:59:32 +00003114 llvm::CallInst *SetJmpResult =
3115 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3116 "setjmp.result");
3117 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003118 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003119
John McCall2dd7d442010-08-04 05:59:32 +00003120 llvm::Value *Threw =
3121 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3122
3123 CatchBlock = CGF.createBasicBlock("catch");
3124 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3125 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3126
3127 CGF.EmitBlock(CatchBlock);
3128 }
3129
3130 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003131
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003132 // Handle catch list. As a special case we check if everything is
3133 // matched and avoid generating code for falling off the end if
3134 // so.
3135 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003136 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3137 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003138
Douglas Gregor46a572b2010-04-26 16:46:50 +00003139 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003140 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003141
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003142 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003143 if (!CatchParam) {
3144 AllMatched = true;
3145 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003146 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003147
John McCallbd309292010-07-06 01:34:17 +00003148 // catch(id e) always matches under this ABI, since only
3149 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003150 // FIXME: For the time being we also match id<X>; this should
3151 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003152 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003153 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003154 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003155
John McCallbd309292010-07-06 01:34:17 +00003156 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003157 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003158 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3159
Anders Carlsson9396a892008-09-11 09:15:33 +00003160 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003161 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003162 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003163
3164 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003165 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003166 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
Anders Carlsson9396a892008-09-11 09:15:33 +00003168 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003169
3170 // The scope of the catch variable ends right here.
3171 CatchVarCleanups.ForceCleanup();
3172
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003173 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003174 break;
3175 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003176
Steve Naroff7cae42b2009-07-10 23:34:53 +00003177 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003178 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003179
3180 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003181 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3182 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003183
3184 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003185 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003186
John McCallbd309292010-07-06 01:34:17 +00003187 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003188 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3189 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003190 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003191
John McCallbd309292010-07-06 01:34:17 +00003192 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3193 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003194
3195 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003196 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003197
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003198 // Emit the @catch block.
3199 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003200
3201 // Collect any cleanups for the catch variable. The scope lasts until
3202 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003203 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003204
John McCall1c9c3fd2010-10-15 04:57:14 +00003205 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003206 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003207
John McCallbd309292010-07-06 01:34:17 +00003208 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003209 llvm::Value *Tmp =
3210 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003211 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003212 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003213
Anders Carlsson9396a892008-09-11 09:15:33 +00003214 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003215
3216 // We're done with the catch variable.
3217 CatchVarCleanups.ForceCleanup();
3218
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003219 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003220
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003221 CGF.EmitBlock(NextCatchBlock);
3222 }
3223
John McCallbd309292010-07-06 01:34:17 +00003224 CGF.ObjCEHValueStack.pop_back();
3225
John McCall2dd7d442010-08-04 05:59:32 +00003226 // If nothing wanted anything to do with the caught exception,
3227 // kill the extract call.
3228 if (Caught->use_empty())
3229 Caught->eraseFromParent();
3230
3231 if (!AllMatched)
3232 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3233
3234 if (HasFinally) {
3235 // Emit the exception handler for the @catch blocks.
3236 CGF.EmitBlock(CatchHandler);
3237
3238 // In theory we might now need a write hazard, but actually it's
3239 // unnecessary because there's no local-accessing code between
3240 // the try's write hazard and here.
3241 //Hazards.emitWriteHazard();
3242
John McCall9916e3f2010-10-04 23:42:51 +00003243 // Extract the new exception and save it to the
3244 // propagating-exception slot.
3245 assert(PropagatingExnVar);
3246 llvm::CallInst *NewCaught =
3247 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3248 ExceptionData, "caught");
3249 NewCaught->setDoesNotThrow();
3250 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3251
John McCall2dd7d442010-08-04 05:59:32 +00003252 // Don't pop the catch handler; the throw already did.
3253 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003254 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003255 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003256 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003257
John McCall42227ed2010-07-31 23:20:56 +00003258 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003259 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003260
John McCallbd309292010-07-06 01:34:17 +00003261 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003262 CGF.Builder.restoreIP(TryFallthroughIP);
3263 if (CGF.HaveInsertPoint())
3264 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003265 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003266 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003267
John McCallbd309292010-07-06 01:34:17 +00003268 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003269 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003270 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003271 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003272 // If we have a propagating-exception variable, check it.
3273 llvm::Value *PropagatingExn;
3274 if (PropagatingExnVar) {
3275 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003276
John McCall9916e3f2010-10-04 23:42:51 +00003277 // Otherwise, just look in the buffer for the exception to throw.
3278 } else {
3279 llvm::CallInst *Caught =
3280 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3281 ExceptionData);
3282 Caught->setDoesNotThrow();
3283 PropagatingExn = Caught;
3284 }
3285
3286 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003287 ->setDoesNotThrow();
3288 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003289 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003290
John McCall42227ed2010-07-31 23:20:56 +00003291 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003292}
3293
3294void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003295 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003296 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003297
Anders Carlssone005aa12008-09-09 16:16:55 +00003298 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003299 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003300 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003301 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003302 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003303 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003304 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003305 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003306 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003307
John McCallbd309292010-07-06 01:34:17 +00003308 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3309 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003310 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003311
3312 // Clear the insertion point to indicate we are in unreachable code.
3313 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003314}
3315
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003316/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003317/// object: objc_read_weak (id *src)
3318///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003319llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003320 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003321 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003322 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3323 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3324 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003325 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003326 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003327 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003328 return read_weak;
3329}
3330
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003331/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3332/// objc_assign_weak (id src, id *dst)
3333///
3334void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003335 llvm::Value *src, llvm::Value *dst) {
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 Jahanian50a12702008-11-19 17:34:06 +00003344 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3345 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003346 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003347 src, dst, "weakassign");
3348 return;
3349}
3350
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003351/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3352/// objc_assign_global (id src, id *dst)
3353///
3354void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003355 llvm::Value *src, llvm::Value *dst,
3356 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003357 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003358 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003359 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003360 assert(Size <= 8 && "does not support size > 8");
3361 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003362 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003363 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3364 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003365 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3366 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003367 if (!threadlocal)
3368 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3369 src, dst, "globalassign");
3370 else
3371 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3372 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003373 return;
3374}
3375
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003376/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003377/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003378///
3379void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003380 llvm::Value *src, llvm::Value *dst,
3381 llvm::Value *ivarOffset) {
3382 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003383 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003384 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003385 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003386 assert(Size <= 8 && "does not support size > 8");
3387 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003388 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003389 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3390 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003391 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3392 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003393 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3394 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003395 return;
3396}
3397
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003398/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3399/// objc_assign_strongCast (id src, id *dst)
3400///
3401void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003402 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003403 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003404 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003405 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003406 assert(Size <= 8 && "does not support size > 8");
3407 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003408 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003409 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3410 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003411 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3412 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003413 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003414 src, dst, "weakassign");
3415 return;
3416}
3417
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003418void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003419 llvm::Value *DestPtr,
3420 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003421 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003422 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3423 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003424 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003425 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003426 return;
3427}
3428
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003429/// EmitObjCValueForIvar - Code Gen for ivar reference.
3430///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003431LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3432 QualType ObjectTy,
3433 llvm::Value *BaseValue,
3434 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003435 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003436 const ObjCInterfaceDecl *ID =
3437 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003438 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3439 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003440}
3441
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003442llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003443 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003444 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003445 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003446 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003447 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3448 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003449}
3450
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003451/* *** Private Interface *** */
3452
3453/// EmitImageInfo - Emit the image info marker used to encode some module
3454/// level information.
3455///
3456/// See: <rdr://4810609&4810587&4810587>
3457/// struct IMAGE_INFO {
3458/// unsigned version;
3459/// unsigned flags;
3460/// };
3461enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003462 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003463 eImageInfo_GarbageCollected = (1 << 1),
3464 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003465 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3466
Daniel Dunbar5e639272010-04-25 20:39:01 +00003467 // A flag indicating that the module has no instances of a @synthesize of a
3468 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003469 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003470};
3471
Daniel Dunbar5e639272010-04-25 20:39:01 +00003472void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003473 unsigned version = 0; // Version is unused?
3474 unsigned flags = 0;
3475
3476 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003477 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003478 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003479 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003480 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003481
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003482 // We never allow @synthesize of a superclass property.
3483 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003484
Chris Lattner2192fe52011-07-18 04:24:23 +00003485 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner5e016ae2010-06-27 07:15:29 +00003486
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003487 // Emitted as int[2];
3488 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003489 llvm::ConstantInt::get(Int32Ty, version),
3490 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003491 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003492 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003493
3494 const char *Section;
3495 if (ObjCABI == 1)
3496 Section = "__OBJC, __image_info,regular";
3497 else
3498 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003499 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003500 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003501 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003502 Section,
3503 0,
3504 true);
3505 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003506}
3507
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003508
3509// struct objc_module {
3510// unsigned long version;
3511// unsigned long size;
3512// const char *name;
3513// Symtab symtab;
3514// };
3515
3516// FIXME: Get from somewhere
3517static const int ModuleVersion = 7;
3518
3519void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003520 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003521
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003522 llvm::Constant *Values[] = {
3523 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3524 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3525 // This used to be the filename, now it is unused. <rdr://4327263>
3526 GetClassName(&CGM.getContext().Idents.get("")),
3527 EmitModuleSymbols()
3528 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003529 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003530 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003531 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003532 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003533}
3534
3535llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003536 unsigned NumClasses = DefinedClasses.size();
3537 unsigned NumCategories = DefinedCategories.size();
3538
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003539 // Return null if no symbols were defined.
3540 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003541 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003542
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003543 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003544 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003545 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003546 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3547 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003548
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003549 // The runtime expects exactly the list of defined classes followed
3550 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003551 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003552 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003553 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003554 ObjCTypes.Int8PtrTy);
3555 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003556 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003557 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003558 ObjCTypes.Int8PtrTy);
3559
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003560 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003561 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003562 NumClasses + NumCategories),
3563 Symbols);
3564
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003565 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003566
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003567 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003568 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3569 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003570 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003571 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003572}
3573
John McCall31168b02011-06-15 23:02:42 +00003574llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3575 IdentifierInfo *II) {
3576 LazySymbols.insert(II);
3577
3578 llvm::GlobalVariable *&Entry = ClassReferences[II];
3579
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003580 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003581 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003582 llvm::ConstantExpr::getBitCast(GetClassName(II),
3583 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003584 Entry =
John McCall31168b02011-06-15 23:02:42 +00003585 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3586 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3587 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003588 }
John McCall31168b02011-06-15 23:02:42 +00003589
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003590 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003591}
3592
John McCall31168b02011-06-15 23:02:42 +00003593llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3594 const ObjCInterfaceDecl *ID) {
3595 return EmitClassRefFromId(Builder, ID->getIdentifier());
3596}
3597
3598llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3599 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3600 return EmitClassRefFromId(Builder, II);
3601}
3602
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003603llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3604 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003605 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003606
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003607 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003608 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003609 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003610 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003611 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003612 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3613 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003614 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003615 }
3616
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003617 if (lvalue)
3618 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003619 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003620}
3621
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003622llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003623 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003624
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003625 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003626 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003627 llvm::ConstantArray::get(VMContext,
3628 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003629 ((ObjCABI == 2) ?
3630 "__TEXT,__objc_classname,cstring_literals" :
3631 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003632 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003633
Owen Anderson170229f2009-07-14 23:10:40 +00003634 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003635}
3636
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003637llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3638 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3639 I = MethodDefinitions.find(MD);
3640 if (I != MethodDefinitions.end())
3641 return I->second;
3642
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003643 return NULL;
3644}
3645
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003646/// GetIvarLayoutName - Returns a unique constant for the given
3647/// ivar layout bitmap.
3648llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003649 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003650 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003651}
3652
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003653void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003654 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003655 bool ForStrongLayout,
3656 bool &HasUnion) {
3657 const RecordDecl *RD = RT->getDecl();
3658 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003659 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003660 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003661 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003662 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003663
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003664 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3665 ForStrongLayout, HasUnion);
3666}
3667
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003668void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003669 const llvm::StructLayout *Layout,
3670 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003671 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003672 unsigned int BytePos, bool ForStrongLayout,
3673 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003674 bool IsUnion = (RD && RD->isUnion());
3675 uint64_t MaxUnionIvarSize = 0;
3676 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003677 const FieldDecl *MaxField = 0;
3678 const FieldDecl *MaxSkippedField = 0;
3679 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003680 uint64_t MaxFieldOffset = 0;
3681 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003682 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003683 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003684
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003685 if (RecFields.empty())
3686 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003687 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3688 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003689 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003690 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003691 FirstFieldDelta =
3692 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3693 }
3694
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003695 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003696 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003697 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003698 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003699 // Note that 'i' here is actually the field index inside RD of Field,
3700 // although this dependency is hidden.
3701 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003702 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003703 } else
John McCall31168b02011-06-15 23:02:42 +00003704 FieldOffset =
3705 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003706
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003707 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003708 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003709 LastFieldBitfieldOrUnnamed = Field;
3710 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003711 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003712 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003713
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003714 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003715 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003716 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003717 if (FQT->isUnionType())
3718 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003719
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003720 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003721 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003722 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003723 continue;
3724 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003725
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003726 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003727 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003728 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003729 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003730 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003731 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003732 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3733 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003734 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003735 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003736 FQT = CArray->getElementType();
3737 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003738
3739 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003740 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003741 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003742 int OldIndex = IvarsInfo.size() - 1;
3743 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003744
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003745 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003746 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003747 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003748
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003749 // Replicate layout information for each array element. Note that
3750 // one element is already done.
3751 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752 for (int FirstIndex = IvarsInfo.size() - 1,
3753 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003754 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003755 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3756 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3757 IvarsInfo[i].ivar_size));
3758 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3759 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3760 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003761 }
3762 continue;
3763 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003764 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003765 // At this point, we are done with Record/Union and array there of.
3766 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003767 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003768
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003769 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003770 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3771 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003772 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003773 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003774 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003775 MaxUnionIvarSize = UnionIvarSize;
3776 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003777 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003778 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003779 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003780 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003781 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003782 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003783 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003784 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3785 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003786 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003787 // FIXME: Why the asymmetry? We divide by word size in bits on other
3788 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003789 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003790 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003791 MaxSkippedUnionIvarSize = UnionIvarSize;
3792 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003793 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003794 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003795 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003796 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003797 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003798 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003799 }
3800 }
3801 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003802
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003803 if (LastFieldBitfieldOrUnnamed) {
3804 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3805 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003806 uint64_t BitFieldSize
3807 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003808 GC_IVAR skivar;
3809 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3810 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3811 + ((BitFieldSize % ByteSizeInBits) != 0);
3812 SkipIvars.push_back(skivar);
3813 } else {
3814 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3815 // Last field was unnamed. Must update skip info.
3816 unsigned FieldSize
3817 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3818 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3819 FieldSize / ByteSizeInBits));
3820 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003821 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003822
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003823 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003824 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003825 MaxUnionIvarSize));
3826 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003827 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003828 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003829}
3830
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003831/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3832/// the computations and returning the layout bitmap (for ivar or blocks) in
3833/// the given argument BitMap string container. Routine reads
3834/// two containers, IvarsInfo and SkipIvars which are assumed to be
3835/// filled already by the caller.
3836llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003837 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003838 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003839
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003840 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003841 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003842 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003843 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003844 if (IvarsInfo[0].ivar_bytepos == 0) {
3845 WordsToSkip = 0;
3846 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003847 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003848 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3849 WordsToScan = IvarsInfo[0].ivar_size;
3850 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003851 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003852 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003853 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003854 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003855 // consecutive 'scanned' object pointers.
3856 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003857 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003858 // Skip over 'gc'able object pointer which lay over each other.
3859 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3860 continue;
3861 // Must skip over 1 or more words. We save current skip/scan values
3862 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003863 SKIP_SCAN SkScan;
3864 SkScan.skip = WordsToSkip;
3865 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003866 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003867
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003868 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003869 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3870 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003871 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003872 WordsToSkip = 0;
3873 WordsToScan = IvarsInfo[i].ivar_size;
3874 }
3875 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003876 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003877 SKIP_SCAN SkScan;
3878 SkScan.skip = WordsToSkip;
3879 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003880 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003881 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003882
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003883 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003884 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003885 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003886 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003887 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003888 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003889 IvarsInfo[LastIndex].ivar_bytepos +
3890 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003891 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003892 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003893 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003894 SKIP_SCAN SkScan;
3895 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3896 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003897 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003898 }
3899 }
3900 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3901 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003902 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003903 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003904 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3905 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3906 // 0xM0 followed by 0x0N detected.
3907 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3908 for (int j = i+1; j < SkipScan; j++)
3909 SkipScanIvars[j] = SkipScanIvars[j+1];
3910 --SkipScan;
3911 }
3912 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003913
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003914 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003915 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003916 unsigned char byte;
3917 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3918 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3919 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3920 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003921
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003922 // first skip big.
3923 for (unsigned int ix = 0; ix < skip_big; ix++)
3924 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003925
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003926 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003927 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003928 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003929 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003930 byte |= 0xf;
3931 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003932 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003933 byte |= scan_small;
3934 scan_small = 0;
3935 }
3936 BitMap += byte;
3937 }
3938 // next scan big
3939 for (unsigned int ix = 0; ix < scan_big; ix++)
3940 BitMap += (unsigned char)(0x0f);
3941 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003942 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003943 byte = scan_small;
3944 BitMap += byte;
3945 }
3946 }
3947 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003948 unsigned char zero = 0;
3949 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003950
3951 llvm::GlobalVariable * Entry =
3952 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3953 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003954 ((ObjCABI == 2) ?
3955 "__TEXT,__objc_classname,cstring_literals" :
3956 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003957 1, true);
3958 return getConstantGEP(VMContext, Entry, 0, 0);
3959}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003960
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003961/// BuildIvarLayout - Builds ivar layout bitmap for the class
3962/// implementation for the __strong or __weak case.
3963/// The layout map displays which words in ivar list must be skipped
3964/// and which must be scanned by GC (see below). String is built of bytes.
3965/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3966/// of words to skip and right nibble is count of words to scan. So, each
3967/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3968/// represented by a 0x00 byte which also ends the string.
3969/// 1. when ForStrongLayout is true, following ivars are scanned:
3970/// - id, Class
3971/// - object *
3972/// - __strong anything
3973///
3974/// 2. When ForStrongLayout is false, following ivars are scanned:
3975/// - __weak anything
3976///
3977llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3978 const ObjCImplementationDecl *OMD,
3979 bool ForStrongLayout) {
3980 bool hasUnion = false;
3981
Chris Lattner2192fe52011-07-18 04:24:23 +00003982 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00003983 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00003984 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003985 return llvm::Constant::getNullValue(PtrTy);
3986
Jordy Rosea91768e2011-07-22 02:08:32 +00003987 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3988 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003989 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003990 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003991 IVD; IVD = IVD->getNextIvar())
3992 RecFields.push_back(cast<FieldDecl>(IVD));
3993 }
3994 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00003995 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00003996 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003997
Jordy Rosea91768e2011-07-22 02:08:32 +00003998 // FIXME: This is not ideal; we shouldn't have to do this copy.
3999 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004000 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004001
4002 if (RecFields.empty())
4003 return llvm::Constant::getNullValue(PtrTy);
4004
4005 SkipIvars.clear();
4006 IvarsInfo.clear();
4007
4008 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4009 if (IvarsInfo.empty())
4010 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004011 // Sort on byte position in case we encounterred a union nested in
4012 // the ivar list.
4013 if (hasUnion && !IvarsInfo.empty())
4014 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4015 if (hasUnion && !SkipIvars.empty())
4016 std::sort(SkipIvars.begin(), SkipIvars.end());
4017
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004018 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004019 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004020
4021 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004022 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004023 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004024 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004025 const unsigned char *s = (unsigned char*)BitMap.c_str();
4026 for (unsigned i = 0; i < BitMap.size(); i++)
4027 if (!(s[i] & 0xf0))
4028 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4029 else
4030 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4031 printf("\n");
4032 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004033 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004034}
4035
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004036llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004037 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4038
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004039 // FIXME: Avoid std::string copying.
4040 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004041 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00004042 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004043 ((ObjCABI == 2) ?
4044 "__TEXT,__objc_methname,cstring_literals" :
4045 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004046 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004047
Owen Anderson170229f2009-07-14 23:10:40 +00004048 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004049}
4050
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004051// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004052llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004053 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4054}
4055
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004056llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004057 std::string TypeStr;
4058 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4059
4060 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004061
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004062 if (!Entry)
4063 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004064 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004065 ((ObjCABI == 2) ?
4066 "__TEXT,__objc_methtype,cstring_literals" :
4067 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004068 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004069
Owen Anderson170229f2009-07-14 23:10:40 +00004070 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004071}
4072
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004073llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4074 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004075 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00004076 if (CGM.getContext().getObjCEncodingForMethodDecl(
4077 const_cast<ObjCMethodDecl*>(D),
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004078 TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004079 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004080
4081 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4082
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004083 if (!Entry)
4084 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004085 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004086 ((ObjCABI == 2) ?
4087 "__TEXT,__objc_methtype,cstring_literals" :
4088 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004089 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004090
Owen Anderson170229f2009-07-14 23:10:40 +00004091 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004092}
4093
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004094// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004095llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004096 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004097
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004098 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004099 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004100 llvm::ConstantArray::get(VMContext,
4101 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004102 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004103 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004104
Owen Anderson170229f2009-07-14 23:10:40 +00004105 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004106}
4107
4108// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004109// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004110llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004111CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4112 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004113 std::string TypeStr;
4114 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004115 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4116}
4117
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004118void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004119 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004120 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004121 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004122 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004123 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4124 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004126 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004127 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004128 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004129}
4130
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004131void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004132 EmitModuleInfo();
4133
Daniel Dunbarc475d422008-10-29 22:36:39 +00004134 // Emit the dummy bodies for any protocols which were referenced but
4135 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004136 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004137 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4138 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004139 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004140
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004141 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004142 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004143 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004144 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004145 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004146 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004147 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004148 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004149 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004150 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004151 }
4152
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004153 // Add assembler directives to add lazy undefined symbol references
4154 // for classes which are referenced but not defined. This is
4155 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004156 //
4157 // FIXME: It would be nice if we had an LLVM construct for this.
4158 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4159 llvm::SmallString<256> Asm;
4160 Asm += CGM.getModule().getModuleInlineAsm();
4161 if (!Asm.empty() && Asm.back() != '\n')
4162 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004163
Daniel Dunbard027a922009-09-07 00:20:42 +00004164 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004165 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4166 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004167 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4168 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004169 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004170 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004171 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004172 }
4173
4174 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4175 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4176 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4177 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004178
Daniel Dunbard027a922009-09-07 00:20:42 +00004179 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004180 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004181}
4182
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004183CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004184 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004185 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004186 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004187 ObjCABI = 2;
4188}
4189
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004190/* *** */
4191
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004192ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004193 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004194 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4195 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004197 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004198 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004199 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004200 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004201 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004202 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004203
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004204 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004205 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004206 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004207
Mike Stump18bb9282009-05-16 07:57:57 +00004208 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4209 // comes out a bit cleaner.
Chris Lattner2192fe52011-07-18 04:24:23 +00004210 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004211 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004212
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004213 // I'm not sure I like this. The implicit coordination is a bit
4214 // gross. We should solve this in a reasonable fashion because this
4215 // is a pretty common task (match some runtime data structure with
4216 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004217
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004218 // FIXME: This is leaked.
4219 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004220
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004221 // struct _objc_super {
4222 // id self;
4223 // Class cls;
4224 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004225 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004226 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004227 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004228 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004229 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004230 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004231 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004232 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004233 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004235 SuperCTy = Ctx.getTagDeclType(RD);
4236 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004237
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004238 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004239 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4240
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004241 // struct _prop_t {
4242 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004243 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004244 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004245 PropertyTy = llvm::StructType::create("struct._prop_t",
4246 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004247
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004248 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004249 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004250 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004251 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004252 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004253 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004254 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4255 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004256 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004257 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004258
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004259 // struct _objc_method {
4260 // SEL _cmd;
4261 // char *method_type;
4262 // char *_imp;
4263 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004264 MethodTy = llvm::StructType::create("struct._objc_method",
4265 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4266 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004267
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004268 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004269 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004270 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004271
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004272}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004273
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004274ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004275 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004276 // struct _objc_method_description {
4277 // SEL name;
4278 // char *types;
4279 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004280 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004281 llvm::StructType::create("struct._objc_method_description",
4282 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004283
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004284 // struct _objc_method_description_list {
4285 // int count;
4286 // struct _objc_method_description[1];
4287 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004288 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004289 llvm::StructType::create("struct._objc_method_description_list",
4290 IntTy,
4291 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004292
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004293 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004294 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004295 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004296
Daniel Dunbarb036db82008-08-13 03:21:16 +00004297 // Protocol description structures
4298
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004299 // struct _objc_protocol_extension {
4300 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4301 // struct _objc_method_description_list *optional_instance_methods;
4302 // struct _objc_method_description_list *optional_class_methods;
4303 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004304 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004305 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004306 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004307 llvm::StructType::create("struct._objc_protocol_extension",
4308 IntTy, MethodDescriptionListPtrTy,
4309 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004310 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004311
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004312 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004313 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004314
Daniel Dunbarc475d422008-10-29 22:36:39 +00004315 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004316
Chris Lattnera5f58b02011-07-09 17:41:47 +00004317 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004318 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004319
Chris Lattnera5f58b02011-07-09 17:41:47 +00004320 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004321 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004322 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004323 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004324 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004325 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004326
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004327 // struct _objc_protocol {
4328 // struct _objc_protocol_extension *isa;
4329 // char *protocol_name;
4330 // struct _objc_protocol **_objc_protocol_list;
4331 // struct _objc_method_description_list *instance_methods;
4332 // struct _objc_method_description_list *class_methods;
4333 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004334 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4335 llvm::PointerType::getUnqual(ProtocolListTy),
4336 MethodDescriptionListPtrTy,
4337 MethodDescriptionListPtrTy,
4338 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004339
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004340 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004341 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004342
Owen Anderson9793f0e2009-07-29 22:16:19 +00004343 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004344
4345 // Class description structures
4346
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004347 // struct _objc_ivar {
4348 // char *ivar_name;
4349 // char *ivar_type;
4350 // int ivar_offset;
4351 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004352 IvarTy = llvm::StructType::create("struct._objc_ivar",
4353 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004354
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004355 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004356 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004357 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004358 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004359
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004360 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004361 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004362 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004363 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004364
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004365 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004366 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004367 llvm::StructType::create("struct._objc_class_extension",
4368 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004369 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004370
Chris Lattner5ec04a52011-08-12 17:43:31 +00004371 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004372
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004373 // struct _objc_class {
4374 // Class isa;
4375 // Class super_class;
4376 // char *name;
4377 // long version;
4378 // long info;
4379 // long instance_size;
4380 // struct _objc_ivar_list *ivars;
4381 // struct _objc_method_list *methods;
4382 // struct _objc_cache *cache;
4383 // struct _objc_protocol_list *protocols;
4384 // char *ivar_layout;
4385 // struct _objc_class_ext *ext;
4386 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004387 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4388 llvm::PointerType::getUnqual(ClassTy),
4389 Int8PtrTy,
4390 LongTy,
4391 LongTy,
4392 LongTy,
4393 IvarListPtrTy,
4394 MethodListPtrTy,
4395 CachePtrTy,
4396 ProtocolListPtrTy,
4397 Int8PtrTy,
4398 ClassExtensionPtrTy,
4399 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004400
Owen Anderson9793f0e2009-07-29 22:16:19 +00004401 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004402
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004403 // struct _objc_category {
4404 // char *category_name;
4405 // char *class_name;
4406 // struct _objc_method_list *instance_method;
4407 // struct _objc_method_list *class_method;
4408 // uint32_t size; // sizeof(struct _objc_category)
4409 // struct _objc_property_list *instance_properties;// category's @property
4410 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004411 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004412 llvm::StructType::create("struct._objc_category",
4413 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4414 MethodListPtrTy, ProtocolListPtrTy,
4415 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004416
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004417 // Global metadata structures
4418
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004419 // struct _objc_symtab {
4420 // long sel_ref_cnt;
4421 // SEL *refs;
4422 // short cls_def_cnt;
4423 // short cat_def_cnt;
4424 // char *defs[cls_def_cnt + cat_def_cnt];
4425 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004426 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004427 llvm::StructType::create("struct._objc_symtab",
4428 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4429 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004430 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004431
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004432 // struct _objc_module {
4433 // long version;
4434 // long size; // sizeof(struct _objc_module)
4435 // char *name;
4436 // struct _objc_symtab* symtab;
4437 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004438 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004439 llvm::StructType::create("struct._objc_module",
4440 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004441
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004442
Mike Stump18bb9282009-05-16 07:57:57 +00004443 // FIXME: This is the size of the setjmp buffer and should be target
4444 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004445 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004446
Anders Carlsson9ff22482008-09-09 10:10:21 +00004447 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004448 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004449 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004450
4451 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004452 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004453 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4454 SetJmpBufferSize),
4455 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004456
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004457}
4458
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004459ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004460 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004461 // struct _method_list_t {
4462 // uint32_t entsize; // sizeof(struct _objc_method)
4463 // uint32_t method_count;
4464 // struct _objc_method method_list[method_count];
4465 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004466 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004467 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4468 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004469 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004470 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004471
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004472 // struct _protocol_t {
4473 // id isa; // NULL
4474 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004475 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004476 // const struct method_list_t * const instance_methods;
4477 // const struct method_list_t * const class_methods;
4478 // const struct method_list_t *optionalInstanceMethods;
4479 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004480 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004481 // const uint32_t size; // sizeof(struct _protocol_t)
4482 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004483 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004484 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004485
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004487 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004488 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004489
Chris Lattnera5f58b02011-07-09 17:41:47 +00004490 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004491 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4492 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4493 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4494 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004495 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4496 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004497
4498 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004499 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004500
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004501 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004502 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004503 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004504 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004505 ProtocolListnfABITy->setBody(LongTy,
4506 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4507 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004508
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004509 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004510 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004511
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004512 // struct _ivar_t {
4513 // unsigned long int *offset; // pointer to ivar offset location
4514 // char *name;
4515 // char *type;
4516 // uint32_t alignment;
4517 // uint32_t size;
4518 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004519 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004520 llvm::StructType::create("struct._ivar_t",
4521 llvm::PointerType::getUnqual(LongTy),
4522 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004524 // struct _ivar_list_t {
4525 // uint32 entsize; // sizeof(struct _ivar_t)
4526 // uint32 count;
4527 // struct _iver_t list[count];
4528 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004529 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004530 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4531 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004532
Owen Anderson9793f0e2009-07-29 22:16:19 +00004533 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004534
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004535 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004536 // uint32_t const flags;
4537 // uint32_t const instanceStart;
4538 // uint32_t const instanceSize;
4539 // uint32_t const reserved; // only when building for 64bit targets
4540 // const uint8_t * const ivarLayout;
4541 // const char *const name;
4542 // const struct _method_list_t * const baseMethods;
4543 // const struct _objc_protocol_list *const baseProtocols;
4544 // const struct _ivar_list_t *const ivars;
4545 // const uint8_t * const weakIvarLayout;
4546 // const struct _prop_list_t * const properties;
4547 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004549 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004550 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4551 IntTy, IntTy, IntTy, Int8PtrTy,
4552 Int8PtrTy, MethodListnfABIPtrTy,
4553 ProtocolListnfABIPtrTy,
4554 IvarListnfABIPtrTy,
4555 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004556
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004557 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004558 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004559 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4560 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004561
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004562 // struct _class_t {
4563 // struct _class_t *isa;
4564 // struct _class_t * const superclass;
4565 // void *cache;
4566 // IMP *vtable;
4567 // struct class_ro_t *ro;
4568 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004569
Chris Lattner5ec04a52011-08-12 17:43:31 +00004570 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004571 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4572 llvm::PointerType::getUnqual(ClassnfABITy),
4573 CachePtrTy,
4574 llvm::PointerType::getUnqual(ImpnfABITy),
4575 llvm::PointerType::getUnqual(ClassRonfABITy),
4576 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004577
Fariborz Jahanian71394042009-01-23 23:53:38 +00004578 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004579 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004580
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004581 // struct _category_t {
4582 // const char * const name;
4583 // struct _class_t *const cls;
4584 // const struct _method_list_t * const instance_methods;
4585 // const struct _method_list_t * const class_methods;
4586 // const struct _protocol_list_t * const protocols;
4587 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004588 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004589 CategorynfABITy = llvm::StructType::create("struct._category_t",
4590 Int8PtrTy, ClassnfABIPtrTy,
4591 MethodListnfABIPtrTy,
4592 MethodListnfABIPtrTy,
4593 ProtocolListnfABIPtrTy,
4594 PropertyListPtrTy,
4595 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004596
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004597 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004598 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4599 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004600
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004601 // MessageRefTy - LLVM for:
4602 // struct _message_ref_t {
4603 // IMP messenger;
4604 // SEL name;
4605 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004607 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004608 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004609 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004610 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004611 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004612 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004613 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004614 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004615 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004616 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004617
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004618 MessageRefCTy = Ctx.getTagDeclType(RD);
4619 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4620 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004621
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004622 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004623 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004625 // SuperMessageRefTy - LLVM for:
4626 // struct _super_message_ref_t {
4627 // SUPER_IMP messenger;
4628 // SEL name;
4629 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004630 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004631 llvm::StructType::create("struct._super_message_ref_t",
4632 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004633
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004634 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004635 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004636
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004637
4638 // struct objc_typeinfo {
4639 // const void** vtable; // objc_ehtype_vtable + 2
4640 // const char* name; // c++ typeinfo string
4641 // Class cls;
4642 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004643 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004644 llvm::StructType::create("struct._objc_typeinfo",
4645 llvm::PointerType::getUnqual(Int8PtrTy),
4646 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004647 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004648}
4649
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004651 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Fariborz Jahanian71394042009-01-23 23:53:38 +00004653 return NULL;
4654}
4655
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004656void CGObjCNonFragileABIMac::AddModuleClassList(const
4657 std::vector<llvm::GlobalValue*>
4658 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004659 const char *SymbolName,
4660 const char *SectionName) {
4661 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004662
Daniel Dunbar19573e72009-05-15 21:48:48 +00004663 if (!NumClasses)
4664 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004665
Daniel Dunbar19573e72009-05-15 21:48:48 +00004666 std::vector<llvm::Constant*> Symbols(NumClasses);
4667 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004668 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004669 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004670 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004671 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004672 NumClasses),
4673 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004674
Daniel Dunbar19573e72009-05-15 21:48:48 +00004675 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004676 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004677 llvm::GlobalValue::InternalLinkage,
4678 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004679 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004680 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004681 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004682 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004683}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004684
Fariborz Jahanian71394042009-01-23 23:53:38 +00004685void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4686 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004687
Daniel Dunbar19573e72009-05-15 21:48:48 +00004688 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004689 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004690 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004691 "\01L_OBJC_LABEL_CLASS_$",
4692 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004693
Fariborz Jahanian67260552009-11-17 21:37:35 +00004694 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4695 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4696 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4697 continue;
4698 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004699 }
4700
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004701 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4702 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4703 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4704 continue;
4705 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4706 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004707
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004708 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004709 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4710 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004711
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004712 // Build list of all implemented category addresses in array
4713 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004714 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004715 "\01L_OBJC_LABEL_CATEGORY_$",
4716 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004717 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004718 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4719 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004720
Daniel Dunbar5e639272010-04-25 20:39:01 +00004721 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004722}
4723
John McCall9e8bb002011-05-14 03:10:52 +00004724/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4725/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004726/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004727/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004728bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4729 // At various points we've experimented with using vtable-based
4730 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004731 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4732 default:
John McCall9e8bb002011-05-14 03:10:52 +00004733 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004734 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004735 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004736 case CodeGenOptions::NonLegacy:
4737 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004738 case CodeGenOptions::Mixed:
4739 break;
4740 }
4741
4742 // If so, see whether this selector is in the white-list of things which must
4743 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004744 if (VTableDispatchMethods.empty()) {
4745 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4746 VTableDispatchMethods.insert(GetNullarySelector("class"));
4747 VTableDispatchMethods.insert(GetNullarySelector("self"));
4748 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4749 VTableDispatchMethods.insert(GetNullarySelector("length"));
4750 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004751
John McCall9e8bb002011-05-14 03:10:52 +00004752 // These are vtable-based if GC is disabled.
4753 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004754 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004755 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4756 VTableDispatchMethods.insert(GetNullarySelector("release"));
4757 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4758 }
4759
4760 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4761 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4762 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4763 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4764 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4765 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4766 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4767
4768 // These are vtable-based if GC is enabled.
4769 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004770 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004771 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4772 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4773
4774 // "countByEnumeratingWithState:objects:count"
4775 IdentifierInfo *KeyIdents[] = {
4776 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4777 &CGM.getContext().Idents.get("objects"),
4778 &CGM.getContext().Idents.get("count")
4779 };
4780 VTableDispatchMethods.insert(
4781 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4782 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004783 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004784
John McCall9e8bb002011-05-14 03:10:52 +00004785 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004786}
4787
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004788// Metadata flags
4789enum MetaDataDlags {
4790 CLS = 0x0,
4791 CLS_META = 0x1,
4792 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004793 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004794 CLS_EXCEPTION = 0x20,
4795
4796 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4797 CLS_HAS_IVAR_RELEASER = 0x40,
4798 /// class was compiled with -fobjc-arr
4799 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004800};
4801/// BuildClassRoTInitializer - generate meta-data for:
4802/// struct _class_ro_t {
4803/// uint32_t const flags;
4804/// uint32_t const instanceStart;
4805/// uint32_t const instanceSize;
4806/// uint32_t const reserved; // only when building for 64bit targets
4807/// const uint8_t * const ivarLayout;
4808/// const char *const name;
4809/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004810/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004811/// const struct _ivar_list_t *const ivars;
4812/// const uint8_t * const weakIvarLayout;
4813/// const struct _prop_list_t * const properties;
4814/// }
4815///
4816llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004817 unsigned flags,
4818 unsigned InstanceStart,
4819 unsigned InstanceSize,
4820 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004821 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004822 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004823
4824 if (CGM.getLangOptions().ObjCAutoRefCount)
4825 flags |= CLS_COMPILED_BY_ARC;
4826
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004827 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4828 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4829 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004830 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004831 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4832 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004833 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004834 // const struct _method_list_t * const baseMethods;
4835 std::vector<llvm::Constant*> Methods;
4836 std::string MethodListName("\01l_OBJC_$_");
4837 if (flags & CLS_META) {
4838 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004839 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004840 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004841 // Class methods should always be defined.
4842 Methods.push_back(GetMethodConstant(*i));
4843 }
4844 } else {
4845 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004846 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004847 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004848 // Instance methods should always be defined.
4849 Methods.push_back(GetMethodConstant(*i));
4850 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004851 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004852 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004853 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004854
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004855 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4856 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004857
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004858 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4859 if (llvm::Constant *C = GetMethodConstant(MD))
4860 Methods.push_back(C);
4861 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4862 if (llvm::Constant *C = GetMethodConstant(MD))
4863 Methods.push_back(C);
4864 }
4865 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004866 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004867 Values[ 5] = EmitMethodList(MethodListName,
4868 "__DATA, __objc_const", Methods);
4869
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004870 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4871 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004872 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004873 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004874 OID->all_referenced_protocol_begin(),
4875 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004877 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004878 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004879 else
4880 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004881 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4882 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004883 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004884 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004885 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004886 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4887 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004888 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004889 Values);
4890 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004891 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4892 llvm::GlobalValue::InternalLinkage,
4893 Init,
4894 (flags & CLS_META) ?
4895 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4896 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004897 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004898 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004899 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004900 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004901
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004902}
4903
4904/// BuildClassMetaData - This routine defines that to-level meta-data
4905/// for the given ClassName for:
4906/// struct _class_t {
4907/// struct _class_t *isa;
4908/// struct _class_t * const superclass;
4909/// void *cache;
4910/// IMP *vtable;
4911/// struct class_ro_t *ro;
4912/// }
4913///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004914llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004915 std::string &ClassName,
4916 llvm::Constant *IsAGV,
4917 llvm::Constant *SuperClassGV,
4918 llvm::Constant *ClassRoGV,
4919 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004920 llvm::Constant *Values[] = {
4921 IsAGV,
4922 SuperClassGV,
4923 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4924 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4925 ClassRoGV // &CLASS_RO_GV
4926 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004927 if (!Values[1])
4928 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004930 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004931 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4932 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004933 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004934 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004935 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004936 if (HiddenVisibility)
4937 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004938 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004939}
4940
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004941bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004942CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004943 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004944}
4945
Daniel Dunbar961202372009-05-03 12:57:56 +00004946void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004947 uint32_t &InstanceStart,
4948 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004949 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004950 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004951
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004952 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004953 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004954
4955 // If there are no fields, the start is the same as the end.
4956 if (!RL.getFieldCount())
4957 InstanceStart = InstanceSize;
4958 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004959 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004960}
4961
Fariborz Jahanian71394042009-01-23 23:53:38 +00004962void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4963 std::string ClassName = ID->getNameAsString();
4964 if (!ObjCEmptyCacheVar) {
4965 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004966 CGM.getModule(),
4967 ObjCTypes.CacheTy,
4968 false,
4969 llvm::GlobalValue::ExternalLinkage,
4970 0,
4971 "_objc_empty_cache");
4972
Fariborz Jahanian71394042009-01-23 23:53:38 +00004973 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004974 CGM.getModule(),
4975 ObjCTypes.ImpnfABITy,
4976 false,
4977 llvm::GlobalValue::ExternalLinkage,
4978 0,
4979 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004980 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004981 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004982 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004983 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004984 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004985 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004986 uint32_t InstanceSize = InstanceStart;
4987 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004988 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4989 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004990
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004991 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004992
4993 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004994 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004995 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004996 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004997 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004998 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004999 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005000 // class is root
5001 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005002 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005003 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005004 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005005 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005006 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5007 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5008 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005009 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005010 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005011 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005012 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005013 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005014 ObjCMetaClassName +
5015 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005016 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005017 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005018 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005019 }
5020 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5021 InstanceStart,
5022 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005023 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005024 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005025 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5026 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005027 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005028
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005029 // Metadata for the class
5030 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005031 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005032 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005033 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005034 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005035
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005036 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005037 flags |= CLS_EXCEPTION;
5038
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005039 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005040 flags |= CLS_ROOT;
5041 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005042 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005043 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005044 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005045 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005046 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005047 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005048 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005049 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005050 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005051 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005052 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005053 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005054 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005056 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005057 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005058 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5059 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005060 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005061
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005062 // Determine if this class is also "non-lazy".
5063 if (ImplementationIsNonLazy(ID))
5064 DefinedNonLazyClasses.push_back(ClassMD);
5065
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005066 // Force the definition of the EHType if necessary.
5067 if (flags & CLS_EXCEPTION)
5068 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005069 // Make sure method definition entries are all clear for next implementation.
5070 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005071}
5072
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005073/// GenerateProtocolRef - This routine is called to generate code for
5074/// a protocol reference expression; as in:
5075/// @code
5076/// @protocol(Proto1);
5077/// @endcode
5078/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5079/// which will hold address of the protocol meta-data.
5080///
5081llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082 const ObjCProtocolDecl *PD) {
5083
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005084 // This routine is called for @protocol only. So, we must build definition
5085 // of protocol's meta-data (not a reference to it!)
5086 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087 llvm::Constant *Init =
5088 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5089 ObjCTypes.ExternalProtocolPtrTy);
5090
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005091 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005092 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005093
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005094 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5095 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005096 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005097 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005098 CGM.getModule(),
5099 Init->getType(), false,
5100 llvm::GlobalValue::WeakAnyLinkage,
5101 Init,
5102 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005103 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5104 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005105 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005106 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005107}
5108
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005109/// GenerateCategory - Build metadata for a category implementation.
5110/// struct _category_t {
5111/// const char * const name;
5112/// struct _class_t *const cls;
5113/// const struct _method_list_t * const instance_methods;
5114/// const struct _method_list_t * const class_methods;
5115/// const struct _protocol_list_t * const protocols;
5116/// const struct _prop_list_t * const properties;
5117/// }
5118///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005119void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005120 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005121 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005122 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5123 "_$_" + OCD->getNameAsString());
5124 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005125 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005126
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005127 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005128 Values[0] = GetClassName(OCD->getIdentifier());
5129 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005130 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005131 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005132 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5133
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005134 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005135 std::vector<llvm::Constant*> Methods;
5136 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005137 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005138 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005139
5140 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005141 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005142 // Instance methods should always be defined.
5143 Methods.push_back(GetMethodConstant(*i));
5144 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005145
5146 Values[2] = EmitMethodList(MethodListName,
5147 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005148 Methods);
5149
5150 MethodListName = Prefix;
5151 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5152 OCD->getNameAsString();
5153 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005154 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005155 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005156 // Class methods should always be defined.
5157 Methods.push_back(GetMethodConstant(*i));
5158 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005159
5160 Values[3] = EmitMethodList(MethodListName,
5161 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005162 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005163 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005164 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005165 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005166 llvm::SmallString<256> ExtName;
5167 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5168 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005169 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005170 + Interface->getName() + "_$_"
5171 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005172 Category->protocol_begin(),
5173 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005174 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5175 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005176 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005177 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5178 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005179 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005180
5181 llvm::Constant *Init =
5182 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005183 Values);
5184 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005185 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005186 false,
5187 llvm::GlobalValue::InternalLinkage,
5188 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005189 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005190 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005191 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005192 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005193 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005194 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005195
5196 // Determine if this category is also "non-lazy".
5197 if (ImplementationIsNonLazy(OCD))
5198 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005199 // method definition entries must be clear for next implementation.
5200 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005201}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005202
5203/// GetMethodConstant - Return a struct objc_method constant for the
5204/// given method if it has been defined. The result is null if the
5205/// method has not been defined. The return value has type MethodPtrTy.
5206llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005207 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005208 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005209 if (!Fn)
5210 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005212 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005213 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005214 ObjCTypes.SelectorPtrTy),
5215 GetMethodVarType(MD),
5216 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5217 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005218 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005219}
5220
5221/// EmitMethodList - Build meta-data for method declarations
5222/// struct _method_list_t {
5223/// uint32_t entsize; // sizeof(struct _objc_method)
5224/// uint32_t method_count;
5225/// struct _objc_method method_list[method_count];
5226/// }
5227///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005228llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005229 const char *Section,
5230 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005231 // Return null for empty list.
5232 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005233 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005234
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005235 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005236 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005237 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005238 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005239 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005240 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005241 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005242 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005243 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005244 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005245
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005246 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005247 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005248 llvm::GlobalValue::InternalLinkage, Init, Name);
5249 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005250 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005251 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005252 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005253}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005254
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005255/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5256/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005257llvm::GlobalVariable *
5258CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5259 const ObjCIvarDecl *Ivar) {
5260 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005261 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005262 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005263 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005264 CGM.getModule().getGlobalVariable(Name);
5265 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005266 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005267 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005268 false,
5269 llvm::GlobalValue::ExternalLinkage,
5270 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005271 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005272 return IvarOffsetGV;
5273}
5274
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005275llvm::Constant *
5276CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5277 const ObjCIvarDecl *Ivar,
5278 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005279 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005280 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005281 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005282 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005283 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005284
Mike Stump18bb9282009-05-16 07:57:57 +00005285 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5286 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005287 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5288 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005289 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005290 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005291 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005292 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005293 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005294 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005295}
5296
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005297/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005298/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005299/// IvarListnfABIPtrTy.
5300/// struct _ivar_t {
5301/// unsigned long int *offset; // pointer to ivar offset location
5302/// char *name;
5303/// char *type;
5304/// uint32_t alignment;
5305/// uint32_t size;
5306/// }
5307/// struct _ivar_list_t {
5308/// uint32 entsize; // sizeof(struct _ivar_t)
5309/// uint32 count;
5310/// struct _iver_t list[count];
5311/// }
5312///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005313
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005314llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005315 const ObjCImplementationDecl *ID) {
5316
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005317 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005318
Jordy Rosea91768e2011-07-22 02:08:32 +00005319 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005320 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005321
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005322 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005323
Jordy Rosea91768e2011-07-22 02:08:32 +00005324 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005325 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005326 // Ignore unnamed bit-fields.
5327 if (!IVD->getDeclName())
5328 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005329 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005330 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005331 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005332 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5333 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005334 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005335 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005336 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005337 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005338 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005339 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005340 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005341 // NOTE. Size of a bitfield does not match gcc's, because of the
5342 // way bitfields are treated special in each. But I am told that
5343 // 'size' for bitfield ivars is ignored by the runtime so it does
5344 // not matter. If it matters, there is enough info to get the
5345 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005346 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005347 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005348 }
5349 // Return null for empty list.
5350 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005351 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005352
5353 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005354 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005355 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5356 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005357 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005358 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005359 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005360 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005361 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5362 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005363 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005364 llvm::GlobalValue::InternalLinkage,
5365 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005366 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005367 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005368 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005369 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370
Chris Lattnerf56501c2009-07-17 23:57:13 +00005371 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005372 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005373}
5374
5375llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005376 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005377 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005379 if (!Entry) {
5380 // We use the initializer as a marker of whether this is a forward
5381 // reference or not. At module finalization we add the empty
5382 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005383 Entry =
5384 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5385 llvm::GlobalValue::ExternalLinkage,
5386 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005387 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005388 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005389 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005390
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 return Entry;
5392}
5393
5394/// GetOrEmitProtocol - Generate the protocol meta-data:
5395/// @code
5396/// struct _protocol_t {
5397/// id isa; // NULL
5398/// const char * const protocol_name;
5399/// const struct _protocol_list_t * protocol_list; // super protocols
5400/// const struct method_list_t * const instance_methods;
5401/// const struct method_list_t * const class_methods;
5402/// const struct method_list_t *optionalInstanceMethods;
5403/// const struct method_list_t *optionalClassMethods;
5404/// const struct _prop_list_t * properties;
5405/// const uint32_t size; // sizeof(struct _protocol_t)
5406/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005407/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005408/// }
5409/// @endcode
5410///
5411
5412llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005415
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005416 // Early exit if a defining object has already been generated.
5417 if (Entry && Entry->hasInitializer())
5418 return Entry;
5419
Douglas Gregora715bff2012-01-01 19:51:50 +00005420 // Use the protocol definition, if there is one.
5421 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5422 PD = Def;
5423
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005424 // Construct method lists.
5425 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5426 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005427 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005428 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005429 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005430 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005431 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005432 if (!C)
5433 return GetOrEmitProtocolRef(PD);
5434
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005435 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5436 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005437 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005438 } else {
5439 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005440 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005441 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005442 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005443
5444 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005445 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005446 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005447 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005448 if (!C)
5449 return GetOrEmitProtocolRef(PD);
5450
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005451 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5452 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005453 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005454 } else {
5455 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005456 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005458 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005459
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005460 MethodTypesExt.insert(MethodTypesExt.end(),
5461 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5462
5463 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005464 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005465 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005466 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005467 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5468 PD->protocol_begin(),
5469 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005470
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005471 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005472 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005473 "__DATA, __objc_const",
5474 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005475 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005476 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005477 "__DATA, __objc_const",
5478 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005479 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005480 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005481 "__DATA, __objc_const",
5482 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005484 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005485 "__DATA, __objc_const",
5486 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005487 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005488 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005489 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005490 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005491 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005492 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005493 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5494 + PD->getName(),
5495 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005496 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005497 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005498
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005499 if (Entry) {
5500 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005501 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005502 Entry->setInitializer(Init);
5503 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005504 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005505 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5506 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5507 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005508 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005509 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005510 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005511 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005512 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005513 CGM.AddUsedGlobal(Entry);
5514
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005515 // Use this protocol meta-data to build protocol list table in section
5516 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005517 llvm::GlobalVariable *PTGV =
5518 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5519 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5520 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005521 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005522 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005523 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005524 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005525 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005526 return Entry;
5527}
5528
5529/// EmitProtocolList - Generate protocol list meta-data:
5530/// @code
5531/// struct _protocol_list_t {
5532/// long protocol_count; // Note, this is 32/64 bit
5533/// struct _protocol_t[protocol_count];
5534/// }
5535/// @endcode
5536///
5537llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005538CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005539 ObjCProtocolDecl::protocol_iterator begin,
5540 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005541 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005542
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005543 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005544 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005545 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005546
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005547 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005548 llvm::SmallString<256> TmpName;
5549 Name.toVector(TmpName);
5550 llvm::GlobalVariable *GV =
5551 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005552 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005553 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005554
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005555 for (; begin != end; ++begin)
5556 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5557
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005558 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005559 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005560 ObjCTypes.ProtocolnfABIPtrTy));
5561
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005562 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005563 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005564 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005565 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005566 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005567 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005568 ProtocolRefs.size()),
5569 ProtocolRefs);
5570
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005571 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005572 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005573 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005574 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005575 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005576 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005577 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005578 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005580 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005581}
5582
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005583/// GetMethodDescriptionConstant - This routine build following meta-data:
5584/// struct _objc_method {
5585/// SEL _cmd;
5586/// char *method_type;
5587/// char *_imp;
5588/// }
5589
5590llvm::Constant *
5591CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005592 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005593 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005594 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5595 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005596 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005597 if (!Desc[1])
5598 return 0;
5599
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005600 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005601 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005602 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005603}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005604
5605/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5606/// This code gen. amounts to generating code for:
5607/// @code
5608/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5609/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005610///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005611LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005612 CodeGen::CodeGenFunction &CGF,
5613 QualType ObjectTy,
5614 llvm::Value *BaseValue,
5615 const ObjCIvarDecl *Ivar,
5616 unsigned CVRQualifiers) {
5617 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005618 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5619 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005620}
5621
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005622llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005623 CodeGen::CodeGenFunction &CGF,
5624 const ObjCInterfaceDecl *Interface,
5625 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005626 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005627}
5628
John McCall234eac82011-05-13 23:16:18 +00005629static void appendSelectorForMessageRefTable(std::string &buffer,
5630 Selector selector) {
5631 if (selector.isUnarySelector()) {
5632 buffer += selector.getNameForSlot(0);
5633 return;
5634 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635
John McCall234eac82011-05-13 23:16:18 +00005636 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5637 buffer += selector.getNameForSlot(i);
5638 buffer += '_';
5639 }
5640}
5641
John McCall9e8bb002011-05-14 03:10:52 +00005642/// Emit a "v-table" message send. We emit a weak hidden-visibility
5643/// struct, initially containing the selector pointer and a pointer to
5644/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5645/// load and call the function pointer, passing the address of the
5646/// struct as the second parameter. The runtime determines whether
5647/// the selector is currently emitted using vtable dispatch; if so, it
5648/// substitutes a stub function which simply tail-calls through the
5649/// appropriate vtable slot, and if not, it substitues a stub function
5650/// which tail-calls objc_msgSend. Both stubs adjust the selector
5651/// argument to correctly point to the selector.
5652RValue
5653CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5654 ReturnValueSlot returnSlot,
5655 QualType resultType,
5656 Selector selector,
5657 llvm::Value *arg0,
5658 QualType arg0Type,
5659 bool isSuper,
5660 const CallArgList &formalArgs,
5661 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005662 // Compute the actual arguments.
5663 CallArgList args;
5664
John McCall9e8bb002011-05-14 03:10:52 +00005665 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005666 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005667 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5668 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005669
John McCall9e8bb002011-05-14 03:10:52 +00005670 // Second argument: a pointer to the message ref structure. Leave
5671 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005672 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5673
5674 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5675
5676 const CGFunctionInfo &fnInfo =
5677 CGM.getTypes().getFunctionInfo(resultType, args,
5678 FunctionType::ExtInfo());
5679
John McCall5880fb82011-05-14 21:12:11 +00005680 NullReturnState nullReturn;
5681
John McCall9e8bb002011-05-14 03:10:52 +00005682 // Find the function to call and the mangled name for the message
5683 // ref structure. Using a different mangled name wouldn't actually
5684 // be a problem; it would just be a waste.
5685 //
5686 // The runtime currently never uses vtable dispatch for anything
5687 // except normal, non-super message-sends.
5688 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005689 llvm::Constant *fn = 0;
5690 std::string messageRefName("\01l_");
5691 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005692 if (isSuper) {
5693 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5694 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005695 } else {
John McCall5880fb82011-05-14 21:12:11 +00005696 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005697 fn = ObjCTypes.getMessageSendStretFixupFn();
5698 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005699 }
John McCall234eac82011-05-13 23:16:18 +00005700 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5701 fn = ObjCTypes.getMessageSendFpretFixupFn();
5702 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005703 } else {
John McCall234eac82011-05-13 23:16:18 +00005704 if (isSuper) {
5705 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5706 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005707 } else {
John McCall234eac82011-05-13 23:16:18 +00005708 fn = ObjCTypes.getMessageSendFixupFn();
5709 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005710 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005711 }
John McCall234eac82011-05-13 23:16:18 +00005712 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5713 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005714
John McCall234eac82011-05-13 23:16:18 +00005715 // Append the selector name, except use underscores anywhere we
5716 // would have used colons.
5717 appendSelectorForMessageRefTable(messageRefName, selector);
5718
5719 llvm::GlobalVariable *messageRef
5720 = CGM.getModule().getGlobalVariable(messageRefName);
5721 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005722 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005723 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005724 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005725 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5726 init->getType(),
5727 /*constant*/ false,
5728 llvm::GlobalValue::WeakAnyLinkage,
5729 init,
5730 messageRefName);
5731 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5732 messageRef->setAlignment(16);
5733 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5734 }
5735 llvm::Value *mref =
5736 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5737
John McCall9e8bb002011-05-14 03:10:52 +00005738 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005739 args[1].RV = RValue::get(mref);
5740
5741 // Load the function to call from the message ref table.
5742 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5743 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5744
5745 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005746 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005747 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5748 callee = CGF.Builder.CreateBitCast(callee,
5749 llvm::PointerType::getUnqual(fnType));
5750
John McCall5880fb82011-05-14 21:12:11 +00005751 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5752 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005753}
5754
5755/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005756CodeGen::RValue
5757CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005758 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005759 QualType ResultType,
5760 Selector Sel,
5761 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005762 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005763 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005764 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005765 return isVTableDispatchedSelector(Sel)
5766 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005767 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005768 false, CallArgs, Method)
5769 : EmitMessageSend(CGF, Return, ResultType,
5770 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005771 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005772 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005773}
5774
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005775llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005776CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005777 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5778
Daniel Dunbara6468342009-03-02 05:18:14 +00005779 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005780 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005781 false, llvm::GlobalValue::ExternalLinkage,
5782 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005783 }
5784
5785 return GV;
5786}
5787
John McCall31168b02011-06-15 23:02:42 +00005788llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5789 IdentifierInfo *II) {
5790 llvm::GlobalVariable *&Entry = ClassReferences[II];
5791
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005792 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005793 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005794 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005795 Entry =
John McCall31168b02011-06-15 23:02:42 +00005796 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5797 false, llvm::GlobalValue::InternalLinkage,
5798 ClassGV,
5799 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005800 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005801 CGM.getTargetData().getABITypeAlignment(
5802 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005803 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005804 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005805 }
John McCall31168b02011-06-15 23:02:42 +00005806
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005807 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005808}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005809
John McCall31168b02011-06-15 23:02:42 +00005810llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5811 const ObjCInterfaceDecl *ID) {
5812 return EmitClassRefFromId(Builder, ID->getIdentifier());
5813}
5814
5815llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5816 CGBuilderTy &Builder) {
5817 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5818 return EmitClassRefFromId(Builder, II);
5819}
5820
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005821llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005822CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005823 const ObjCInterfaceDecl *ID) {
5824 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005825
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005826 if (!Entry) {
5827 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5828 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005829 Entry =
5830 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005831 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005832 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005833 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005834 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005835 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005836 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005837 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005838 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005839 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005840
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005841 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005842}
5843
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005844/// EmitMetaClassRef - Return a Value * of the address of _class_t
5845/// meta-data
5846///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005847llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5848 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005849 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5850 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005851 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005852
Daniel Dunbar15894b72009-04-07 05:48:37 +00005853 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005854 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005855 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005856 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005857 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005858 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005859 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005860 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005861 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005862 ObjCTypes.ClassnfABIPtrTy));
5863
Daniel Dunbare60aa052009-04-15 19:03:14 +00005864 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005865 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005866
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005867 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005868}
5869
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005870/// GetClass - Return a reference to the class for the given interface
5871/// decl.
5872llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5873 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005874 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005875 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5876 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5877 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5878 }
5879
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005880 return EmitClassRef(Builder, ID);
5881}
5882
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005883/// Generates a message send where the super is the receiver. This is
5884/// a message send to self with special delivery semantics indicating
5885/// which class's method should be called.
5886CodeGen::RValue
5887CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005888 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005889 QualType ResultType,
5890 Selector Sel,
5891 const ObjCInterfaceDecl *Class,
5892 bool isCategoryImpl,
5893 llvm::Value *Receiver,
5894 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005895 const CodeGen::CallArgList &CallArgs,
5896 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005897 // ...
5898 // Create and init a super structure; this is a (receiver, class)
5899 // pair we will pass to objc_msgSendSuper.
5900 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005901 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005902
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005903 llvm::Value *ReceiverAsObject =
5904 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5905 CGF.Builder.CreateStore(ReceiverAsObject,
5906 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005907
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005908 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005909 llvm::Value *Target;
5910 if (IsClassMessage) {
5911 if (isCategoryImpl) {
5912 // Message sent to "super' in a class method defined in
5913 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005914 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005915 Target = CGF.Builder.CreateStructGEP(Target, 0);
5916 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005917 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005918 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005919 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005920 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005921
Mike Stump18bb9282009-05-16 07:57:57 +00005922 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5923 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00005924 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005925 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5926 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5927 CGF.Builder.CreateStore(Target,
5928 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005929
John McCall9e8bb002011-05-14 03:10:52 +00005930 return (isVTableDispatchedSelector(Sel))
5931 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005933 true, CallArgs, Method)
5934 : EmitMessageSend(CGF, Return, ResultType,
5935 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005936 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005937 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005938}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005939
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005940llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005941 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005942 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005944 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005945 llvm::Constant *Casted =
5946 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5947 ObjCTypes.SelectorPtrTy);
5948 Entry =
5949 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5950 llvm::GlobalValue::InternalLinkage,
5951 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005952 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005953 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005954 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005955
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005956 if (lval)
5957 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00005958 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
5959
5960 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5961 llvm::MDNode::get(VMContext,
5962 ArrayRef<llvm::Value*>()));
5963 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005964}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005965/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005966/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005967///
5968void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005969 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005970 llvm::Value *dst,
5971 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005972 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005973 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005974 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005975 assert(Size <= 8 && "does not support size > 8");
5976 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5977 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005978 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5979 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005980 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5981 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005982 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5983 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005984 return;
5985}
5986
5987/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5988/// objc_assign_strongCast (id src, id *dst)
5989///
5990void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005991 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005992 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005993 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005994 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005995 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005996 assert(Size <= 8 && "does not support size > 8");
5997 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005998 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005999 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6000 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006001 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6002 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006003 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006004 src, dst, "weakassign");
6005 return;
6006}
6007
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006008void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006009 CodeGen::CodeGenFunction &CGF,
6010 llvm::Value *DestPtr,
6011 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006012 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006013 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6014 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006015 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006016 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006017 return;
6018}
6019
Fariborz Jahanian06292952009-02-16 22:52:32 +00006020/// EmitObjCWeakRead - Code gen for loading value of a __weak
6021/// object: objc_read_weak (id *src)
6022///
6023llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006024 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006025 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006026 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006027 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6028 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006029 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006030 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006031 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006032 return read_weak;
6033}
6034
6035/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6036/// objc_assign_weak (id src, id *dst)
6037///
6038void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006039 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006040 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006041 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006042 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006043 assert(Size <= 8 && "does not support size > 8");
6044 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6045 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006046 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6047 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006048 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6049 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006050 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006051 src, dst, "weakassign");
6052 return;
6053}
6054
6055/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6056/// objc_assign_global (id src, id *dst)
6057///
6058void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006059 llvm::Value *src, llvm::Value *dst,
6060 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006061 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006062 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006063 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006064 assert(Size <= 8 && "does not support size > 8");
6065 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6066 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006067 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6068 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006069 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6070 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006071 if (!threadlocal)
6072 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6073 src, dst, "globalassign");
6074 else
6075 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6076 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006077 return;
6078}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006079
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006080void
John McCallbd309292010-07-06 01:34:17 +00006081CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6082 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006083 EmitAtSynchronizedStmt(CGF, S,
6084 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6085 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006086}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006087
John McCall2ca705e2010-07-24 00:37:23 +00006088llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006089CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006090 // There's a particular fixed type info for 'id'.
6091 if (T->isObjCIdType() ||
6092 T->isObjCQualifiedIdType()) {
6093 llvm::Constant *IDEHType =
6094 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6095 if (!IDEHType)
6096 IDEHType =
6097 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6098 false,
6099 llvm::GlobalValue::ExternalLinkage,
6100 0, "OBJC_EHTYPE_id");
6101 return IDEHType;
6102 }
6103
6104 // All other types should be Objective-C interface pointer types.
6105 const ObjCObjectPointerType *PT =
6106 T->getAs<ObjCObjectPointerType>();
6107 assert(PT && "Invalid @catch type.");
6108 const ObjCInterfaceType *IT = PT->getInterfaceType();
6109 assert(IT && "Invalid @catch type.");
6110 return GetInterfaceEHType(IT->getDecl(), false);
6111}
6112
John McCallbd309292010-07-06 01:34:17 +00006113void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6114 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006115 EmitTryCatchStmt(CGF, S,
6116 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6117 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6118 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006119}
6120
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006121/// EmitThrowStmt - Generate code for a throw statement.
6122void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6123 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006124 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006125 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006126 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006127 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006128 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006129 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006130 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006131 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006132 }
6133
John McCall17afe452010-10-16 08:21:07 +00006134 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006135 CGF.Builder.ClearInsertionPoint();
6136}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006137
John McCall2ca705e2010-07-24 00:37:23 +00006138llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006139CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006140 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006141 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006142
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006143 // If we don't need a definition, return the entry if found or check
6144 // if we use an external reference.
6145 if (!ForDefinition) {
6146 if (Entry)
6147 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006148
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006149 // If this type (or a super class) has the __objc_exception__
6150 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006151 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006152 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006153 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006154 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006155 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006156 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006157 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006158 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006159
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006160 // Otherwise we need to either make a new entry or fill in the
6161 // initializer.
6162 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006163 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006164 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006165 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006166 CGM.getModule().getGlobalVariable(VTableName);
6167 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006168 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6169 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006170 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006171 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006172
Chris Lattner5e016ae2010-06-27 07:15:29 +00006173 llvm::Value *VTableIdx =
6174 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006175
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006176 llvm::Constant *Values[] = {
6177 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6178 GetClassName(ID->getIdentifier()),
6179 GetClassGlobal(ClassName)
6180 };
Owen Anderson170229f2009-07-14 23:10:40 +00006181 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006182 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006183
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006184 if (Entry) {
6185 Entry->setInitializer(Init);
6186 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006187 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006188 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006189 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006190 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006191 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006192 }
6193
John McCall457a04e2010-10-22 21:05:15 +00006194 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006195 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006196 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6197 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006198
6199 if (ForDefinition) {
6200 Entry->setSection("__DATA,__objc_const");
6201 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6202 } else {
6203 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6204 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006205
6206 return Entry;
6207}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006208
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006209/* *** */
6210
Daniel Dunbarb036db82008-08-13 03:21:16 +00006211CodeGen::CGObjCRuntime *
6212CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006213 if (CGM.getLangOptions().ObjCNonFragileABI)
6214 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006215 return new CGObjCMac(CGM);
6216}