blob: b4896920b49c19d25e5620e54af842273e71440f [file] [log] [blame]
Daniel Dunbarc17a4d32008-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 Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046
Daniel Dunbar6bff2512009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000049
Fariborz Jahanianee0af742009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000053
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000054private:
John McCall0774cb82011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCallf85e1932011-06-15 23:02:42 +000068 "objc_msgSend",
69 llvm::Attribute::NonLazyBind);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000070 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000071
John McCall0774cb82011-05-15 01:53:33 +000072 /// void objc_msgSend_stret (id, SEL, ...)
73 ///
74 /// The messenger used when the return value is an aggregate returned
75 /// by indirect reference in the first argument, and therefore the
76 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000077 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000078 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000079 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
80 params, true),
81 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000082
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000083 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000084
John McCall0774cb82011-05-15 01:53:33 +000085 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
86 ///
87 /// The messenger used when the return value is returned on the x87
88 /// floating-point stack; without a special entrypoint, the nil case
89 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000090 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000091 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000092 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
93 params, true),
John McCall0774cb82011-05-15 01:53:33 +000094 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000095
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000096 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000097
Anders Carlssoneea64802011-10-31 16:27:11 +000098 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
99 ///
100 /// The messenger used when the return value is returned in two values on the
101 /// x87 floating point stack; without a special entrypoint, the nil case
102 /// would be unbalanced. Only used on 64-bit X86.
103 llvm::Constant *getMessageSendFp2retFn() const {
104 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
105 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
106 llvm::Type *resultType =
107 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
108
109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
110 params, true),
111 "objc_msgSend_fp2ret");
112 }
113
John McCall0774cb82011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
115 ///
116 /// The messenger used for super calls, which have different dispatch
117 /// semantics. The class passed is the superclass of the current
118 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000120 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000121 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000122 params, true),
123 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000124 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000125
John McCall0774cb82011-05-15 01:53:33 +0000126 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
127 ///
128 /// A slightly different messenger used for super calls. The class
129 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000131 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000132 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000133 params, true),
134 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000135 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000136
John McCall0774cb82011-05-15 01:53:33 +0000137 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
138 /// SEL op, ...)
139 ///
140 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000142 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000143 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000144 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000145 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000146 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000147
John McCall0774cb82011-05-15 01:53:33 +0000148 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
149 /// SEL op, ...)
150 ///
151 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000153 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000154 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000155 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000156 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000157 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000158
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000159 llvm::Constant *getMessageSendSuperFpretFn() const {
160 // There is no objc_msgSendSuper_fpret? How can that work?
161 return getMessageSendSuperFn();
162 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000163
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000164 llvm::Constant *getMessageSendSuperFpretFn2() const {
165 // There is no objc_msgSendSuper_fpret? How can that work?
166 return getMessageSendSuperFn2();
167 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000168
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000169protected:
170 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000171
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000172public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000173 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000174 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000176 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000177 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000179 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000180 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000182 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000183 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000184
185private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000186 /// ProtocolPtrTy - LLVM type for external protocol handles
187 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000188 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000189
190public:
191 llvm::Type *getExternalProtocolPtrTy() {
192 if (!ExternalProtocolPtrTy) {
193 // FIXME: It would be nice to unify this with the opaque type, so that the
194 // IR comes out a bit cleaner.
195 CodeGen::CodeGenTypes &Types = CGM.getTypes();
196 ASTContext &Ctx = CGM.getContext();
197 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
198 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
199 }
200
201 return ExternalProtocolPtrTy;
202 }
203
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000204 // SuperCTy - clang type for struct objc_super.
205 QualType SuperCTy;
206 // SuperPtrCTy - clang type for struct objc_super *.
207 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000208
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000209 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000210 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000211 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000212 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000213
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000214 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
215 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000216 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000217
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000218 /// PropertyListTy - LLVM type for struct objc_property_list
219 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000220 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000221 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000222 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000223
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000224 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000225 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000226
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000227 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000228 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000229 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000230 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000231
Chris Lattner72db6c32009-04-22 02:44:54 +0000232 llvm::Constant *getGetPropertyFn() {
233 CodeGen::CodeGenTypes &Types = CGM.getTypes();
234 ASTContext &Ctx = CGM.getContext();
235 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000237 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
238 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000239 Params.push_back(IdType);
240 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000241 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000242 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000243 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000244 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000245 FunctionType::ExtInfo()),
246 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000247 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
248 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000249
Chris Lattner72db6c32009-04-22 02:44:54 +0000250 llvm::Constant *getSetPropertyFn() {
251 CodeGen::CodeGenTypes &Types = CGM.getTypes();
252 ASTContext &Ctx = CGM.getContext();
253 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000254 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000255 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
256 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000257 Params.push_back(IdType);
258 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000259 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000260 Params.push_back(IdType);
261 Params.push_back(Ctx.BoolTy);
262 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000263 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000264 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000265 FunctionType::ExtInfo()),
266 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000267 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
268 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000269
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000270
271 llvm::Constant *getCopyStructFn() {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000275 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000276 Params.push_back(Ctx.VoidPtrTy);
277 Params.push_back(Ctx.VoidPtrTy);
278 Params.push_back(Ctx.LongTy);
279 Params.push_back(Ctx.BoolTy);
280 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000281 llvm::FunctionType *FTy =
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000282 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
283 FunctionType::ExtInfo()),
284 false);
285 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
286 }
287
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000288 /// This routine declares and returns address of:
289 /// void objc_copyCppObjectAtomic(
290 /// void *dest, const void *src,
291 /// void (*copyHelper) (void *dest, const void *source));
292 llvm::Constant *getCppAtomicObjectFunction() {
293 CodeGen::CodeGenTypes &Types = CGM.getTypes();
294 ASTContext &Ctx = CGM.getContext();
295 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
296 SmallVector<CanQualType,3> Params;
297 Params.push_back(Ctx.VoidPtrTy);
298 Params.push_back(Ctx.VoidPtrTy);
299 Params.push_back(Ctx.VoidPtrTy);
300 llvm::FunctionType *FTy =
301 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
302 FunctionType::ExtInfo()),
303 false);
304 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
305 }
306
Chris Lattner72db6c32009-04-22 02:44:54 +0000307 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000310 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000312 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000313 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000314 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000315 FunctionType::ExtInfo()),
316 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000317 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
318 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000319
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000320 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000321 llvm::Constant *getGcReadWeakFn() {
322 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000323 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000324 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000325 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000327 }
328
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000329 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000330 llvm::Constant *getGcAssignWeakFn() {
331 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000332 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000333 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000334 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000335 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
336 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000337
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000338 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000339 llvm::Constant *getGcAssignGlobalFn() {
340 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000341 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000342 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000343 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000344 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
345 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000346
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000347 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
348 llvm::Constant *getGcAssignThreadLocalFn() {
349 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000350 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000351 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000352 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
354 }
355
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000357 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000358 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
360 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000361 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000362 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000363 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
364 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000365
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000366 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
367 llvm::Constant *GcMemmoveCollectableFn() {
368 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000369 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000370 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000376 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
381 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000382
383 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000384 llvm::Constant *getExceptionThrowFn() {
385 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000387 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000388 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
390 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000391
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000392 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
393 llvm::Constant *getExceptionRethrowFn() {
394 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000395 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000396 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
397 }
398
Daniel Dunbar1c566672009-02-24 01:43:46 +0000399 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000400 llvm::Constant *getSyncEnterFn() {
401 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000402 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000403 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000404 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000405 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
406 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000407
Daniel Dunbar1c566672009-02-24 01:43:46 +0000408 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000409 llvm::Constant *getSyncExitFn() {
410 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000411 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000412 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000413 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000414 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
415 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000416
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000417 llvm::Constant *getSendFn(bool IsSuper) const {
418 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
419 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000420
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn2(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
423 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000424
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendStretFn(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
427 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000428
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000429 llvm::Constant *getSendStretFn2(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
431 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000432
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000433 llvm::Constant *getSendFpretFn(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
435 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000436
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000437 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
439 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000440
Anders Carlssoneea64802011-10-31 16:27:11 +0000441 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
443 }
444
445 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
446 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
447 }
448
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000449 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
450 ~ObjCCommonTypesHelper(){}
451};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000452
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000453/// ObjCTypesHelper - Helper class that encapsulates lazy
454/// construction of varies types used during ObjC generation.
455class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000456public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000457 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000458 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000459 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000460 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000461 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000462 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000463
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000464 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000465 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000466 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000467 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000468 /// ProtocolExtensionTy - LLVM type for struct
469 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000470 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000471 /// ProtocolExtensionTy - LLVM type for struct
472 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000473 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000474 /// MethodDescriptionTy - LLVM type for struct
475 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000476 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000477 /// MethodDescriptionListTy - LLVM type for struct
478 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000479 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000480 /// MethodDescriptionListPtrTy - LLVM type for struct
481 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000482 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000483 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000484 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000485 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000486 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000487 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000488 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000489 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000490 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000491 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000499 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000500 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000501 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000503 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000504 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000505 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507
Anders Carlsson124526b2008-09-09 10:10:21 +0000508 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000510
Anders Carlsson124526b2008-09-09 10:10:21 +0000511 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000512 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000513 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000514 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000515 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000516 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000517 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000518
519 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000520 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000522 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000523 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000524 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000525 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000526
527 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000528 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000529 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000530 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000531 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000532 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000533 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000534
Anders Carlsson124526b2008-09-09 10:10:21 +0000535 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000536 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000538 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000539 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000540 "objc_exception_match");
541
Chris Lattner34b02a12009-04-22 02:26:14 +0000542 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Anders Carlsson124526b2008-09-09 10:10:21 +0000544 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000545 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000546 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000547 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000548 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
549 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000550 "_setjmp",
551 llvm::Attribute::ReturnsTwice);
Chris Lattner34b02a12009-04-22 02:26:14 +0000552 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000553
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000554public:
555 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000556 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000557};
558
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000559/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000560/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000561class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000562public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000563
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000564 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000566
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000567 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000568 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000569
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000570 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000571 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572
Daniel Dunbar948e2582009-02-15 07:36:20 +0000573 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000575
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000576 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000577 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000578
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000579 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000580 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000582 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000584
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000585 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000586 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000587
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000588 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000589 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000591 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000592 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000593
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000594 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000595 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000596
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000597 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000598 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000599
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000600 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000601 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000603 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000604 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000606 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000607
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000608 // MessageRefTy - LLVM for:
609 // struct _message_ref_t {
610 // IMP messenger;
611 // SEL name;
612 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000613 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000614 // MessageRefCTy - clang type for struct _message_ref_t
615 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000616
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000617 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000618 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000619 // MessageRefCPtrTy - clang type for struct _message_ref_t*
620 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Fariborz Jahanianef163782009-02-05 01:13:09 +0000622 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000623 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000625 // SuperMessageRefTy - LLVM for:
626 // struct _super_message_ref_t {
627 // SUPER_IMP messenger;
628 // SEL name;
629 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000630 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000632 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000633 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000634
Chris Lattner1c02f862009-04-22 02:53:24 +0000635 llvm::Constant *getMessageSendFixupFn() {
636 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000637 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000638 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000639 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000640 "objc_msgSend_fixup");
641 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Chris Lattner1c02f862009-04-22 02:53:24 +0000643 llvm::Constant *getMessageSendFpretFixupFn() {
644 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000645 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000646 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000647 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000648 "objc_msgSend_fpret_fixup");
649 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000650
Chris Lattner1c02f862009-04-22 02:53:24 +0000651 llvm::Constant *getMessageSendStretFixupFn() {
652 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000653 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000655 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000656 "objc_msgSend_stret_fixup");
657 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Chris Lattner1c02f862009-04-22 02:53:24 +0000659 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000661 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000662 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000663 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000664 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000665 "objc_msgSendSuper2_fixup");
666 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000667
Chris Lattner1c02f862009-04-22 02:53:24 +0000668 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000669 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000670 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000671 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000672 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000673 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000674 "objc_msgSendSuper2_stret_fixup");
675 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000676
Chris Lattner8a569112009-04-22 02:15:23 +0000677 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000678 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000679 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000680
Chris Lattner8a569112009-04-22 02:15:23 +0000681 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000682
Chris Lattner8a569112009-04-22 02:15:23 +0000683 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000684 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000686 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000687 "objc_begin_catch");
688 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000689
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 llvm::StructType *EHTypeTy;
691 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000692
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000693 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
694 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000695};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000696
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000697class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000698public:
699 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000700 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000701 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000702 unsigned ivar_bytepos;
703 unsigned ivar_size;
704 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000705 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000706
707 // Allow sorting based on byte pos.
708 bool operator<(const GC_IVAR &b) const {
709 return ivar_bytepos < b.ivar_bytepos;
710 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000711 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000713 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000714 public:
715 unsigned skip;
716 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000718 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000719 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000720
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000721protected:
722 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000723 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000724 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000725 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000726
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000727 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000728 SmallVector<GC_IVAR, 16> SkipIvars;
729 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000730
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000731 /// LazySymbols - Symbols to generate a lazy reference for. See
732 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000733 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000734
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000735 /// DefinedSymbols - External symbols which are defined by this
736 /// module. The symbols in this list and LazySymbols are used to add
737 /// special linker symbols which ensure that Objective-C modules are
738 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000739 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000741 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000742 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000743
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000744 /// MethodVarNames - uniqued method variable names.
745 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000746
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000747 /// DefinedCategoryNames - list of category names in form Class_Category.
748 llvm::SetVector<std::string> DefinedCategoryNames;
749
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000750 /// MethodVarTypes - uniqued method type signatures. We have to use
751 /// a StringMap here because have no other unique reference.
752 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000754 /// MethodDefinitions - map of methods which have been defined in
755 /// this translation unit.
756 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000758 /// PropertyNames - uniqued method variable names.
759 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000760
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000761 /// ClassReferences - uniqued class references.
762 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000763
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000764 /// SelectorReferences - uniqued selector references.
765 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000767 /// Protocols - Protocols for which an objc_protocol structure has
768 /// been emitted. Forward declarations are handled by creating an
769 /// empty structure whose initializer is filled in when/if defined.
770 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000771
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000772 /// DefinedProtocols - Protocols which have actually been
773 /// defined. We should not need this, see FIXME in GenerateProtocol.
774 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000775
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000776 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000777 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000778
779 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000780 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000782 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000783 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000784
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000785 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000786 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000788 /// GetNameForMethod - Return a name for the given method.
789 /// \param[out] NameOut - The return value.
790 void GetNameForMethod(const ObjCMethodDecl *OMD,
791 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000792 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000794 /// GetMethodVarName - Return a unique constant for the given
795 /// selector's name. The return value has type char *.
796 llvm::Constant *GetMethodVarName(Selector Sel);
797 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000798
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000799 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000800 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000802 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000803 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
804 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000805 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000806
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000807 /// GetPropertyName - Return a unique constant for the given
808 /// name. The return value has type char *.
809 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000810
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000811 // FIXME: This can be dropped once string functions are unified.
812 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
813 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000814
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000815 /// GetClassName - Return a unique constant for the given selector's
816 /// name. The return value has type char *.
817 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000818
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000819 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
820
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000821 /// BuildIvarLayout - Builds ivar layout bitmap for the class
822 /// implementation for the __strong or __weak case.
823 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000824 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
825 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000826
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000827 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000829 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000830 unsigned int BytePos, bool ForStrongLayout,
831 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000832 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000833 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000834 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000835 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000836 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000837 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000838
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000839 /// GetIvarLayoutName - Returns a unique constant for the given
840 /// ivar layout bitmap.
841 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
842 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000843
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000844 /// EmitPropertyList - Emit the given property list. The return
845 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000846 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000847 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000848 const ObjCContainerDecl *OCD,
849 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Bob Wilsondc8dab62011-11-30 01:57:58 +0000851 /// EmitProtocolMethodTypes - Generate the array of extended method type
852 /// strings. The return value has type Int8PtrPtrTy.
853 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000854 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000855 const ObjCCommonTypesHelper &ObjCTypes);
856
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000857 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000858 void PushProtocolProperties(
859 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
860 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
861 const Decl *Container,
862 const ObjCProtocolDecl *PROTO,
863 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000864
Fariborz Jahanianda320092009-01-29 19:24:30 +0000865 /// GetProtocolRef - Return a reference to the internal protocol
866 /// description, creating an empty one if it has not been
867 /// defined. The return value has type ProtocolPtrTy.
868 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000869
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000870 /// CreateMetadataVar - Create a global variable with internal
871 /// linkage for use by the Objective-C runtime.
872 ///
873 /// This is a convenience wrapper which not only creates the
874 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000875 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000876 ///
877 /// \param Name - The variable name.
878 /// \param Init - The variable initializer; this is also used to
879 /// define the type of the variable.
880 /// \param Section - The section the variable should go into, or 0.
881 /// \param Align - The alignment for the variable, or 0.
882 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000883 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000884 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000885 llvm::Constant *Init,
886 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000887 unsigned Align,
888 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000889
John McCall944c8432011-05-14 03:10:52 +0000890 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
891 ReturnValueSlot Return,
892 QualType ResultType,
893 llvm::Value *Sel,
894 llvm::Value *Arg0,
895 QualType Arg0Ty,
896 bool IsSuper,
897 const CallArgList &CallArgs,
898 const ObjCMethodDecl *OMD,
899 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000900
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000901 /// EmitImageInfo - Emit the image info marker used to encode some module
902 /// level information.
903 void EmitImageInfo();
904
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000905public:
Owen Anderson69243822009-07-13 04:10:07 +0000906 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000907 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000908
David Chisnall0d13f6f2010-01-23 02:40:42 +0000909 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000910
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000911 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
912 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913
Fariborz Jahanianda320092009-01-29 19:24:30 +0000914 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000915
Fariborz Jahanianda320092009-01-29 19:24:30 +0000916 /// GetOrEmitProtocol - Get the protocol object for the given
917 /// declaration, emitting it if necessary. The return value has type
918 /// ProtocolPtrTy.
919 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanianda320092009-01-29 19:24:30 +0000921 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
922 /// object for the given declaration, emitting it if needed. These
923 /// forward references will be filled in with empty bodies if no
924 /// definition is seen. The return value has type ProtocolPtrTy.
925 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000926 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
927 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000928
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000929};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000930
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000931class CGObjCMac : public CGObjCCommonMac {
932private:
933 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000934
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000935 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000937 void EmitModuleInfo();
938
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000939 /// EmitModuleSymols - Emit module symbols, the list of defined
940 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000941 llvm::Constant *EmitModuleSymbols();
942
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000943 /// FinishModule - Write out global data structures at the end of
944 /// processing a translation unit.
945 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000946
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000947 /// EmitClassExtension - Generate the class extension structure used
948 /// to store the weak ivar layout and properties. The return value
949 /// has type ClassExtensionPtrTy.
950 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
951
952 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
953 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000954 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000955 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000956
John McCallf85e1932011-06-15 23:02:42 +0000957 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
958 IdentifierInfo *II);
959
960 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
961
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000962 /// EmitSuperClassRef - Emits reference to class's main metadata class.
963 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000964
965 /// EmitIvarList - Emit the ivar list for the given
966 /// implementation. If ForClass is true the list of class ivars
967 /// (i.e. metaclass ivars) is emitted, otherwise the list of
968 /// interface ivars will be emitted. The return value has type
969 /// IvarListPtrTy.
970 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000971 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000972
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000973 /// EmitMetaClass - Emit a forward reference to the class structure
974 /// for the metaclass of the given interface. The return value has
975 /// type ClassPtrTy.
976 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
977
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000978 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000979 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000980 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
981 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +0000982 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000983
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000984 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000985
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000986 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987
988 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000989 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000990 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000991 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +0000992 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000993
994 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000995 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000996 /// - TypeName: The name for the type containing the methods.
997 /// - IsProtocol: True iff these methods are for a protocol.
998 /// - ClassMethds: True iff these are class methods.
999 /// - Required: When true, only "required" methods are
1000 /// listed. Similarly, when false only "optional" methods are
1001 /// listed. For classes this should always be true.
1002 /// - begin, end: The method list to output.
1003 ///
1004 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001005 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001006 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001007 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001008
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001009 /// GetOrEmitProtocol - Get the protocol object for the given
1010 /// declaration, emitting it if necessary. The return value has type
1011 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001012 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001013
1014 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1015 /// object for the given declaration, emitting it if needed. These
1016 /// forward references will be filled in with empty bodies if no
1017 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001018 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001019
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020 /// EmitProtocolExtension - Generate the protocol extension
1021 /// structure used to store optional instance and class methods, and
1022 /// protocol properties. The return value has type
1023 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001024 llvm::Constant *
1025 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001026 ArrayRef<llvm::Constant*> OptInstanceMethods,
1027 ArrayRef<llvm::Constant*> OptClassMethods,
1028 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001029
1030 /// EmitProtocolList - Generate the list of referenced
1031 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001032 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001033 ObjCProtocolDecl::protocol_iterator begin,
1034 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001035
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001036 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1037 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001038 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1039 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001040
1041public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001042 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001043
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001044 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001045
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001046 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001047 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001048 QualType ResultType,
1049 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001050 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001051 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001052 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001053 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001054
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001055 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001056 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001057 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001058 QualType ResultType,
1059 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001060 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001061 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001062 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001063 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001064 const CallArgList &CallArgs,
1065 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001066
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001067 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001068 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001069
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001070 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1071 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001072
1073 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1074 /// untyped one.
1075 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1076 const ObjCMethodDecl *Method);
1077
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001078 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001079
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001080 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001081
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001082 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001083
David Chisnall29254f42012-01-31 18:59:20 +00001084 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1085
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001086 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001087 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001088
Chris Lattner74391b42009-03-22 21:03:39 +00001089 virtual llvm::Constant *GetPropertyGetFunction();
1090 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001091 virtual llvm::Constant *GetGetStructFunction();
1092 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001093 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001094 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001095
John McCallf1549f62010-07-06 01:34:17 +00001096 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1097 const ObjCAtTryStmt &S);
1098 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1099 const ObjCAtSynchronizedStmt &S);
1100 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001101 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1102 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001103 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001104 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001105 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001106 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001107 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001108 llvm::Value *src, llvm::Value *dest,
1109 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001110 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001111 llvm::Value *src, llvm::Value *dest,
1112 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001113 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1114 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001115 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1116 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001117 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001118
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001119 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1120 QualType ObjectTy,
1121 llvm::Value *BaseValue,
1122 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001123 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001124 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001125 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001126 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001127
1128 /// GetClassGlobal - Return the global variable for the Objective-C
1129 /// class of the given name.
1130 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001131 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001132 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001133};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001134
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001135class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001136private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001137 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001138 llvm::GlobalVariable* ObjCEmptyCacheVar;
1139 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001140
Daniel Dunbar11394522009-04-18 08:51:00 +00001141 /// SuperClassReferences - uniqued super class references.
1142 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001143
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001144 /// MetaClassReferences - uniqued meta class references.
1145 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001146
1147 /// EHTypeReferences - uniqued class ehtype references.
1148 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
John McCall944c8432011-05-14 03:10:52 +00001150 /// VTableDispatchMethods - List of methods for which we generate
1151 /// vtable-based message dispatch.
1152 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001153
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001154 /// DefinedMetaClasses - List of defined meta-classes.
1155 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1156
John McCall944c8432011-05-14 03:10:52 +00001157 /// isVTableDispatchedSelector - Returns true if SEL is a
1158 /// vtable-based selector.
1159 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001160
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001161 /// FinishNonFragileABIModule - Write out global data structures at the end of
1162 /// processing a translation unit.
1163 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001164
Daniel Dunbar463b8762009-05-15 21:48:48 +00001165 /// AddModuleClassList - Add the given list of class pointers to the
1166 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001167 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001168 const char *SymbolName,
1169 const char *SectionName);
1170
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001171 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1172 unsigned InstanceStart,
1173 unsigned InstanceSize,
1174 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001175 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001177 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001178 llvm::Constant *ClassRoGV,
1179 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001180
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001181 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001182
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001183 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001184
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001185 /// EmitMethodList - Emit the method list for the given
1186 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001187 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001188 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001189 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001190 /// EmitIvarList - Emit the ivar list for the given
1191 /// implementation. If ForClass is true the list of class ivars
1192 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1193 /// interface ivars will be emitted. The return value has type
1194 /// IvarListnfABIPtrTy.
1195 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001196
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001197 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001198 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001199 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
Fariborz Jahanianda320092009-01-29 19:24:30 +00001201 /// GetOrEmitProtocol - Get the protocol object for the given
1202 /// declaration, emitting it if necessary. The return value has type
1203 /// ProtocolPtrTy.
1204 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001205
Fariborz Jahanianda320092009-01-29 19:24:30 +00001206 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1207 /// object for the given declaration, emitting it if needed. These
1208 /// forward references will be filled in with empty bodies if no
1209 /// definition is seen. The return value has type ProtocolPtrTy.
1210 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001211
Fariborz Jahanianda320092009-01-29 19:24:30 +00001212 /// EmitProtocolList - Generate the list of referenced
1213 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001214 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001215 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001216 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001217
John McCall944c8432011-05-14 03:10:52 +00001218 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1219 ReturnValueSlot Return,
1220 QualType ResultType,
1221 Selector Sel,
1222 llvm::Value *Receiver,
1223 QualType Arg0Ty,
1224 bool IsSuper,
1225 const CallArgList &CallArgs,
1226 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001227
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001228 /// GetClassGlobal - Return the global variable for the Objective-C
1229 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001230 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001231
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001232 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001233 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001235 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001236
1237 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1238 IdentifierInfo *II);
1239
1240 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241
Daniel Dunbar11394522009-04-18 08:51:00 +00001242 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1243 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1245 const ObjCInterfaceDecl *ID);
1246
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001247 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1248 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001249 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001250 const ObjCInterfaceDecl *ID);
1251
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001252 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1253 /// the given ivar.
1254 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001255 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001256 const ObjCInterfaceDecl *ID,
1257 const ObjCIvarDecl *Ivar);
1258
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001259 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1260 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001261 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1262 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001263
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001264 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001265 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001266 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001267 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001268
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001269 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001270 return "OBJC_METACLASS_$_";
1271 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001272
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001273 const char *getClassSymbolPrefix() const {
1274 return "OBJC_CLASS_$_";
1275 }
1276
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001277 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001278 uint32_t &InstanceStart,
1279 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001280
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001281 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001282 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001283 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1284 return CGM.getContext().Selectors.getSelector(0, &II);
1285 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001286
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001287 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001288 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1289 return CGM.getContext().Selectors.getSelector(1, &II);
1290 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001291
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001292 /// ImplementationIsNonLazy - Check whether the given category or
1293 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001294 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001295
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001296public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001297 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001298 // FIXME. All stubs for now!
1299 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001300
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001301 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001302 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001303 QualType ResultType,
1304 Selector Sel,
1305 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001306 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001307 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001308 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001309
1310 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001311 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001312 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001313 QualType ResultType,
1314 Selector Sel,
1315 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001316 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001317 llvm::Value *Receiver,
1318 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001319 const CallArgList &CallArgs,
1320 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001321
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001322 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001323 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001325 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1326 bool lvalue = false)
1327 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001328
1329 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1330 /// untyped one.
1331 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1332 const ObjCMethodDecl *Method)
1333 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001334
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001335 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001336
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001337 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001338
1339 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1340
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001341 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001342 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001343
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001344 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001345
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001346 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001347 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001348 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349 virtual llvm::Constant *GetPropertySetFunction() {
1350 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001351 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001352
David Chisnall8fac25d2010-12-26 22:13:16 +00001353 virtual llvm::Constant *GetSetStructFunction() {
1354 return ObjCTypes.getCopyStructFn();
1355 }
1356 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001357 return ObjCTypes.getCopyStructFn();
1358 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001359 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1360 return ObjCTypes.getCppAtomicObjectFunction();
1361 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001362
Chris Lattner74391b42009-03-22 21:03:39 +00001363 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001364 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001365 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001366
John McCallf1549f62010-07-06 01:34:17 +00001367 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1368 const ObjCAtTryStmt &S);
1369 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1370 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001371 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001372 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001373 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001374 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001375 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001376 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001377 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001378 llvm::Value *src, llvm::Value *dest,
1379 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001380 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001381 llvm::Value *src, llvm::Value *dest,
1382 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001383 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001384 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001385 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1386 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001387 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001388 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1389 QualType ObjectTy,
1390 llvm::Value *BaseValue,
1391 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001392 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001393 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001394 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001395 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001396};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001397
1398/// A helper class for performing the null-initialization of a return
1399/// value.
1400struct NullReturnState {
1401 llvm::BasicBlock *NullBB;
1402 llvm::BasicBlock *callBB;
1403 NullReturnState() : NullBB(0), callBB(0) {}
1404
1405 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1406 // Make blocks for the null-init and call edges.
1407 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1408 callBB = CGF.createBasicBlock("msgSend.call");
1409
1410 // Check for a null receiver and, if there is one, jump to the
1411 // null-init test.
1412 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1413 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1414
1415 // Otherwise, start performing the call.
1416 CGF.EmitBlock(callBB);
1417 }
1418
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001419 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1420 const CallArgList &CallArgs,
1421 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001422 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001423
1424 llvm::Value *NullInitPtr = 0;
1425 if (result.isScalar() && !resultType->isVoidType()) {
1426 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1427 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1428 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001429
1430 // Finish the call path.
1431 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1432 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1433
1434 // Emit the null-init block and perform the null-initialization there.
1435 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001436
1437 // Release consumed arguments along the null-receiver path.
1438 if (Method) {
1439 CallArgList::const_iterator I = CallArgs.begin();
1440 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1441 e = Method->param_end(); i != e; ++i, ++I) {
1442 const ParmVarDecl *ParamDecl = (*i);
1443 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1444 RValue RV = I->RV;
1445 assert(RV.isScalar() &&
1446 "NullReturnState::complete - arg not on object");
1447 CGF.EmitARCRelease(RV.getScalarVal(), true);
1448 }
1449 }
1450 }
1451
1452 if (result.isScalar()) {
1453 if (NullInitPtr)
1454 CGF.EmitNullInitialization(NullInitPtr, resultType);
1455 // Jump to the continuation block.
1456 CGF.EmitBlock(contBB);
1457 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1458 : result;
1459 }
1460
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001461 if (!resultType->isAnyComplexType()) {
1462 assert(result.isAggregate() && "null init of non-aggregate result?");
1463 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1464 // Jump to the continuation block.
1465 CGF.EmitBlock(contBB);
1466 return result;
1467 }
1468
1469 // _Complex type
1470 // FIXME. Now easy to handle any other scalar type whose result is returned
1471 // in memory due to ABI limitations.
1472 CGF.EmitBlock(contBB);
1473 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1474 llvm::Type *MemberType = CallCV.first->getType();
1475 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1476 // Create phi instruction for scalar complex value.
1477 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1478 PHIReal->addIncoming(ZeroCV, NullBB);
1479 PHIReal->addIncoming(CallCV.first, callBB);
1480 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1481 PHIImag->addIncoming(ZeroCV, NullBB);
1482 PHIImag->addIncoming(CallCV.second, callBB);
1483 return RValue::getComplex(PHIReal, PHIImag);
1484 }
1485};
1486
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001487} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001488
1489/* *** Helper Functions *** */
1490
1491/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001492static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001493 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001494 unsigned idx0,
1495 unsigned idx1) {
1496 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001497 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1498 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001499 };
Jay Foada5c04342011-07-21 14:31:17 +00001500 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001501}
1502
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001503/// hasObjCExceptionAttribute - Return true if this class or any super
1504/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001505static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001506 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001507 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001508 return true;
1509 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001510 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001511 return false;
1512}
1513
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001514/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001515
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001516CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001517 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001518 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001519 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001520}
1521
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001522/// GetClass - Return a reference to the class for the given interface
1523/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001524llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001525 const ObjCInterfaceDecl *ID) {
1526 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001527}
1528
1529/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001530llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1531 bool lval) {
1532 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001533}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001534llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001535 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001536 return EmitSelector(Builder, Method->getSelector());
1537}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001538
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001539llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001540 if (T->isObjCIdType() ||
1541 T->isObjCQualifiedIdType()) {
1542 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001543 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001544 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001545 if (T->isObjCClassType() ||
1546 T->isObjCQualifiedClassType()) {
1547 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001548 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001549 }
1550 if (T->isObjCObjectPointerType())
1551 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1552
John McCall5a180392010-07-24 00:37:23 +00001553 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001554}
1555
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001556/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001557/*
1558 struct __builtin_CFString {
1559 const int *isa; // point to __CFConstantStringClassReference
1560 int flags;
1561 const char *str;
1562 long length;
1563 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001564*/
1565
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001566/// or Generate a constant NSString object.
1567/*
1568 struct __builtin_NSString {
1569 const int *isa; // point to __NSConstantStringClassReference
1570 const char *str;
1571 unsigned int length;
1572 };
1573*/
1574
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001575llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001576 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001577 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1578 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001579 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001580}
1581
1582/// Generates a message send where the super is the receiver. This is
1583/// a message send to self with special delivery semantics indicating
1584/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001585CodeGen::RValue
1586CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001587 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001588 QualType ResultType,
1589 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001590 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001591 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001592 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001593 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001594 const CodeGen::CallArgList &CallArgs,
1595 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001596 // Create and init a super structure; this is a (receiver, class)
1597 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001598 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001599 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001600 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001601 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001602 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001603 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001604
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001605 // If this is a class message the metaclass is passed as the target.
1606 llvm::Value *Target;
1607 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001608 if (isCategoryImpl) {
1609 // Message sent to 'super' in a class method defined in a category
1610 // implementation requires an odd treatment.
1611 // If we are in a class method, we must retrieve the
1612 // _metaclass_ for the current class, pointed at by
1613 // the class's "isa" pointer. The following assumes that
1614 // isa" is the first ivar in a class (which it must be).
1615 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1616 Target = CGF.Builder.CreateStructGEP(Target, 0);
1617 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001618 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001619 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1620 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1621 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1622 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001623 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001624 }
1625 else if (isCategoryImpl)
1626 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1627 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001628 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1629 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1630 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001631 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001632 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1633 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001634 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001635 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001636 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001637 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001638 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001639 return EmitMessageSend(CGF, Return, ResultType,
1640 EmitSelector(CGF.Builder, Sel),
1641 ObjCSuper, ObjCTypes.SuperPtrCTy,
1642 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001643}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001644
1645/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001646CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001647 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001648 QualType ResultType,
1649 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001650 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001651 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001652 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001653 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001654 return EmitMessageSend(CGF, Return, ResultType,
1655 EmitSelector(CGF.Builder, Sel),
1656 Receiver, CGF.getContext().getObjCIdType(),
1657 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001658}
1659
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001660CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001661CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1662 ReturnValueSlot Return,
1663 QualType ResultType,
1664 llvm::Value *Sel,
1665 llvm::Value *Arg0,
1666 QualType Arg0Ty,
1667 bool IsSuper,
1668 const CallArgList &CallArgs,
1669 const ObjCMethodDecl *Method,
1670 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001671 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001672 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001673 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001674 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1675 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001676 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001677
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001678 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001679 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001680 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001681 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001682 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001683
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001684 if (Method)
1685 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1686 CGM.getContext().getCanonicalType(ResultType) &&
1687 "Result type mismatch!");
1688
John McCallcba681a2011-05-14 21:12:11 +00001689 NullReturnState nullReturn;
1690
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001691 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001692 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001693 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001694 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001695 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001696 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1697 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1698 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001699 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1700 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1701 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001702 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001703 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001704 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001705 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001706
1707 bool requiresnullCheck = false;
1708 if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1709 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1710 e = Method->param_end(); i != e; ++i) {
1711 const ParmVarDecl *ParamDecl = (*i);
1712 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1713 if (!nullReturn.NullBB)
1714 nullReturn.init(CGF, Arg0);
1715 requiresnullCheck = true;
1716 break;
1717 }
1718 }
1719
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001720 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001721 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001722 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1723 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001724}
1725
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001726static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1727 if (FQT.isObjCGCStrong())
1728 return Qualifiers::Strong;
1729
John McCallf85e1932011-06-15 23:02:42 +00001730 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001731 return Qualifiers::Weak;
1732
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001733 // check for __unsafe_unretained
1734 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1735 return Qualifiers::GCNone;
1736
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001737 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1738 return Qualifiers::Strong;
1739
1740 if (const PointerType *PT = FQT->getAs<PointerType>())
1741 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1742
1743 return Qualifiers::GCNone;
1744}
1745
John McCall6b5a61b2011-02-07 10:33:21 +00001746llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1747 const CGBlockInfo &blockInfo) {
Chris Lattner8b418682012-02-07 00:39:47 +00001748 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall6b5a61b2011-02-07 10:33:21 +00001749
Douglas Gregore289d812011-09-13 17:21:33 +00001750 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001751 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001752 return nullPtr;
1753
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001754 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001755 SkipIvars.clear();
1756 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001757 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1758 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001759
Fariborz Jahanian81979822010-09-09 00:21:45 +00001760 // __isa is the first field in block descriptor and must assume by runtime's
1761 // convention that it is GC'able.
1762 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001763
1764 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1765
1766 // Calculate the basic layout of the block structure.
1767 const llvm::StructLayout *layout =
1768 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1769
1770 // Ignore the optional 'this' capture: C++ objects are not assumed
1771 // to be GC'ed.
1772
1773 // Walk the captured variables.
1774 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1775 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1776 const VarDecl *variable = ci->getVariable();
1777 QualType type = variable->getType();
1778
1779 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1780
1781 // Ignore constant captures.
1782 if (capture.isConstant()) continue;
1783
1784 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1785
1786 // __block variables are passed by their descriptor address.
1787 if (ci->isByRef()) {
1788 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001789 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001790 }
1791
1792 assert(!type->isArrayType() && "array variable should not be caught");
1793 if (const RecordType *record = type->getAs<RecordType>()) {
1794 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001795 continue;
1796 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001797
John McCall6b5a61b2011-02-07 10:33:21 +00001798 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1799 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1800
1801 if (GCAttr == Qualifiers::Strong)
1802 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1803 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001804 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001805 SkipIvars.push_back(GC_IVAR(fieldOffset,
1806 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001807 }
1808
1809 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001810 return nullPtr;
1811
1812 // Sort on byte position; captures might not be allocated in order,
1813 // and unions can do funny things.
1814 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1815 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001816
1817 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001818 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001819 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1820 printf("\n block variable layout for block: ");
1821 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001822 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001823 if (!(s[i] & 0xf0))
1824 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1825 else
1826 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1827 printf("\n");
1828 }
1829
1830 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001831}
1832
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001833llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001834 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001835 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001836 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001837 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1838
Owen Anderson3c4972d2009-07-29 18:54:39 +00001839 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001840 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001841}
1842
Fariborz Jahanianda320092009-01-29 19:24:30 +00001843void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001844 // FIXME: We shouldn't need this, the protocol decl should contain enough
1845 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001846 DefinedProtocols.insert(PD->getIdentifier());
1847
1848 // If we have generated a forward reference to this protocol, emit
1849 // it now. Otherwise do nothing, the protocol objects are lazily
1850 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001851 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001852 GetOrEmitProtocol(PD);
1853}
1854
Fariborz Jahanianda320092009-01-29 19:24:30 +00001855llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001856 if (DefinedProtocols.count(PD->getIdentifier()))
1857 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001858
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001859 return GetOrEmitProtocolRef(PD);
1860}
1861
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001862/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1864struct _objc_protocol {
1865struct _objc_protocol_extension *isa;
1866char *protocol_name;
1867struct _objc_protocol_list *protocol_list;
1868struct _objc__method_prototype_list *instance_methods;
1869struct _objc__method_prototype_list *class_methods
1870};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001871
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001872See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001874llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1875 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1876
1877 // Early exit if a defining object has already been generated.
1878 if (Entry && Entry->hasInitializer())
1879 return Entry;
1880
Douglas Gregor1d784b22012-01-01 19:51:50 +00001881 // Use the protocol definition, if there is one.
1882 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1883 PD = Def;
1884
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001885 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001886 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001887 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1888
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001889 // Construct method lists.
1890 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1891 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001892 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001893 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001894 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001895 ObjCMethodDecl *MD = *i;
1896 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001897 if (!C)
1898 return GetOrEmitProtocolRef(PD);
1899
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001900 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1901 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001902 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001903 } else {
1904 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001905 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001906 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001907 }
1908
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001909 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001910 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001911 ObjCMethodDecl *MD = *i;
1912 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001913 if (!C)
1914 return GetOrEmitProtocolRef(PD);
1915
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001916 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1917 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001918 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001919 } else {
1920 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001921 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001922 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001923 }
1924
Bob Wilsondc8dab62011-11-30 01:57:58 +00001925 MethodTypesExt.insert(MethodTypesExt.end(),
1926 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1927
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001928 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001929 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1930 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001931 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001932 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001933 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001934 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001935 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001936 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001937 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001938 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001939 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001940 ClassMethods)
1941 };
Owen Anderson08e25242009-07-27 22:29:56 +00001942 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001943 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001944
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001946 // Already created, fix the linkage and update the initializer.
1947 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001948 Entry->setInitializer(Init);
1949 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001951 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001952 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001953 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001954 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001955 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956 // FIXME: Is this necessary? Why only for protocol?
1957 Entry->setAlignment(4);
1958 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001959 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001960
1961 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001962}
1963
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001964llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1966
1967 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001968 // We use the initializer as a marker of whether this is a forward
1969 // reference or not. At module finalization we add the empty
1970 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001971 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001972 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001973 llvm::GlobalValue::ExternalLinkage,
1974 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001975 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001976 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001977 // FIXME: Is this necessary? Why only for protocol?
1978 Entry->setAlignment(4);
1979 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001980
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001981 return Entry;
1982}
1983
1984/*
1985 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001986 uint32_t size;
1987 struct objc_method_description_list *optional_instance_methods;
1988 struct objc_method_description_list *optional_class_methods;
1989 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001990 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001991 };
1992*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001993llvm::Constant *
1994CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001995 ArrayRef<llvm::Constant*> OptInstanceMethods,
1996 ArrayRef<llvm::Constant*> OptClassMethods,
1997 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001998 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001999 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002000 llvm::Constant *Values[] = {
2001 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002002 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002003 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002004 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002005 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002006 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002007 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002008 OptClassMethods),
2009 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002010 ObjCTypes),
2011 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2012 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002013 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002014
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002015 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002016 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002017 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002018 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002019
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002020 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002021 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002023 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002024 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002025 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002026 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002027}
2028
2029/*
2030 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002031 struct objc_protocol_list *next;
2032 long count;
2033 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002034 };
2035*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002036llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002037CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002038 ObjCProtocolDecl::protocol_iterator begin,
2039 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002040 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002041
Daniel Dunbardbc93372008-08-21 21:57:41 +00002042 for (; begin != end; ++begin)
2043 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002044
2045 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002046 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002047 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002048
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002049 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002050 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002051
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002052 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002053 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002054 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002055 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002056 ProtocolRefs.size() - 1);
2057 Values[2] =
2058 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2059 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002060 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002061
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002062 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002063 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002064 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002065 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002066 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002067}
2068
Bill Wendlingbb028552012-02-07 09:25:09 +00002069void CGObjCCommonMac::
2070PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002071 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002072 const Decl *Container,
2073 const ObjCProtocolDecl *PROTO,
2074 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002075 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2076 E = PROTO->protocol_end(); P != E; ++P)
2077 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2078 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2079 E = PROTO->prop_end(); I != E; ++I) {
2080 const ObjCPropertyDecl *PD = *I;
2081 if (!PropertySet.insert(PD->getIdentifier()))
2082 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002083 llvm::Constant *Prop[] = {
2084 GetPropertyName(PD->getIdentifier()),
2085 GetPropertyTypeString(PD, Container)
2086 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002087 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2088 }
2089}
2090
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002091/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002092 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002093 const char * const name;
2094 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002095 };
2096
2097 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002098 uint32_t entsize; // sizeof (struct _objc_property)
2099 uint32_t prop_count;
2100 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002101 };
2102*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002103llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104 const Decl *Container,
2105 const ObjCContainerDecl *OCD,
2106 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002107 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002108 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2110 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002111 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002112 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002113 llvm::Constant *Prop[] = {
2114 GetPropertyName(PD->getIdentifier()),
2115 GetPropertyTypeString(PD, Container)
2116 };
Owen Anderson08e25242009-07-27 22:29:56 +00002117 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002118 Prop));
2119 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002120 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002121 for (ObjCInterfaceDecl::all_protocol_iterator
2122 P = OID->all_referenced_protocol_begin(),
2123 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002124 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2125 ObjCTypes);
2126 }
2127 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2128 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2129 E = CD->protocol_end(); P != E; ++P)
2130 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2131 ObjCTypes);
2132 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002133
2134 // Return null for empty list.
2135 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002136 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002137
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002138 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002139 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002140 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002141 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2142 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002143 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002144 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002145 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002146 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002147
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002148 llvm::GlobalVariable *GV =
2149 CreateMetadataVar(Name, Init,
2150 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002151 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002152 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002153 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002154 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002155}
2156
Bill Wendlingbb028552012-02-07 09:25:09 +00002157llvm::Constant *
2158CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2159 ArrayRef<llvm::Constant*> MethodTypes,
2160 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002161 // Return null for empty list.
2162 if (MethodTypes.empty())
2163 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2164
2165 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2166 MethodTypes.size());
2167 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2168
2169 llvm::GlobalVariable *GV =
2170 CreateMetadataVar(Name, Init,
2171 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2172 (ObjCABI == 2) ? 8 : 4,
2173 true);
2174 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2175}
2176
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002177/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002178 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002179 int count;
2180 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002181 };
2182*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002183llvm::Constant *
2184CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002185 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002187 ObjCTypes.SelectorPtrTy),
2188 GetMethodVarType(MD)
2189 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002190 if (!Desc[1])
2191 return 0;
2192
Owen Anderson08e25242009-07-27 22:29:56 +00002193 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002194 Desc);
2195}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002196
Bill Wendlingbb028552012-02-07 09:25:09 +00002197llvm::Constant *
2198CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2199 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002200 // Return null for empty list.
2201 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002202 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002203
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002204 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002205 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002206 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002207 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002208 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002209 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002210
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002211 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002212 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002213 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002214}
2215
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002216/*
2217 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002218 char *category_name;
2219 char *class_name;
2220 struct _objc_method_list *instance_methods;
2221 struct _objc_method_list *class_methods;
2222 struct _objc_protocol_list *protocols;
2223 uint32_t size; // <rdar://4585769>
2224 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002225 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002226*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002227void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002228 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002229
Mike Stumpf5408fe2009-05-16 07:57:57 +00002230 // FIXME: This is poor design, the OCD should have a pointer to the category
2231 // decl. Additionally, note that Category can be null for the @implementation
2232 // w/o an @interface case. Sema should just create one for us as it does for
2233 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002234 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002235 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002236 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002237
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002238 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002239 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2240 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002241
Bill Wendling3964e622012-02-09 22:16:49 +00002242 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002243 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002244 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002245 // Instance methods should always be defined.
2246 InstanceMethods.push_back(GetMethodConstant(*i));
2247 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002248 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002249 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002250 // Class methods should always be defined.
2251 ClassMethods.push_back(GetMethodConstant(*i));
2252 }
2253
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002254 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002255 Values[0] = GetClassName(OCD->getIdentifier());
2256 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002257 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002258 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002259 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002260 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002261 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002262 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002263 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002264 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002265 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002266 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002267 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002268 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002269 Category->protocol_begin(),
2270 Category->protocol_end());
2271 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002272 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002273 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002274 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002275
2276 // If there is no category @interface then there can be no properties.
2277 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002278 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002279 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002280 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002281 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002282 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002283
Owen Anderson08e25242009-07-27 22:29:56 +00002284 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002285 Values);
2286
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002287 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002288 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002289 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002290 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002291 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002292 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002293 // method definition entries must be clear for next implementation.
2294 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002295}
2296
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297// FIXME: Get from somewhere?
2298enum ClassFlags {
2299 eClassFlags_Factory = 0x00001,
2300 eClassFlags_Meta = 0x00002,
2301 // <rdr://5142207>
2302 eClassFlags_HasCXXStructors = 0x02000,
2303 eClassFlags_Hidden = 0x20000,
2304 eClassFlags_ABI2_Hidden = 0x00010,
2305 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2306};
2307
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308/*
2309 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002310 Class isa;
2311 Class super_class;
2312 const char *name;
2313 long version;
2314 long info;
2315 long instance_size;
2316 struct _objc_ivar_list *ivars;
2317 struct _objc_method_list *methods;
2318 struct _objc_cache *cache;
2319 struct _objc_protocol_list *protocols;
2320 // Objective-C 1.0 extensions (<rdr://4585769>)
2321 const char *ivar_layout;
2322 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 };
2324
2325 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002326*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002328 DefinedSymbols.insert(ID->getIdentifier());
2329
Chris Lattner8ec03f52008-11-24 03:54:41 +00002330 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002331 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002332 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002334 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002335 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002336 Interface->all_referenced_protocol_begin(),
2337 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002338 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002339 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002340 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002341 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002342 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343
2344 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002345 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 Flags |= eClassFlags_Hidden;
2347
Bill Wendling3964e622012-02-09 22:16:49 +00002348 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002349 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002350 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002351 // Instance methods should always be defined.
2352 InstanceMethods.push_back(GetMethodConstant(*i));
2353 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002354 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002355 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002356 // Class methods should always be defined.
2357 ClassMethods.push_back(GetMethodConstant(*i));
2358 }
2359
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002360 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002361 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002362 ObjCPropertyImplDecl *PID = *i;
2363
2364 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2365 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2366
2367 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2368 if (llvm::Constant *C = GetMethodConstant(MD))
2369 InstanceMethods.push_back(C);
2370 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2371 if (llvm::Constant *C = GetMethodConstant(MD))
2372 InstanceMethods.push_back(C);
2373 }
2374 }
2375
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002376 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002377 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002378 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002379 // Record a reference to the super class.
2380 LazySymbols.insert(Super->getIdentifier());
2381
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002383 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002384 ObjCTypes.ClassPtrTy);
2385 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002386 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002387 }
2388 Values[ 2] = GetClassName(ID->getIdentifier());
2389 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002390 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2391 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2392 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002393 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002394 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002395 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002396 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002397 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002398 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002399 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002401 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002402 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002403 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002404 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002405 std::string Name("\01L_OBJC_CLASS_");
2406 Name += ClassName;
2407 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2408 // Check for a forward reference.
2409 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2410 if (GV) {
2411 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2412 "Forward metaclass reference has incorrect type.");
2413 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2414 GV->setInitializer(Init);
2415 GV->setSection(Section);
2416 GV->setAlignment(4);
2417 CGM.AddUsedGlobal(GV);
2418 }
2419 else
2420 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002422 // method definition entries must be clear for next implementation.
2423 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002424}
2425
2426llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2427 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00002428 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002430 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431
John McCall1fb0caa2010-10-22 21:05:15 +00002432 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002434
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002435 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002436 // The isa for the metaclass is the root of the hierarchy.
2437 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2438 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2439 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002441 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002443 // The super class for the metaclass is emitted as the name of the
2444 // super class. The runtime fixes this up to point to the
2445 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002447 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002448 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002449 ObjCTypes.ClassPtrTy);
2450 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002451 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 }
2453 Values[ 2] = GetClassName(ID->getIdentifier());
2454 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002455 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2456 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2457 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002458 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002459 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002460 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002461 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002462 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002463 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002464 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002465 Values[ 9] = Protocols;
2466 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002467 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002468 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002469 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002470 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471 Values);
2472
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002473 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002474 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002475
2476 // Check for a forward reference.
2477 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2478 if (GV) {
2479 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2480 "Forward metaclass reference has incorrect type.");
2481 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2482 GV->setInitializer(Init);
2483 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002484 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002485 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002486 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002487 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002489 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002490 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002491
2492 return GV;
2493}
2494
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002495llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002496 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002497
Mike Stumpf5408fe2009-05-16 07:57:57 +00002498 // FIXME: Should we look these up somewhere other than the module. Its a bit
2499 // silly since we only generate these while processing an implementation, so
2500 // exactly one pointer would work if know when we entered/exitted an
2501 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002502
2503 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002504 // Previously, metaclass with internal linkage may have been defined.
2505 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002506 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2507 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002508 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2509 "Forward metaclass reference has incorrect type.");
2510 return GV;
2511 } else {
2512 // Generate as an external reference to keep a consistent
2513 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002514 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002515 llvm::GlobalValue::ExternalLinkage,
2516 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002517 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002518 }
2519}
2520
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002521llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2522 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2523
2524 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2525 true)) {
2526 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2527 "Forward class metadata reference has incorrect type.");
2528 return GV;
2529 } else {
2530 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2531 llvm::GlobalValue::ExternalLinkage,
2532 0,
2533 Name);
2534 }
2535}
2536
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002537/*
2538 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002539 uint32_t size;
2540 const char *weak_ivar_layout;
2541 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002542 };
2543*/
2544llvm::Constant *
2545CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002546 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002547 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002548
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002549 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002550 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002551 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002552 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002553 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002554
2555 // Return null if no extension bits are used.
2556 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002557 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002558
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002559 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002560 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002561 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002562 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002563 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002564}
2565
2566/*
2567 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002568 char *ivar_name;
2569 char *ivar_type;
2570 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002571 };
2572
2573 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002574 int ivar_count;
2575 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002576 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002577*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002578llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002579 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002580 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002581
2582 // When emitting the root class GCC emits ivar entries for the
2583 // actual class structure. It is not clear if we need to follow this
2584 // behavior; for now lets try and get away with not doing it. If so,
2585 // the cleanest solution would be to make up an ObjCInterfaceDecl
2586 // for the class.
2587 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002588 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002589
Jordy Rosedb8264e2011-07-22 02:08:32 +00002590 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002591
Jordy Rosedb8264e2011-07-22 02:08:32 +00002592 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002593 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002594 // Ignore unnamed bit-fields.
2595 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002596 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002597 llvm::Constant *Ivar[] = {
2598 GetMethodVarName(IVD->getIdentifier()),
2599 GetMethodVarType(IVD),
2600 llvm::ConstantInt::get(ObjCTypes.IntTy,
2601 ComputeIvarBaseOffset(CGM, OID, IVD))
2602 };
Owen Anderson08e25242009-07-27 22:29:56 +00002603 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002604 }
2605
2606 // Return null for empty list.
2607 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002608 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002609
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002610 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002611 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002612 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002613 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002614 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002615 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002616
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002617 llvm::GlobalVariable *GV;
2618 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002619 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002620 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002621 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002622 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002623 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002624 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002625 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002626 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002627}
2628
2629/*
2630 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002631 SEL method_name;
2632 char *method_types;
2633 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002634 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002635
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002636 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002637 struct objc_method_list *obsolete;
2638 int count;
2639 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002640 };
2641*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002642
2643/// GetMethodConstant - Return a struct objc_method constant for the
2644/// given method if it has been defined. The result is null if the
2645/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002646llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002647 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002648 if (!Fn)
2649 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002650
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002651 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002652 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002653 ObjCTypes.SelectorPtrTy),
2654 GetMethodVarType(MD),
2655 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2656 };
Owen Anderson08e25242009-07-27 22:29:56 +00002657 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002658}
2659
Chris Lattner5f9e2722011-07-23 10:55:15 +00002660llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002661 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00002662 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002663 // Return null for empty list.
2664 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002665 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002666
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002667 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002668 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002669 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002670 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002671 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002672 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002673 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002674
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002675 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002676 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002677}
2678
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002679llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002680 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002681 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002682 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002683
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002684 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002685 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002686 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002687 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002688 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002689 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002690 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002691 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002692 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002693
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002694 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002695}
2696
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002697llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002698CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002699 llvm::Constant *Init,
2700 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002701 unsigned Align,
2702 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002703 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002704 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002705 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002706 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002707 if (Section)
2708 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002709 if (Align)
2710 GV->setAlignment(Align);
2711 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002712 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002713 return GV;
2714}
2715
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002716llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002717 // Abuse this interface function as a place to finalize.
2718 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002719 return NULL;
2720}
2721
Chris Lattner74391b42009-03-22 21:03:39 +00002722llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002723 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002724}
2725
Chris Lattner74391b42009-03-22 21:03:39 +00002726llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002727 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002728}
2729
David Chisnall8fac25d2010-12-26 22:13:16 +00002730llvm::Constant *CGObjCMac::GetGetStructFunction() {
2731 return ObjCTypes.getCopyStructFn();
2732}
2733llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002734 return ObjCTypes.getCopyStructFn();
2735}
2736
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002737llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2738 return ObjCTypes.getCppAtomicObjectFunction();
2739}
2740
Chris Lattner74391b42009-03-22 21:03:39 +00002741llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002742 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002743}
2744
John McCallf1549f62010-07-06 01:34:17 +00002745void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2746 return EmitTryOrSynchronizedStmt(CGF, S);
2747}
2748
2749void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2750 const ObjCAtSynchronizedStmt &S) {
2751 return EmitTryOrSynchronizedStmt(CGF, S);
2752}
2753
John McCallcc505292010-07-21 06:59:36 +00002754namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002755 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002756 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002757 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002758 llvm::Value *CallTryExitVar;
2759 llvm::Value *ExceptionData;
2760 ObjCTypesHelper &ObjCTypes;
2761 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002762 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002763 llvm::Value *CallTryExitVar,
2764 llvm::Value *ExceptionData,
2765 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002766 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002767 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2768
John McCallad346f42011-07-12 20:27:29 +00002769 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002770 // Check whether we need to call objc_exception_try_exit.
2771 // In optimized code, this branch will always be folded.
2772 llvm::BasicBlock *FinallyCallExit =
2773 CGF.createBasicBlock("finally.call_exit");
2774 llvm::BasicBlock *FinallyNoCallExit =
2775 CGF.createBasicBlock("finally.no_call_exit");
2776 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2777 FinallyCallExit, FinallyNoCallExit);
2778
2779 CGF.EmitBlock(FinallyCallExit);
2780 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2781 ->setDoesNotThrow();
2782
2783 CGF.EmitBlock(FinallyNoCallExit);
2784
2785 if (isa<ObjCAtTryStmt>(S)) {
2786 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002787 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2788 // Save the current cleanup destination in case there's
2789 // control flow inside the finally statement.
2790 llvm::Value *CurCleanupDest =
2791 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2792
John McCallcc505292010-07-21 06:59:36 +00002793 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2794
John McCalld96a8e72010-08-11 00:16:14 +00002795 if (CGF.HaveInsertPoint()) {
2796 CGF.Builder.CreateStore(CurCleanupDest,
2797 CGF.getNormalCleanupDestSlot());
2798 } else {
2799 // Currently, the end of the cleanup must always exist.
2800 CGF.EnsureInsertPoint();
2801 }
2802 }
John McCallcc505292010-07-21 06:59:36 +00002803 } else {
2804 // Emit objc_sync_exit(expr); as finally's sole statement for
2805 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002806 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002807 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2808 ->setDoesNotThrow();
2809 }
2810 }
2811 };
John McCall87bb5822010-07-31 23:20:56 +00002812
2813 class FragileHazards {
2814 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002815 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002816 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2817
2818 llvm::InlineAsm *ReadHazard;
2819 llvm::InlineAsm *WriteHazard;
2820
2821 llvm::FunctionType *GetAsmFnType();
2822
2823 void collectLocals();
2824 void emitReadHazard(CGBuilderTy &Builder);
2825
2826 public:
2827 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002828
John McCall87bb5822010-07-31 23:20:56 +00002829 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002830 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002831 };
2832}
2833
2834/// Create the fragile-ABI read and write hazards based on the current
2835/// state of the function, which is presumed to be immediately prior
2836/// to a @try block. These hazards are used to maintain correct
2837/// semantics in the face of optimization and the fragile ABI's
2838/// cavalier use of setjmp/longjmp.
2839FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2840 collectLocals();
2841
2842 if (Locals.empty()) return;
2843
2844 // Collect all the blocks in the function.
2845 for (llvm::Function::iterator
2846 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2847 BlocksBeforeTry.insert(&*I);
2848
2849 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2850
2851 // Create a read hazard for the allocas. This inhibits dead-store
2852 // optimizations and forces the values to memory. This hazard is
2853 // inserted before any 'throwing' calls in the protected scope to
2854 // reflect the possibility that the variables might be read from the
2855 // catch block if the call throws.
2856 {
2857 std::string Constraint;
2858 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2859 if (I) Constraint += ',';
2860 Constraint += "*m";
2861 }
2862
2863 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2864 }
2865
2866 // Create a write hazard for the allocas. This inhibits folding
2867 // loads across the hazard. This hazard is inserted at the
2868 // beginning of the catch path to reflect the possibility that the
2869 // variables might have been written within the protected scope.
2870 {
2871 std::string Constraint;
2872 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2873 if (I) Constraint += ',';
2874 Constraint += "=*m";
2875 }
2876
2877 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2878 }
2879}
2880
2881/// Emit a write hazard at the current location.
2882void FragileHazards::emitWriteHazard() {
2883 if (Locals.empty()) return;
2884
Jay Foad4c7d9f12011-07-15 08:37:34 +00002885 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002886}
2887
John McCall87bb5822010-07-31 23:20:56 +00002888void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2889 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002890 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002891}
2892
2893/// Emit read hazards in all the protected blocks, i.e. all the blocks
2894/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002895void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002896 if (Locals.empty()) return;
2897
2898 CGBuilderTy Builder(CGF.getLLVMContext());
2899
2900 // Iterate through all blocks, skipping those prior to the try.
2901 for (llvm::Function::iterator
2902 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2903 llvm::BasicBlock &BB = *FI;
2904 if (BlocksBeforeTry.count(&BB)) continue;
2905
2906 // Walk through all the calls in the block.
2907 for (llvm::BasicBlock::iterator
2908 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2909 llvm::Instruction &I = *BI;
2910
2911 // Ignore instructions that aren't non-intrinsic calls.
2912 // These are the only calls that can possibly call longjmp.
2913 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2914 if (isa<llvm::IntrinsicInst>(I))
2915 continue;
2916
2917 // Ignore call sites marked nounwind. This may be questionable,
2918 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2919 llvm::CallSite CS(&I);
2920 if (CS.doesNotThrow()) continue;
2921
John McCall0b251722010-08-04 05:59:32 +00002922 // Insert a read hazard before the call. This will ensure that
2923 // any writes to the locals are performed before making the
2924 // call. If the call throws, then this is sufficient to
2925 // guarantee correctness as long as it doesn't also write to any
2926 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002927 Builder.SetInsertPoint(&BB, BI);
2928 emitReadHazard(Builder);
2929 }
2930 }
2931}
2932
2933static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2934 if (V) S.insert(V);
2935}
2936
2937void FragileHazards::collectLocals() {
2938 // Compute a set of allocas to ignore.
2939 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2940 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2941 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002942
2943 // Collect all the allocas currently in the function. This is
2944 // probably way too aggressive.
2945 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2946 for (llvm::BasicBlock::iterator
2947 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2948 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2949 Locals.push_back(&*I);
2950}
2951
2952llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002953 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002954 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2955 tys[i] = Locals[i]->getType();
2956 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002957}
2958
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002959/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002960
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002961 Objective-C setjmp-longjmp (sjlj) Exception Handling
2962 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002963
John McCallf1549f62010-07-06 01:34:17 +00002964 A catch buffer is a setjmp buffer plus:
2965 - a pointer to the exception that was caught
2966 - a pointer to the previous exception data buffer
2967 - two pointers of reserved storage
2968 Therefore catch buffers form a stack, with a pointer to the top
2969 of the stack kept in thread-local storage.
2970
2971 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2972 objc_exception_try_exit pops the given catch buffer, which is
2973 required to be the top of the EH stack.
2974 objc_exception_throw pops the top of the EH stack, writes the
2975 thrown exception into the appropriate field, and longjmps
2976 to the setjmp buffer. It crashes the process (with a printf
2977 and an abort()) if there are no catch buffers on the stack.
2978 objc_exception_extract just reads the exception pointer out of the
2979 catch buffer.
2980
2981 There's no reason an implementation couldn't use a light-weight
2982 setjmp here --- something like __builtin_setjmp, but API-compatible
2983 with the heavyweight setjmp. This will be more important if we ever
2984 want to implement correct ObjC/C++ exception interactions for the
2985 fragile ABI.
2986
2987 Note that for this use of setjmp/longjmp to be correct, we may need
2988 to mark some local variables volatile: if a non-volatile local
2989 variable is modified between the setjmp and the longjmp, it has
2990 indeterminate value. For the purposes of LLVM IR, it may be
2991 sufficient to make loads and stores within the @try (to variables
2992 declared outside the @try) volatile. This is necessary for
2993 optimized correctness, but is not currently being done; this is
2994 being tracked as rdar://problem/8160285
2995
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002996 The basic framework for a @try-catch-finally is as follows:
2997 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002998 objc_exception_data d;
2999 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003000 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003001
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003002 objc_exception_try_enter(&d);
3003 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003004 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003005 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003006 // exception path
3007 id _caught = objc_exception_extract(&d);
3008
3009 // enter new try scope for handlers
3010 if (!setjmp(d.jmp_buf)) {
3011 ... match exception and execute catch blocks ...
3012
3013 // fell off end, rethrow.
3014 _rethrow = _caught;
3015 ... jump-through-finally to finally_rethrow ...
3016 } else {
3017 // exception in catch block
3018 _rethrow = objc_exception_extract(&d);
3019 _call_try_exit = false;
3020 ... jump-through-finally to finally_rethrow ...
3021 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003022 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003023 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003024
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003025 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003026 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003028
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003029 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003030 ... dispatch to finally destination ...
3031
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003032 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003033 objc_exception_throw(_rethrow);
3034
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003035 finally_end:
3036 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003037
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003038 This framework differs slightly from the one gcc uses, in that gcc
3039 uses _rethrow to determine if objc_exception_try_exit should be called
3040 and if the object should be rethrown. This breaks in the face of
3041 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003042
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003043 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003044
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003045 - If there are no catch blocks, then we avoid emitting the second
3046 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003047
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003048 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3049 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003050
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003051 - FIXME: If there is no @finally block we can do a few more
3052 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003053
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003054 Rethrows and Jumps-Through-Finally
3055 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003056
John McCallf1549f62010-07-06 01:34:17 +00003057 '@throw;' is supported by pushing the currently-caught exception
3058 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003059
John McCallf1549f62010-07-06 01:34:17 +00003060 Branches through the @finally block are handled with an ordinary
3061 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3062 exceptions are not compatible with C++ exceptions, and this is
3063 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003064
John McCallf1549f62010-07-06 01:34:17 +00003065 @synchronized(expr) { stmt; } is emitted as if it were:
3066 id synch_value = expr;
3067 objc_sync_enter(synch_value);
3068 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003069*/
3070
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003071void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3072 const Stmt &S) {
3073 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003074
3075 // A destination for the fall-through edges of the catch handlers to
3076 // jump to.
3077 CodeGenFunction::JumpDest FinallyEnd =
3078 CGF.getJumpDestInCurrentScope("finally.end");
3079
3080 // A destination for the rethrow edge of the catch handlers to jump
3081 // to.
3082 CodeGenFunction::JumpDest FinallyRethrow =
3083 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003084
Daniel Dunbar1c566672009-02-24 01:43:46 +00003085 // For @synchronized, call objc_sync_enter(sync.expr). The
3086 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003087 // @synchronized. We can't avoid a temp here because we need the
3088 // value to be preserved. If the backend ever does liveness
3089 // correctly after setjmp, this will be unnecessary.
3090 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003091 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003092 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003093 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3094 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003095 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3096 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003097
3098 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3099 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003100 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003101
John McCall0b251722010-08-04 05:59:32 +00003102 // Allocate memory for the setjmp buffer. This needs to be kept
3103 // live throughout the try and catch blocks.
3104 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3105 "exceptiondata.ptr");
3106
John McCall87bb5822010-07-31 23:20:56 +00003107 // Create the fragile hazards. Note that this will not capture any
3108 // of the allocas required for exception processing, but will
3109 // capture the current basic block (which extends all the way to the
3110 // setjmp call) as "before the @try".
3111 FragileHazards Hazards(CGF);
3112
John McCallf1549f62010-07-06 01:34:17 +00003113 // Create a flag indicating whether the cleanup needs to call
3114 // objc_exception_try_exit. This is true except when
3115 // - no catches match and we're branching through the cleanup
3116 // just to rethrow the exception, or
3117 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003118 // The setjmp-safety rule here is that we should always store to this
3119 // variable in a place that dominates the branch through the cleanup
3120 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003121 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003122 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003123
John McCall9e2213d2010-10-04 23:42:51 +00003124 // A slot containing the exception to rethrow. Only needed when we
3125 // have both a @catch and a @finally.
3126 llvm::Value *PropagatingExnVar = 0;
3127
John McCallf1549f62010-07-06 01:34:17 +00003128 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003129 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003130 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003131 CallTryExitVar,
3132 ExceptionData,
3133 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003134
3135 // Enter a try block:
3136 // - Call objc_exception_try_enter to push ExceptionData on top of
3137 // the EH stack.
3138 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3139 ->setDoesNotThrow();
3140
3141 // - Call setjmp on the exception data buffer.
3142 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3143 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3144 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003145 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003146 llvm::CallInst *SetJmpResult =
3147 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3148 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003149 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003150
3151 // If setjmp returned 0, enter the protected block; otherwise,
3152 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003153 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3154 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003155 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003156 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3157 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003158
John McCallf1549f62010-07-06 01:34:17 +00003159 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003160 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003161 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003162 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003163 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003164
3165 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003166
John McCallf1549f62010-07-06 01:34:17 +00003167 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003168 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003169
John McCall87bb5822010-07-31 23:20:56 +00003170 // Don't optimize loads of the in-scope locals across this point.
3171 Hazards.emitWriteHazard();
3172
John McCallf1549f62010-07-06 01:34:17 +00003173 // For a @synchronized (or a @try with no catches), just branch
3174 // through the cleanup to the rethrow block.
3175 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3176 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003177 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003178 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003179
3180 // Otherwise, we have to match against the caught exceptions.
3181 } else {
John McCall0b251722010-08-04 05:59:32 +00003182 // Retrieve the exception object. We may emit multiple blocks but
3183 // nothing can cross this so the value is already in SSA form.
3184 llvm::CallInst *Caught =
3185 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3186 ExceptionData, "caught");
3187 Caught->setDoesNotThrow();
3188
John McCallf1549f62010-07-06 01:34:17 +00003189 // Push the exception to rethrow onto the EH value stack for the
3190 // benefit of any @throws in the handlers.
3191 CGF.ObjCEHValueStack.push_back(Caught);
3192
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003193 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003194
John McCall0b251722010-08-04 05:59:32 +00003195 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003196
John McCall0b251722010-08-04 05:59:32 +00003197 llvm::BasicBlock *CatchBlock = 0;
3198 llvm::BasicBlock *CatchHandler = 0;
3199 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003200 // Save the currently-propagating exception before
3201 // objc_exception_try_enter clears the exception slot.
3202 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3203 "propagating_exception");
3204 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3205
John McCall0b251722010-08-04 05:59:32 +00003206 // Enter a new exception try block (in case a @catch block
3207 // throws an exception).
3208 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3209 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003210
John McCall0b251722010-08-04 05:59:32 +00003211 llvm::CallInst *SetJmpResult =
3212 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3213 "setjmp.result");
3214 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003215 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003216
John McCall0b251722010-08-04 05:59:32 +00003217 llvm::Value *Threw =
3218 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3219
3220 CatchBlock = CGF.createBasicBlock("catch");
3221 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3222 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3223
3224 CGF.EmitBlock(CatchBlock);
3225 }
3226
3227 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003228
Daniel Dunbar55e40722008-09-27 07:03:52 +00003229 // Handle catch list. As a special case we check if everything is
3230 // matched and avoid generating code for falling off the end if
3231 // so.
3232 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003233 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3234 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003235
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003236 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003237 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003238
Anders Carlsson80f25672008-09-09 17:59:25 +00003239 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003240 if (!CatchParam) {
3241 AllMatched = true;
3242 } else {
John McCall183700f2009-09-21 23:43:11 +00003243 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003244
John McCallf1549f62010-07-06 01:34:17 +00003245 // catch(id e) always matches under this ABI, since only
3246 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003247 // FIXME: For the time being we also match id<X>; this should
3248 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003249 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003250 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003251 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003252
John McCallf1549f62010-07-06 01:34:17 +00003253 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003254 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003255 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3256
Anders Carlssondde0a942008-09-11 09:15:33 +00003257 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003258 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003259 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003260
3261 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003262 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003263 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003264
Anders Carlssondde0a942008-09-11 09:15:33 +00003265 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003266
3267 // The scope of the catch variable ends right here.
3268 CatchVarCleanups.ForceCleanup();
3269
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003270 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003271 break;
3272 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003273
Steve Naroff14108da2009-07-10 23:34:53 +00003274 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003275 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003276
3277 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003278 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3279 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003280
3281 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003282 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003283
John McCallf1549f62010-07-06 01:34:17 +00003284 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003285 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3286 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003287 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003288
John McCallf1549f62010-07-06 01:34:17 +00003289 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3290 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003291
3292 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003293 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003294
Anders Carlsson80f25672008-09-09 17:59:25 +00003295 // Emit the @catch block.
3296 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003297
3298 // Collect any cleanups for the catch variable. The scope lasts until
3299 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003300 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003301
John McCallb6bbcc92010-10-15 04:57:14 +00003302 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003303 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003304
John McCallf1549f62010-07-06 01:34:17 +00003305 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003306 llvm::Value *Tmp =
3307 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003308 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003309 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003310
Anders Carlssondde0a942008-09-11 09:15:33 +00003311 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003312
3313 // We're done with the catch variable.
3314 CatchVarCleanups.ForceCleanup();
3315
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003316 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003317
Anders Carlsson80f25672008-09-09 17:59:25 +00003318 CGF.EmitBlock(NextCatchBlock);
3319 }
3320
John McCallf1549f62010-07-06 01:34:17 +00003321 CGF.ObjCEHValueStack.pop_back();
3322
John McCall0b251722010-08-04 05:59:32 +00003323 // If nothing wanted anything to do with the caught exception,
3324 // kill the extract call.
3325 if (Caught->use_empty())
3326 Caught->eraseFromParent();
3327
3328 if (!AllMatched)
3329 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3330
3331 if (HasFinally) {
3332 // Emit the exception handler for the @catch blocks.
3333 CGF.EmitBlock(CatchHandler);
3334
3335 // In theory we might now need a write hazard, but actually it's
3336 // unnecessary because there's no local-accessing code between
3337 // the try's write hazard and here.
3338 //Hazards.emitWriteHazard();
3339
John McCall9e2213d2010-10-04 23:42:51 +00003340 // Extract the new exception and save it to the
3341 // propagating-exception slot.
3342 assert(PropagatingExnVar);
3343 llvm::CallInst *NewCaught =
3344 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3345 ExceptionData, "caught");
3346 NewCaught->setDoesNotThrow();
3347 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3348
John McCall0b251722010-08-04 05:59:32 +00003349 // Don't pop the catch handler; the throw already did.
3350 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003351 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003352 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003353 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354
John McCall87bb5822010-07-31 23:20:56 +00003355 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003356 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003357
John McCallf1549f62010-07-06 01:34:17 +00003358 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003359 CGF.Builder.restoreIP(TryFallthroughIP);
3360 if (CGF.HaveInsertPoint())
3361 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003362 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003363 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003364
John McCallf1549f62010-07-06 01:34:17 +00003365 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003366 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003367 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003368 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003369 // If we have a propagating-exception variable, check it.
3370 llvm::Value *PropagatingExn;
3371 if (PropagatingExnVar) {
3372 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003373
John McCall9e2213d2010-10-04 23:42:51 +00003374 // Otherwise, just look in the buffer for the exception to throw.
3375 } else {
3376 llvm::CallInst *Caught =
3377 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3378 ExceptionData);
3379 Caught->setDoesNotThrow();
3380 PropagatingExn = Caught;
3381 }
3382
3383 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003384 ->setDoesNotThrow();
3385 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003386 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003387
John McCall87bb5822010-07-31 23:20:56 +00003388 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003389}
3390
3391void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003392 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003393 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003394
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003395 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003396 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003397 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003398 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003399 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003400 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003401 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003402 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003404
John McCallf1549f62010-07-06 01:34:17 +00003405 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3406 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003407 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003408
3409 // Clear the insertion point to indicate we are in unreachable code.
3410 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003411}
3412
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003413/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003414/// object: objc_read_weak (id *src)
3415///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003416llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003417 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003418 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003419 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3420 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3421 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003422 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003423 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003424 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003425 return read_weak;
3426}
3427
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003428/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3429/// objc_assign_weak (id src, id *dst)
3430///
3431void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003432 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003433 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003434 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003435 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003436 assert(Size <= 8 && "does not support size > 8");
3437 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003438 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003439 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3440 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003441 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3442 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003443 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003444 src, dst, "weakassign");
3445 return;
3446}
3447
Fariborz Jahanian58626502008-11-19 00:59:10 +00003448/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3449/// objc_assign_global (id src, id *dst)
3450///
3451void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003452 llvm::Value *src, llvm::Value *dst,
3453 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003454 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003455 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003456 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003457 assert(Size <= 8 && "does not support size > 8");
3458 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003459 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003460 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3461 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003462 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3463 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003464 if (!threadlocal)
3465 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3466 src, dst, "globalassign");
3467 else
3468 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3469 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003470 return;
3471}
3472
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003473/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003474/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003475///
3476void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003477 llvm::Value *src, llvm::Value *dst,
3478 llvm::Value *ivarOffset) {
3479 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003480 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003481 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003482 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003483 assert(Size <= 8 && "does not support size > 8");
3484 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003485 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003486 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3487 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003488 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3489 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003490 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3491 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003492 return;
3493}
3494
Fariborz Jahanian58626502008-11-19 00:59:10 +00003495/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3496/// objc_assign_strongCast (id src, id *dst)
3497///
3498void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003499 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003500 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003501 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003502 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003503 assert(Size <= 8 && "does not support size > 8");
3504 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003505 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003506 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3507 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003508 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3509 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003510 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003511 src, dst, "weakassign");
3512 return;
3513}
3514
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003515void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003516 llvm::Value *DestPtr,
3517 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003518 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003519 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3520 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003521 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003522 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003523 return;
3524}
3525
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003526/// EmitObjCValueForIvar - Code Gen for ivar reference.
3527///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003528LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3529 QualType ObjectTy,
3530 llvm::Value *BaseValue,
3531 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003532 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003533 const ObjCInterfaceDecl *ID =
3534 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003535 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3536 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003537}
3538
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003539llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003540 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003541 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003542 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003543 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003544 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3545 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003546}
3547
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003548/* *** Private Interface *** */
3549
3550/// EmitImageInfo - Emit the image info marker used to encode some module
3551/// level information.
3552///
3553/// See: <rdr://4810609&4810587&4810587>
3554/// struct IMAGE_INFO {
3555/// unsigned version;
3556/// unsigned flags;
3557/// };
3558enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003559 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003560 eImageInfo_GarbageCollected = (1 << 1),
3561 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003562 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3563
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003564 // A flag indicating that the module has no instances of a @synthesize of a
3565 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003566 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003567};
3568
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003569void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003570 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00003571 const char *Section = (ObjCABI == 1) ?
3572 "__OBJC, __image_info,regular" :
3573 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003574
Bill Wendling3973acc2012-02-16 01:13:30 +00003575 // Generate module-level named metadata to convey this information to the
3576 // linker and code-gen.
3577 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003578
Bill Wendling3973acc2012-02-16 01:13:30 +00003579 // Add the ObjC ABI version to the module flags.
3580 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3581 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3582 version);
3583 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3584 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003585
Bill Wendling3973acc2012-02-16 01:13:30 +00003586 if (CGM.getLangOptions().getGC() == LangOptions::NonGC) {
3587 // Non-GC overrides those files which specify GC.
3588 Mod.addModuleFlag(llvm::Module::Override,
3589 "Objective-C Garbage Collection", (uint32_t)0);
3590 } else {
3591 // Add the ObjC garbage collection value.
3592 Mod.addModuleFlag(llvm::Module::Error,
3593 "Objective-C Garbage Collection",
3594 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003595
Bill Wendling3973acc2012-02-16 01:13:30 +00003596 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) {
3597 // Add the ObjC GC Only value.
3598 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3599 eImageInfo_GCOnly);
3600
3601 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3602 llvm::Value *Ops[2] = {
3603 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3604 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3605 eImageInfo_GarbageCollected)
3606 };
3607 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3608 llvm::MDNode::get(VMContext, Ops));
3609 }
3610 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003611}
3612
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003613// struct objc_module {
3614// unsigned long version;
3615// unsigned long size;
3616// const char *name;
3617// Symtab symtab;
3618// };
3619
3620// FIXME: Get from somewhere
3621static const int ModuleVersion = 7;
3622
3623void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003624 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003625
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003626 llvm::Constant *Values[] = {
3627 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3628 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3629 // This used to be the filename, now it is unused. <rdr://4327263>
3630 GetClassName(&CGM.getContext().Idents.get("")),
3631 EmitModuleSymbols()
3632 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003633 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003634 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003635 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003636 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003637}
3638
3639llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003640 unsigned NumClasses = DefinedClasses.size();
3641 unsigned NumCategories = DefinedCategories.size();
3642
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003643 // Return null if no symbols were defined.
3644 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003645 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003646
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003647 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003648 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003649 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003650 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3651 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003652
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003653 // The runtime expects exactly the list of defined classes followed
3654 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00003655 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003656 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003657 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003658 ObjCTypes.Int8PtrTy);
3659 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003660 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003661 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003662 ObjCTypes.Int8PtrTy);
3663
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003664 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003665 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00003666 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003667 Symbols);
3668
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003669 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003670
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003671 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003672 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3673 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003674 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003675 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003676}
3677
John McCallf85e1932011-06-15 23:02:42 +00003678llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3679 IdentifierInfo *II) {
3680 LazySymbols.insert(II);
3681
3682 llvm::GlobalVariable *&Entry = ClassReferences[II];
3683
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003684 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003686 llvm::ConstantExpr::getBitCast(GetClassName(II),
3687 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003688 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003689 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3690 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3691 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003692 }
John McCallf85e1932011-06-15 23:02:42 +00003693
Benjamin Kramer578faa82011-09-27 21:06:10 +00003694 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003695}
3696
John McCallf85e1932011-06-15 23:02:42 +00003697llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3698 const ObjCInterfaceDecl *ID) {
3699 return EmitClassRefFromId(Builder, ID->getIdentifier());
3700}
3701
3702llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3703 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3704 return EmitClassRefFromId(Builder, II);
3705}
3706
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003707llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3708 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003709 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003710
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003711 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003712 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003713 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003714 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003715 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003716 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3717 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003718 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003719 }
3720
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003721 if (lvalue)
3722 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003723 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003724}
3725
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003726llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003727 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003728
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003729 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003730 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00003731 llvm::ConstantDataArray::getString(VMContext,
3732 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003733 ((ObjCABI == 2) ?
3734 "__TEXT,__objc_classname,cstring_literals" :
3735 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003736 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003737
Owen Andersona1cf15f2009-07-14 23:10:40 +00003738 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003739}
3740
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003741llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3742 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3743 I = MethodDefinitions.find(MD);
3744 if (I != MethodDefinitions.end())
3745 return I->second;
3746
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003747 return NULL;
3748}
3749
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003750/// GetIvarLayoutName - Returns a unique constant for the given
3751/// ivar layout bitmap.
3752llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003754 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003755}
3756
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003757void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003758 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003759 bool ForStrongLayout,
3760 bool &HasUnion) {
3761 const RecordDecl *RD = RT->getDecl();
3762 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003763 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003764 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003765 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003766 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003767
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003768 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3769 ForStrongLayout, HasUnion);
3770}
3771
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003772void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003773 const llvm::StructLayout *Layout,
3774 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003775 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003776 unsigned int BytePos, bool ForStrongLayout,
3777 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003778 bool IsUnion = (RD && RD->isUnion());
3779 uint64_t MaxUnionIvarSize = 0;
3780 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003781 const FieldDecl *MaxField = 0;
3782 const FieldDecl *MaxSkippedField = 0;
3783 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003784 uint64_t MaxFieldOffset = 0;
3785 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003786 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003787 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003788
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003789 if (RecFields.empty())
3790 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003791 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3792 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003793 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003794 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003795 FirstFieldDelta =
3796 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3797 }
3798
Chris Lattnerf1690852009-03-31 08:48:01 +00003799 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003800 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003801 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003802 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003803 // Note that 'i' here is actually the field index inside RD of Field,
3804 // although this dependency is hidden.
3805 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003806 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003807 } else
John McCallf85e1932011-06-15 23:02:42 +00003808 FieldOffset =
3809 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003810
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003811 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003812 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003813 LastFieldBitfieldOrUnnamed = Field;
3814 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003815 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003816 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003817
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003818 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003819 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003820 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003821 if (FQT->isUnionType())
3822 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003823
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003824 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003825 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003826 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003827 continue;
3828 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003829
Chris Lattnerf1690852009-03-31 08:48:01 +00003830 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003831 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003832 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003833 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003834 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003835 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003836 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3837 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003838 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003839 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003840 FQT = CArray->getElementType();
3841 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003842
3843 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003844 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003845 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003846 int OldIndex = IvarsInfo.size() - 1;
3847 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848
Ted Kremenek6217b802009-07-29 21:53:49 +00003849 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003850 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003851 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003852
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003853 // Replicate layout information for each array element. Note that
3854 // one element is already done.
3855 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003856 for (int FirstIndex = IvarsInfo.size() - 1,
3857 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003858 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003859 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3860 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3861 IvarsInfo[i].ivar_size));
3862 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3863 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3864 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003865 }
3866 continue;
3867 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003868 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003869 // At this point, we are done with Record/Union and array there of.
3870 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003871 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003872
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003873 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003874 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3875 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003876 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003877 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003878 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003879 MaxUnionIvarSize = UnionIvarSize;
3880 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003881 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003882 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003883 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003884 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003885 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003886 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003887 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003888 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3889 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003890 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003891 // FIXME: Why the asymmetry? We divide by word size in bits on other
3892 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003893 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003894 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003895 MaxSkippedUnionIvarSize = UnionIvarSize;
3896 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003897 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003898 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003899 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003900 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003901 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003902 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003903 }
3904 }
3905 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003906
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003907 if (LastFieldBitfieldOrUnnamed) {
3908 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3909 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003910 uint64_t BitFieldSize
3911 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003912 GC_IVAR skivar;
3913 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3914 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3915 + ((BitFieldSize % ByteSizeInBits) != 0);
3916 SkipIvars.push_back(skivar);
3917 } else {
3918 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3919 // Last field was unnamed. Must update skip info.
3920 unsigned FieldSize
3921 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3922 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3923 FieldSize / ByteSizeInBits));
3924 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003925 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003926
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003927 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003928 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003929 MaxUnionIvarSize));
3930 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003931 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003932 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003933}
3934
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003935/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3936/// the computations and returning the layout bitmap (for ivar or blocks) in
3937/// the given argument BitMap string container. Routine reads
3938/// two containers, IvarsInfo and SkipIvars which are assumed to be
3939/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00003940llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003941 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00003942 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003943
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003944 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003945 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003947 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003948 if (IvarsInfo[0].ivar_bytepos == 0) {
3949 WordsToSkip = 0;
3950 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003951 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003952 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3953 WordsToScan = IvarsInfo[0].ivar_size;
3954 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003955 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003956 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003957 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003958 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003959 // consecutive 'scanned' object pointers.
3960 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003961 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003962 // Skip over 'gc'able object pointer which lay over each other.
3963 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3964 continue;
3965 // Must skip over 1 or more words. We save current skip/scan values
3966 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003967 SKIP_SCAN SkScan;
3968 SkScan.skip = WordsToSkip;
3969 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003970 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003971
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003972 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003973 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3974 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003975 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003976 WordsToSkip = 0;
3977 WordsToScan = IvarsInfo[i].ivar_size;
3978 }
3979 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003980 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003981 SKIP_SCAN SkScan;
3982 SkScan.skip = WordsToSkip;
3983 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003984 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003985 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003986
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003987 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003988 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003989 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003990 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003991 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003992 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003993 IvarsInfo[LastIndex].ivar_bytepos +
3994 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003995 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003996 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003997 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003998 SKIP_SCAN SkScan;
3999 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4000 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004001 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004002 }
4003 }
4004 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4005 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004006 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004007 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004008 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4009 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4010 // 0xM0 followed by 0x0N detected.
4011 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4012 for (int j = i+1; j < SkipScan; j++)
4013 SkipScanIvars[j] = SkipScanIvars[j+1];
4014 --SkipScan;
4015 }
4016 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004017
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004018 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004019 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004020 unsigned char byte;
4021 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4022 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4023 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4024 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004025
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004026 // first skip big.
4027 for (unsigned int ix = 0; ix < skip_big; ix++)
4028 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004029
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004030 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004031 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004032 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004033 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004034 byte |= 0xf;
4035 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004036 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004037 byte |= scan_small;
4038 scan_small = 0;
4039 }
4040 BitMap += byte;
4041 }
4042 // next scan big
4043 for (unsigned int ix = 0; ix < scan_big; ix++)
4044 BitMap += (unsigned char)(0x0f);
4045 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004046 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004047 byte = scan_small;
4048 BitMap += byte;
4049 }
4050 }
4051 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004052 unsigned char zero = 0;
4053 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004054
4055 llvm::GlobalVariable * Entry =
4056 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004057 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004058 ((ObjCABI == 2) ?
4059 "__TEXT,__objc_classname,cstring_literals" :
4060 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004061 1, true);
4062 return getConstantGEP(VMContext, Entry, 0, 0);
4063}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004064
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004065/// BuildIvarLayout - Builds ivar layout bitmap for the class
4066/// implementation for the __strong or __weak case.
4067/// The layout map displays which words in ivar list must be skipped
4068/// and which must be scanned by GC (see below). String is built of bytes.
4069/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4070/// of words to skip and right nibble is count of words to scan. So, each
4071/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4072/// represented by a 0x00 byte which also ends the string.
4073/// 1. when ForStrongLayout is true, following ivars are scanned:
4074/// - id, Class
4075/// - object *
4076/// - __strong anything
4077///
4078/// 2. When ForStrongLayout is false, following ivars are scanned:
4079/// - __weak anything
4080///
4081llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4082 const ObjCImplementationDecl *OMD,
4083 bool ForStrongLayout) {
4084 bool hasUnion = false;
4085
Chris Lattner8b418682012-02-07 00:39:47 +00004086 llvm::Type *PtrTy = CGM.Int8PtrTy;
Douglas Gregore289d812011-09-13 17:21:33 +00004087 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00004088 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004089 return llvm::Constant::getNullValue(PtrTy);
4090
Jordy Rosedb8264e2011-07-22 02:08:32 +00004091 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4092 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004093 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004094 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004095 IVD; IVD = IVD->getNextIvar())
4096 RecFields.push_back(cast<FieldDecl>(IVD));
4097 }
4098 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004099 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004100 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004101
Jordy Rosedb8264e2011-07-22 02:08:32 +00004102 // FIXME: This is not ideal; we shouldn't have to do this copy.
4103 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004104 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004105
4106 if (RecFields.empty())
4107 return llvm::Constant::getNullValue(PtrTy);
4108
4109 SkipIvars.clear();
4110 IvarsInfo.clear();
4111
4112 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4113 if (IvarsInfo.empty())
4114 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004115 // Sort on byte position in case we encounterred a union nested in
4116 // the ivar list.
4117 if (hasUnion && !IvarsInfo.empty())
4118 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4119 if (hasUnion && !SkipIvars.empty())
4120 std::sort(SkipIvars.begin(), SkipIvars.end());
4121
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004122 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004123 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004124
4125 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004126 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004127 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004128 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004129 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004130 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004131 if (!(s[i] & 0xf0))
4132 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4133 else
4134 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4135 printf("\n");
4136 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004137 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004138}
4139
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004140llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004141 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4142
Chris Lattner0b239712012-02-06 22:16:34 +00004143 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004144 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004145 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004146 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004147 ((ObjCABI == 2) ?
4148 "__TEXT,__objc_methname,cstring_literals" :
4149 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004150 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004151
Owen Andersona1cf15f2009-07-14 23:10:40 +00004152 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004153}
4154
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004155// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004156llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004157 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4158}
4159
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004160llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004161 std::string TypeStr;
4162 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4163
4164 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004165
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004166 if (!Entry)
4167 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004168 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004169 ((ObjCABI == 2) ?
4170 "__TEXT,__objc_methtype,cstring_literals" :
4171 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004172 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004173
Owen Andersona1cf15f2009-07-14 23:10:40 +00004174 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004175}
4176
Bob Wilsondc8dab62011-11-30 01:57:58 +00004177llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4178 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004179 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004180 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004181 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004182
4183 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4184
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004185 if (!Entry)
4186 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004187 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004188 ((ObjCABI == 2) ?
4189 "__TEXT,__objc_methtype,cstring_literals" :
4190 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004191 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004192
Owen Andersona1cf15f2009-07-14 23:10:40 +00004193 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004194}
4195
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004196// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004197llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004198 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004199
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004200 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004201 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004202 llvm::ConstantDataArray::getString(VMContext,
4203 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004204 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004205 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004206
Owen Andersona1cf15f2009-07-14 23:10:40 +00004207 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004208}
4209
4210// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004211// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004212llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004213CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4214 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004215 std::string TypeStr;
4216 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004217 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4218}
4219
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004220void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004221 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004222 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004223 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004224 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004225 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4226 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004227 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004228 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004229 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004230 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004231}
4232
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004233void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004234 EmitModuleInfo();
4235
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004236 // Emit the dummy bodies for any protocols which were referenced but
4237 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004238 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004239 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4240 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004241 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004242
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004243 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004244 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004245 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004246 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004247 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004248 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004249 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004250 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004251 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004252 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004253 }
4254
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004255 // Add assembler directives to add lazy undefined symbol references
4256 // for classes which are referenced but not defined. This is
4257 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004258 //
4259 // FIXME: It would be nice if we had an LLVM construct for this.
4260 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004261 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004262 Asm += CGM.getModule().getModuleInlineAsm();
4263 if (!Asm.empty() && Asm.back() != '\n')
4264 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004265
Daniel Dunbar33063492009-09-07 00:20:42 +00004266 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004267 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4268 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004269 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4270 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004271 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004272 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004273 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004274 }
4275
Bill Wendling13562a12012-02-07 09:06:01 +00004276 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004277 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4278 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4279 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004280
Daniel Dunbar33063492009-09-07 00:20:42 +00004281 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004282 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004283}
4284
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004285CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004286 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004287 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004288 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004289 ObjCABI = 2;
4290}
4291
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004292/* *** */
4293
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004294ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004295 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4296{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004297 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4298 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004299
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004300 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004301 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004302 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004303 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004304 Int8PtrTy = CGM.Int8PtrTy;
4305 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004306
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004307 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004308 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004309 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004310
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004311 // I'm not sure I like this. The implicit coordination is a bit
4312 // gross. We should solve this in a reasonable fashion because this
4313 // is a pretty common task (match some runtime data structure with
4314 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004315
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004316 // FIXME: This is leaked.
4317 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004318
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004319 // struct _objc_super {
4320 // id self;
4321 // Class cls;
4322 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004323 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004324 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004325 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004326 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004327 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004328 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004329 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004330 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004331 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004332
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004333 SuperCTy = Ctx.getTagDeclType(RD);
4334 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004335
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004336 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004337 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4338
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004339 // struct _prop_t {
4340 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004341 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004342 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004343 PropertyTy = llvm::StructType::create("struct._prop_t",
4344 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004345
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004346 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004347 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004348 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004349 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004350 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004351 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004352 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4353 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004354 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004355 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004356
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004357 // struct _objc_method {
4358 // SEL _cmd;
4359 // char *method_type;
4360 // char *_imp;
4361 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004362 MethodTy = llvm::StructType::create("struct._objc_method",
4363 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4364 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004365
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004366 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004367 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004368 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004369
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004370}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004371
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004372ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004373 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004374 // struct _objc_method_description {
4375 // SEL name;
4376 // char *types;
4377 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004378 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004379 llvm::StructType::create("struct._objc_method_description",
4380 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004381
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004382 // struct _objc_method_description_list {
4383 // int count;
4384 // struct _objc_method_description[1];
4385 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004386 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004387 llvm::StructType::create("struct._objc_method_description_list",
4388 IntTy,
4389 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004390
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004391 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004392 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004393 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004394
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004395 // Protocol description structures
4396
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004397 // struct _objc_protocol_extension {
4398 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4399 // struct _objc_method_description_list *optional_instance_methods;
4400 // struct _objc_method_description_list *optional_class_methods;
4401 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004402 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004403 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004404 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004405 llvm::StructType::create("struct._objc_protocol_extension",
4406 IntTy, MethodDescriptionListPtrTy,
4407 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004408 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004410 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004411 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004412
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004413 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004414
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004415 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004416 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004417
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004418 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004419 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004420 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004421 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004422 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004423 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004424
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004425 // struct _objc_protocol {
4426 // struct _objc_protocol_extension *isa;
4427 // char *protocol_name;
4428 // struct _objc_protocol **_objc_protocol_list;
4429 // struct _objc_method_description_list *instance_methods;
4430 // struct _objc_method_description_list *class_methods;
4431 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004432 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4433 llvm::PointerType::getUnqual(ProtocolListTy),
4434 MethodDescriptionListPtrTy,
4435 MethodDescriptionListPtrTy,
4436 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004437
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004438 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004439 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004440
Owen Anderson96e0fc72009-07-29 22:16:19 +00004441 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004442
4443 // Class description structures
4444
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004445 // struct _objc_ivar {
4446 // char *ivar_name;
4447 // char *ivar_type;
4448 // int ivar_offset;
4449 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004450 IvarTy = llvm::StructType::create("struct._objc_ivar",
4451 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004452
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004453 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004454 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004455 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004456 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004457
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004458 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004459 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004460 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004461 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004462
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004463 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004464 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004465 llvm::StructType::create("struct._objc_class_extension",
4466 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004467 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004468
Chris Lattnerc1c20112011-08-12 17:43:31 +00004469 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004470
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004471 // struct _objc_class {
4472 // Class isa;
4473 // Class super_class;
4474 // char *name;
4475 // long version;
4476 // long info;
4477 // long instance_size;
4478 // struct _objc_ivar_list *ivars;
4479 // struct _objc_method_list *methods;
4480 // struct _objc_cache *cache;
4481 // struct _objc_protocol_list *protocols;
4482 // char *ivar_layout;
4483 // struct _objc_class_ext *ext;
4484 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004485 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4486 llvm::PointerType::getUnqual(ClassTy),
4487 Int8PtrTy,
4488 LongTy,
4489 LongTy,
4490 LongTy,
4491 IvarListPtrTy,
4492 MethodListPtrTy,
4493 CachePtrTy,
4494 ProtocolListPtrTy,
4495 Int8PtrTy,
4496 ClassExtensionPtrTy,
4497 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498
Owen Anderson96e0fc72009-07-29 22:16:19 +00004499 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004500
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004501 // struct _objc_category {
4502 // char *category_name;
4503 // char *class_name;
4504 // struct _objc_method_list *instance_method;
4505 // struct _objc_method_list *class_method;
4506 // uint32_t size; // sizeof(struct _objc_category)
4507 // struct _objc_property_list *instance_properties;// category's @property
4508 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004509 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004510 llvm::StructType::create("struct._objc_category",
4511 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4512 MethodListPtrTy, ProtocolListPtrTy,
4513 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004514
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004515 // Global metadata structures
4516
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004517 // struct _objc_symtab {
4518 // long sel_ref_cnt;
4519 // SEL *refs;
4520 // short cls_def_cnt;
4521 // short cat_def_cnt;
4522 // char *defs[cls_def_cnt + cat_def_cnt];
4523 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004524 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004525 llvm::StructType::create("struct._objc_symtab",
4526 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4527 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004528 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004529
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004530 // struct _objc_module {
4531 // long version;
4532 // long size; // sizeof(struct _objc_module)
4533 // char *name;
4534 // struct _objc_symtab* symtab;
4535 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004537 llvm::StructType::create("struct._objc_module",
4538 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004539
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004540
Mike Stumpf5408fe2009-05-16 07:57:57 +00004541 // FIXME: This is the size of the setjmp buffer and should be target
4542 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004543 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004544
Anders Carlsson124526b2008-09-09 10:10:21 +00004545 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00004546 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004547
4548 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004549 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00004550 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4551 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004552
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004553}
4554
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004555ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004556 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004557 // struct _method_list_t {
4558 // uint32_t entsize; // sizeof(struct _objc_method)
4559 // uint32_t method_count;
4560 // struct _objc_method method_list[method_count];
4561 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004562 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004563 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4564 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004565 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004566 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004567
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004568 // struct _protocol_t {
4569 // id isa; // NULL
4570 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004571 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004572 // const struct method_list_t * const instance_methods;
4573 // const struct method_list_t * const class_methods;
4574 // const struct method_list_t *optionalInstanceMethods;
4575 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004576 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004577 // const uint32_t size; // sizeof(struct _protocol_t)
4578 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004579 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004580 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004581
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004582 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004583 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004584 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004585
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004586 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004587 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4588 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4589 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4590 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004591 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4592 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004593
4594 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004595 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596
Fariborz Jahanianda320092009-01-29 19:24:30 +00004597 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004598 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004599 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004600 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004601 ProtocolListnfABITy->setBody(LongTy,
4602 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4603 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004605 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004606 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004607
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004608 // struct _ivar_t {
4609 // unsigned long int *offset; // pointer to ivar offset location
4610 // char *name;
4611 // char *type;
4612 // uint32_t alignment;
4613 // uint32_t size;
4614 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004615 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004616 llvm::StructType::create("struct._ivar_t",
4617 llvm::PointerType::getUnqual(LongTy),
4618 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004619
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004620 // struct _ivar_list_t {
4621 // uint32 entsize; // sizeof(struct _ivar_t)
4622 // uint32 count;
4623 // struct _iver_t list[count];
4624 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004625 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004626 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4627 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004628
Owen Anderson96e0fc72009-07-29 22:16:19 +00004629 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004631 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004632 // uint32_t const flags;
4633 // uint32_t const instanceStart;
4634 // uint32_t const instanceSize;
4635 // uint32_t const reserved; // only when building for 64bit targets
4636 // const uint8_t * const ivarLayout;
4637 // const char *const name;
4638 // const struct _method_list_t * const baseMethods;
4639 // const struct _objc_protocol_list *const baseProtocols;
4640 // const struct _ivar_list_t *const ivars;
4641 // const uint8_t * const weakIvarLayout;
4642 // const struct _prop_list_t * const properties;
4643 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004645 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004646 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4647 IntTy, IntTy, IntTy, Int8PtrTy,
4648 Int8PtrTy, MethodListnfABIPtrTy,
4649 ProtocolListnfABIPtrTy,
4650 IvarListnfABIPtrTy,
4651 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004652
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004653 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004654 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004655 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4656 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004657
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004658 // struct _class_t {
4659 // struct _class_t *isa;
4660 // struct _class_t * const superclass;
4661 // void *cache;
4662 // IMP *vtable;
4663 // struct class_ro_t *ro;
4664 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004665
Chris Lattnerc1c20112011-08-12 17:43:31 +00004666 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004667 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4668 llvm::PointerType::getUnqual(ClassnfABITy),
4669 CachePtrTy,
4670 llvm::PointerType::getUnqual(ImpnfABITy),
4671 llvm::PointerType::getUnqual(ClassRonfABITy),
4672 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004674 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004675 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004676
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004677 // struct _category_t {
4678 // const char * const name;
4679 // struct _class_t *const cls;
4680 // const struct _method_list_t * const instance_methods;
4681 // const struct _method_list_t * const class_methods;
4682 // const struct _protocol_list_t * const protocols;
4683 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004684 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004685 CategorynfABITy = llvm::StructType::create("struct._category_t",
4686 Int8PtrTy, ClassnfABIPtrTy,
4687 MethodListnfABIPtrTy,
4688 MethodListnfABIPtrTy,
4689 ProtocolListnfABIPtrTy,
4690 PropertyListPtrTy,
4691 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004693 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004694 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4695 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004696
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004697 // MessageRefTy - LLVM for:
4698 // struct _message_ref_t {
4699 // IMP messenger;
4700 // SEL name;
4701 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004702
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004703 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004704 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004705 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004706 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004707 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004708 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004709 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004710 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004711 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004712 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004713
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004714 MessageRefCTy = Ctx.getTagDeclType(RD);
4715 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4716 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004717
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004718 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004719 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004720
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004721 // SuperMessageRefTy - LLVM for:
4722 // struct _super_message_ref_t {
4723 // SUPER_IMP messenger;
4724 // SEL name;
4725 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004726 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004727 llvm::StructType::create("struct._super_message_ref_t",
4728 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004729
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004730 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004731 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004732
Daniel Dunbare588b992009-03-01 04:46:24 +00004733
4734 // struct objc_typeinfo {
4735 // const void** vtable; // objc_ehtype_vtable + 2
4736 // const char* name; // c++ typeinfo string
4737 // Class cls;
4738 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004739 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004740 llvm::StructType::create("struct._objc_typeinfo",
4741 llvm::PointerType::getUnqual(Int8PtrTy),
4742 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004743 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004744}
4745
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004746llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004747 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004748
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004749 return NULL;
4750}
4751
Bill Wendlingbb028552012-02-07 09:25:09 +00004752void CGObjCNonFragileABIMac::
4753AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4754 const char *SymbolName,
4755 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00004756 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004757
Daniel Dunbar463b8762009-05-15 21:48:48 +00004758 if (!NumClasses)
4759 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Chris Lattner0b239712012-02-06 22:16:34 +00004761 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004762 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004763 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004764 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00004765 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004766 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004767 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00004768 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004769
Daniel Dunbar463b8762009-05-15 21:48:48 +00004770 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004771 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004772 llvm::GlobalValue::InternalLinkage,
4773 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004774 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004775 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004776 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004777 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004778}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004779
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004780void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4781 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004782
Daniel Dunbar463b8762009-05-15 21:48:48 +00004783 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004784 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004786 "\01L_OBJC_LABEL_CLASS_$",
4787 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004788
Bill Wendling13562a12012-02-07 09:06:01 +00004789 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004790 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4791 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4792 continue;
4793 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004794 }
4795
Bill Wendling13562a12012-02-07 09:06:01 +00004796 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004797 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4798 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4799 continue;
4800 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4801 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004802
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004803 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004804 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4805 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004806
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004807 // Build list of all implemented category addresses in array
4808 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004809 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004810 "\01L_OBJC_LABEL_CATEGORY_$",
4811 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004812 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004813 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4814 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004815
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004816 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004817}
4818
John McCall944c8432011-05-14 03:10:52 +00004819/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4820/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004821/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004822/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004823bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4824 // At various points we've experimented with using vtable-based
4825 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004826 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004827 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004828 return false;
John McCall944c8432011-05-14 03:10:52 +00004829 case CodeGenOptions::NonLegacy:
4830 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004831 case CodeGenOptions::Mixed:
4832 break;
4833 }
4834
4835 // If so, see whether this selector is in the white-list of things which must
4836 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004837 if (VTableDispatchMethods.empty()) {
4838 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4839 VTableDispatchMethods.insert(GetNullarySelector("class"));
4840 VTableDispatchMethods.insert(GetNullarySelector("self"));
4841 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4842 VTableDispatchMethods.insert(GetNullarySelector("length"));
4843 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844
John McCall944c8432011-05-14 03:10:52 +00004845 // These are vtable-based if GC is disabled.
4846 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004847 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004848 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4849 VTableDispatchMethods.insert(GetNullarySelector("release"));
4850 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4851 }
4852
4853 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4854 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4855 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4856 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4857 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4858 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4859 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4860
4861 // These are vtable-based if GC is enabled.
4862 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004863 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004864 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4865 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4866
4867 // "countByEnumeratingWithState:objects:count"
4868 IdentifierInfo *KeyIdents[] = {
4869 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4870 &CGM.getContext().Idents.get("objects"),
4871 &CGM.getContext().Idents.get("count")
4872 };
4873 VTableDispatchMethods.insert(
4874 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4875 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004876 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004877
John McCall944c8432011-05-14 03:10:52 +00004878 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004879}
4880
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004881// Metadata flags
4882enum MetaDataDlags {
4883 CLS = 0x0,
4884 CLS_META = 0x1,
4885 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004886 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004887 CLS_EXCEPTION = 0x20,
4888
4889 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4890 CLS_HAS_IVAR_RELEASER = 0x40,
4891 /// class was compiled with -fobjc-arr
4892 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004893};
4894/// BuildClassRoTInitializer - generate meta-data for:
4895/// struct _class_ro_t {
4896/// uint32_t const flags;
4897/// uint32_t const instanceStart;
4898/// uint32_t const instanceSize;
4899/// uint32_t const reserved; // only when building for 64bit targets
4900/// const uint8_t * const ivarLayout;
4901/// const char *const name;
4902/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004903/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004904/// const struct _ivar_list_t *const ivars;
4905/// const uint8_t * const weakIvarLayout;
4906/// const struct _prop_list_t * const properties;
4907/// }
4908///
4909llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004910 unsigned flags,
4911 unsigned InstanceStart,
4912 unsigned InstanceSize,
4913 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004914 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004915 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004916
4917 if (CGM.getLangOptions().ObjCAutoRefCount)
4918 flags |= CLS_COMPILED_BY_ARC;
4919
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004920 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4921 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4922 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004923 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004924 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4925 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004926 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004927 // const struct _method_list_t * const baseMethods;
4928 std::vector<llvm::Constant*> Methods;
4929 std::string MethodListName("\01l_OBJC_$_");
4930 if (flags & CLS_META) {
4931 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004932 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004933 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004934 // Class methods should always be defined.
4935 Methods.push_back(GetMethodConstant(*i));
4936 }
4937 } else {
4938 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004939 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004940 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004941 // Instance methods should always be defined.
4942 Methods.push_back(GetMethodConstant(*i));
4943 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004944 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004945 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004946 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004947
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004948 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4949 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004950
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004951 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4952 if (llvm::Constant *C = GetMethodConstant(MD))
4953 Methods.push_back(C);
4954 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4955 if (llvm::Constant *C = GetMethodConstant(MD))
4956 Methods.push_back(C);
4957 }
4958 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004959 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960 Values[ 5] = EmitMethodList(MethodListName,
4961 "__DATA, __objc_const", Methods);
4962
Fariborz Jahanianda320092009-01-29 19:24:30 +00004963 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4964 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004965 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004966 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004967 OID->all_referenced_protocol_begin(),
4968 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004969
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004970 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004971 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004972 else
4973 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004974 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4975 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004976 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004977 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004978 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004979 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4980 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004981 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004982 Values);
4983 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004984 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4985 llvm::GlobalValue::InternalLinkage,
4986 Init,
4987 (flags & CLS_META) ?
4988 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4989 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004990 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004991 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004992 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004993 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004994
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004995}
4996
4997/// BuildClassMetaData - This routine defines that to-level meta-data
4998/// for the given ClassName for:
4999/// struct _class_t {
5000/// struct _class_t *isa;
5001/// struct _class_t * const superclass;
5002/// void *cache;
5003/// IMP *vtable;
5004/// struct class_ro_t *ro;
5005/// }
5006///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005007llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 std::string &ClassName,
5009 llvm::Constant *IsAGV,
5010 llvm::Constant *SuperClassGV,
5011 llvm::Constant *ClassRoGV,
5012 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005013 llvm::Constant *Values[] = {
5014 IsAGV,
5015 SuperClassGV,
5016 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5017 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5018 ClassRoGV // &CLASS_RO_GV
5019 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005020 if (!Values[1])
5021 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005022 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005023 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005024 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5025 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005026 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005027 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005028 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005029 if (HiddenVisibility)
5030 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005031 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005032}
5033
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005034bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005035CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005036 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005037}
5038
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005039void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005040 uint32_t &InstanceStart,
5041 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005043 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005044
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005045 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005046 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005047
5048 // If there are no fields, the start is the same as the end.
5049 if (!RL.getFieldCount())
5050 InstanceStart = InstanceSize;
5051 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005052 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005053}
5054
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005055void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5056 std::string ClassName = ID->getNameAsString();
5057 if (!ObjCEmptyCacheVar) {
5058 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005059 CGM.getModule(),
5060 ObjCTypes.CacheTy,
5061 false,
5062 llvm::GlobalValue::ExternalLinkage,
5063 0,
5064 "_objc_empty_cache");
5065
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005066 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005067 CGM.getModule(),
5068 ObjCTypes.ImpnfABITy,
5069 false,
5070 llvm::GlobalValue::ExternalLinkage,
5071 0,
5072 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005074 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005075 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005076 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005077 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00005078 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005079 uint32_t InstanceSize = InstanceStart;
5080 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005081 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5082 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005083
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005084 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085
5086 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005087 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005088 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005089 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005090 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005091 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005092 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005093 // class is root
5094 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005095 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005096 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005097 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005098 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005099 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5100 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5101 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005102 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005103 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005104 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005105 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005106 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005107 ObjCMetaClassName +
5108 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005109 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005110 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005111 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005112 }
5113 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5114 InstanceStart,
5115 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005116 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005117 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005118 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5119 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005120 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005121
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005122 // Metadata for the class
5123 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005124 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005125 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005126 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005127 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005128
Douglas Gregor68584ed2009-06-18 16:11:24 +00005129 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005130 flags |= CLS_EXCEPTION;
5131
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005132 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005133 flags |= CLS_ROOT;
5134 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005135 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005136 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005137 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005138 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005139 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005140 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005141 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005142 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005143 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005144 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005145 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005147 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005148
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005149 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005151 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5152 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005153 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005154
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005155 // Determine if this class is also "non-lazy".
5156 if (ImplementationIsNonLazy(ID))
5157 DefinedNonLazyClasses.push_back(ClassMD);
5158
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005159 // Force the definition of the EHType if necessary.
5160 if (flags & CLS_EXCEPTION)
5161 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005162 // Make sure method definition entries are all clear for next implementation.
5163 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005164}
5165
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005166/// GenerateProtocolRef - This routine is called to generate code for
5167/// a protocol reference expression; as in:
5168/// @code
5169/// @protocol(Proto1);
5170/// @endcode
5171/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5172/// which will hold address of the protocol meta-data.
5173///
5174llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005175 const ObjCProtocolDecl *PD) {
5176
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005177 // This routine is called for @protocol only. So, we must build definition
5178 // of protocol's meta-data (not a reference to it!)
5179 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005180 llvm::Constant *Init =
5181 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005182 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005183
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005184 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005185 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005187 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5188 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005189 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005190 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005191 CGM.getModule(),
5192 Init->getType(), false,
5193 llvm::GlobalValue::WeakAnyLinkage,
5194 Init,
5195 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005196 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5197 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005198 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005199 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005200}
5201
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005202/// GenerateCategory - Build metadata for a category implementation.
5203/// struct _category_t {
5204/// const char * const name;
5205/// struct _class_t *const cls;
5206/// const struct _method_list_t * const instance_methods;
5207/// const struct _method_list_t * const class_methods;
5208/// const struct _protocol_list_t * const protocols;
5209/// const struct _prop_list_t * const properties;
5210/// }
5211///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005212void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005213 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005214 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005215 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5216 "_$_" + OCD->getNameAsString());
5217 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005218 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005219
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005220 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005221 Values[0] = GetClassName(OCD->getIdentifier());
5222 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005223 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005224 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005225 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5226
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005227 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005228 std::vector<llvm::Constant*> Methods;
5229 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005231 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005232
5233 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005234 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005235 // Instance methods should always be defined.
5236 Methods.push_back(GetMethodConstant(*i));
5237 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238
5239 Values[2] = EmitMethodList(MethodListName,
5240 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005241 Methods);
5242
5243 MethodListName = Prefix;
5244 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5245 OCD->getNameAsString();
5246 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005248 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005249 // Class methods should always be defined.
5250 Methods.push_back(GetMethodConstant(*i));
5251 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252
5253 Values[3] = EmitMethodList(MethodListName,
5254 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005255 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005256 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005257 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005258 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005259 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005260 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5261 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005262 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005263 + Interface->getName() + "_$_"
5264 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005265 Category->protocol_begin(),
5266 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005267 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5268 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005269 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005270 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5271 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005272 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005273
5274 llvm::Constant *Init =
5275 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005276 Values);
5277 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005279 false,
5280 llvm::GlobalValue::InternalLinkage,
5281 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005282 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005283 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005284 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005285 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005286 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005287 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005288
5289 // Determine if this category is also "non-lazy".
5290 if (ImplementationIsNonLazy(OCD))
5291 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005292 // method definition entries must be clear for next implementation.
5293 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005294}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005295
5296/// GetMethodConstant - Return a struct objc_method constant for the
5297/// given method if it has been defined. The result is null if the
5298/// method has not been defined. The return value has type MethodPtrTy.
5299llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005300 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005301 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005302 if (!Fn)
5303 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005305 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005306 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005307 ObjCTypes.SelectorPtrTy),
5308 GetMethodVarType(MD),
5309 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5310 };
Owen Anderson08e25242009-07-27 22:29:56 +00005311 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005312}
5313
5314/// EmitMethodList - Build meta-data for method declarations
5315/// struct _method_list_t {
5316/// uint32_t entsize; // sizeof(struct _objc_method)
5317/// uint32_t method_count;
5318/// struct _objc_method method_list[method_count];
5319/// }
5320///
Bill Wendlingbb028552012-02-07 09:25:09 +00005321llvm::Constant *
5322CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5323 const char *Section,
5324 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005325 // Return null for empty list.
5326 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005327 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005328
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005329 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005330 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005331 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005332 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005333 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005334 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005335 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005336 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005337 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005338 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005339
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005340 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005341 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005342 llvm::GlobalValue::InternalLinkage, Init, Name);
5343 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005344 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005345 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005346 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005347}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005348
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005349/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5350/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005351llvm::GlobalVariable *
5352CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5353 const ObjCIvarDecl *Ivar) {
5354 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005355 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005356 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005358 CGM.getModule().getGlobalVariable(Name);
5359 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005361 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005362 false,
5363 llvm::GlobalValue::ExternalLinkage,
5364 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005365 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005366 return IvarOffsetGV;
5367}
5368
Daniel Dunbare83be122010-04-02 21:14:02 +00005369llvm::Constant *
5370CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5371 const ObjCIvarDecl *Ivar,
5372 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005373 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005375 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005376 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005377 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005378
Mike Stumpf5408fe2009-05-16 07:57:57 +00005379 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5380 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005381 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5382 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005383 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005384 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005385 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005386 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005387 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005388 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005389}
5390
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005391/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005392/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005393/// IvarListnfABIPtrTy.
5394/// struct _ivar_t {
5395/// unsigned long int *offset; // pointer to ivar offset location
5396/// char *name;
5397/// char *type;
5398/// uint32_t alignment;
5399/// uint32_t size;
5400/// }
5401/// struct _ivar_list_t {
5402/// uint32 entsize; // sizeof(struct _ivar_t)
5403/// uint32 count;
5404/// struct _iver_t list[count];
5405/// }
5406///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005407
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005408llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409 const ObjCImplementationDecl *ID) {
5410
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005411 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005412
Jordy Rosedb8264e2011-07-22 02:08:32 +00005413 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005414 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005415
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005416 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417
Jordy Rosedb8264e2011-07-22 02:08:32 +00005418 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005419 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005420 // Ignore unnamed bit-fields.
5421 if (!IVD->getDeclName())
5422 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005423 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005424 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005425 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005426 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5427 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005428 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005429 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005430 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005431 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005433 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005434 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005435 // NOTE. Size of a bitfield does not match gcc's, because of the
5436 // way bitfields are treated special in each. But I am told that
5437 // 'size' for bitfield ivars is ignored by the runtime so it does
5438 // not matter. If it matters, there is enough info to get the
5439 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005440 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005441 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005442 }
5443 // Return null for empty list.
5444 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005445 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005446
5447 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005448 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005449 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5450 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005451 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005452 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005453 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005454 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005455 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5456 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005457 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005458 llvm::GlobalValue::InternalLinkage,
5459 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005460 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005461 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005462 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005463 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005464
Chris Lattnerad64e022009-07-17 23:57:13 +00005465 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005466 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005467}
5468
5469llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005470 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005471 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005472
Fariborz Jahanianda320092009-01-29 19:24:30 +00005473 if (!Entry) {
5474 // We use the initializer as a marker of whether this is a forward
5475 // reference or not. At module finalization we add the empty
5476 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477 Entry =
5478 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5479 llvm::GlobalValue::ExternalLinkage,
5480 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005481 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005482 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005483 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484
Fariborz Jahanianda320092009-01-29 19:24:30 +00005485 return Entry;
5486}
5487
5488/// GetOrEmitProtocol - Generate the protocol meta-data:
5489/// @code
5490/// struct _protocol_t {
5491/// id isa; // NULL
5492/// const char * const protocol_name;
5493/// const struct _protocol_list_t * protocol_list; // super protocols
5494/// const struct method_list_t * const instance_methods;
5495/// const struct method_list_t * const class_methods;
5496/// const struct method_list_t *optionalInstanceMethods;
5497/// const struct method_list_t *optionalClassMethods;
5498/// const struct _prop_list_t * properties;
5499/// const uint32_t size; // sizeof(struct _protocol_t)
5500/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005501/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005502/// }
5503/// @endcode
5504///
5505
5506llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005507 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005508 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005509
Fariborz Jahanianda320092009-01-29 19:24:30 +00005510 // Early exit if a defining object has already been generated.
5511 if (Entry && Entry->hasInitializer())
5512 return Entry;
5513
Douglas Gregor1d784b22012-01-01 19:51:50 +00005514 // Use the protocol definition, if there is one.
5515 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5516 PD = Def;
5517
Fariborz Jahanianda320092009-01-29 19:24:30 +00005518 // Construct method lists.
5519 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5520 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005521 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005522 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005523 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005524 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005525 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005526 if (!C)
5527 return GetOrEmitProtocolRef(PD);
5528
Fariborz Jahanianda320092009-01-29 19:24:30 +00005529 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5530 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005531 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005532 } else {
5533 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005534 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005536 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005537
5538 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005539 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005540 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005541 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005542 if (!C)
5543 return GetOrEmitProtocolRef(PD);
5544
Fariborz Jahanianda320092009-01-29 19:24:30 +00005545 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5546 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005547 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005548 } else {
5549 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005550 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005551 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005552 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005553
Bob Wilsondc8dab62011-11-30 01:57:58 +00005554 MethodTypesExt.insert(MethodTypesExt.end(),
5555 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5556
5557 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005558 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005559 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005560 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005561 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5562 PD->protocol_begin(),
5563 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005564
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005565 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005566 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005567 "__DATA, __objc_const",
5568 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005569 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005570 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005571 "__DATA, __objc_const",
5572 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005573 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005574 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005575 "__DATA, __objc_const",
5576 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005577 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005578 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005579 "__DATA, __objc_const",
5580 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005581 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005582 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005583 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005584 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005585 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005586 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005587 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5588 + PD->getName(),
5589 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005590 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005591 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005592
Fariborz Jahanianda320092009-01-29 19:24:30 +00005593 if (Entry) {
5594 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005595 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005596 Entry->setInitializer(Init);
5597 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005598 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005599 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5600 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5601 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005602 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005603 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005604 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005605 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005606 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005607 CGM.AddUsedGlobal(Entry);
5608
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005609 // Use this protocol meta-data to build protocol list table in section
5610 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005611 llvm::GlobalVariable *PTGV =
5612 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5613 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5614 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005615 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005616 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005617 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005618 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005619 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005620 return Entry;
5621}
5622
5623/// EmitProtocolList - Generate protocol list meta-data:
5624/// @code
5625/// struct _protocol_list_t {
5626/// long protocol_count; // Note, this is 32/64 bit
5627/// struct _protocol_t[protocol_count];
5628/// }
5629/// @endcode
5630///
5631llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005632CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005633 ObjCProtocolDecl::protocol_iterator begin,
5634 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00005635 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005636
Fariborz Jahanianda320092009-01-29 19:24:30 +00005637 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005638 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005639 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005640
Daniel Dunbar948e2582009-02-15 07:36:20 +00005641 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005642 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005643 Name.toVector(TmpName);
5644 llvm::GlobalVariable *GV =
5645 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005646 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005647 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648
Daniel Dunbar948e2582009-02-15 07:36:20 +00005649 for (; begin != end; ++begin)
5650 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5651
Fariborz Jahanianda320092009-01-29 19:24:30 +00005652 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005653 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005654 ObjCTypes.ProtocolnfABIPtrTy));
5655
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005656 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005657 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005658 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00005660 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5661 ProtocolRefs.size()),
5662 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005664 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005665 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005666 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005667 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005668 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005669 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005670 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005671 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005672 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005673 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005674}
5675
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005676/// GetMethodDescriptionConstant - This routine build following meta-data:
5677/// struct _objc_method {
5678/// SEL _cmd;
5679/// char *method_type;
5680/// char *_imp;
5681/// }
5682
5683llvm::Constant *
5684CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005685 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005686 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005687 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5688 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005689 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005690 if (!Desc[1])
5691 return 0;
5692
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005693 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005694 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005695 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005696}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005697
5698/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5699/// This code gen. amounts to generating code for:
5700/// @code
5701/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5702/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005704LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005705 CodeGen::CodeGenFunction &CGF,
5706 QualType ObjectTy,
5707 llvm::Value *BaseValue,
5708 const ObjCIvarDecl *Ivar,
5709 unsigned CVRQualifiers) {
5710 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005711 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5712 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005713}
5714
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005715llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716 CodeGen::CodeGenFunction &CGF,
5717 const ObjCInterfaceDecl *Interface,
5718 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005719 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005720}
5721
John McCallb1e81442011-05-13 23:16:18 +00005722static void appendSelectorForMessageRefTable(std::string &buffer,
5723 Selector selector) {
5724 if (selector.isUnarySelector()) {
5725 buffer += selector.getNameForSlot(0);
5726 return;
5727 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005728
John McCallb1e81442011-05-13 23:16:18 +00005729 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5730 buffer += selector.getNameForSlot(i);
5731 buffer += '_';
5732 }
5733}
5734
John McCall944c8432011-05-14 03:10:52 +00005735/// Emit a "v-table" message send. We emit a weak hidden-visibility
5736/// struct, initially containing the selector pointer and a pointer to
5737/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5738/// load and call the function pointer, passing the address of the
5739/// struct as the second parameter. The runtime determines whether
5740/// the selector is currently emitted using vtable dispatch; if so, it
5741/// substitutes a stub function which simply tail-calls through the
5742/// appropriate vtable slot, and if not, it substitues a stub function
5743/// which tail-calls objc_msgSend. Both stubs adjust the selector
5744/// argument to correctly point to the selector.
5745RValue
5746CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5747 ReturnValueSlot returnSlot,
5748 QualType resultType,
5749 Selector selector,
5750 llvm::Value *arg0,
5751 QualType arg0Type,
5752 bool isSuper,
5753 const CallArgList &formalArgs,
5754 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005755 // Compute the actual arguments.
5756 CallArgList args;
5757
John McCall944c8432011-05-14 03:10:52 +00005758 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005759 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005760 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5761 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005762
John McCall944c8432011-05-14 03:10:52 +00005763 // Second argument: a pointer to the message ref structure. Leave
5764 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005765 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5766
5767 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5768
5769 const CGFunctionInfo &fnInfo =
5770 CGM.getTypes().getFunctionInfo(resultType, args,
5771 FunctionType::ExtInfo());
5772
John McCallcba681a2011-05-14 21:12:11 +00005773 NullReturnState nullReturn;
5774
John McCall944c8432011-05-14 03:10:52 +00005775 // Find the function to call and the mangled name for the message
5776 // ref structure. Using a different mangled name wouldn't actually
5777 // be a problem; it would just be a waste.
5778 //
5779 // The runtime currently never uses vtable dispatch for anything
5780 // except normal, non-super message-sends.
5781 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005782 llvm::Constant *fn = 0;
5783 std::string messageRefName("\01l_");
5784 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005785 if (isSuper) {
5786 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5787 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005788 } else {
John McCallcba681a2011-05-14 21:12:11 +00005789 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005790 fn = ObjCTypes.getMessageSendStretFixupFn();
5791 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005792 }
John McCallb1e81442011-05-13 23:16:18 +00005793 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5794 fn = ObjCTypes.getMessageSendFpretFixupFn();
5795 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005796 } else {
John McCallb1e81442011-05-13 23:16:18 +00005797 if (isSuper) {
5798 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5799 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005800 } else {
John McCallb1e81442011-05-13 23:16:18 +00005801 fn = ObjCTypes.getMessageSendFixupFn();
5802 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005803 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005804 }
John McCallb1e81442011-05-13 23:16:18 +00005805 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5806 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005807
John McCallb1e81442011-05-13 23:16:18 +00005808 // Append the selector name, except use underscores anywhere we
5809 // would have used colons.
5810 appendSelectorForMessageRefTable(messageRefName, selector);
5811
5812 llvm::GlobalVariable *messageRef
5813 = CGM.getModule().getGlobalVariable(messageRefName);
5814 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005815 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005816 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005817 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005818 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5819 init->getType(),
5820 /*constant*/ false,
5821 llvm::GlobalValue::WeakAnyLinkage,
5822 init,
5823 messageRefName);
5824 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5825 messageRef->setAlignment(16);
5826 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5827 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005828
5829 bool requiresnullCheck = false;
5830 if (CGM.getLangOptions().ObjCAutoRefCount && method)
5831 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5832 e = method->param_end(); i != e; ++i) {
5833 const ParmVarDecl *ParamDecl = (*i);
5834 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5835 if (!nullReturn.NullBB)
5836 nullReturn.init(CGF, arg0);
5837 requiresnullCheck = true;
5838 break;
5839 }
5840 }
5841
John McCallb1e81442011-05-13 23:16:18 +00005842 llvm::Value *mref =
5843 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5844
John McCall944c8432011-05-14 03:10:52 +00005845 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005846 args[1].RV = RValue::get(mref);
5847
5848 // Load the function to call from the message ref table.
5849 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5850 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5851
5852 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005853 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005854 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5855 callee = CGF.Builder.CreateBitCast(callee,
5856 llvm::PointerType::getUnqual(fnType));
5857
John McCallcba681a2011-05-14 21:12:11 +00005858 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005859 return nullReturn.complete(CGF, result, resultType, formalArgs,
5860 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005861}
5862
5863/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005864CodeGen::RValue
5865CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005866 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005867 QualType ResultType,
5868 Selector Sel,
5869 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005870 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005871 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005872 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005873 return isVTableDispatchedSelector(Sel)
5874 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005875 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005876 false, CallArgs, Method)
5877 : EmitMessageSend(CGF, Return, ResultType,
5878 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005880 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005881}
5882
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005883llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005884CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005885 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5886
Daniel Dunbardfff2302009-03-02 05:18:14 +00005887 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005888 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005889 false, llvm::GlobalValue::ExternalLinkage,
5890 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005891 }
5892
5893 return GV;
5894}
5895
John McCallf85e1932011-06-15 23:02:42 +00005896llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5897 IdentifierInfo *II) {
5898 llvm::GlobalVariable *&Entry = ClassReferences[II];
5899
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005900 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005901 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005902 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005903 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005904 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5905 false, llvm::GlobalValue::InternalLinkage,
5906 ClassGV,
5907 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005908 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005909 CGM.getTargetData().getABITypeAlignment(
5910 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005911 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005912 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005913 }
John McCallf85e1932011-06-15 23:02:42 +00005914
Benjamin Kramer578faa82011-09-27 21:06:10 +00005915 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005916}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005917
John McCallf85e1932011-06-15 23:02:42 +00005918llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5919 const ObjCInterfaceDecl *ID) {
5920 return EmitClassRefFromId(Builder, ID->getIdentifier());
5921}
5922
5923llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5924 CGBuilderTy &Builder) {
5925 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5926 return EmitClassRefFromId(Builder, II);
5927}
5928
Daniel Dunbar11394522009-04-18 08:51:00 +00005929llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005930CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005931 const ObjCInterfaceDecl *ID) {
5932 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005933
Daniel Dunbar11394522009-04-18 08:51:00 +00005934 if (!Entry) {
5935 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5936 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005937 Entry =
5938 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005939 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005940 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005941 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005942 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005943 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005944 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005945 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005946 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005947 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005948
Benjamin Kramer578faa82011-09-27 21:06:10 +00005949 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005950}
5951
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005952/// EmitMetaClassRef - Return a Value * of the address of _class_t
5953/// meta-data
5954///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005955llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5956 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005957 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5958 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005959 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005960
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005961 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005962 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005963 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005964 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005965 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005966 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005967 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005968 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005969 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005970 ObjCTypes.ClassnfABIPtrTy));
5971
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005972 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005973 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005974
Benjamin Kramer578faa82011-09-27 21:06:10 +00005975 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005976}
5977
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005978/// GetClass - Return a reference to the class for the given interface
5979/// decl.
5980llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5981 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005982 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005983 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5984 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5985 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5986 }
5987
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005988 return EmitClassRef(Builder, ID);
5989}
5990
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005991/// Generates a message send where the super is the receiver. This is
5992/// a message send to self with special delivery semantics indicating
5993/// which class's method should be called.
5994CodeGen::RValue
5995CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005996 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005997 QualType ResultType,
5998 Selector Sel,
5999 const ObjCInterfaceDecl *Class,
6000 bool isCategoryImpl,
6001 llvm::Value *Receiver,
6002 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006003 const CodeGen::CallArgList &CallArgs,
6004 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006005 // ...
6006 // Create and init a super structure; this is a (receiver, class)
6007 // pair we will pass to objc_msgSendSuper.
6008 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006009 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006010
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006011 llvm::Value *ReceiverAsObject =
6012 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6013 CGF.Builder.CreateStore(ReceiverAsObject,
6014 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006015
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006016 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006017 llvm::Value *Target;
6018 if (IsClassMessage) {
6019 if (isCategoryImpl) {
6020 // Message sent to "super' in a class method defined in
6021 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00006022 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006023 Target = CGF.Builder.CreateStructGEP(Target, 0);
6024 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00006025 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006026 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00006027 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00006028 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006029
Mike Stumpf5408fe2009-05-16 07:57:57 +00006030 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6031 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006032 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006033 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6034 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6035 CGF.Builder.CreateStore(Target,
6036 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006037
John McCall944c8432011-05-14 03:10:52 +00006038 return (isVTableDispatchedSelector(Sel))
6039 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006040 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006041 true, CallArgs, Method)
6042 : EmitMessageSend(CGF, Return, ResultType,
6043 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006044 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006045 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006046}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006047
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006048llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006049 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006050 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006051
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006052 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006053 llvm::Constant *Casted =
6054 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6055 ObjCTypes.SelectorPtrTy);
6056 Entry =
6057 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6058 llvm::GlobalValue::InternalLinkage,
6059 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006060 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006061 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006062 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006063
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006064 if (lval)
6065 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006066 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6067
6068 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6069 llvm::MDNode::get(VMContext,
6070 ArrayRef<llvm::Value*>()));
6071 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006072}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006073/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006074/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006075///
6076void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006077 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006078 llvm::Value *dst,
6079 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006080 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006081 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006082 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006083 assert(Size <= 8 && "does not support size > 8");
6084 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6085 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006086 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6087 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006088 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6089 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006090 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6091 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006092 return;
6093}
6094
6095/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6096/// objc_assign_strongCast (id src, id *dst)
6097///
6098void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006099 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006100 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006101 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006102 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006103 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006104 assert(Size <= 8 && "does not support size > 8");
6105 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006106 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006107 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6108 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006109 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6110 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006111 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006112 src, dst, "weakassign");
6113 return;
6114}
6115
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006116void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006117 CodeGen::CodeGenFunction &CGF,
6118 llvm::Value *DestPtr,
6119 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006120 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006121 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6122 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006123 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006124 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006125 return;
6126}
6127
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006128/// EmitObjCWeakRead - Code gen for loading value of a __weak
6129/// object: objc_read_weak (id *src)
6130///
6131llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006132 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006133 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006134 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006135 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6136 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006137 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006138 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006139 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006140 return read_weak;
6141}
6142
6143/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6144/// objc_assign_weak (id src, id *dst)
6145///
6146void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006147 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006148 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006149 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006150 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006151 assert(Size <= 8 && "does not support size > 8");
6152 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6153 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006154 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6155 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006156 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6157 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006158 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006159 src, dst, "weakassign");
6160 return;
6161}
6162
6163/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6164/// objc_assign_global (id src, id *dst)
6165///
6166void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006167 llvm::Value *src, llvm::Value *dst,
6168 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006169 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006170 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006171 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006172 assert(Size <= 8 && "does not support size > 8");
6173 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6174 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006175 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6176 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006177 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6178 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006179 if (!threadlocal)
6180 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6181 src, dst, "globalassign");
6182 else
6183 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6184 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006185 return;
6186}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006187
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006188void
John McCallf1549f62010-07-06 01:34:17 +00006189CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6190 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006191 EmitAtSynchronizedStmt(CGF, S,
6192 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6193 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006194}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006195
John McCall5a180392010-07-24 00:37:23 +00006196llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006197CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006198 // There's a particular fixed type info for 'id'.
6199 if (T->isObjCIdType() ||
6200 T->isObjCQualifiedIdType()) {
6201 llvm::Constant *IDEHType =
6202 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6203 if (!IDEHType)
6204 IDEHType =
6205 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6206 false,
6207 llvm::GlobalValue::ExternalLinkage,
6208 0, "OBJC_EHTYPE_id");
6209 return IDEHType;
6210 }
6211
6212 // All other types should be Objective-C interface pointer types.
6213 const ObjCObjectPointerType *PT =
6214 T->getAs<ObjCObjectPointerType>();
6215 assert(PT && "Invalid @catch type.");
6216 const ObjCInterfaceType *IT = PT->getInterfaceType();
6217 assert(IT && "Invalid @catch type.");
6218 return GetInterfaceEHType(IT->getDecl(), false);
6219}
6220
John McCallf1549f62010-07-06 01:34:17 +00006221void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6222 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006223 EmitTryCatchStmt(CGF, S,
6224 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6225 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6226 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006227}
6228
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006229/// EmitThrowStmt - Generate code for a throw statement.
6230void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6231 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006232 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006233 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006234 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006235 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006236 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006237 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006238 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006239 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006240 }
6241
John McCall7ec404c2010-10-16 08:21:07 +00006242 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006243 CGF.Builder.ClearInsertionPoint();
6244}
Daniel Dunbare588b992009-03-01 04:46:24 +00006245
John McCall5a180392010-07-24 00:37:23 +00006246llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006247CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006248 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006249 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006250
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006251 // If we don't need a definition, return the entry if found or check
6252 // if we use an external reference.
6253 if (!ForDefinition) {
6254 if (Entry)
6255 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006256
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006257 // If this type (or a super class) has the __objc_exception__
6258 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006259 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006260 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006261 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006262 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006263 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006264 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006265 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006266 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006267
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006268 // Otherwise we need to either make a new entry or fill in the
6269 // initializer.
6270 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006271 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006272 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006273 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006274 CGM.getModule().getGlobalVariable(VTableName);
6275 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006276 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6277 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006278 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006279 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006280
Chris Lattner8b418682012-02-07 00:39:47 +00006281 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006282
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006283 llvm::Constant *Values[] = {
6284 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6285 GetClassName(ID->getIdentifier()),
6286 GetClassGlobal(ClassName)
6287 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006288 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006289 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006290
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006291 if (Entry) {
6292 Entry->setInitializer(Init);
6293 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006294 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006295 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006296 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006297 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006298 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006299 }
6300
John McCall1fb0caa2010-10-22 21:05:15 +00006301 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006302 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006303 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6304 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006305
6306 if (ForDefinition) {
6307 Entry->setSection("__DATA,__objc_const");
6308 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6309 } else {
6310 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6311 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006312
6313 return Entry;
6314}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006315
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006316/* *** */
6317
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006318CodeGen::CGObjCRuntime *
6319CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006320 if (CGM.getLangOptions().ObjCNonFragileABI)
6321 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006322 return new CGObjCMac(CGM);
6323}