blob: 3171d988d3388f6f61e552dc7faf961ad9c13ca1 [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
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000276 /// This routine declares and returns address of:
277 /// void objc_copyCppObjectAtomic(
278 /// void *dest, const void *src,
279 /// void (*copyHelper) (void *dest, const void *source));
280 llvm::Constant *getCppAtomicObjectFunction() {
281 CodeGen::CodeGenTypes &Types = CGM.getTypes();
282 ASTContext &Ctx = CGM.getContext();
283 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
284 SmallVector<CanQualType,3> Params;
285 Params.push_back(Ctx.VoidPtrTy);
286 Params.push_back(Ctx.VoidPtrTy);
287 Params.push_back(Ctx.VoidPtrTy);
288 llvm::FunctionType *FTy =
289 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
290 FunctionType::ExtInfo()),
291 false);
292 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
293 }
294
Chris Lattnerce8754e2009-04-22 02:44:54 +0000295 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000296 CodeGen::CodeGenTypes &Types = CGM.getTypes();
297 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000298 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000299 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000300 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000301 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000302 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000303 FunctionType::ExtInfo()),
304 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000305 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
306 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000307
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000308 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000309 llvm::Constant *getGcReadWeakFn() {
310 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000311 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000312 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000313 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000314 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000315 }
316
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000317 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000318 llvm::Constant *getGcAssignWeakFn() {
319 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000320 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000321 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000322 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000323 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
324 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000325
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000326 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000327 llvm::Constant *getGcAssignGlobalFn() {
328 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000329 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
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_global");
333 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000334
Fariborz Jahanian217af242010-07-20 20:30:03 +0000335 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
336 llvm::Constant *getGcAssignThreadLocalFn() {
337 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000338 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000339 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000340 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
342 }
343
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000344 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000345 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000346 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000347 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
348 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000349 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000350 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000351 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
352 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000353
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000354 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
355 llvm::Constant *GcMemmoveCollectableFn() {
356 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000357 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000358 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
360 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000361
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000362 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000363 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000364 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000365 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000366 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000367 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
369 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000370
371 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000372 llvm::Constant *getExceptionThrowFn() {
373 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000374 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000375 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000376 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000379
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000380 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
381 llvm::Constant *getExceptionRethrowFn() {
382 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000383 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000384 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
385 }
386
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000387 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000388 llvm::Constant *getSyncEnterFn() {
389 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000390 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000391 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000392 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
394 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000395
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000396 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000397 llvm::Constant *getSyncExitFn() {
398 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000399 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000400 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000401 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000402 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
403 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000404
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendFn(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
407 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000408
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFn2(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
411 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000412
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000413 llvm::Constant *getSendStretFn(bool IsSuper) const {
414 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
415 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000416
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000417 llvm::Constant *getSendStretFn2(bool IsSuper) const {
418 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000420
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFpretFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
423 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000424
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
427 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000428
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000429 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
431 }
432
433 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
435 }
436
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000437 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
438 ~ObjCCommonTypesHelper(){}
439};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000440
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000441/// ObjCTypesHelper - Helper class that encapsulates lazy
442/// construction of varies types used during ObjC generation.
443class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000444public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000445 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000446 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000447 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000448 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000449 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000450 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000451
Daniel Dunbarb036db82008-08-13 03:21:16 +0000452 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000453 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000454 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000455 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000456 /// ProtocolExtensionTy - LLVM type for struct
457 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000458 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000459 /// ProtocolExtensionTy - LLVM type for struct
460 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000461 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000462 /// MethodDescriptionTy - LLVM type for struct
463 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000464 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000465 /// MethodDescriptionListTy - LLVM type for struct
466 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000467 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000468 /// MethodDescriptionListPtrTy - LLVM type for struct
469 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000470 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000471 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000472 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000473 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000474 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000475 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000476 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000477 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000478 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000479 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000480 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000481 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000482 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000483 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000484 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000485 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000486 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000487 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000488 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000489 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000490 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000491 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000492 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000493 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000494 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000495
Anders Carlsson9ff22482008-09-09 10:10:21 +0000496 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000497 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000498
Anders Carlsson9ff22482008-09-09 10:10:21 +0000499 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000500 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000501 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000502 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000503 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000504 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000505 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000506
507 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000508 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000509 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000510 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000511 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000512 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000513 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000514
515 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000516 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000517 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000518 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000519 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000520 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000521 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000522
Anders Carlsson9ff22482008-09-09 10:10:21 +0000523 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000524 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000525 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000526 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000527 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000528 "objc_exception_match");
529
Chris Lattnerc6406db2009-04-22 02:26:14 +0000530 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Anders Carlsson9ff22482008-09-09 10:10:21 +0000532 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000533 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000534 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000535 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000536 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
537 params, false),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000538 "_setjmp",
539 llvm::Attribute::ReturnsTwice);
Chris Lattnerc6406db2009-04-22 02:26:14 +0000540 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000541
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000542public:
543 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000544 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000545};
546
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000547/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000548/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000549class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000550public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000551
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000552 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000553 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000554
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000555 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000556 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000557
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000558 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000559 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000560
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000561 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000562 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000563
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000564 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000565 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000566
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000567 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000568 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000569
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000570 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000571 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000572
Fariborz Jahanian71394042009-01-23 23:53:38 +0000573 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000574 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000576 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000577 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000578
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000579 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000580 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000581
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000582 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000583 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000584
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000585 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000586 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000587
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000588 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000589 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000591 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000592 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000593
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000594 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000595
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000596 // MessageRefTy - LLVM for:
597 // struct _message_ref_t {
598 // IMP messenger;
599 // SEL name;
600 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000601 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000602 // MessageRefCTy - clang type for struct _message_ref_t
603 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000605 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000606 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000607 // MessageRefCPtrTy - clang type for struct _message_ref_t*
608 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000610 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000611 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000612
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000613 // SuperMessageRefTy - LLVM for:
614 // struct _super_message_ref_t {
615 // SUPER_IMP messenger;
616 // SEL name;
617 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000618 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000620 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000621 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000622
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000623 llvm::Constant *getMessageSendFixupFn() {
624 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000625 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000626 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000627 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000628 "objc_msgSend_fixup");
629 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000630
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000631 llvm::Constant *getMessageSendFpretFixupFn() {
632 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000633 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000635 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000636 "objc_msgSend_fpret_fixup");
637 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendStretFixupFn() {
640 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000641 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000642 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000643 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000644 "objc_msgSend_stret_fixup");
645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000647 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000648 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000649 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000650 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000651 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000652 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000653 "objc_msgSendSuper2_fixup");
654 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000656 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000657 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000658 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000659 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000660 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000661 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000662 "objc_msgSendSuper2_stret_fixup");
663 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Chris Lattnera7c00b42009-04-22 02:15:23 +0000665 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000666 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000667 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000668
Chris Lattnera7c00b42009-04-22 02:15:23 +0000669 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Chris Lattnera7c00b42009-04-22 02:15:23 +0000671 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000672 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000674 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000675 "objc_begin_catch");
676 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000677
Chris Lattnera5f58b02011-07-09 17:41:47 +0000678 llvm::StructType *EHTypeTy;
679 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000680
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000681 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
682 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000683};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000685class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000686public:
687 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000688 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000689 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000690 unsigned ivar_bytepos;
691 unsigned ivar_size;
692 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000693 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000694
695 // Allow sorting based on byte pos.
696 bool operator<(const GC_IVAR &b) const {
697 return ivar_bytepos < b.ivar_bytepos;
698 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000699 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000700
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000701 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000702 public:
703 unsigned skip;
704 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000705 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000706 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000707 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000708
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000709protected:
710 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000711 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000712 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000713 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000714
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000715 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000716 SmallVector<GC_IVAR, 16> SkipIvars;
717 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000718
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000719 /// LazySymbols - Symbols to generate a lazy reference for. See
720 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000721 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000722
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000723 /// DefinedSymbols - External symbols which are defined by this
724 /// module. The symbols in this list and LazySymbols are used to add
725 /// special linker symbols which ensure that Objective-C modules are
726 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000727 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000729 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000730 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000732 /// MethodVarNames - uniqued method variable names.
733 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000734
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000735 /// DefinedCategoryNames - list of category names in form Class_Category.
736 llvm::SetVector<std::string> DefinedCategoryNames;
737
Daniel Dunbarb036db82008-08-13 03:21:16 +0000738 /// MethodVarTypes - uniqued method type signatures. We have to use
739 /// a StringMap here because have no other unique reference.
740 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000741
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000742 /// MethodDefinitions - map of methods which have been defined in
743 /// this translation unit.
744 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000745
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000746 /// PropertyNames - uniqued method variable names.
747 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000748
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000749 /// ClassReferences - uniqued class references.
750 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000751
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000752 /// SelectorReferences - uniqued selector references.
753 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000754
Daniel Dunbarb036db82008-08-13 03:21:16 +0000755 /// Protocols - Protocols for which an objc_protocol structure has
756 /// been emitted. Forward declarations are handled by creating an
757 /// empty structure whose initializer is filled in when/if defined.
758 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000759
Daniel Dunbarc475d422008-10-29 22:36:39 +0000760 /// DefinedProtocols - Protocols which have actually been
761 /// defined. We should not need this, see FIXME in GenerateProtocol.
762 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000763
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000764 /// DefinedClasses - List of defined classes.
765 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000766
767 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
768 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000769
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000770 /// DefinedCategories - List of defined categories.
771 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000772
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000773 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
774 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000775
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000776 /// GetNameForMethod - Return a name for the given method.
777 /// \param[out] NameOut - The return value.
778 void GetNameForMethod(const ObjCMethodDecl *OMD,
779 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000780 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000781
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000782 /// GetMethodVarName - Return a unique constant for the given
783 /// selector's name. The return value has type char *.
784 llvm::Constant *GetMethodVarName(Selector Sel);
785 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000786
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000787 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000788 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000789
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000790 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000791 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
792 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000793 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000794
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000795 /// GetPropertyName - Return a unique constant for the given
796 /// name. The return value has type char *.
797 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000798
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000799 // FIXME: This can be dropped once string functions are unified.
800 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
801 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000802
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000803 /// GetClassName - Return a unique constant for the given selector's
804 /// name. The return value has type char *.
805 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000806
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000807 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
808
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000809 /// BuildIvarLayout - Builds ivar layout bitmap for the class
810 /// implementation for the __strong or __weak case.
811 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000812 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
813 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000814
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000815 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000816
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000817 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000818 unsigned int BytePos, bool ForStrongLayout,
819 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000820 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000821 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000822 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +0000823 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000824 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000825 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000826
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000827 /// GetIvarLayoutName - Returns a unique constant for the given
828 /// ivar layout bitmap.
829 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
830 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000831
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000832 /// EmitPropertyList - Emit the given property list. The return
833 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000834 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000836 const ObjCContainerDecl *OCD,
837 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000838
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000839 /// EmitProtocolMethodTypes - Generate the array of extended method type
840 /// strings. The return value has type Int8PtrPtrTy.
841 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
842 const ConstantVector &MethodTypes,
843 const ObjCCommonTypesHelper &ObjCTypes);
844
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000845 /// PushProtocolProperties - Push protocol's property on the input stack.
846 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
847 std::vector<llvm::Constant*> &Properties,
848 const Decl *Container,
849 const ObjCProtocolDecl *PROTO,
850 const ObjCCommonTypesHelper &ObjCTypes);
851
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000852 /// GetProtocolRef - Return a reference to the internal protocol
853 /// description, creating an empty one if it has not been
854 /// defined. The return value has type ProtocolPtrTy.
855 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000856
Daniel Dunbar30c65362009-03-09 20:09:19 +0000857 /// CreateMetadataVar - Create a global variable with internal
858 /// linkage for use by the Objective-C runtime.
859 ///
860 /// This is a convenience wrapper which not only creates the
861 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000862 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000863 ///
864 /// \param Name - The variable name.
865 /// \param Init - The variable initializer; this is also used to
866 /// define the type of the variable.
867 /// \param Section - The section the variable should go into, or 0.
868 /// \param Align - The alignment for the variable, or 0.
869 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000870 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000871 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000872 llvm::Constant *Init,
873 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000874 unsigned Align,
875 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000876
John McCall9e8bb002011-05-14 03:10:52 +0000877 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
878 ReturnValueSlot Return,
879 QualType ResultType,
880 llvm::Value *Sel,
881 llvm::Value *Arg0,
882 QualType Arg0Ty,
883 bool IsSuper,
884 const CallArgList &CallArgs,
885 const ObjCMethodDecl *OMD,
886 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000887
Daniel Dunbar5e639272010-04-25 20:39:01 +0000888 /// EmitImageInfo - Emit the image info marker used to encode some module
889 /// level information.
890 void EmitImageInfo();
891
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000892public:
Owen Andersonae86c192009-07-13 04:10:07 +0000893 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000894 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000895
David Chisnall481e3a82010-01-23 02:40:42 +0000896 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000897
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000898 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
899 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000900
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000901 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000903 /// GetOrEmitProtocol - Get the protocol object for the given
904 /// declaration, emitting it if necessary. The return value has type
905 /// ProtocolPtrTy.
906 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000908 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
909 /// object for the given declaration, emitting it if needed. These
910 /// forward references will be filled in with empty bodies if no
911 /// definition is seen. The return value has type ProtocolPtrTy.
912 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000913 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
914 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000915
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000916};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000917
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000918class CGObjCMac : public CGObjCCommonMac {
919private:
920 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000921
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000922 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000924 void EmitModuleInfo();
925
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000926 /// EmitModuleSymols - Emit module symbols, the list of defined
927 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000928 llvm::Constant *EmitModuleSymbols();
929
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000930 /// FinishModule - Write out global data structures at the end of
931 /// processing a translation unit.
932 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000933
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000934 /// EmitClassExtension - Generate the class extension structure used
935 /// to store the weak ivar layout and properties. The return value
936 /// has type ClassExtensionPtrTy.
937 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
938
939 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
940 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000941 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000942 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000943
John McCall31168b02011-06-15 23:02:42 +0000944 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
945 IdentifierInfo *II);
946
947 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
948
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000949 /// EmitSuperClassRef - Emits reference to class's main metadata class.
950 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000951
952 /// EmitIvarList - Emit the ivar list for the given
953 /// implementation. If ForClass is true the list of class ivars
954 /// (i.e. metaclass ivars) is emitted, otherwise the list of
955 /// interface ivars will be emitted. The return value has type
956 /// IvarListPtrTy.
957 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000958 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000959
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000960 /// EmitMetaClass - Emit a forward reference to the class structure
961 /// for the metaclass of the given interface. The return value has
962 /// type ClassPtrTy.
963 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
964
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000965 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000966 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000967 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
968 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000969 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000970
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000971 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000972
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000973 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000974
975 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000976 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000977 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000978 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000979 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000980
981 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000982 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000983 /// - TypeName: The name for the type containing the methods.
984 /// - IsProtocol: True iff these methods are for a protocol.
985 /// - ClassMethds: True iff these are class methods.
986 /// - Required: When true, only "required" methods are
987 /// listed. Similarly, when false only "optional" methods are
988 /// listed. For classes this should always be true.
989 /// - begin, end: The method list to output.
990 ///
991 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000992 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000993 const char *Section,
994 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000995
Daniel Dunbarc475d422008-10-29 22:36:39 +0000996 /// GetOrEmitProtocol - Get the protocol object for the given
997 /// declaration, emitting it if necessary. The return value has type
998 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000999 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001000
1001 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1002 /// object for the given declaration, emitting it if needed. These
1003 /// forward references will be filled in with empty bodies if no
1004 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001005 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001006
Daniel Dunbarb036db82008-08-13 03:21:16 +00001007 /// EmitProtocolExtension - Generate the protocol extension
1008 /// structure used to store optional instance and class methods, and
1009 /// protocol properties. The return value has type
1010 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001011 llvm::Constant *
1012 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1013 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001014 const ConstantVector &OptClassMethods,
1015 const ConstantVector &MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001016
1017 /// EmitProtocolList - Generate the list of referenced
1018 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001019 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001020 ObjCProtocolDecl::protocol_iterator begin,
1021 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001022
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001023 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1024 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001025 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1026 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001027
1028public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001029 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001030
Fariborz Jahanian71394042009-01-23 23:53:38 +00001031 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001032
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001033 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001034 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001035 QualType ResultType,
1036 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001037 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001038 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001039 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001040 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001041
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001042 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001043 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001044 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001045 QualType ResultType,
1046 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001047 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001048 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001049 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001050 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001051 const CallArgList &CallArgs,
1052 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001053
Daniel Dunbarcb463852008-11-01 01:53:16 +00001054 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001055 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001056
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001057 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1058 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001059
1060 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1061 /// untyped one.
1062 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1063 const ObjCMethodDecl *Method);
1064
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001065 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001066
Daniel Dunbar92992502008-08-15 22:20:32 +00001067 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001068
Daniel Dunbar92992502008-08-15 22:20:32 +00001069 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001070
Daniel Dunbarcb463852008-11-01 01:53:16 +00001071 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001072 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001073
Chris Lattnerd4808922009-03-22 21:03:39 +00001074 virtual llvm::Constant *GetPropertyGetFunction();
1075 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001076 virtual llvm::Constant *GetGetStructFunction();
1077 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001078 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001079 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001080
John McCallbd309292010-07-06 01:34:17 +00001081 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1082 const ObjCAtTryStmt &S);
1083 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1084 const ObjCAtSynchronizedStmt &S);
1085 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001086 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1087 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001088 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001090 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001091 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001092 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001093 llvm::Value *src, llvm::Value *dest,
1094 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001095 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001096 llvm::Value *src, llvm::Value *dest,
1097 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001098 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1099 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001100 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1101 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001102 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001103
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001104 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1105 QualType ObjectTy,
1106 llvm::Value *BaseValue,
1107 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001108 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001109 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001110 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001111 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001112
1113 /// GetClassGlobal - Return the global variable for the Objective-C
1114 /// class of the given name.
1115 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001116 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001117 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001118};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001119
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001120class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001121private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001122 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001123 llvm::GlobalVariable* ObjCEmptyCacheVar;
1124 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001125
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001126 /// SuperClassReferences - uniqued super class references.
1127 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001128
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001129 /// MetaClassReferences - uniqued meta class references.
1130 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001131
1132 /// EHTypeReferences - uniqued class ehtype references.
1133 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001134
John McCall9e8bb002011-05-14 03:10:52 +00001135 /// VTableDispatchMethods - List of methods for which we generate
1136 /// vtable-based message dispatch.
1137 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138
Fariborz Jahanian67260552009-11-17 21:37:35 +00001139 /// DefinedMetaClasses - List of defined meta-classes.
1140 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1141
John McCall9e8bb002011-05-14 03:10:52 +00001142 /// isVTableDispatchedSelector - Returns true if SEL is a
1143 /// vtable-based selector.
1144 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001145
Fariborz Jahanian71394042009-01-23 23:53:38 +00001146 /// FinishNonFragileABIModule - Write out global data structures at the end of
1147 /// processing a translation unit.
1148 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001149
Daniel Dunbar19573e72009-05-15 21:48:48 +00001150 /// AddModuleClassList - Add the given list of class pointers to the
1151 /// module with the provided symbol and section names.
1152 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1153 const char *SymbolName,
1154 const char *SectionName);
1155
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001156 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1157 unsigned InstanceStart,
1158 unsigned InstanceSize,
1159 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001160 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001161 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001162 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001163 llvm::Constant *ClassRoGV,
1164 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001165
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001166 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001167
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001168 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001169
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001170 /// EmitMethodList - Emit the method list for the given
1171 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001173 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001174 const ConstantVector &Methods);
1175 /// EmitIvarList - Emit the ivar list for the given
1176 /// implementation. If ForClass is true the list of class ivars
1177 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1178 /// interface ivars will be emitted. The return value has type
1179 /// IvarListnfABIPtrTy.
1180 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001182 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001183 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001184 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001185
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001186 /// GetOrEmitProtocol - Get the protocol object for the given
1187 /// declaration, emitting it if necessary. The return value has type
1188 /// ProtocolPtrTy.
1189 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001190
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001191 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1192 /// object for the given declaration, emitting it if needed. These
1193 /// forward references will be filled in with empty bodies if no
1194 /// definition is seen. The return value has type ProtocolPtrTy.
1195 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001197 /// EmitProtocolList - Generate the list of referenced
1198 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001199 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001200 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001201 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202
John McCall9e8bb002011-05-14 03:10:52 +00001203 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1204 ReturnValueSlot Return,
1205 QualType ResultType,
1206 Selector Sel,
1207 llvm::Value *Receiver,
1208 QualType Arg0Ty,
1209 bool IsSuper,
1210 const CallArgList &CallArgs,
1211 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001212
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001213 /// GetClassGlobal - Return the global variable for the Objective-C
1214 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001215 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001216
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001217 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001218 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001219 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001220 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001221
1222 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1223 IdentifierInfo *II);
1224
1225 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001226
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001227 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1228 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001229 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1230 const ObjCInterfaceDecl *ID);
1231
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001232 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1233 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001235 const ObjCInterfaceDecl *ID);
1236
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001237 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1238 /// the given ivar.
1239 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001240 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001241 const ObjCInterfaceDecl *ID,
1242 const ObjCIvarDecl *Ivar);
1243
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001244 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1245 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001246 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1247 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001248
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001249 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001250 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001251 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001252 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001253
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001254 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001255 return "OBJC_METACLASS_$_";
1256 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001257
Daniel Dunbar15894b72009-04-07 05:48:37 +00001258 const char *getClassSymbolPrefix() const {
1259 return "OBJC_CLASS_$_";
1260 }
1261
Daniel Dunbar961202372009-05-03 12:57:56 +00001262 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001263 uint32_t &InstanceStart,
1264 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001265
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001266 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001267 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001268 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1269 return CGM.getContext().Selectors.getSelector(0, &II);
1270 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001271
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001272 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001273 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1274 return CGM.getContext().Selectors.getSelector(1, &II);
1275 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001276
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001277 /// ImplementationIsNonLazy - Check whether the given category or
1278 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001279 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001280
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001281public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001282 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001283 // FIXME. All stubs for now!
1284 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001285
Fariborz Jahanian71394042009-01-23 23:53:38 +00001286 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001287 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001288 QualType ResultType,
1289 Selector Sel,
1290 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001291 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001292 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001293 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001294
1295 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001296 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001297 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001298 QualType ResultType,
1299 Selector Sel,
1300 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001301 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001302 llvm::Value *Receiver,
1303 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001304 const CallArgList &CallArgs,
1305 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001306
Fariborz Jahanian71394042009-01-23 23:53:38 +00001307 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001308 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001309
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001310 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1311 bool lvalue = false)
1312 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001313
1314 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1315 /// untyped one.
1316 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1317 const ObjCMethodDecl *Method)
1318 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001319
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001320 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001321
Fariborz Jahanian71394042009-01-23 23:53:38 +00001322 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001323 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001324 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001325
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001326 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001327
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001328 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001329 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001330 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001331 virtual llvm::Constant *GetPropertySetFunction() {
1332 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001333 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001334
David Chisnall168b80f2010-12-26 22:13:16 +00001335 virtual llvm::Constant *GetSetStructFunction() {
1336 return ObjCTypes.getCopyStructFn();
1337 }
1338 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001339 return ObjCTypes.getCopyStructFn();
1340 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001341 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1342 return ObjCTypes.getCppAtomicObjectFunction();
1343 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001344
Chris Lattnerd4808922009-03-22 21:03:39 +00001345 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001346 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001347 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001348
John McCallbd309292010-07-06 01:34:17 +00001349 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1350 const ObjCAtTryStmt &S);
1351 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1352 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001353 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001354 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001355 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001356 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001357 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001358 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001359 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001360 llvm::Value *src, llvm::Value *dest,
1361 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001362 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001363 llvm::Value *src, llvm::Value *dest,
1364 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001365 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001366 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001367 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1368 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001369 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001370 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1371 QualType ObjectTy,
1372 llvm::Value *BaseValue,
1373 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001374 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001375 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001376 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001377 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001378};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001379
John McCall5880fb82011-05-14 21:12:11 +00001380/// A helper class for performing the null-initialization of a return
1381/// value.
1382struct NullReturnState {
1383 llvm::BasicBlock *NullBB;
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001384 llvm::BasicBlock *callBB;
1385 NullReturnState() : NullBB(0), callBB(0) {}
John McCall5880fb82011-05-14 21:12:11 +00001386
1387 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1388 // Make blocks for the null-init and call edges.
1389 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001390 callBB = CGF.createBasicBlock("msgSend.call");
John McCall5880fb82011-05-14 21:12:11 +00001391
1392 // Check for a null receiver and, if there is one, jump to the
1393 // null-init test.
1394 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1395 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1396
1397 // Otherwise, start performing the call.
1398 CGF.EmitBlock(callBB);
1399 }
1400
1401 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1402 if (!NullBB) return result;
1403
1404 // Finish the call path.
1405 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1406 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1407
1408 // Emit the null-init block and perform the null-initialization there.
1409 CGF.EmitBlock(NullBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001410 if (!resultType->isAnyComplexType()) {
1411 assert(result.isAggregate() && "null init of non-aggregate result?");
1412 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1413 // Jump to the continuation block.
1414 CGF.EmitBlock(contBB);
1415 return result;
1416 }
John McCall5880fb82011-05-14 21:12:11 +00001417
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001418 // _Complex type
1419 // FIXME. Now easy to handle any other scalar type whose result is returned
1420 // in memory due to ABI limitations.
John McCall5880fb82011-05-14 21:12:11 +00001421 CGF.EmitBlock(contBB);
Fariborz Jahanianb0050702011-10-26 20:53:59 +00001422 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1423 llvm::Type *MemberType = CallCV.first->getType();
1424 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1425 // Create phi instruction for scalar complex value.
1426 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1427 PHIReal->addIncoming(ZeroCV, NullBB);
1428 PHIReal->addIncoming(CallCV.first, callBB);
1429 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1430 PHIImag->addIncoming(ZeroCV, NullBB);
1431 PHIImag->addIncoming(CallCV.second, callBB);
1432 return RValue::getComplex(PHIReal, PHIImag);
John McCall5880fb82011-05-14 21:12:11 +00001433 }
1434};
1435
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001436} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001437
1438/* *** Helper Functions *** */
1439
1440/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001441static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001442 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001443 unsigned idx0,
1444 unsigned idx1) {
1445 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001446 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1447 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001448 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001449 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001450}
1451
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001452/// hasObjCExceptionAttribute - Return true if this class or any super
1453/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001454static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001455 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001456 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001457 return true;
1458 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001459 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001460 return false;
1461}
1462
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001463/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001464
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001465CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001466 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001467 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001468 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001469}
1470
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001471/// GetClass - Return a reference to the class for the given interface
1472/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001473llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001474 const ObjCInterfaceDecl *ID) {
1475 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001476}
1477
1478/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001479llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1480 bool lval) {
1481 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001482}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001483llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001484 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001485 return EmitSelector(Builder, Method->getSelector());
1486}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001487
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001488llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001489 if (T->isObjCIdType() ||
1490 T->isObjCQualifiedIdType()) {
1491 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001492 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001493 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001494 if (T->isObjCClassType() ||
1495 T->isObjCQualifiedClassType()) {
1496 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001497 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001498 }
1499 if (T->isObjCObjectPointerType())
1500 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1501
John McCall2ca705e2010-07-24 00:37:23 +00001502 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1503 return 0;
1504}
1505
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001506/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001507/*
1508 struct __builtin_CFString {
1509 const int *isa; // point to __CFConstantStringClassReference
1510 int flags;
1511 const char *str;
1512 long length;
1513 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001514*/
1515
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001516/// or Generate a constant NSString object.
1517/*
1518 struct __builtin_NSString {
1519 const int *isa; // point to __NSConstantStringClassReference
1520 const char *str;
1521 unsigned int length;
1522 };
1523*/
1524
Fariborz Jahanian71394042009-01-23 23:53:38 +00001525llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001526 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001527 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1528 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001529 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001530}
1531
1532/// Generates a message send where the super is the receiver. This is
1533/// a message send to self with special delivery semantics indicating
1534/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001535CodeGen::RValue
1536CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001537 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001538 QualType ResultType,
1539 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001540 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001541 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001542 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001543 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001544 const CodeGen::CallArgList &CallArgs,
1545 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001546 // Create and init a super structure; this is a (receiver, class)
1547 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001548 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001549 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001550 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001551 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001552 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001553 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001554
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001555 // If this is a class message the metaclass is passed as the target.
1556 llvm::Value *Target;
1557 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001558 if (isCategoryImpl) {
1559 // Message sent to 'super' in a class method defined in a category
1560 // implementation requires an odd treatment.
1561 // If we are in a class method, we must retrieve the
1562 // _metaclass_ for the current class, pointed at by
1563 // the class's "isa" pointer. The following assumes that
1564 // isa" is the first ivar in a class (which it must be).
1565 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1566 Target = CGF.Builder.CreateStructGEP(Target, 0);
1567 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001568 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001569 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1570 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1571 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1572 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001573 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001574 }
1575 else if (isCategoryImpl)
1576 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1577 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001578 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1579 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1580 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001581 }
Mike Stump18bb9282009-05-16 07:57:57 +00001582 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1583 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001584 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001585 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001586 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001587 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001588 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001589 return EmitMessageSend(CGF, Return, ResultType,
1590 EmitSelector(CGF.Builder, Sel),
1591 ObjCSuper, ObjCTypes.SuperPtrCTy,
1592 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001593}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001594
1595/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001596CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001597 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001598 QualType ResultType,
1599 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001600 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001601 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001602 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001603 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001604 return EmitMessageSend(CGF, Return, ResultType,
1605 EmitSelector(CGF.Builder, Sel),
1606 Receiver, CGF.getContext().getObjCIdType(),
1607 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001608}
1609
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001610CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001611CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1612 ReturnValueSlot Return,
1613 QualType ResultType,
1614 llvm::Value *Sel,
1615 llvm::Value *Arg0,
1616 QualType Arg0Ty,
1617 bool IsSuper,
1618 const CallArgList &CallArgs,
1619 const ObjCMethodDecl *Method,
1620 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001621 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001622 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001623 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001624 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1625 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001626 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001627
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001628 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001629 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001630 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001631 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001632 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001633
Anders Carlsson280e61f12010-06-21 20:59:55 +00001634 if (Method)
1635 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1636 CGM.getContext().getCanonicalType(ResultType) &&
1637 "Result type mismatch!");
1638
John McCall5880fb82011-05-14 21:12:11 +00001639 NullReturnState nullReturn;
1640
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001641 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001642 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001643 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001644 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001645 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001646 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1647 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1648 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001649 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1650 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1651 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001652 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001653 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001654 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001655 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001656 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001657 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1658 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001659}
1660
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001661static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1662 if (FQT.isObjCGCStrong())
1663 return Qualifiers::Strong;
1664
John McCall31168b02011-06-15 23:02:42 +00001665 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001666 return Qualifiers::Weak;
1667
1668 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1669 return Qualifiers::Strong;
1670
1671 if (const PointerType *PT = FQT->getAs<PointerType>())
1672 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1673
1674 return Qualifiers::GCNone;
1675}
1676
John McCall351762c2011-02-07 10:33:21 +00001677llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1678 const CGBlockInfo &blockInfo) {
1679 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001680 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001681
Douglas Gregor79a91412011-09-13 17:21:33 +00001682 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001683 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001684 return nullPtr;
1685
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001686 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001687 SkipIvars.clear();
1688 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001689 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1690 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001691
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001692 // __isa is the first field in block descriptor and must assume by runtime's
1693 // convention that it is GC'able.
1694 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001695
1696 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1697
1698 // Calculate the basic layout of the block structure.
1699 const llvm::StructLayout *layout =
1700 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1701
1702 // Ignore the optional 'this' capture: C++ objects are not assumed
1703 // to be GC'ed.
1704
1705 // Walk the captured variables.
1706 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1707 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1708 const VarDecl *variable = ci->getVariable();
1709 QualType type = variable->getType();
1710
1711 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1712
1713 // Ignore constant captures.
1714 if (capture.isConstant()) continue;
1715
1716 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1717
1718 // __block variables are passed by their descriptor address.
1719 if (ci->isByRef()) {
1720 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001721 continue;
John McCall351762c2011-02-07 10:33:21 +00001722 }
1723
1724 assert(!type->isArrayType() && "array variable should not be caught");
1725 if (const RecordType *record = type->getAs<RecordType>()) {
1726 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001727 continue;
1728 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001729
John McCall351762c2011-02-07 10:33:21 +00001730 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1731 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1732
1733 if (GCAttr == Qualifiers::Strong)
1734 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1735 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001736 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001737 SkipIvars.push_back(GC_IVAR(fieldOffset,
1738 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001739 }
1740
1741 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001742 return nullPtr;
1743
1744 // Sort on byte position; captures might not be allocated in order,
1745 // and unions can do funny things.
1746 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1747 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001748
1749 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001750 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001751 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1752 printf("\n block variable layout for block: ");
1753 const unsigned char *s = (unsigned char*)BitMap.c_str();
1754 for (unsigned i = 0; i < BitMap.size(); i++)
1755 if (!(s[i] & 0xf0))
1756 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1757 else
1758 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1759 printf("\n");
1760 }
1761
1762 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001763}
1764
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001765llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001766 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001767 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001768 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001769 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1770
Owen Andersonade90fd2009-07-29 18:54:39 +00001771 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001772 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001773}
1774
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001775void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001776 // FIXME: We shouldn't need this, the protocol decl should contain enough
1777 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001778 DefinedProtocols.insert(PD->getIdentifier());
1779
1780 // If we have generated a forward reference to this protocol, emit
1781 // it now. Otherwise do nothing, the protocol objects are lazily
1782 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001783 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001784 GetOrEmitProtocol(PD);
1785}
1786
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001787llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001788 if (DefinedProtocols.count(PD->getIdentifier()))
1789 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001790
Daniel Dunbarc475d422008-10-29 22:36:39 +00001791 return GetOrEmitProtocolRef(PD);
1792}
1793
Daniel Dunbarb036db82008-08-13 03:21:16 +00001794/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001795// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1796struct _objc_protocol {
1797struct _objc_protocol_extension *isa;
1798char *protocol_name;
1799struct _objc_protocol_list *protocol_list;
1800struct _objc__method_prototype_list *instance_methods;
1801struct _objc__method_prototype_list *class_methods
1802};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001803
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001804See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001805*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001806llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1807 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1808
1809 // Early exit if a defining object has already been generated.
1810 if (Entry && Entry->hasInitializer())
1811 return Entry;
1812
Douglas Gregora715bff2012-01-01 19:51:50 +00001813 // Use the protocol definition, if there is one.
1814 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1815 PD = Def;
1816
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001817 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001818 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001819 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1820
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001821 // Construct method lists.
1822 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1823 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001824 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001825 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001826 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001827 ObjCMethodDecl *MD = *i;
1828 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001829 if (!C)
1830 return GetOrEmitProtocolRef(PD);
1831
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001832 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1833 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001834 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001835 } else {
1836 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001837 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001838 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001839 }
1840
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001841 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001842 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001843 ObjCMethodDecl *MD = *i;
1844 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001845 if (!C)
1846 return GetOrEmitProtocolRef(PD);
1847
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001848 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1849 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001850 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001851 } else {
1852 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001853 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001854 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001855 }
1856
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001857 MethodTypesExt.insert(MethodTypesExt.end(),
1858 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1859
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001860 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001861 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1862 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001863 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001864 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001865 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001866 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001867 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001868 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001869 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001870 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001871 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001872 ClassMethods)
1873 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001874 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001875 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001876
Daniel Dunbarb036db82008-08-13 03:21:16 +00001877 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001878 // Already created, fix the linkage and update the initializer.
1879 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001880 Entry->setInitializer(Init);
1881 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001882 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001883 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001884 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001885 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001886 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001887 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001888 // FIXME: Is this necessary? Why only for protocol?
1889 Entry->setAlignment(4);
1890 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001891 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001892
1893 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001894}
1895
Daniel Dunbarc475d422008-10-29 22:36:39 +00001896llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001897 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1898
1899 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001900 // We use the initializer as a marker of whether this is a forward
1901 // reference or not. At module finalization we add the empty
1902 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001903 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001904 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001905 llvm::GlobalValue::ExternalLinkage,
1906 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001907 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001908 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001909 // FIXME: Is this necessary? Why only for protocol?
1910 Entry->setAlignment(4);
1911 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001912
Daniel Dunbarb036db82008-08-13 03:21:16 +00001913 return Entry;
1914}
1915
1916/*
1917 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001918 uint32_t size;
1919 struct objc_method_description_list *optional_instance_methods;
1920 struct objc_method_description_list *optional_class_methods;
1921 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001922 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923 };
1924*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001925llvm::Constant *
1926CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1927 const ConstantVector &OptInstanceMethods,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001928 const ConstantVector &OptClassMethods,
1929 const ConstantVector &MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001930 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001931 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001932 llvm::Constant *Values[] = {
1933 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001934 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001935 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001936 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001937 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001938 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001939 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001940 OptClassMethods),
1941 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001942 ObjCTypes),
1943 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
1944 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001945 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00001946
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001947 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001948 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001949 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001950 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001951
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001952 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001953 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001954
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001955 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001956 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001957 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001958 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001959}
1960
1961/*
1962 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001963 struct objc_protocol_list *next;
1964 long count;
1965 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001966 };
1967*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001968llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001969CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001970 ObjCProtocolDecl::protocol_iterator begin,
1971 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001972 std::vector<llvm::Constant*> ProtocolRefs;
1973
Daniel Dunbardec75f82008-08-21 21:57:41 +00001974 for (; begin != end; ++begin)
1975 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001976
1977 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001978 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001979 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001980
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001981 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001982 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001983
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001984 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001985 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001986 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001987 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988 ProtocolRefs.size() - 1);
1989 Values[2] =
1990 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1991 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001993
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001994 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001995 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001996 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001997 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001998 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001999}
2000
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002001void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
2002 std::vector<llvm::Constant*> &Properties,
2003 const Decl *Container,
2004 const ObjCProtocolDecl *PROTO,
2005 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002006 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2007 E = PROTO->protocol_end(); P != E; ++P)
2008 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2009 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2010 E = PROTO->prop_end(); I != E; ++I) {
2011 const ObjCPropertyDecl *PD = *I;
2012 if (!PropertySet.insert(PD->getIdentifier()))
2013 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002014 llvm::Constant *Prop[] = {
2015 GetPropertyName(PD->getIdentifier()),
2016 GetPropertyTypeString(PD, Container)
2017 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002018 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2019 }
2020}
2021
Daniel Dunbarb036db82008-08-13 03:21:16 +00002022/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002023 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002024 const char * const name;
2025 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002026 };
2027
2028 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002029 uint32_t entsize; // sizeof (struct _objc_property)
2030 uint32_t prop_count;
2031 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002032 };
2033*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002034llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002035 const Decl *Container,
2036 const ObjCContainerDecl *OCD,
2037 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002038 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002039 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002040 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2041 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002042 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002043 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002044 llvm::Constant *Prop[] = {
2045 GetPropertyName(PD->getIdentifier()),
2046 GetPropertyTypeString(PD, Container)
2047 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002048 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002049 Prop));
2050 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002051 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002052 for (ObjCInterfaceDecl::all_protocol_iterator
2053 P = OID->all_referenced_protocol_begin(),
2054 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002055 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2056 ObjCTypes);
2057 }
2058 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2059 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2060 E = CD->protocol_end(); P != E; ++P)
2061 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2062 ObjCTypes);
2063 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002064
2065 // Return null for empty list.
2066 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002067 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002068
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002070 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002071 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002072 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2073 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002074 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002075 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002076 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002077 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002078
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002079 llvm::GlobalVariable *GV =
2080 CreateMetadataVar(Name, Init,
2081 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002082 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002083 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002084 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002085 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002086}
2087
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002088llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2089 const ConstantVector &MethodTypes,
2090 const ObjCCommonTypesHelper &ObjCTypes) {
2091 // Return null for empty list.
2092 if (MethodTypes.empty())
2093 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2094
2095 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2096 MethodTypes.size());
2097 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2098
2099 llvm::GlobalVariable *GV =
2100 CreateMetadataVar(Name, Init,
2101 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2102 (ObjCABI == 2) ? 8 : 4,
2103 true);
2104 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2105}
2106
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002107/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002108 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002109 int count;
2110 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002111 };
2112*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002113llvm::Constant *
2114CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002115 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002116 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002117 ObjCTypes.SelectorPtrTy),
2118 GetMethodVarType(MD)
2119 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002120 if (!Desc[1])
2121 return 0;
2122
Owen Anderson0e0189d2009-07-27 22:29:56 +00002123 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002124 Desc);
2125}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002126
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002127llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002128 const char *Section,
2129 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002130 // Return null for empty list.
2131 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002132 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002133
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002134 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002135 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002136 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002137 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002138 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002139 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002140
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002141 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002142 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002143 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002144}
2145
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002146/*
2147 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002148 char *category_name;
2149 char *class_name;
2150 struct _objc_method_list *instance_methods;
2151 struct _objc_method_list *class_methods;
2152 struct _objc_protocol_list *protocols;
2153 uint32_t size; // <rdar://4585769>
2154 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002155 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002156*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002157void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002158 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002159
Mike Stump18bb9282009-05-16 07:57:57 +00002160 // FIXME: This is poor design, the OCD should have a pointer to the category
2161 // decl. Additionally, note that Category can be null for the @implementation
2162 // w/o an @interface case. Sema should just create one for us as it does for
2163 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002164 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002165 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002166 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002167
2168 llvm::SmallString<256> ExtName;
2169 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2170 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002171
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002172 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002173 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002174 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002175 // Instance methods should always be defined.
2176 InstanceMethods.push_back(GetMethodConstant(*i));
2177 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002178 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002179 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002180 // Class methods should always be defined.
2181 ClassMethods.push_back(GetMethodConstant(*i));
2182 }
2183
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002184 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002185 Values[0] = GetClassName(OCD->getIdentifier());
2186 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002187 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002188 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002189 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002190 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002191 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002192 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002193 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002194 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002195 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002196 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002197 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002198 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002199 Category->protocol_begin(),
2200 Category->protocol_end());
2201 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002202 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002203 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002204 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002205
2206 // If there is no category @interface then there can be no properties.
2207 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002208 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002209 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002210 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002211 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002212 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002213
Owen Anderson0e0189d2009-07-27 22:29:56 +00002214 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002215 Values);
2216
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002217 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002218 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002219 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002220 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002221 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002222 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002223 // method definition entries must be clear for next implementation.
2224 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002225}
2226
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002227// FIXME: Get from somewhere?
2228enum ClassFlags {
2229 eClassFlags_Factory = 0x00001,
2230 eClassFlags_Meta = 0x00002,
2231 // <rdr://5142207>
2232 eClassFlags_HasCXXStructors = 0x02000,
2233 eClassFlags_Hidden = 0x20000,
2234 eClassFlags_ABI2_Hidden = 0x00010,
2235 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2236};
2237
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238/*
2239 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002240 Class isa;
2241 Class super_class;
2242 const char *name;
2243 long version;
2244 long info;
2245 long instance_size;
2246 struct _objc_ivar_list *ivars;
2247 struct _objc_method_list *methods;
2248 struct _objc_cache *cache;
2249 struct _objc_protocol_list *protocols;
2250 // Objective-C 1.0 extensions (<rdr://4585769>)
2251 const char *ivar_layout;
2252 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002253 };
2254
2255 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002257void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002258 DefinedSymbols.insert(ID->getIdentifier());
2259
Chris Lattner86d7d912008-11-24 03:54:41 +00002260 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002261 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002262 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002263 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002264 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002265 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002266 Interface->all_referenced_protocol_begin(),
2267 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002268 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002269 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002270 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002271 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002272 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002273
2274 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002275 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002276 Flags |= eClassFlags_Hidden;
2277
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002278 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002279 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002280 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002281 // Instance methods should always be defined.
2282 InstanceMethods.push_back(GetMethodConstant(*i));
2283 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002284 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002285 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002286 // Class methods should always be defined.
2287 ClassMethods.push_back(GetMethodConstant(*i));
2288 }
2289
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002290 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002291 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002292 ObjCPropertyImplDecl *PID = *i;
2293
2294 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2295 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2296
2297 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2298 if (llvm::Constant *C = GetMethodConstant(MD))
2299 InstanceMethods.push_back(C);
2300 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2301 if (llvm::Constant *C = GetMethodConstant(MD))
2302 InstanceMethods.push_back(C);
2303 }
2304 }
2305
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002306 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002307 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002308 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002309 // Record a reference to the super class.
2310 LazySymbols.insert(Super->getIdentifier());
2311
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002312 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002313 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002314 ObjCTypes.ClassPtrTy);
2315 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002316 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002317 }
2318 Values[ 2] = GetClassName(ID->getIdentifier());
2319 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002320 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2321 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2322 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002323 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002324 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002325 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002326 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002327 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002328 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002329 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002330 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002331 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002332 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002333 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002334 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002335 std::string Name("\01L_OBJC_CLASS_");
2336 Name += ClassName;
2337 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2338 // Check for a forward reference.
2339 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2340 if (GV) {
2341 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2342 "Forward metaclass reference has incorrect type.");
2343 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2344 GV->setInitializer(Init);
2345 GV->setSection(Section);
2346 GV->setAlignment(4);
2347 CGM.AddUsedGlobal(GV);
2348 }
2349 else
2350 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002351 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002352 // method definition entries must be clear for next implementation.
2353 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002354}
2355
2356llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2357 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002358 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002359 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002360 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002361
John McCall457a04e2010-10-22 21:05:15 +00002362 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002363 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002364
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002365 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002366 // The isa for the metaclass is the root of the hierarchy.
2367 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2368 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2369 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002370 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002371 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002372 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002373 // The super class for the metaclass is emitted as the name of the
2374 // super class. The runtime fixes this up to point to the
2375 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002376 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002377 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002378 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002379 ObjCTypes.ClassPtrTy);
2380 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002381 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002382 }
2383 Values[ 2] = GetClassName(ID->getIdentifier());
2384 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002385 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2386 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2387 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002388 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002389 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002390 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002391 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002392 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002393 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002394 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002395 Values[ 9] = Protocols;
2396 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002397 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002399 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002400 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002401 Values);
2402
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002403 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002404 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002405
2406 // Check for a forward reference.
2407 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2408 if (GV) {
2409 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2410 "Forward metaclass reference has incorrect type.");
2411 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2412 GV->setInitializer(Init);
2413 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002414 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002415 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002416 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002417 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002418 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002419 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002420 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421
2422 return GV;
2423}
2424
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002426 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002427
Mike Stump18bb9282009-05-16 07:57:57 +00002428 // FIXME: Should we look these up somewhere other than the module. Its a bit
2429 // silly since we only generate these while processing an implementation, so
2430 // exactly one pointer would work if know when we entered/exitted an
2431 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002432
2433 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002434 // Previously, metaclass with internal linkage may have been defined.
2435 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002436 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2437 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002438 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2439 "Forward metaclass reference has incorrect type.");
2440 return GV;
2441 } else {
2442 // Generate as an external reference to keep a consistent
2443 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002444 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002445 llvm::GlobalValue::ExternalLinkage,
2446 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002447 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002448 }
2449}
2450
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002451llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2452 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2453
2454 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2455 true)) {
2456 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2457 "Forward class metadata reference has incorrect type.");
2458 return GV;
2459 } else {
2460 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2461 llvm::GlobalValue::ExternalLinkage,
2462 0,
2463 Name);
2464 }
2465}
2466
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002467/*
2468 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002469 uint32_t size;
2470 const char *weak_ivar_layout;
2471 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002472 };
2473*/
2474llvm::Constant *
2475CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002476 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002477 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002478
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002479 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002480 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002481 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002482 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002483 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002484
2485 // Return null if no extension bits are used.
2486 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002487 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002488
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002489 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002490 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002491 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002492 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002493 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002494}
2495
2496/*
2497 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002498 char *ivar_name;
2499 char *ivar_type;
2500 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002501 };
2502
2503 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002504 int ivar_count;
2505 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002506 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002507*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002508llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002509 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002510 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002511
2512 // When emitting the root class GCC emits ivar entries for the
2513 // actual class structure. It is not clear if we need to follow this
2514 // behavior; for now lets try and get away with not doing it. If so,
2515 // the cleanest solution would be to make up an ObjCInterfaceDecl
2516 // for the class.
2517 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002518 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002519
Jordy Rosea91768e2011-07-22 02:08:32 +00002520 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002521
Jordy Rosea91768e2011-07-22 02:08:32 +00002522 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002523 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002524 // Ignore unnamed bit-fields.
2525 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002526 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002527 llvm::Constant *Ivar[] = {
2528 GetMethodVarName(IVD->getIdentifier()),
2529 GetMethodVarType(IVD),
2530 llvm::ConstantInt::get(ObjCTypes.IntTy,
2531 ComputeIvarBaseOffset(CGM, OID, IVD))
2532 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002533 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002534 }
2535
2536 // Return null for empty list.
2537 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002538 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002539
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002540 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002541 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002542 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002543 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002544 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002545 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002546
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002547 llvm::GlobalVariable *GV;
2548 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002549 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002550 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002551 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002552 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002553 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002554 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002555 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002556 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002557}
2558
2559/*
2560 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002561 SEL method_name;
2562 char *method_types;
2563 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002564 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002565
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002566 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002567 struct objc_method_list *obsolete;
2568 int count;
2569 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002570 };
2571*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002572
2573/// GetMethodConstant - Return a struct objc_method constant for the
2574/// given method if it has been defined. The result is null if the
2575/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002576llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002577 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002578 if (!Fn)
2579 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002580
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002581 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002582 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002583 ObjCTypes.SelectorPtrTy),
2584 GetMethodVarType(MD),
2585 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2586 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002587 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002588}
2589
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002590llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002591 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002592 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002593 // Return null for empty list.
2594 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002595 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002596
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002597 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002598 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002599 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002600 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002601 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002602 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002603 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002604
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002605 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002606 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002607}
2608
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002609llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002610 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002611 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002612 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002613
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002614 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002615 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002616 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002617 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002618 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002619 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002620 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002621 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002622 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002623
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002624 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002625}
2626
Daniel Dunbar30c65362009-03-09 20:09:19 +00002627llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002628CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002629 llvm::Constant *Init,
2630 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002631 unsigned Align,
2632 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002633 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002634 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002635 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002636 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002637 if (Section)
2638 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002639 if (Align)
2640 GV->setAlignment(Align);
2641 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002642 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002643 return GV;
2644}
2645
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002646llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002647 // Abuse this interface function as a place to finalize.
2648 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002649 return NULL;
2650}
2651
Chris Lattnerd4808922009-03-22 21:03:39 +00002652llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002653 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002654}
2655
Chris Lattnerd4808922009-03-22 21:03:39 +00002656llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002657 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002658}
2659
David Chisnall168b80f2010-12-26 22:13:16 +00002660llvm::Constant *CGObjCMac::GetGetStructFunction() {
2661 return ObjCTypes.getCopyStructFn();
2662}
2663llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002664 return ObjCTypes.getCopyStructFn();
2665}
2666
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002667llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2668 return ObjCTypes.getCppAtomicObjectFunction();
2669}
2670
Chris Lattnerd4808922009-03-22 21:03:39 +00002671llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002672 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002673}
2674
John McCallbd309292010-07-06 01:34:17 +00002675void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2676 return EmitTryOrSynchronizedStmt(CGF, S);
2677}
2678
2679void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2680 const ObjCAtSynchronizedStmt &S) {
2681 return EmitTryOrSynchronizedStmt(CGF, S);
2682}
2683
John McCall65bea082010-07-21 06:59:36 +00002684namespace {
John McCallcda666c2010-07-21 07:22:38 +00002685 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002686 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002687 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002688 llvm::Value *CallTryExitVar;
2689 llvm::Value *ExceptionData;
2690 ObjCTypesHelper &ObjCTypes;
2691 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002692 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002693 llvm::Value *CallTryExitVar,
2694 llvm::Value *ExceptionData,
2695 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002696 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002697 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2698
John McCall30317fd2011-07-12 20:27:29 +00002699 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002700 // Check whether we need to call objc_exception_try_exit.
2701 // In optimized code, this branch will always be folded.
2702 llvm::BasicBlock *FinallyCallExit =
2703 CGF.createBasicBlock("finally.call_exit");
2704 llvm::BasicBlock *FinallyNoCallExit =
2705 CGF.createBasicBlock("finally.no_call_exit");
2706 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2707 FinallyCallExit, FinallyNoCallExit);
2708
2709 CGF.EmitBlock(FinallyCallExit);
2710 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2711 ->setDoesNotThrow();
2712
2713 CGF.EmitBlock(FinallyNoCallExit);
2714
2715 if (isa<ObjCAtTryStmt>(S)) {
2716 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002717 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2718 // Save the current cleanup destination in case there's
2719 // control flow inside the finally statement.
2720 llvm::Value *CurCleanupDest =
2721 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2722
John McCall65bea082010-07-21 06:59:36 +00002723 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2724
John McCallcebe0ca2010-08-11 00:16:14 +00002725 if (CGF.HaveInsertPoint()) {
2726 CGF.Builder.CreateStore(CurCleanupDest,
2727 CGF.getNormalCleanupDestSlot());
2728 } else {
2729 // Currently, the end of the cleanup must always exist.
2730 CGF.EnsureInsertPoint();
2731 }
2732 }
John McCall65bea082010-07-21 06:59:36 +00002733 } else {
2734 // Emit objc_sync_exit(expr); as finally's sole statement for
2735 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002736 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002737 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2738 ->setDoesNotThrow();
2739 }
2740 }
2741 };
John McCall42227ed2010-07-31 23:20:56 +00002742
2743 class FragileHazards {
2744 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002745 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002746 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2747
2748 llvm::InlineAsm *ReadHazard;
2749 llvm::InlineAsm *WriteHazard;
2750
2751 llvm::FunctionType *GetAsmFnType();
2752
2753 void collectLocals();
2754 void emitReadHazard(CGBuilderTy &Builder);
2755
2756 public:
2757 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002758
John McCall42227ed2010-07-31 23:20:56 +00002759 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002760 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002761 };
2762}
2763
2764/// Create the fragile-ABI read and write hazards based on the current
2765/// state of the function, which is presumed to be immediately prior
2766/// to a @try block. These hazards are used to maintain correct
2767/// semantics in the face of optimization and the fragile ABI's
2768/// cavalier use of setjmp/longjmp.
2769FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2770 collectLocals();
2771
2772 if (Locals.empty()) return;
2773
2774 // Collect all the blocks in the function.
2775 for (llvm::Function::iterator
2776 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2777 BlocksBeforeTry.insert(&*I);
2778
2779 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2780
2781 // Create a read hazard for the allocas. This inhibits dead-store
2782 // optimizations and forces the values to memory. This hazard is
2783 // inserted before any 'throwing' calls in the protected scope to
2784 // reflect the possibility that the variables might be read from the
2785 // catch block if the call throws.
2786 {
2787 std::string Constraint;
2788 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2789 if (I) Constraint += ',';
2790 Constraint += "*m";
2791 }
2792
2793 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2794 }
2795
2796 // Create a write hazard for the allocas. This inhibits folding
2797 // loads across the hazard. This hazard is inserted at the
2798 // beginning of the catch path to reflect the possibility that the
2799 // variables might have been written within the protected scope.
2800 {
2801 std::string Constraint;
2802 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2803 if (I) Constraint += ',';
2804 Constraint += "=*m";
2805 }
2806
2807 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2808 }
2809}
2810
2811/// Emit a write hazard at the current location.
2812void FragileHazards::emitWriteHazard() {
2813 if (Locals.empty()) return;
2814
Jay Foad5bd375a2011-07-15 08:37:34 +00002815 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002816}
2817
John McCall42227ed2010-07-31 23:20:56 +00002818void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2819 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002820 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002821}
2822
2823/// Emit read hazards in all the protected blocks, i.e. all the blocks
2824/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002825void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002826 if (Locals.empty()) return;
2827
2828 CGBuilderTy Builder(CGF.getLLVMContext());
2829
2830 // Iterate through all blocks, skipping those prior to the try.
2831 for (llvm::Function::iterator
2832 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2833 llvm::BasicBlock &BB = *FI;
2834 if (BlocksBeforeTry.count(&BB)) continue;
2835
2836 // Walk through all the calls in the block.
2837 for (llvm::BasicBlock::iterator
2838 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2839 llvm::Instruction &I = *BI;
2840
2841 // Ignore instructions that aren't non-intrinsic calls.
2842 // These are the only calls that can possibly call longjmp.
2843 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2844 if (isa<llvm::IntrinsicInst>(I))
2845 continue;
2846
2847 // Ignore call sites marked nounwind. This may be questionable,
2848 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2849 llvm::CallSite CS(&I);
2850 if (CS.doesNotThrow()) continue;
2851
John McCall2dd7d442010-08-04 05:59:32 +00002852 // Insert a read hazard before the call. This will ensure that
2853 // any writes to the locals are performed before making the
2854 // call. If the call throws, then this is sufficient to
2855 // guarantee correctness as long as it doesn't also write to any
2856 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002857 Builder.SetInsertPoint(&BB, BI);
2858 emitReadHazard(Builder);
2859 }
2860 }
2861}
2862
2863static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2864 if (V) S.insert(V);
2865}
2866
2867void FragileHazards::collectLocals() {
2868 // Compute a set of allocas to ignore.
2869 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2870 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2871 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002872
2873 // Collect all the allocas currently in the function. This is
2874 // probably way too aggressive.
2875 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2876 for (llvm::BasicBlock::iterator
2877 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2878 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2879 Locals.push_back(&*I);
2880}
2881
2882llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002883 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002884 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2885 tys[i] = Locals[i]->getType();
2886 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002887}
2888
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002889/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002890
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002891 Objective-C setjmp-longjmp (sjlj) Exception Handling
2892 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002893
John McCallbd309292010-07-06 01:34:17 +00002894 A catch buffer is a setjmp buffer plus:
2895 - a pointer to the exception that was caught
2896 - a pointer to the previous exception data buffer
2897 - two pointers of reserved storage
2898 Therefore catch buffers form a stack, with a pointer to the top
2899 of the stack kept in thread-local storage.
2900
2901 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2902 objc_exception_try_exit pops the given catch buffer, which is
2903 required to be the top of the EH stack.
2904 objc_exception_throw pops the top of the EH stack, writes the
2905 thrown exception into the appropriate field, and longjmps
2906 to the setjmp buffer. It crashes the process (with a printf
2907 and an abort()) if there are no catch buffers on the stack.
2908 objc_exception_extract just reads the exception pointer out of the
2909 catch buffer.
2910
2911 There's no reason an implementation couldn't use a light-weight
2912 setjmp here --- something like __builtin_setjmp, but API-compatible
2913 with the heavyweight setjmp. This will be more important if we ever
2914 want to implement correct ObjC/C++ exception interactions for the
2915 fragile ABI.
2916
2917 Note that for this use of setjmp/longjmp to be correct, we may need
2918 to mark some local variables volatile: if a non-volatile local
2919 variable is modified between the setjmp and the longjmp, it has
2920 indeterminate value. For the purposes of LLVM IR, it may be
2921 sufficient to make loads and stores within the @try (to variables
2922 declared outside the @try) volatile. This is necessary for
2923 optimized correctness, but is not currently being done; this is
2924 being tracked as rdar://problem/8160285
2925
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002926 The basic framework for a @try-catch-finally is as follows:
2927 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002928 objc_exception_data d;
2929 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002930 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002932 objc_exception_try_enter(&d);
2933 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002934 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002935 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002936 // exception path
2937 id _caught = objc_exception_extract(&d);
2938
2939 // enter new try scope for handlers
2940 if (!setjmp(d.jmp_buf)) {
2941 ... match exception and execute catch blocks ...
2942
2943 // fell off end, rethrow.
2944 _rethrow = _caught;
2945 ... jump-through-finally to finally_rethrow ...
2946 } else {
2947 // exception in catch block
2948 _rethrow = objc_exception_extract(&d);
2949 _call_try_exit = false;
2950 ... jump-through-finally to finally_rethrow ...
2951 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002952 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002953 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002954
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002955 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002956 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002957 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002958
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002959 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002960 ... dispatch to finally destination ...
2961
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002962 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002963 objc_exception_throw(_rethrow);
2964
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002965 finally_end:
2966 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002967
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002968 This framework differs slightly from the one gcc uses, in that gcc
2969 uses _rethrow to determine if objc_exception_try_exit should be called
2970 and if the object should be rethrown. This breaks in the face of
2971 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002972
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002973 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002974
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002975 - If there are no catch blocks, then we avoid emitting the second
2976 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002977
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002978 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2979 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002980
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002981 - FIXME: If there is no @finally block we can do a few more
2982 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002983
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002984 Rethrows and Jumps-Through-Finally
2985 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002986
John McCallbd309292010-07-06 01:34:17 +00002987 '@throw;' is supported by pushing the currently-caught exception
2988 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002989
John McCallbd309292010-07-06 01:34:17 +00002990 Branches through the @finally block are handled with an ordinary
2991 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2992 exceptions are not compatible with C++ exceptions, and this is
2993 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002994
John McCallbd309292010-07-06 01:34:17 +00002995 @synchronized(expr) { stmt; } is emitted as if it were:
2996 id synch_value = expr;
2997 objc_sync_enter(synch_value);
2998 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002999*/
3000
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003001void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3002 const Stmt &S) {
3003 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003004
3005 // A destination for the fall-through edges of the catch handlers to
3006 // jump to.
3007 CodeGenFunction::JumpDest FinallyEnd =
3008 CGF.getJumpDestInCurrentScope("finally.end");
3009
3010 // A destination for the rethrow edge of the catch handlers to jump
3011 // to.
3012 CodeGenFunction::JumpDest FinallyRethrow =
3013 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003014
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003015 // For @synchronized, call objc_sync_enter(sync.expr). The
3016 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003017 // @synchronized. We can't avoid a temp here because we need the
3018 // value to be preserved. If the backend ever does liveness
3019 // correctly after setjmp, this will be unnecessary.
3020 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003021 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003022 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003023 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3024 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003025 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3026 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003027
3028 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3029 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003030 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003031
John McCall2dd7d442010-08-04 05:59:32 +00003032 // Allocate memory for the setjmp buffer. This needs to be kept
3033 // live throughout the try and catch blocks.
3034 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3035 "exceptiondata.ptr");
3036
John McCall42227ed2010-07-31 23:20:56 +00003037 // Create the fragile hazards. Note that this will not capture any
3038 // of the allocas required for exception processing, but will
3039 // capture the current basic block (which extends all the way to the
3040 // setjmp call) as "before the @try".
3041 FragileHazards Hazards(CGF);
3042
John McCallbd309292010-07-06 01:34:17 +00003043 // Create a flag indicating whether the cleanup needs to call
3044 // objc_exception_try_exit. This is true except when
3045 // - no catches match and we're branching through the cleanup
3046 // just to rethrow the exception, or
3047 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003048 // The setjmp-safety rule here is that we should always store to this
3049 // variable in a place that dominates the branch through the cleanup
3050 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003051 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003052 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003053
John McCall9916e3f2010-10-04 23:42:51 +00003054 // A slot containing the exception to rethrow. Only needed when we
3055 // have both a @catch and a @finally.
3056 llvm::Value *PropagatingExnVar = 0;
3057
John McCallbd309292010-07-06 01:34:17 +00003058 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003059 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003060 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003061 CallTryExitVar,
3062 ExceptionData,
3063 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003064
3065 // Enter a try block:
3066 // - Call objc_exception_try_enter to push ExceptionData on top of
3067 // the EH stack.
3068 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3069 ->setDoesNotThrow();
3070
3071 // - Call setjmp on the exception data buffer.
3072 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3073 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3074 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003075 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003076 llvm::CallInst *SetJmpResult =
3077 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3078 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003079 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003080
3081 // If setjmp returned 0, enter the protected block; otherwise,
3082 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003083 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3084 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003085 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003086 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3087 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003088
John McCallbd309292010-07-06 01:34:17 +00003089 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003090 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003091 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003092 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003093 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003094
3095 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003096
John McCallbd309292010-07-06 01:34:17 +00003097 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003098 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003099
John McCall42227ed2010-07-31 23:20:56 +00003100 // Don't optimize loads of the in-scope locals across this point.
3101 Hazards.emitWriteHazard();
3102
John McCallbd309292010-07-06 01:34:17 +00003103 // For a @synchronized (or a @try with no catches), just branch
3104 // through the cleanup to the rethrow block.
3105 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3106 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003107 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003108 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003109
3110 // Otherwise, we have to match against the caught exceptions.
3111 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003112 // Retrieve the exception object. We may emit multiple blocks but
3113 // nothing can cross this so the value is already in SSA form.
3114 llvm::CallInst *Caught =
3115 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3116 ExceptionData, "caught");
3117 Caught->setDoesNotThrow();
3118
John McCallbd309292010-07-06 01:34:17 +00003119 // Push the exception to rethrow onto the EH value stack for the
3120 // benefit of any @throws in the handlers.
3121 CGF.ObjCEHValueStack.push_back(Caught);
3122
Douglas Gregor96c79492010-04-23 22:50:49 +00003123 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003124
John McCall2dd7d442010-08-04 05:59:32 +00003125 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003126
John McCall2dd7d442010-08-04 05:59:32 +00003127 llvm::BasicBlock *CatchBlock = 0;
3128 llvm::BasicBlock *CatchHandler = 0;
3129 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003130 // Save the currently-propagating exception before
3131 // objc_exception_try_enter clears the exception slot.
3132 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3133 "propagating_exception");
3134 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3135
John McCall2dd7d442010-08-04 05:59:32 +00003136 // Enter a new exception try block (in case a @catch block
3137 // throws an exception).
3138 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3139 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003140
John McCall2dd7d442010-08-04 05:59:32 +00003141 llvm::CallInst *SetJmpResult =
3142 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3143 "setjmp.result");
3144 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003145 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003146
John McCall2dd7d442010-08-04 05:59:32 +00003147 llvm::Value *Threw =
3148 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3149
3150 CatchBlock = CGF.createBasicBlock("catch");
3151 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3152 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3153
3154 CGF.EmitBlock(CatchBlock);
3155 }
3156
3157 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003158
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003159 // Handle catch list. As a special case we check if everything is
3160 // matched and avoid generating code for falling off the end if
3161 // so.
3162 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003163 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3164 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003165
Douglas Gregor46a572b2010-04-26 16:46:50 +00003166 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003167 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003168
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003169 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003170 if (!CatchParam) {
3171 AllMatched = true;
3172 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003173 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003174
John McCallbd309292010-07-06 01:34:17 +00003175 // catch(id e) always matches under this ABI, since only
3176 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003177 // FIXME: For the time being we also match id<X>; this should
3178 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003179 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003180 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003181 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003182
John McCallbd309292010-07-06 01:34:17 +00003183 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003184 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003185 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3186
Anders Carlsson9396a892008-09-11 09:15:33 +00003187 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003188 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003189 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003190
3191 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003192 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003193 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003194
Anders Carlsson9396a892008-09-11 09:15:33 +00003195 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003196
3197 // The scope of the catch variable ends right here.
3198 CatchVarCleanups.ForceCleanup();
3199
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003200 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003201 break;
3202 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003203
Steve Naroff7cae42b2009-07-10 23:34:53 +00003204 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003205 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003206
3207 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003208 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3209 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003210
3211 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003212 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003213
John McCallbd309292010-07-06 01:34:17 +00003214 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003215 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3216 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003217 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003218
John McCallbd309292010-07-06 01:34:17 +00003219 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3220 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003221
3222 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003223 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003224
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003225 // Emit the @catch block.
3226 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003227
3228 // Collect any cleanups for the catch variable. The scope lasts until
3229 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003230 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003231
John McCall1c9c3fd2010-10-15 04:57:14 +00003232 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003233 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003234
John McCallbd309292010-07-06 01:34:17 +00003235 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003236 llvm::Value *Tmp =
3237 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003238 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003239 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003240
Anders Carlsson9396a892008-09-11 09:15:33 +00003241 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003242
3243 // We're done with the catch variable.
3244 CatchVarCleanups.ForceCleanup();
3245
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003246 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003247
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003248 CGF.EmitBlock(NextCatchBlock);
3249 }
3250
John McCallbd309292010-07-06 01:34:17 +00003251 CGF.ObjCEHValueStack.pop_back();
3252
John McCall2dd7d442010-08-04 05:59:32 +00003253 // If nothing wanted anything to do with the caught exception,
3254 // kill the extract call.
3255 if (Caught->use_empty())
3256 Caught->eraseFromParent();
3257
3258 if (!AllMatched)
3259 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3260
3261 if (HasFinally) {
3262 // Emit the exception handler for the @catch blocks.
3263 CGF.EmitBlock(CatchHandler);
3264
3265 // In theory we might now need a write hazard, but actually it's
3266 // unnecessary because there's no local-accessing code between
3267 // the try's write hazard and here.
3268 //Hazards.emitWriteHazard();
3269
John McCall9916e3f2010-10-04 23:42:51 +00003270 // Extract the new exception and save it to the
3271 // propagating-exception slot.
3272 assert(PropagatingExnVar);
3273 llvm::CallInst *NewCaught =
3274 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3275 ExceptionData, "caught");
3276 NewCaught->setDoesNotThrow();
3277 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3278
John McCall2dd7d442010-08-04 05:59:32 +00003279 // Don't pop the catch handler; the throw already did.
3280 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003281 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003282 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003283 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003284
John McCall42227ed2010-07-31 23:20:56 +00003285 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003286 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003287
John McCallbd309292010-07-06 01:34:17 +00003288 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003289 CGF.Builder.restoreIP(TryFallthroughIP);
3290 if (CGF.HaveInsertPoint())
3291 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003292 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003293 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003294
John McCallbd309292010-07-06 01:34:17 +00003295 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003296 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003297 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003298 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003299 // If we have a propagating-exception variable, check it.
3300 llvm::Value *PropagatingExn;
3301 if (PropagatingExnVar) {
3302 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003303
John McCall9916e3f2010-10-04 23:42:51 +00003304 // Otherwise, just look in the buffer for the exception to throw.
3305 } else {
3306 llvm::CallInst *Caught =
3307 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3308 ExceptionData);
3309 Caught->setDoesNotThrow();
3310 PropagatingExn = Caught;
3311 }
3312
3313 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003314 ->setDoesNotThrow();
3315 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003316 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003317
John McCall42227ed2010-07-31 23:20:56 +00003318 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003319}
3320
3321void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003322 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003323 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003324
Anders Carlssone005aa12008-09-09 16:16:55 +00003325 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003326 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003327 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003328 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003329 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003330 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003331 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003332 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003333 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003334
John McCallbd309292010-07-06 01:34:17 +00003335 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3336 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003337 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003338
3339 // Clear the insertion point to indicate we are in unreachable code.
3340 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003341}
3342
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003343/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003344/// object: objc_read_weak (id *src)
3345///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003346llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003347 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003348 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003349 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3350 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3351 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003352 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003353 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003354 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003355 return read_weak;
3356}
3357
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003358/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3359/// objc_assign_weak (id src, id *dst)
3360///
3361void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003362 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003363 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003364 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003365 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003366 assert(Size <= 8 && "does not support size > 8");
3367 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003368 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003369 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3370 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003371 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3372 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003373 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003374 src, dst, "weakassign");
3375 return;
3376}
3377
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003378/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3379/// objc_assign_global (id src, id *dst)
3380///
3381void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003382 llvm::Value *src, llvm::Value *dst,
3383 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003384 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003385 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003386 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003387 assert(Size <= 8 && "does not support size > 8");
3388 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003390 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3391 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003392 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3393 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003394 if (!threadlocal)
3395 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3396 src, dst, "globalassign");
3397 else
3398 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3399 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003400 return;
3401}
3402
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003403/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003404/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003405///
3406void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003407 llvm::Value *src, llvm::Value *dst,
3408 llvm::Value *ivarOffset) {
3409 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003410 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003411 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003412 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003413 assert(Size <= 8 && "does not support size > 8");
3414 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003415 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003416 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3417 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003418 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3419 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003420 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3421 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003422 return;
3423}
3424
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003425/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3426/// objc_assign_strongCast (id src, id *dst)
3427///
3428void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003429 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003430 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003431 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003432 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003433 assert(Size <= 8 && "does not support size > 8");
3434 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003435 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003436 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3437 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003438 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3439 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003440 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003441 src, dst, "weakassign");
3442 return;
3443}
3444
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003445void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003446 llvm::Value *DestPtr,
3447 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003448 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003449 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3450 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003451 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003452 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003453 return;
3454}
3455
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003456/// EmitObjCValueForIvar - Code Gen for ivar reference.
3457///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003458LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3459 QualType ObjectTy,
3460 llvm::Value *BaseValue,
3461 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003462 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003463 const ObjCInterfaceDecl *ID =
3464 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003465 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3466 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003467}
3468
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003469llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003470 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003471 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003472 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003473 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003474 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3475 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003476}
3477
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003478/* *** Private Interface *** */
3479
3480/// EmitImageInfo - Emit the image info marker used to encode some module
3481/// level information.
3482///
3483/// See: <rdr://4810609&4810587&4810587>
3484/// struct IMAGE_INFO {
3485/// unsigned version;
3486/// unsigned flags;
3487/// };
3488enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003489 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003490 eImageInfo_GarbageCollected = (1 << 1),
3491 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003492 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3493
Daniel Dunbar5e639272010-04-25 20:39:01 +00003494 // A flag indicating that the module has no instances of a @synthesize of a
3495 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003496 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003497};
3498
Daniel Dunbar5e639272010-04-25 20:39:01 +00003499void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003500 unsigned version = 0; // Version is unused?
3501 unsigned flags = 0;
3502
3503 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003504 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003505 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003506 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003507 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003508
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003509 // We never allow @synthesize of a superclass property.
3510 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003511
Chris Lattner2192fe52011-07-18 04:24:23 +00003512 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner5e016ae2010-06-27 07:15:29 +00003513
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003514 // Emitted as int[2];
3515 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003516 llvm::ConstantInt::get(Int32Ty, version),
3517 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003518 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003519 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003520
3521 const char *Section;
3522 if (ObjCABI == 1)
3523 Section = "__OBJC, __image_info,regular";
3524 else
3525 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003526 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003527 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003528 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003529 Section,
3530 0,
3531 true);
3532 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003533}
3534
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003535
3536// struct objc_module {
3537// unsigned long version;
3538// unsigned long size;
3539// const char *name;
3540// Symtab symtab;
3541// };
3542
3543// FIXME: Get from somewhere
3544static const int ModuleVersion = 7;
3545
3546void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003547 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003548
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003549 llvm::Constant *Values[] = {
3550 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3551 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3552 // This used to be the filename, now it is unused. <rdr://4327263>
3553 GetClassName(&CGM.getContext().Idents.get("")),
3554 EmitModuleSymbols()
3555 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003556 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003557 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003558 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003559 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003560}
3561
3562llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003563 unsigned NumClasses = DefinedClasses.size();
3564 unsigned NumCategories = DefinedCategories.size();
3565
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003566 // Return null if no symbols were defined.
3567 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003568 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003569
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003570 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003571 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003572 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003573 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3574 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003575
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003576 // The runtime expects exactly the list of defined classes followed
3577 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003578 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003579 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003580 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003581 ObjCTypes.Int8PtrTy);
3582 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003583 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003584 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003585 ObjCTypes.Int8PtrTy);
3586
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003587 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003588 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003589 NumClasses + NumCategories),
3590 Symbols);
3591
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003592 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003593
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003594 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003595 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3596 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003597 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003598 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003599}
3600
John McCall31168b02011-06-15 23:02:42 +00003601llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3602 IdentifierInfo *II) {
3603 LazySymbols.insert(II);
3604
3605 llvm::GlobalVariable *&Entry = ClassReferences[II];
3606
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003607 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003608 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003609 llvm::ConstantExpr::getBitCast(GetClassName(II),
3610 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003611 Entry =
John McCall31168b02011-06-15 23:02:42 +00003612 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3613 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3614 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003615 }
John McCall31168b02011-06-15 23:02:42 +00003616
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003617 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003618}
3619
John McCall31168b02011-06-15 23:02:42 +00003620llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3621 const ObjCInterfaceDecl *ID) {
3622 return EmitClassRefFromId(Builder, ID->getIdentifier());
3623}
3624
3625llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3626 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3627 return EmitClassRefFromId(Builder, II);
3628}
3629
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003630llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3631 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003632 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003633
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003634 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003635 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003636 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003637 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003638 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003639 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3640 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003641 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003642 }
3643
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003644 if (lvalue)
3645 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003646 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003647}
3648
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003649llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003650 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003651
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003652 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003653 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003654 llvm::ConstantArray::get(VMContext,
3655 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003656 ((ObjCABI == 2) ?
3657 "__TEXT,__objc_classname,cstring_literals" :
3658 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003659 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003660
Owen Anderson170229f2009-07-14 23:10:40 +00003661 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003662}
3663
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003664llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3665 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3666 I = MethodDefinitions.find(MD);
3667 if (I != MethodDefinitions.end())
3668 return I->second;
3669
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003670 return NULL;
3671}
3672
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003673/// GetIvarLayoutName - Returns a unique constant for the given
3674/// ivar layout bitmap.
3675llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003676 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003677 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003678}
3679
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003680void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003681 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003682 bool ForStrongLayout,
3683 bool &HasUnion) {
3684 const RecordDecl *RD = RT->getDecl();
3685 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003686 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003687 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003688 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003689 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003690
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003691 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3692 ForStrongLayout, HasUnion);
3693}
3694
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003695void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003696 const llvm::StructLayout *Layout,
3697 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003698 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003699 unsigned int BytePos, bool ForStrongLayout,
3700 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003701 bool IsUnion = (RD && RD->isUnion());
3702 uint64_t MaxUnionIvarSize = 0;
3703 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003704 const FieldDecl *MaxField = 0;
3705 const FieldDecl *MaxSkippedField = 0;
3706 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003707 uint64_t MaxFieldOffset = 0;
3708 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003709 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003710 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003711
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003712 if (RecFields.empty())
3713 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003714 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3715 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003716 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003717 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003718 FirstFieldDelta =
3719 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3720 }
3721
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003722 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003723 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003724 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003725 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003726 // Note that 'i' here is actually the field index inside RD of Field,
3727 // although this dependency is hidden.
3728 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003729 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003730 } else
John McCall31168b02011-06-15 23:02:42 +00003731 FieldOffset =
3732 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003733
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003734 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003735 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003736 LastFieldBitfieldOrUnnamed = Field;
3737 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003738 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003739 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003740
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003741 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003742 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003743 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003744 if (FQT->isUnionType())
3745 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003746
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003747 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003748 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003749 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003750 continue;
3751 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003753 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003754 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003755 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003756 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003757 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003758 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003759 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3760 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003761 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003762 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003763 FQT = CArray->getElementType();
3764 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003765
3766 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003767 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003768 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003769 int OldIndex = IvarsInfo.size() - 1;
3770 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003771
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003772 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003773 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003774 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003775
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003776 // Replicate layout information for each array element. Note that
3777 // one element is already done.
3778 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003779 for (int FirstIndex = IvarsInfo.size() - 1,
3780 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003781 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003782 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3783 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3784 IvarsInfo[i].ivar_size));
3785 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3786 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3787 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003788 }
3789 continue;
3790 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003791 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003792 // At this point, we are done with Record/Union and array there of.
3793 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003794 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003795
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003796 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003797 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3798 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003799 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003800 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003801 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003802 MaxUnionIvarSize = UnionIvarSize;
3803 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003804 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003805 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003806 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003807 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003808 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003809 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003810 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003811 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3812 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003813 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003814 // FIXME: Why the asymmetry? We divide by word size in bits on other
3815 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003816 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003817 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003818 MaxSkippedUnionIvarSize = UnionIvarSize;
3819 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003820 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003821 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003822 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003823 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003824 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003825 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003826 }
3827 }
3828 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003829
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003830 if (LastFieldBitfieldOrUnnamed) {
3831 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3832 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003833 uint64_t BitFieldSize
3834 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003835 GC_IVAR skivar;
3836 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3837 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3838 + ((BitFieldSize % ByteSizeInBits) != 0);
3839 SkipIvars.push_back(skivar);
3840 } else {
3841 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3842 // Last field was unnamed. Must update skip info.
3843 unsigned FieldSize
3844 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3845 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3846 FieldSize / ByteSizeInBits));
3847 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003848 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003849
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003850 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003851 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003852 MaxUnionIvarSize));
3853 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003854 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003855 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003856}
3857
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003858/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3859/// the computations and returning the layout bitmap (for ivar or blocks) in
3860/// the given argument BitMap string container. Routine reads
3861/// two containers, IvarsInfo and SkipIvars which are assumed to be
3862/// filled already by the caller.
3863llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003864 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003865 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003866
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003867 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003868 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003869 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003870 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003871 if (IvarsInfo[0].ivar_bytepos == 0) {
3872 WordsToSkip = 0;
3873 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003874 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003875 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3876 WordsToScan = IvarsInfo[0].ivar_size;
3877 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003878 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003879 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003880 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003881 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003882 // consecutive 'scanned' object pointers.
3883 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003884 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003885 // Skip over 'gc'able object pointer which lay over each other.
3886 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3887 continue;
3888 // Must skip over 1 or more words. We save current skip/scan values
3889 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003890 SKIP_SCAN SkScan;
3891 SkScan.skip = WordsToSkip;
3892 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003893 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003894
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003895 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003896 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3897 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003898 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003899 WordsToSkip = 0;
3900 WordsToScan = IvarsInfo[i].ivar_size;
3901 }
3902 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003903 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003904 SKIP_SCAN SkScan;
3905 SkScan.skip = WordsToSkip;
3906 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003907 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003908 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003909
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003910 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003911 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003912 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003913 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003914 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003915 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003916 IvarsInfo[LastIndex].ivar_bytepos +
3917 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003918 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003919 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003920 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003921 SKIP_SCAN SkScan;
3922 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3923 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003924 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003925 }
3926 }
3927 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3928 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003929 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003930 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003931 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3932 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3933 // 0xM0 followed by 0x0N detected.
3934 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3935 for (int j = i+1; j < SkipScan; j++)
3936 SkipScanIvars[j] = SkipScanIvars[j+1];
3937 --SkipScan;
3938 }
3939 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003940
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003941 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003942 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003943 unsigned char byte;
3944 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3945 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3946 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3947 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003948
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003949 // first skip big.
3950 for (unsigned int ix = 0; ix < skip_big; ix++)
3951 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003952
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003953 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003954 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003955 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003956 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003957 byte |= 0xf;
3958 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003959 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003960 byte |= scan_small;
3961 scan_small = 0;
3962 }
3963 BitMap += byte;
3964 }
3965 // next scan big
3966 for (unsigned int ix = 0; ix < scan_big; ix++)
3967 BitMap += (unsigned char)(0x0f);
3968 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003969 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003970 byte = scan_small;
3971 BitMap += byte;
3972 }
3973 }
3974 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003975 unsigned char zero = 0;
3976 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003977
3978 llvm::GlobalVariable * Entry =
3979 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3980 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003981 ((ObjCABI == 2) ?
3982 "__TEXT,__objc_classname,cstring_literals" :
3983 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003984 1, true);
3985 return getConstantGEP(VMContext, Entry, 0, 0);
3986}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003987
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003988/// BuildIvarLayout - Builds ivar layout bitmap for the class
3989/// implementation for the __strong or __weak case.
3990/// The layout map displays which words in ivar list must be skipped
3991/// and which must be scanned by GC (see below). String is built of bytes.
3992/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3993/// of words to skip and right nibble is count of words to scan. So, each
3994/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3995/// represented by a 0x00 byte which also ends the string.
3996/// 1. when ForStrongLayout is true, following ivars are scanned:
3997/// - id, Class
3998/// - object *
3999/// - __strong anything
4000///
4001/// 2. When ForStrongLayout is false, following ivars are scanned:
4002/// - __weak anything
4003///
4004llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4005 const ObjCImplementationDecl *OMD,
4006 bool ForStrongLayout) {
4007 bool hasUnion = false;
4008
Chris Lattner2192fe52011-07-18 04:24:23 +00004009 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00004010 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00004011 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004012 return llvm::Constant::getNullValue(PtrTy);
4013
Jordy Rosea91768e2011-07-22 02:08:32 +00004014 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4015 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004016 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004017 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004018 IVD; IVD = IVD->getNextIvar())
4019 RecFields.push_back(cast<FieldDecl>(IVD));
4020 }
4021 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004022 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004023 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004024
Jordy Rosea91768e2011-07-22 02:08:32 +00004025 // FIXME: This is not ideal; we shouldn't have to do this copy.
4026 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004027 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004028
4029 if (RecFields.empty())
4030 return llvm::Constant::getNullValue(PtrTy);
4031
4032 SkipIvars.clear();
4033 IvarsInfo.clear();
4034
4035 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4036 if (IvarsInfo.empty())
4037 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004038 // Sort on byte position in case we encounterred a union nested in
4039 // the ivar list.
4040 if (hasUnion && !IvarsInfo.empty())
4041 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4042 if (hasUnion && !SkipIvars.empty())
4043 std::sort(SkipIvars.begin(), SkipIvars.end());
4044
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004045 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004046 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004047
4048 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004049 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004050 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004051 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004052 const unsigned char *s = (unsigned char*)BitMap.c_str();
4053 for (unsigned i = 0; i < BitMap.size(); i++)
4054 if (!(s[i] & 0xf0))
4055 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4056 else
4057 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4058 printf("\n");
4059 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004060 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004061}
4062
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004063llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004064 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4065
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004066 // FIXME: Avoid std::string copying.
4067 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004068 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00004069 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004070 ((ObjCABI == 2) ?
4071 "__TEXT,__objc_methname,cstring_literals" :
4072 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004073 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004074
Owen Anderson170229f2009-07-14 23:10:40 +00004075 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004076}
4077
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004078// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004079llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004080 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4081}
4082
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004083llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004084 std::string TypeStr;
4085 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4086
4087 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004088
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004089 if (!Entry)
4090 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004091 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004092 ((ObjCABI == 2) ?
4093 "__TEXT,__objc_methtype,cstring_literals" :
4094 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004095 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004096
Owen Anderson170229f2009-07-14 23:10:40 +00004097 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004098}
4099
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004100llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4101 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004102 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00004103 if (CGM.getContext().getObjCEncodingForMethodDecl(
4104 const_cast<ObjCMethodDecl*>(D),
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004105 TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004106 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004107
4108 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4109
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004110 if (!Entry)
4111 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00004112 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004113 ((ObjCABI == 2) ?
4114 "__TEXT,__objc_methtype,cstring_literals" :
4115 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004116 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004117
Owen Anderson170229f2009-07-14 23:10:40 +00004118 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004119}
4120
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004121// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004122llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004123 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004124
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004125 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004126 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004127 llvm::ConstantArray::get(VMContext,
4128 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004129 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004130 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004131
Owen Anderson170229f2009-07-14 23:10:40 +00004132 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004133}
4134
4135// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004136// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004137llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4139 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004140 std::string TypeStr;
4141 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004142 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4143}
4144
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004145void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004146 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004147 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004148 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004149 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004150 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4151 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004152 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004153 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004154 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004155 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004156}
4157
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004158void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004159 EmitModuleInfo();
4160
Daniel Dunbarc475d422008-10-29 22:36:39 +00004161 // Emit the dummy bodies for any protocols which were referenced but
4162 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004163 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004164 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4165 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004166 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004167
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004168 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004169 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004170 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004171 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004172 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004173 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004174 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004175 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004176 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004177 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004178 }
4179
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004180 // Add assembler directives to add lazy undefined symbol references
4181 // for classes which are referenced but not defined. This is
4182 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004183 //
4184 // FIXME: It would be nice if we had an LLVM construct for this.
4185 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4186 llvm::SmallString<256> Asm;
4187 Asm += CGM.getModule().getModuleInlineAsm();
4188 if (!Asm.empty() && Asm.back() != '\n')
4189 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004190
Daniel Dunbard027a922009-09-07 00:20:42 +00004191 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004192 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4193 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004194 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4195 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004196 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004197 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004198 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004199 }
4200
4201 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4202 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4203 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4204 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004205
Daniel Dunbard027a922009-09-07 00:20:42 +00004206 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004207 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004208}
4209
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004210CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004211 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004212 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004213 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004214 ObjCABI = 2;
4215}
4216
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004217/* *** */
4218
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004219ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004220 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004221 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4222 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004223
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004224 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004225 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004226 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004227 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004228 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004229 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004230
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004231 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004232 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004233 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234
Mike Stump18bb9282009-05-16 07:57:57 +00004235 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4236 // comes out a bit cleaner.
Chris Lattner2192fe52011-07-18 04:24:23 +00004237 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004238 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004239
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004240 // I'm not sure I like this. The implicit coordination is a bit
4241 // gross. We should solve this in a reasonable fashion because this
4242 // is a pretty common task (match some runtime data structure with
4243 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004244
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004245 // FIXME: This is leaked.
4246 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004247
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004248 // struct _objc_super {
4249 // id self;
4250 // Class cls;
4251 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004252 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004253 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004254 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004255 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004256 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004257 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004258 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004259 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004260 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004261
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004262 SuperCTy = Ctx.getTagDeclType(RD);
4263 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004264
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004265 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004266 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4267
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004268 // struct _prop_t {
4269 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004270 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004271 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004272 PropertyTy = llvm::StructType::create("struct._prop_t",
4273 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004274
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004275 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004276 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004277 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004278 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004279 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004280 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004281 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4282 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004283 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004284 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004285
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004286 // struct _objc_method {
4287 // SEL _cmd;
4288 // char *method_type;
4289 // char *_imp;
4290 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004291 MethodTy = llvm::StructType::create("struct._objc_method",
4292 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4293 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004294
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004295 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004296 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004297 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004298
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004299}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004300
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004301ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004302 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004303 // struct _objc_method_description {
4304 // SEL name;
4305 // char *types;
4306 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004307 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004308 llvm::StructType::create("struct._objc_method_description",
4309 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004310
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004311 // struct _objc_method_description_list {
4312 // int count;
4313 // struct _objc_method_description[1];
4314 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004315 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004316 llvm::StructType::create("struct._objc_method_description_list",
4317 IntTy,
4318 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004319
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004320 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004321 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004322 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004323
Daniel Dunbarb036db82008-08-13 03:21:16 +00004324 // Protocol description structures
4325
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004326 // struct _objc_protocol_extension {
4327 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4328 // struct _objc_method_description_list *optional_instance_methods;
4329 // struct _objc_method_description_list *optional_class_methods;
4330 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004331 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004332 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004333 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004334 llvm::StructType::create("struct._objc_protocol_extension",
4335 IntTy, MethodDescriptionListPtrTy,
4336 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004337 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004338
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004339 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004340 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004341
Daniel Dunbarc475d422008-10-29 22:36:39 +00004342 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004343
Chris Lattnera5f58b02011-07-09 17:41:47 +00004344 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004345 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004346
Chris Lattnera5f58b02011-07-09 17:41:47 +00004347 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004348 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004349 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004350 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004351 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004352 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004353
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004354 // struct _objc_protocol {
4355 // struct _objc_protocol_extension *isa;
4356 // char *protocol_name;
4357 // struct _objc_protocol **_objc_protocol_list;
4358 // struct _objc_method_description_list *instance_methods;
4359 // struct _objc_method_description_list *class_methods;
4360 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004361 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4362 llvm::PointerType::getUnqual(ProtocolListTy),
4363 MethodDescriptionListPtrTy,
4364 MethodDescriptionListPtrTy,
4365 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004366
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004367 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004368 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004369
Owen Anderson9793f0e2009-07-29 22:16:19 +00004370 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004371
4372 // Class description structures
4373
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004374 // struct _objc_ivar {
4375 // char *ivar_name;
4376 // char *ivar_type;
4377 // int ivar_offset;
4378 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004379 IvarTy = llvm::StructType::create("struct._objc_ivar",
4380 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004381
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004382 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004383 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004384 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004385 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004386
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004387 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004388 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004389 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004390 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004391
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004392 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004393 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004394 llvm::StructType::create("struct._objc_class_extension",
4395 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004396 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004397
Chris Lattner5ec04a52011-08-12 17:43:31 +00004398 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004399
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004400 // struct _objc_class {
4401 // Class isa;
4402 // Class super_class;
4403 // char *name;
4404 // long version;
4405 // long info;
4406 // long instance_size;
4407 // struct _objc_ivar_list *ivars;
4408 // struct _objc_method_list *methods;
4409 // struct _objc_cache *cache;
4410 // struct _objc_protocol_list *protocols;
4411 // char *ivar_layout;
4412 // struct _objc_class_ext *ext;
4413 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004414 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4415 llvm::PointerType::getUnqual(ClassTy),
4416 Int8PtrTy,
4417 LongTy,
4418 LongTy,
4419 LongTy,
4420 IvarListPtrTy,
4421 MethodListPtrTy,
4422 CachePtrTy,
4423 ProtocolListPtrTy,
4424 Int8PtrTy,
4425 ClassExtensionPtrTy,
4426 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004427
Owen Anderson9793f0e2009-07-29 22:16:19 +00004428 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004429
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004430 // struct _objc_category {
4431 // char *category_name;
4432 // char *class_name;
4433 // struct _objc_method_list *instance_method;
4434 // struct _objc_method_list *class_method;
4435 // uint32_t size; // sizeof(struct _objc_category)
4436 // struct _objc_property_list *instance_properties;// category's @property
4437 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004438 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004439 llvm::StructType::create("struct._objc_category",
4440 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4441 MethodListPtrTy, ProtocolListPtrTy,
4442 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004443
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004444 // Global metadata structures
4445
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004446 // struct _objc_symtab {
4447 // long sel_ref_cnt;
4448 // SEL *refs;
4449 // short cls_def_cnt;
4450 // short cat_def_cnt;
4451 // char *defs[cls_def_cnt + cat_def_cnt];
4452 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004453 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004454 llvm::StructType::create("struct._objc_symtab",
4455 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4456 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004457 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004458
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004459 // struct _objc_module {
4460 // long version;
4461 // long size; // sizeof(struct _objc_module)
4462 // char *name;
4463 // struct _objc_symtab* symtab;
4464 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004465 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004466 llvm::StructType::create("struct._objc_module",
4467 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004468
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004469
Mike Stump18bb9282009-05-16 07:57:57 +00004470 // FIXME: This is the size of the setjmp buffer and should be target
4471 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004472 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004473
Anders Carlsson9ff22482008-09-09 10:10:21 +00004474 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004475 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004476 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004477
4478 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004479 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004480 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4481 SetJmpBufferSize),
4482 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004483
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004484}
4485
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004486ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004487 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004488 // struct _method_list_t {
4489 // uint32_t entsize; // sizeof(struct _objc_method)
4490 // uint32_t method_count;
4491 // struct _objc_method method_list[method_count];
4492 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004493 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004494 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4495 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004496 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004497 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004498
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004499 // struct _protocol_t {
4500 // id isa; // NULL
4501 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004502 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004503 // const struct method_list_t * const instance_methods;
4504 // const struct method_list_t * const class_methods;
4505 // const struct method_list_t *optionalInstanceMethods;
4506 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004507 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004508 // const uint32_t size; // sizeof(struct _protocol_t)
4509 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004510 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004511 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004512
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004513 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004514 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004515 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004516
Chris Lattnera5f58b02011-07-09 17:41:47 +00004517 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004518 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4519 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4520 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4521 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004522 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4523 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004524
4525 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004526 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004527
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004528 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004529 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004530 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004531 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004532 ProtocolListnfABITy->setBody(LongTy,
4533 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4534 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004535
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004536 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004537 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004538
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004539 // struct _ivar_t {
4540 // unsigned long int *offset; // pointer to ivar offset location
4541 // char *name;
4542 // char *type;
4543 // uint32_t alignment;
4544 // uint32_t size;
4545 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004546 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004547 llvm::StructType::create("struct._ivar_t",
4548 llvm::PointerType::getUnqual(LongTy),
4549 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004550
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004551 // struct _ivar_list_t {
4552 // uint32 entsize; // sizeof(struct _ivar_t)
4553 // uint32 count;
4554 // struct _iver_t list[count];
4555 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004556 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004557 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4558 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004559
Owen Anderson9793f0e2009-07-29 22:16:19 +00004560 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004561
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004562 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004563 // uint32_t const flags;
4564 // uint32_t const instanceStart;
4565 // uint32_t const instanceSize;
4566 // uint32_t const reserved; // only when building for 64bit targets
4567 // const uint8_t * const ivarLayout;
4568 // const char *const name;
4569 // const struct _method_list_t * const baseMethods;
4570 // const struct _objc_protocol_list *const baseProtocols;
4571 // const struct _ivar_list_t *const ivars;
4572 // const uint8_t * const weakIvarLayout;
4573 // const struct _prop_list_t * const properties;
4574 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004575
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004576 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004577 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4578 IntTy, IntTy, IntTy, Int8PtrTy,
4579 Int8PtrTy, MethodListnfABIPtrTy,
4580 ProtocolListnfABIPtrTy,
4581 IvarListnfABIPtrTy,
4582 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004584 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004585 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004586 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4587 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004588
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004589 // struct _class_t {
4590 // struct _class_t *isa;
4591 // struct _class_t * const superclass;
4592 // void *cache;
4593 // IMP *vtable;
4594 // struct class_ro_t *ro;
4595 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004596
Chris Lattner5ec04a52011-08-12 17:43:31 +00004597 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004598 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4599 llvm::PointerType::getUnqual(ClassnfABITy),
4600 CachePtrTy,
4601 llvm::PointerType::getUnqual(ImpnfABITy),
4602 llvm::PointerType::getUnqual(ClassRonfABITy),
4603 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004604
Fariborz Jahanian71394042009-01-23 23:53:38 +00004605 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004606 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004607
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004608 // struct _category_t {
4609 // const char * const name;
4610 // struct _class_t *const cls;
4611 // const struct _method_list_t * const instance_methods;
4612 // const struct _method_list_t * const class_methods;
4613 // const struct _protocol_list_t * const protocols;
4614 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004615 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004616 CategorynfABITy = llvm::StructType::create("struct._category_t",
4617 Int8PtrTy, ClassnfABIPtrTy,
4618 MethodListnfABIPtrTy,
4619 MethodListnfABIPtrTy,
4620 ProtocolListnfABIPtrTy,
4621 PropertyListPtrTy,
4622 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004623
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004624 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004625 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4626 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004627
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004628 // MessageRefTy - LLVM for:
4629 // struct _message_ref_t {
4630 // IMP messenger;
4631 // SEL name;
4632 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004633
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004634 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004635 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004636 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004637 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004638 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004639 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004640 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004641 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004642 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004643 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004644
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004645 MessageRefCTy = Ctx.getTagDeclType(RD);
4646 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4647 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004648
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004649 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004650 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004651
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004652 // SuperMessageRefTy - LLVM for:
4653 // struct _super_message_ref_t {
4654 // SUPER_IMP messenger;
4655 // SEL name;
4656 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004657 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004658 llvm::StructType::create("struct._super_message_ref_t",
4659 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004660
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004661 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004662 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004663
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004664
4665 // struct objc_typeinfo {
4666 // const void** vtable; // objc_ehtype_vtable + 2
4667 // const char* name; // c++ typeinfo string
4668 // Class cls;
4669 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004670 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004671 llvm::StructType::create("struct._objc_typeinfo",
4672 llvm::PointerType::getUnqual(Int8PtrTy),
4673 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004674 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004675}
4676
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004677llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004678 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004679
Fariborz Jahanian71394042009-01-23 23:53:38 +00004680 return NULL;
4681}
4682
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004683void CGObjCNonFragileABIMac::AddModuleClassList(const
4684 std::vector<llvm::GlobalValue*>
4685 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004686 const char *SymbolName,
4687 const char *SectionName) {
4688 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004689
Daniel Dunbar19573e72009-05-15 21:48:48 +00004690 if (!NumClasses)
4691 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004692
Daniel Dunbar19573e72009-05-15 21:48:48 +00004693 std::vector<llvm::Constant*> Symbols(NumClasses);
4694 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004695 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004696 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004697 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004698 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004699 NumClasses),
4700 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004701
Daniel Dunbar19573e72009-05-15 21:48:48 +00004702 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004703 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004704 llvm::GlobalValue::InternalLinkage,
4705 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004706 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004707 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004708 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004709 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004710}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004711
Fariborz Jahanian71394042009-01-23 23:53:38 +00004712void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4713 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004714
Daniel Dunbar19573e72009-05-15 21:48:48 +00004715 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004716 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004717 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004718 "\01L_OBJC_LABEL_CLASS_$",
4719 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004720
Fariborz Jahanian67260552009-11-17 21:37:35 +00004721 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4722 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4723 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4724 continue;
4725 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004726 }
4727
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004728 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4729 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4730 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4731 continue;
4732 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4733 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004734
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004735 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004736 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4737 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004738
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004739 // Build list of all implemented category addresses in array
4740 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004741 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004742 "\01L_OBJC_LABEL_CATEGORY_$",
4743 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004745 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4746 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004747
Daniel Dunbar5e639272010-04-25 20:39:01 +00004748 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004749}
4750
John McCall9e8bb002011-05-14 03:10:52 +00004751/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4752/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004753/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004754/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004755bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4756 // At various points we've experimented with using vtable-based
4757 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004758 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004759 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004760 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004761 case CodeGenOptions::NonLegacy:
4762 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004763 case CodeGenOptions::Mixed:
4764 break;
4765 }
4766
4767 // If so, see whether this selector is in the white-list of things which must
4768 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004769 if (VTableDispatchMethods.empty()) {
4770 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4771 VTableDispatchMethods.insert(GetNullarySelector("class"));
4772 VTableDispatchMethods.insert(GetNullarySelector("self"));
4773 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4774 VTableDispatchMethods.insert(GetNullarySelector("length"));
4775 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004776
John McCall9e8bb002011-05-14 03:10:52 +00004777 // These are vtable-based if GC is disabled.
4778 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004779 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004780 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4781 VTableDispatchMethods.insert(GetNullarySelector("release"));
4782 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4783 }
4784
4785 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4786 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4787 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4788 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4789 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4790 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4791 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4792
4793 // These are vtable-based if GC is enabled.
4794 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004795 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004796 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4797 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4798
4799 // "countByEnumeratingWithState:objects:count"
4800 IdentifierInfo *KeyIdents[] = {
4801 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4802 &CGM.getContext().Idents.get("objects"),
4803 &CGM.getContext().Idents.get("count")
4804 };
4805 VTableDispatchMethods.insert(
4806 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4807 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004808 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004809
John McCall9e8bb002011-05-14 03:10:52 +00004810 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004811}
4812
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004813// Metadata flags
4814enum MetaDataDlags {
4815 CLS = 0x0,
4816 CLS_META = 0x1,
4817 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004818 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004819 CLS_EXCEPTION = 0x20,
4820
4821 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4822 CLS_HAS_IVAR_RELEASER = 0x40,
4823 /// class was compiled with -fobjc-arr
4824 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004825};
4826/// BuildClassRoTInitializer - generate meta-data for:
4827/// struct _class_ro_t {
4828/// uint32_t const flags;
4829/// uint32_t const instanceStart;
4830/// uint32_t const instanceSize;
4831/// uint32_t const reserved; // only when building for 64bit targets
4832/// const uint8_t * const ivarLayout;
4833/// const char *const name;
4834/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004835/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004836/// const struct _ivar_list_t *const ivars;
4837/// const uint8_t * const weakIvarLayout;
4838/// const struct _prop_list_t * const properties;
4839/// }
4840///
4841llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842 unsigned flags,
4843 unsigned InstanceStart,
4844 unsigned InstanceSize,
4845 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004846 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004847 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004848
4849 if (CGM.getLangOptions().ObjCAutoRefCount)
4850 flags |= CLS_COMPILED_BY_ARC;
4851
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004852 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4853 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4854 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004855 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004856 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4857 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004858 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004859 // const struct _method_list_t * const baseMethods;
4860 std::vector<llvm::Constant*> Methods;
4861 std::string MethodListName("\01l_OBJC_$_");
4862 if (flags & CLS_META) {
4863 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004864 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004865 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004866 // Class methods should always be defined.
4867 Methods.push_back(GetMethodConstant(*i));
4868 }
4869 } else {
4870 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004871 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004872 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004873 // Instance methods should always be defined.
4874 Methods.push_back(GetMethodConstant(*i));
4875 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004876 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004877 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004878 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004879
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004880 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4881 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004882
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004883 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4884 if (llvm::Constant *C = GetMethodConstant(MD))
4885 Methods.push_back(C);
4886 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4887 if (llvm::Constant *C = GetMethodConstant(MD))
4888 Methods.push_back(C);
4889 }
4890 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004891 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004892 Values[ 5] = EmitMethodList(MethodListName,
4893 "__DATA, __objc_const", Methods);
4894
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004895 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4896 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004897 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004898 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004899 OID->all_referenced_protocol_begin(),
4900 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004901
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004902 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004903 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004904 else
4905 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004906 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4907 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004908 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004909 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004910 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004911 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4912 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004913 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004914 Values);
4915 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004916 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4917 llvm::GlobalValue::InternalLinkage,
4918 Init,
4919 (flags & CLS_META) ?
4920 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4921 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004922 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004923 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004924 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004925 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004926
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004927}
4928
4929/// BuildClassMetaData - This routine defines that to-level meta-data
4930/// for the given ClassName for:
4931/// struct _class_t {
4932/// struct _class_t *isa;
4933/// struct _class_t * const superclass;
4934/// void *cache;
4935/// IMP *vtable;
4936/// struct class_ro_t *ro;
4937/// }
4938///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004939llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004940 std::string &ClassName,
4941 llvm::Constant *IsAGV,
4942 llvm::Constant *SuperClassGV,
4943 llvm::Constant *ClassRoGV,
4944 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004945 llvm::Constant *Values[] = {
4946 IsAGV,
4947 SuperClassGV,
4948 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4949 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4950 ClassRoGV // &CLASS_RO_GV
4951 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004952 if (!Values[1])
4953 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004954 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004955 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004956 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4957 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004958 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004959 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004960 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004961 if (HiddenVisibility)
4962 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004963 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004964}
4965
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004966bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004967CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004968 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004969}
4970
Daniel Dunbar961202372009-05-03 12:57:56 +00004971void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004972 uint32_t &InstanceStart,
4973 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004974 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004975 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004976
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004977 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004978 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004979
4980 // If there are no fields, the start is the same as the end.
4981 if (!RL.getFieldCount())
4982 InstanceStart = InstanceSize;
4983 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004984 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004985}
4986
Fariborz Jahanian71394042009-01-23 23:53:38 +00004987void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4988 std::string ClassName = ID->getNameAsString();
4989 if (!ObjCEmptyCacheVar) {
4990 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004991 CGM.getModule(),
4992 ObjCTypes.CacheTy,
4993 false,
4994 llvm::GlobalValue::ExternalLinkage,
4995 0,
4996 "_objc_empty_cache");
4997
Fariborz Jahanian71394042009-01-23 23:53:38 +00004998 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004999 CGM.getModule(),
5000 ObjCTypes.ImpnfABITy,
5001 false,
5002 llvm::GlobalValue::ExternalLinkage,
5003 0,
5004 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005005 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005006 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005007 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005008 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005009 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005010 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005011 uint32_t InstanceSize = InstanceStart;
5012 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005013 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5014 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005015
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005016 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005017
5018 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005019 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005020 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005021 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005022 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005023 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005024 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005025 // class is root
5026 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005027 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005028 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005029 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005030 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005031 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5032 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5033 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005034 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005035 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005036 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005037 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005038 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005039 ObjCMetaClassName +
5040 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005041 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005042 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005043 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005044 }
5045 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5046 InstanceStart,
5047 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005048 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005049 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005050 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5051 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005052 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005053
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005054 // Metadata for the class
5055 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005056 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005057 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005058 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005059 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005060
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005061 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005062 flags |= CLS_EXCEPTION;
5063
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005064 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005065 flags |= CLS_ROOT;
5066 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005067 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005068 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005069 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005070 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005071 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005072 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005073 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005074 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005075 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005076 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005077 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005078 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005079 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005080
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005081 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005083 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5084 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005085 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005086
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005087 // Determine if this class is also "non-lazy".
5088 if (ImplementationIsNonLazy(ID))
5089 DefinedNonLazyClasses.push_back(ClassMD);
5090
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005091 // Force the definition of the EHType if necessary.
5092 if (flags & CLS_EXCEPTION)
5093 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005094 // Make sure method definition entries are all clear for next implementation.
5095 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005096}
5097
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005098/// GenerateProtocolRef - This routine is called to generate code for
5099/// a protocol reference expression; as in:
5100/// @code
5101/// @protocol(Proto1);
5102/// @endcode
5103/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5104/// which will hold address of the protocol meta-data.
5105///
5106llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005107 const ObjCProtocolDecl *PD) {
5108
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005109 // This routine is called for @protocol only. So, we must build definition
5110 // of protocol's meta-data (not a reference to it!)
5111 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005112 llvm::Constant *Init =
5113 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5114 ObjCTypes.ExternalProtocolPtrTy);
5115
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005116 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005117 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005118
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005119 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5120 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005121 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005122 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005123 CGM.getModule(),
5124 Init->getType(), false,
5125 llvm::GlobalValue::WeakAnyLinkage,
5126 Init,
5127 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005128 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5129 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005130 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005131 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005132}
5133
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005134/// GenerateCategory - Build metadata for a category implementation.
5135/// struct _category_t {
5136/// const char * const name;
5137/// struct _class_t *const cls;
5138/// const struct _method_list_t * const instance_methods;
5139/// const struct _method_list_t * const class_methods;
5140/// const struct _protocol_list_t * const protocols;
5141/// const struct _prop_list_t * const properties;
5142/// }
5143///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005144void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005145 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005146 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005147 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5148 "_$_" + OCD->getNameAsString());
5149 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005150 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005151
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005152 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005153 Values[0] = GetClassName(OCD->getIdentifier());
5154 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005155 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005156 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005157 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5158
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005159 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005160 std::vector<llvm::Constant*> Methods;
5161 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005162 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005163 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005164
5165 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005166 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005167 // Instance methods should always be defined.
5168 Methods.push_back(GetMethodConstant(*i));
5169 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005170
5171 Values[2] = EmitMethodList(MethodListName,
5172 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005173 Methods);
5174
5175 MethodListName = Prefix;
5176 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5177 OCD->getNameAsString();
5178 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005179 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005180 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005181 // Class methods should always be defined.
5182 Methods.push_back(GetMethodConstant(*i));
5183 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005184
5185 Values[3] = EmitMethodList(MethodListName,
5186 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005187 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005188 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005189 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005190 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005191 llvm::SmallString<256> ExtName;
5192 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5193 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005194 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005195 + Interface->getName() + "_$_"
5196 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005197 Category->protocol_begin(),
5198 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005199 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5200 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005201 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005202 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5203 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005204 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005205
5206 llvm::Constant *Init =
5207 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005208 Values);
5209 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005210 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005211 false,
5212 llvm::GlobalValue::InternalLinkage,
5213 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005214 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005215 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005216 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005217 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005218 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005219 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005220
5221 // Determine if this category is also "non-lazy".
5222 if (ImplementationIsNonLazy(OCD))
5223 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005224 // method definition entries must be clear for next implementation.
5225 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005226}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005227
5228/// GetMethodConstant - Return a struct objc_method constant for the
5229/// given method if it has been defined. The result is null if the
5230/// method has not been defined. The return value has type MethodPtrTy.
5231llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005232 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005233 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005234 if (!Fn)
5235 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005236
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005237 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005238 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005239 ObjCTypes.SelectorPtrTy),
5240 GetMethodVarType(MD),
5241 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5242 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005243 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005244}
5245
5246/// EmitMethodList - Build meta-data for method declarations
5247/// struct _method_list_t {
5248/// uint32_t entsize; // sizeof(struct _objc_method)
5249/// uint32_t method_count;
5250/// struct _objc_method method_list[method_count];
5251/// }
5252///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005253llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005254 const char *Section,
5255 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005256 // Return null for empty list.
5257 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005258 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005259
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005260 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005261 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005262 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005263 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005264 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005265 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005266 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005267 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005268 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005269 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005270
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005271 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005272 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005273 llvm::GlobalValue::InternalLinkage, Init, Name);
5274 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005275 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005276 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005277 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005278}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005279
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005280/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5281/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005282llvm::GlobalVariable *
5283CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5284 const ObjCIvarDecl *Ivar) {
5285 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005286 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005287 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005289 CGM.getModule().getGlobalVariable(Name);
5290 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005291 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005292 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005293 false,
5294 llvm::GlobalValue::ExternalLinkage,
5295 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005296 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005297 return IvarOffsetGV;
5298}
5299
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005300llvm::Constant *
5301CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5302 const ObjCIvarDecl *Ivar,
5303 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005304 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005305 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005306 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005307 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005308 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005309
Mike Stump18bb9282009-05-16 07:57:57 +00005310 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5311 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005312 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5313 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005314 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005315 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005316 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005317 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005318 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005319 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005320}
5321
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005322/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005323/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005324/// IvarListnfABIPtrTy.
5325/// struct _ivar_t {
5326/// unsigned long int *offset; // pointer to ivar offset location
5327/// char *name;
5328/// char *type;
5329/// uint32_t alignment;
5330/// uint32_t size;
5331/// }
5332/// struct _ivar_list_t {
5333/// uint32 entsize; // sizeof(struct _ivar_t)
5334/// uint32 count;
5335/// struct _iver_t list[count];
5336/// }
5337///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005338
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005339llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005340 const ObjCImplementationDecl *ID) {
5341
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005342 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005343
Jordy Rosea91768e2011-07-22 02:08:32 +00005344 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005345 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005346
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005347 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005348
Jordy Rosea91768e2011-07-22 02:08:32 +00005349 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005350 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005351 // Ignore unnamed bit-fields.
5352 if (!IVD->getDeclName())
5353 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005354 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005355 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005356 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005357 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5358 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005359 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005360 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005361 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005362 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005363 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005364 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005365 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005366 // NOTE. Size of a bitfield does not match gcc's, because of the
5367 // way bitfields are treated special in each. But I am told that
5368 // 'size' for bitfield ivars is ignored by the runtime so it does
5369 // not matter. If it matters, there is enough info to get the
5370 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005371 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005372 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005373 }
5374 // Return null for empty list.
5375 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005376 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005377
5378 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005379 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005380 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5381 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005382 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005383 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005384 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005385 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005386 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5387 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005388 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005389 llvm::GlobalValue::InternalLinkage,
5390 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005391 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005392 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005393 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005394 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005395
Chris Lattnerf56501c2009-07-17 23:57:13 +00005396 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005397 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005398}
5399
5400llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005401 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005404 if (!Entry) {
5405 // We use the initializer as a marker of whether this is a forward
5406 // reference or not. At module finalization we add the empty
5407 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005408 Entry =
5409 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5410 llvm::GlobalValue::ExternalLinkage,
5411 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005412 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005413 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005414 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005415
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005416 return Entry;
5417}
5418
5419/// GetOrEmitProtocol - Generate the protocol meta-data:
5420/// @code
5421/// struct _protocol_t {
5422/// id isa; // NULL
5423/// const char * const protocol_name;
5424/// const struct _protocol_list_t * protocol_list; // super protocols
5425/// const struct method_list_t * const instance_methods;
5426/// const struct method_list_t * const class_methods;
5427/// const struct method_list_t *optionalInstanceMethods;
5428/// const struct method_list_t *optionalClassMethods;
5429/// const struct _prop_list_t * properties;
5430/// const uint32_t size; // sizeof(struct _protocol_t)
5431/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005432/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433/// }
5434/// @endcode
5435///
5436
5437llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005438 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005439 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005440
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005441 // Early exit if a defining object has already been generated.
5442 if (Entry && Entry->hasInitializer())
5443 return Entry;
5444
Douglas Gregora715bff2012-01-01 19:51:50 +00005445 // Use the protocol definition, if there is one.
5446 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5447 PD = Def;
5448
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005449 // Construct method lists.
5450 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5451 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005452 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005453 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005454 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005455 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005456 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005457 if (!C)
5458 return GetOrEmitProtocolRef(PD);
5459
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005460 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5461 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005462 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005463 } else {
5464 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005465 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005466 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005467 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468
5469 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005470 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005471 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005472 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005473 if (!C)
5474 return GetOrEmitProtocolRef(PD);
5475
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005476 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5477 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005478 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005479 } else {
5480 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005481 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005482 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005483 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005484
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005485 MethodTypesExt.insert(MethodTypesExt.end(),
5486 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5487
5488 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005489 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005490 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005491 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005492 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5493 PD->protocol_begin(),
5494 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005495
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005496 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005497 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005498 "__DATA, __objc_const",
5499 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005500 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005501 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005502 "__DATA, __objc_const",
5503 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005504 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005505 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005506 "__DATA, __objc_const",
5507 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005508 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005509 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005510 "__DATA, __objc_const",
5511 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005512 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005513 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005514 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005515 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005516 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005517 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005518 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5519 + PD->getName(),
5520 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005521 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005522 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005523
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005524 if (Entry) {
5525 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005526 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005527 Entry->setInitializer(Init);
5528 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005529 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005530 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5531 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5532 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005533 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005534 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005535 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005536 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005537 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005538 CGM.AddUsedGlobal(Entry);
5539
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005540 // Use this protocol meta-data to build protocol list table in section
5541 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005542 llvm::GlobalVariable *PTGV =
5543 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5544 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5545 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005546 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005547 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005548 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005549 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005550 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005551 return Entry;
5552}
5553
5554/// EmitProtocolList - Generate protocol list meta-data:
5555/// @code
5556/// struct _protocol_list_t {
5557/// long protocol_count; // Note, this is 32/64 bit
5558/// struct _protocol_t[protocol_count];
5559/// }
5560/// @endcode
5561///
5562llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005563CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005564 ObjCProtocolDecl::protocol_iterator begin,
5565 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005566 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005567
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005568 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005569 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005570 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005571
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005572 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005573 llvm::SmallString<256> TmpName;
5574 Name.toVector(TmpName);
5575 llvm::GlobalVariable *GV =
5576 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005577 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005578 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005580 for (; begin != end; ++begin)
5581 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5582
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005583 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005584 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005585 ObjCTypes.ProtocolnfABIPtrTy));
5586
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005587 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005588 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005589 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005590 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005591 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005592 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005593 ProtocolRefs.size()),
5594 ProtocolRefs);
5595
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005596 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005597 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005598 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005599 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005600 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005601 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005602 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005603 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005604 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005605 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005606}
5607
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005608/// GetMethodDescriptionConstant - This routine build following meta-data:
5609/// struct _objc_method {
5610/// SEL _cmd;
5611/// char *method_type;
5612/// char *_imp;
5613/// }
5614
5615llvm::Constant *
5616CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005617 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005618 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005619 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5620 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005621 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005622 if (!Desc[1])
5623 return 0;
5624
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005625 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005626 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005627 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005628}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005629
5630/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5631/// This code gen. amounts to generating code for:
5632/// @code
5633/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5634/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005635///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005636LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005637 CodeGen::CodeGenFunction &CGF,
5638 QualType ObjectTy,
5639 llvm::Value *BaseValue,
5640 const ObjCIvarDecl *Ivar,
5641 unsigned CVRQualifiers) {
5642 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005643 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5644 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005645}
5646
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005647llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005648 CodeGen::CodeGenFunction &CGF,
5649 const ObjCInterfaceDecl *Interface,
5650 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005651 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005652}
5653
John McCall234eac82011-05-13 23:16:18 +00005654static void appendSelectorForMessageRefTable(std::string &buffer,
5655 Selector selector) {
5656 if (selector.isUnarySelector()) {
5657 buffer += selector.getNameForSlot(0);
5658 return;
5659 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005660
John McCall234eac82011-05-13 23:16:18 +00005661 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5662 buffer += selector.getNameForSlot(i);
5663 buffer += '_';
5664 }
5665}
5666
John McCall9e8bb002011-05-14 03:10:52 +00005667/// Emit a "v-table" message send. We emit a weak hidden-visibility
5668/// struct, initially containing the selector pointer and a pointer to
5669/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5670/// load and call the function pointer, passing the address of the
5671/// struct as the second parameter. The runtime determines whether
5672/// the selector is currently emitted using vtable dispatch; if so, it
5673/// substitutes a stub function which simply tail-calls through the
5674/// appropriate vtable slot, and if not, it substitues a stub function
5675/// which tail-calls objc_msgSend. Both stubs adjust the selector
5676/// argument to correctly point to the selector.
5677RValue
5678CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5679 ReturnValueSlot returnSlot,
5680 QualType resultType,
5681 Selector selector,
5682 llvm::Value *arg0,
5683 QualType arg0Type,
5684 bool isSuper,
5685 const CallArgList &formalArgs,
5686 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005687 // Compute the actual arguments.
5688 CallArgList args;
5689
John McCall9e8bb002011-05-14 03:10:52 +00005690 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005691 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005692 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5693 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005694
John McCall9e8bb002011-05-14 03:10:52 +00005695 // Second argument: a pointer to the message ref structure. Leave
5696 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005697 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5698
5699 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5700
5701 const CGFunctionInfo &fnInfo =
5702 CGM.getTypes().getFunctionInfo(resultType, args,
5703 FunctionType::ExtInfo());
5704
John McCall5880fb82011-05-14 21:12:11 +00005705 NullReturnState nullReturn;
5706
John McCall9e8bb002011-05-14 03:10:52 +00005707 // Find the function to call and the mangled name for the message
5708 // ref structure. Using a different mangled name wouldn't actually
5709 // be a problem; it would just be a waste.
5710 //
5711 // The runtime currently never uses vtable dispatch for anything
5712 // except normal, non-super message-sends.
5713 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005714 llvm::Constant *fn = 0;
5715 std::string messageRefName("\01l_");
5716 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005717 if (isSuper) {
5718 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5719 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005720 } else {
John McCall5880fb82011-05-14 21:12:11 +00005721 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005722 fn = ObjCTypes.getMessageSendStretFixupFn();
5723 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005724 }
John McCall234eac82011-05-13 23:16:18 +00005725 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5726 fn = ObjCTypes.getMessageSendFpretFixupFn();
5727 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005728 } else {
John McCall234eac82011-05-13 23:16:18 +00005729 if (isSuper) {
5730 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5731 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005732 } else {
John McCall234eac82011-05-13 23:16:18 +00005733 fn = ObjCTypes.getMessageSendFixupFn();
5734 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005735 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005736 }
John McCall234eac82011-05-13 23:16:18 +00005737 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5738 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005739
John McCall234eac82011-05-13 23:16:18 +00005740 // Append the selector name, except use underscores anywhere we
5741 // would have used colons.
5742 appendSelectorForMessageRefTable(messageRefName, selector);
5743
5744 llvm::GlobalVariable *messageRef
5745 = CGM.getModule().getGlobalVariable(messageRefName);
5746 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005747 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005748 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005749 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005750 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5751 init->getType(),
5752 /*constant*/ false,
5753 llvm::GlobalValue::WeakAnyLinkage,
5754 init,
5755 messageRefName);
5756 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5757 messageRef->setAlignment(16);
5758 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5759 }
5760 llvm::Value *mref =
5761 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5762
John McCall9e8bb002011-05-14 03:10:52 +00005763 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005764 args[1].RV = RValue::get(mref);
5765
5766 // Load the function to call from the message ref table.
5767 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5768 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5769
5770 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005771 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005772 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5773 callee = CGF.Builder.CreateBitCast(callee,
5774 llvm::PointerType::getUnqual(fnType));
5775
John McCall5880fb82011-05-14 21:12:11 +00005776 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5777 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005778}
5779
5780/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005781CodeGen::RValue
5782CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005783 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005784 QualType ResultType,
5785 Selector Sel,
5786 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005787 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005788 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005789 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005790 return isVTableDispatchedSelector(Sel)
5791 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005792 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005793 false, CallArgs, Method)
5794 : EmitMessageSend(CGF, Return, ResultType,
5795 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005796 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005797 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005798}
5799
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005800llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005801CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005802 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5803
Daniel Dunbara6468342009-03-02 05:18:14 +00005804 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005805 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005806 false, llvm::GlobalValue::ExternalLinkage,
5807 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005808 }
5809
5810 return GV;
5811}
5812
John McCall31168b02011-06-15 23:02:42 +00005813llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5814 IdentifierInfo *II) {
5815 llvm::GlobalVariable *&Entry = ClassReferences[II];
5816
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005817 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005818 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005819 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005820 Entry =
John McCall31168b02011-06-15 23:02:42 +00005821 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5822 false, llvm::GlobalValue::InternalLinkage,
5823 ClassGV,
5824 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005825 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005826 CGM.getTargetData().getABITypeAlignment(
5827 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005828 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005829 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005830 }
John McCall31168b02011-06-15 23:02:42 +00005831
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005832 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005833}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005834
John McCall31168b02011-06-15 23:02:42 +00005835llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5836 const ObjCInterfaceDecl *ID) {
5837 return EmitClassRefFromId(Builder, ID->getIdentifier());
5838}
5839
5840llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5841 CGBuilderTy &Builder) {
5842 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5843 return EmitClassRefFromId(Builder, II);
5844}
5845
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005846llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005847CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005848 const ObjCInterfaceDecl *ID) {
5849 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005850
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005851 if (!Entry) {
5852 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5853 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005854 Entry =
5855 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005856 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005857 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005858 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005859 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005860 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005861 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005862 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005863 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005864 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005865
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005866 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005867}
5868
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005869/// EmitMetaClassRef - Return a Value * of the address of _class_t
5870/// meta-data
5871///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005872llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5873 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005874 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5875 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005876 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005877
Daniel Dunbar15894b72009-04-07 05:48:37 +00005878 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005879 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005880 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005881 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005882 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005883 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005884 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005885 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005886 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005887 ObjCTypes.ClassnfABIPtrTy));
5888
Daniel Dunbare60aa052009-04-15 19:03:14 +00005889 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005890 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005891
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005892 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005893}
5894
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005895/// GetClass - Return a reference to the class for the given interface
5896/// decl.
5897llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5898 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005899 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005900 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5901 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5902 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5903 }
5904
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005905 return EmitClassRef(Builder, ID);
5906}
5907
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005908/// Generates a message send where the super is the receiver. This is
5909/// a message send to self with special delivery semantics indicating
5910/// which class's method should be called.
5911CodeGen::RValue
5912CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005913 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005914 QualType ResultType,
5915 Selector Sel,
5916 const ObjCInterfaceDecl *Class,
5917 bool isCategoryImpl,
5918 llvm::Value *Receiver,
5919 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005920 const CodeGen::CallArgList &CallArgs,
5921 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005922 // ...
5923 // Create and init a super structure; this is a (receiver, class)
5924 // pair we will pass to objc_msgSendSuper.
5925 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005926 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005927
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005928 llvm::Value *ReceiverAsObject =
5929 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5930 CGF.Builder.CreateStore(ReceiverAsObject,
5931 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005933 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005934 llvm::Value *Target;
5935 if (IsClassMessage) {
5936 if (isCategoryImpl) {
5937 // Message sent to "super' in a class method defined in
5938 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005939 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005940 Target = CGF.Builder.CreateStructGEP(Target, 0);
5941 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005942 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005943 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005944 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005945 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005946
Mike Stump18bb9282009-05-16 07:57:57 +00005947 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5948 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00005949 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005950 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5951 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5952 CGF.Builder.CreateStore(Target,
5953 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005954
John McCall9e8bb002011-05-14 03:10:52 +00005955 return (isVTableDispatchedSelector(Sel))
5956 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005957 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005958 true, CallArgs, Method)
5959 : EmitMessageSend(CGF, Return, ResultType,
5960 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005961 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005962 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005963}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005964
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005965llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005966 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005967 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005968
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005969 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005970 llvm::Constant *Casted =
5971 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5972 ObjCTypes.SelectorPtrTy);
5973 Entry =
5974 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5975 llvm::GlobalValue::InternalLinkage,
5976 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005977 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005978 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005979 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005980
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005981 if (lval)
5982 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00005983 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
5984
5985 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5986 llvm::MDNode::get(VMContext,
5987 ArrayRef<llvm::Value*>()));
5988 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005989}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005990/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005991/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005992///
5993void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005994 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005995 llvm::Value *dst,
5996 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005997 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005998 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005999 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006000 assert(Size <= 8 && "does not support size > 8");
6001 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6002 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006003 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6004 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006005 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6006 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006007 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6008 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006009 return;
6010}
6011
6012/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6013/// objc_assign_strongCast (id src, id *dst)
6014///
6015void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006016 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006017 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006018 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006019 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006020 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006021 assert(Size <= 8 && "does not support size > 8");
6022 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006023 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006024 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6025 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006026 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6027 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006028 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006029 src, dst, "weakassign");
6030 return;
6031}
6032
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006033void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006034 CodeGen::CodeGenFunction &CGF,
6035 llvm::Value *DestPtr,
6036 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006037 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006038 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6039 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006040 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006041 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006042 return;
6043}
6044
Fariborz Jahanian06292952009-02-16 22:52:32 +00006045/// EmitObjCWeakRead - Code gen for loading value of a __weak
6046/// object: objc_read_weak (id *src)
6047///
6048llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006049 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006050 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006051 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006052 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6053 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006054 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006055 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006056 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006057 return read_weak;
6058}
6059
6060/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6061/// objc_assign_weak (id src, id *dst)
6062///
6063void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006064 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006065 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006066 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006067 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006068 assert(Size <= 8 && "does not support size > 8");
6069 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6070 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006071 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6072 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006073 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6074 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006075 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006076 src, dst, "weakassign");
6077 return;
6078}
6079
6080/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6081/// objc_assign_global (id src, id *dst)
6082///
6083void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006084 llvm::Value *src, llvm::Value *dst,
6085 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006086 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006087 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006088 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006089 assert(Size <= 8 && "does not support size > 8");
6090 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6091 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006092 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6093 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006094 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6095 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006096 if (!threadlocal)
6097 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6098 src, dst, "globalassign");
6099 else
6100 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6101 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006102 return;
6103}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006104
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006105void
John McCallbd309292010-07-06 01:34:17 +00006106CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6107 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006108 EmitAtSynchronizedStmt(CGF, S,
6109 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6110 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006111}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006112
John McCall2ca705e2010-07-24 00:37:23 +00006113llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006114CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006115 // There's a particular fixed type info for 'id'.
6116 if (T->isObjCIdType() ||
6117 T->isObjCQualifiedIdType()) {
6118 llvm::Constant *IDEHType =
6119 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6120 if (!IDEHType)
6121 IDEHType =
6122 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6123 false,
6124 llvm::GlobalValue::ExternalLinkage,
6125 0, "OBJC_EHTYPE_id");
6126 return IDEHType;
6127 }
6128
6129 // All other types should be Objective-C interface pointer types.
6130 const ObjCObjectPointerType *PT =
6131 T->getAs<ObjCObjectPointerType>();
6132 assert(PT && "Invalid @catch type.");
6133 const ObjCInterfaceType *IT = PT->getInterfaceType();
6134 assert(IT && "Invalid @catch type.");
6135 return GetInterfaceEHType(IT->getDecl(), false);
6136}
6137
John McCallbd309292010-07-06 01:34:17 +00006138void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6139 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006140 EmitTryCatchStmt(CGF, S,
6141 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6142 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6143 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006144}
6145
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006146/// EmitThrowStmt - Generate code for a throw statement.
6147void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6148 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006149 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006150 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006151 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006152 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006153 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006154 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006155 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006156 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006157 }
6158
John McCall17afe452010-10-16 08:21:07 +00006159 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006160 CGF.Builder.ClearInsertionPoint();
6161}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006162
John McCall2ca705e2010-07-24 00:37:23 +00006163llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006164CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006165 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006166 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006167
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006168 // If we don't need a definition, return the entry if found or check
6169 // if we use an external reference.
6170 if (!ForDefinition) {
6171 if (Entry)
6172 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006173
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006174 // If this type (or a super class) has the __objc_exception__
6175 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006176 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006177 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006178 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006179 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006180 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006181 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006182 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006183 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006184
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006185 // Otherwise we need to either make a new entry or fill in the
6186 // initializer.
6187 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006188 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006189 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006190 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006191 CGM.getModule().getGlobalVariable(VTableName);
6192 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006193 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6194 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006195 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006196 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006197
Chris Lattner5e016ae2010-06-27 07:15:29 +00006198 llvm::Value *VTableIdx =
6199 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006200
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006201 llvm::Constant *Values[] = {
6202 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6203 GetClassName(ID->getIdentifier()),
6204 GetClassGlobal(ClassName)
6205 };
Owen Anderson170229f2009-07-14 23:10:40 +00006206 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006207 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006208
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006209 if (Entry) {
6210 Entry->setInitializer(Init);
6211 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006212 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006213 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006214 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006215 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006216 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006217 }
6218
John McCall457a04e2010-10-22 21:05:15 +00006219 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006220 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006221 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6222 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006223
6224 if (ForDefinition) {
6225 Entry->setSection("__DATA,__objc_const");
6226 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6227 } else {
6228 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6229 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006230
6231 return Entry;
6232}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006233
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006234/* *** */
6235
Daniel Dunbarb036db82008-08-13 03:21:16 +00006236CodeGen::CGObjCRuntime *
6237CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006238 if (CGM.getLangOptions().ObjCNonFragileABI)
6239 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006240 return new CGObjCMac(CGM);
6241}