blob: 63f6f2c9f048a35cdff8648bccc2a7cff30bd106 [file] [log] [blame]
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbar303e2c22008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000045
Daniel Dunbar303e2c22008-08-11 02:45:11 +000046namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000047
Daniel Dunbar59e476b2009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000049
Daniel Dunbar59e476b2009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000052
Fariborz Jahanian279eda62009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000056
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000057private:
John McCall9dc0db22011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCall31168b02011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000073 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000074
John McCall9dc0db22011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000085
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000086 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000087
John McCall9dc0db22011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson41a75022009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall9dc0db22011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000099
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000101
John McCall9dc0db22011-05-15 01:53:33 +0000102 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
103 ///
104 /// The messenger used for super calls, which have different dispatch
105 /// semantics. The class passed is the superclass of the current
106 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000107 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000108 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000110 params, true),
111 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000112 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000113
John McCall9dc0db22011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
115 ///
116 /// A slightly different messenger used for super calls. The class
117 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000118 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000119 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000121 params, true),
122 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000123 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000124
John McCall9dc0db22011-05-15 01:53:33 +0000125 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
126 /// SEL op, ...)
127 ///
128 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000129 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000130 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000131 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000132 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000133 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000134 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000135
John McCall9dc0db22011-05-15 01:53:33 +0000136 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
137 /// SEL op, ...)
138 ///
139 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000140 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000141 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000142 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000143 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000144 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000145 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000146
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000147 llvm::Constant *getMessageSendSuperFpretFn() const {
148 // There is no objc_msgSendSuper_fpret? How can that work?
149 return getMessageSendSuperFn();
150 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000151
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperFpretFn2() const {
153 // There is no objc_msgSendSuper_fpret? How can that work?
154 return getMessageSendSuperFn2();
155 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000157protected:
158 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000159
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000160public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000161 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
162 llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000163
Daniel Dunbar5d715592008-08-12 05:28:47 +0000164 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000165 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000166
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000167 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000168 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000169
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000170 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000171 llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000172 /// ProtocolPtrTy - LLVM type for external protocol handles
173 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000174 llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Daniel Dunbarc722b852008-08-30 03:02:31 +0000176 // SuperCTy - clang type for struct objc_super.
177 QualType SuperCTy;
178 // SuperPtrCTy - clang type for struct objc_super *.
179 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000180
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000181 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000182 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000183 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000184 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000186 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
187 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000188 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000189
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000190 /// PropertyListTy - LLVM type for struct objc_property_list
191 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000192 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000193 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000194 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000195
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000196 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000197 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000198
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000199 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000200 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000201 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000202 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000203
Chris Lattnerce8754e2009-04-22 02:44:54 +0000204 llvm::Constant *getGetPropertyFn() {
205 CodeGen::CodeGenTypes &Types = CGM.getTypes();
206 ASTContext &Ctx = CGM.getContext();
207 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000209 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
210 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000211 Params.push_back(IdType);
212 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000213 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000214 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000215 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000216 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000217 FunctionType::ExtInfo()),
218 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000219 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
220 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000221
Chris Lattnerce8754e2009-04-22 02:44:54 +0000222 llvm::Constant *getSetPropertyFn() {
223 CodeGen::CodeGenTypes &Types = CGM.getTypes();
224 ASTContext &Ctx = CGM.getContext();
225 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000226 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000227 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
228 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000229 Params.push_back(IdType);
230 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000231 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000232 Params.push_back(IdType);
233 Params.push_back(Ctx.BoolTy);
234 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000235 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000236 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000237 FunctionType::ExtInfo()),
238 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000239 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
240 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000241
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000242
243 llvm::Constant *getCopyStructFn() {
244 CodeGen::CodeGenTypes &Types = CGM.getTypes();
245 ASTContext &Ctx = CGM.getContext();
246 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000248 Params.push_back(Ctx.VoidPtrTy);
249 Params.push_back(Ctx.VoidPtrTy);
250 Params.push_back(Ctx.LongTy);
251 Params.push_back(Ctx.BoolTy);
252 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000253 llvm::FunctionType *FTy =
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000254 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
255 FunctionType::ExtInfo()),
256 false);
257 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
258 }
259
Chris Lattnerce8754e2009-04-22 02:44:54 +0000260 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000261 CodeGen::CodeGenTypes &Types = CGM.getTypes();
262 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000263 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000264 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000265 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000266 llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000267 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000268 FunctionType::ExtInfo()),
269 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
271 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000272
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000273 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000274 llvm::Constant *getGcReadWeakFn() {
275 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000276 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000278 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000280 }
281
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000282 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000283 llvm::Constant *getGcAssignWeakFn() {
284 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000285 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000286 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000287 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000288 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
289 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000290
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000291 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000292 llvm::Constant *getGcAssignGlobalFn() {
293 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000294 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000295 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000296 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000297 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
298 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000299
Fariborz Jahanian217af242010-07-20 20:30:03 +0000300 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
301 llvm::Constant *getGcAssignThreadLocalFn() {
302 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000303 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000304 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000305 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000306 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
307 }
308
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000309 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000310 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000311 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000312 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
313 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000314 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000315 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000316 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
317 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000318
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000319 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
320 llvm::Constant *GcMemmoveCollectableFn() {
321 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000322 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000323 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000324 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
325 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000326
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000327 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000328 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000329 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000330 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000331 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000332 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000333 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
334 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000335
336 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000337 llvm::Constant *getExceptionThrowFn() {
338 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000339 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000340 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000341 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
343 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000344
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000345 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
346 llvm::Constant *getExceptionRethrowFn() {
347 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000348 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
350 }
351
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000352 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000353 llvm::Constant *getSyncEnterFn() {
354 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000355 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000356 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
359 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000360
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000361 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000362 llvm::Constant *getSyncExitFn() {
363 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000364 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000365 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000366 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000367 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
368 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000369
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000370 llvm::Constant *getSendFn(bool IsSuper) const {
371 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
372 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000373
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000374 llvm::Constant *getSendFn2(bool IsSuper) const {
375 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
376 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000377
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000378 llvm::Constant *getSendStretFn(bool IsSuper) const {
379 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
380 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000382 llvm::Constant *getSendStretFn2(bool IsSuper) const {
383 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000385
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFpretFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
388 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000389
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
392 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000393
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000394 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
395 ~ObjCCommonTypesHelper(){}
396};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000397
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000398/// ObjCTypesHelper - Helper class that encapsulates lazy
399/// construction of varies types used during ObjC generation.
400class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000401public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000402 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000403 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000404 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000405 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000406 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000407 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000408
Daniel Dunbarb036db82008-08-13 03:21:16 +0000409 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000410 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000411 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000412 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000415 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000416 /// ProtocolExtensionTy - LLVM type for struct
417 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000418 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000419 /// MethodDescriptionTy - LLVM type for struct
420 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000421 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000422 /// MethodDescriptionListTy - LLVM type for struct
423 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000424 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000425 /// MethodDescriptionListPtrTy - LLVM type for struct
426 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000427 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000428 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000429 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000430 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000431 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000432 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000433 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000434 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000435 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000436 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000437 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000438 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000439 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000440 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000441 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000442 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000443 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000444 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000445 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000446 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000447 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000448 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000449 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000450 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000451 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000452
Anders Carlsson9ff22482008-09-09 10:10:21 +0000453 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000454 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000455
Anders Carlsson9ff22482008-09-09 10:10:21 +0000456 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000457 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000458 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000459 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000460 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000461 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000462 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000463
464 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000465 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000466 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000467 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000468 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000469 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000470 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000471
472 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000473 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000474 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000475 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000476 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000477 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000478 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000479
Anders Carlsson9ff22482008-09-09 10:10:21 +0000480 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000482 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000483 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485 "objc_exception_match");
486
Chris Lattnerc6406db2009-04-22 02:26:14 +0000487 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000488
Anders Carlsson9ff22482008-09-09 10:10:21 +0000489 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000490 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000491 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000492 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000493 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
494 params, false),
495 "_setjmp");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000496 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000497
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000498public:
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000500 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000501};
502
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000503/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000504/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000505class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000506public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000508 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000509 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000510
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000512 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000513
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000514 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000515 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000516
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000518 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000519
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000521 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000522
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000524 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000525
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000526 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000527 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000528
Fariborz Jahanian71394042009-01-23 23:53:38 +0000529 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000530 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000531
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000532 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000533 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000534
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000535 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000536 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000537
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000539 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000541 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000542 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000545 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000546
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000547 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000548 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000549
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000550 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000551
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
554 // IMP messenger;
555 // SEL name;
556 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000557 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000560
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000562 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000565
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000566 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000567 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000568
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
572 // SEL name;
573 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000574 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000577 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000578
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000581 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000583 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000584 "objc_msgSend_fixup");
585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000586
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000587 llvm::Constant *getMessageSendFpretFixupFn() {
588 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000589 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000590 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000591 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000592 "objc_msgSend_fpret_fixup");
593 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000594
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000595 llvm::Constant *getMessageSendStretFixupFn() {
596 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000597 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000599 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000600 "objc_msgSend_stret_fixup");
601 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000603 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000605 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000606 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000608 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000609 "objc_msgSendSuper2_fixup");
610 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000611
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000613 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000614 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000615 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000616 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000617 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000618 "objc_msgSendSuper2_stret_fixup");
619 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000620
Chris Lattnera7c00b42009-04-22 02:15:23 +0000621 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000623 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000624
Chris Lattnera7c00b42009-04-22 02:15:23 +0000625 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000626
Chris Lattnera7c00b42009-04-22 02:15:23 +0000627 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000628 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000629 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000630 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000631 "objc_begin_catch");
632 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000633
Chris Lattnera5f58b02011-07-09 17:41:47 +0000634 llvm::StructType *EHTypeTy;
635 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000636
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000637 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
638 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000639};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000641class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000642public:
643 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000644 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000645 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000646 unsigned ivar_bytepos;
647 unsigned ivar_size;
648 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000650
651 // Allow sorting based on byte pos.
652 bool operator<(const GC_IVAR &b) const {
653 return ivar_bytepos < b.ivar_bytepos;
654 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000655 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000656
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000657 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000658 public:
659 unsigned skip;
660 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000662 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000663 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000664
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000665protected:
666 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000667 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000668 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000669 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000670
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000671 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000672 SmallVector<GC_IVAR, 16> SkipIvars;
673 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000674
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000675 /// LazySymbols - Symbols to generate a lazy reference for. See
676 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000677 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000678
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000679 /// DefinedSymbols - External symbols which are defined by this
680 /// module. The symbols in this list and LazySymbols are used to add
681 /// special linker symbols which ensure that Objective-C modules are
682 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000683 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000684
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000685 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000686 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000688 /// MethodVarNames - uniqued method variable names.
689 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000690
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000691 /// DefinedCategoryNames - list of category names in form Class_Category.
692 llvm::SetVector<std::string> DefinedCategoryNames;
693
Daniel Dunbarb036db82008-08-13 03:21:16 +0000694 /// MethodVarTypes - uniqued method type signatures. We have to use
695 /// a StringMap here because have no other unique reference.
696 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000697
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000698 /// MethodDefinitions - map of methods which have been defined in
699 /// this translation unit.
700 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000701
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000702 /// PropertyNames - uniqued method variable names.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000705 /// ClassReferences - uniqued class references.
706 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000707
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000708 /// SelectorReferences - uniqued selector references.
709 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000710
Daniel Dunbarb036db82008-08-13 03:21:16 +0000711 /// Protocols - Protocols for which an objc_protocol structure has
712 /// been emitted. Forward declarations are handled by creating an
713 /// empty structure whose initializer is filled in when/if defined.
714 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000715
Daniel Dunbarc475d422008-10-29 22:36:39 +0000716 /// DefinedProtocols - Protocols which have actually been
717 /// defined. We should not need this, see FIXME in GenerateProtocol.
718 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000719
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000720 /// DefinedClasses - List of defined classes.
721 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000722
723 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
724 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000726 /// DefinedCategories - List of defined categories.
727 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000728
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000729 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
730 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000732 /// GetNameForMethod - Return a name for the given method.
733 /// \param[out] NameOut - The return value.
734 void GetNameForMethod(const ObjCMethodDecl *OMD,
735 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000736 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000737
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000738 /// GetMethodVarName - Return a unique constant for the given
739 /// selector's name. The return value has type char *.
740 llvm::Constant *GetMethodVarName(Selector Sel);
741 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000743 /// GetMethodVarType - Return a unique constant for the given
744 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000745
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000746 // FIXME: This is a horrible name.
747 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000748 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000750 /// GetPropertyName - Return a unique constant for the given
751 /// name. The return value has type char *.
752 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000753
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000754 // FIXME: This can be dropped once string functions are unified.
755 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
756 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000758 /// GetClassName - Return a unique constant for the given selector's
759 /// name. The return value has type char *.
760 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000762 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
763
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000764 /// BuildIvarLayout - Builds ivar layout bitmap for the class
765 /// implementation for the __strong or __weak case.
766 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000767 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
768 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000769
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000770 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000771
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000772 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000773 unsigned int BytePos, bool ForStrongLayout,
774 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000775 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000776 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000777 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +0000778 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000779 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000780 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000781
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000782 /// GetIvarLayoutName - Returns a unique constant for the given
783 /// ivar layout bitmap.
784 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
785 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000786
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000787 /// EmitPropertyList - Emit the given property list. The return
788 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000789 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000791 const ObjCContainerDecl *OCD,
792 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000793
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000794 /// PushProtocolProperties - Push protocol's property on the input stack.
795 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
796 std::vector<llvm::Constant*> &Properties,
797 const Decl *Container,
798 const ObjCProtocolDecl *PROTO,
799 const ObjCCommonTypesHelper &ObjCTypes);
800
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000801 /// GetProtocolRef - Return a reference to the internal protocol
802 /// description, creating an empty one if it has not been
803 /// defined. The return value has type ProtocolPtrTy.
804 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000805
Daniel Dunbar30c65362009-03-09 20:09:19 +0000806 /// CreateMetadataVar - Create a global variable with internal
807 /// linkage for use by the Objective-C runtime.
808 ///
809 /// This is a convenience wrapper which not only creates the
810 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000811 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000812 ///
813 /// \param Name - The variable name.
814 /// \param Init - The variable initializer; this is also used to
815 /// define the type of the variable.
816 /// \param Section - The section the variable should go into, or 0.
817 /// \param Align - The alignment for the variable, or 0.
818 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000819 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000820 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000821 llvm::Constant *Init,
822 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000823 unsigned Align,
824 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000825
John McCall9e8bb002011-05-14 03:10:52 +0000826 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
827 ReturnValueSlot Return,
828 QualType ResultType,
829 llvm::Value *Sel,
830 llvm::Value *Arg0,
831 QualType Arg0Ty,
832 bool IsSuper,
833 const CallArgList &CallArgs,
834 const ObjCMethodDecl *OMD,
835 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000836
Daniel Dunbar5e639272010-04-25 20:39:01 +0000837 /// EmitImageInfo - Emit the image info marker used to encode some module
838 /// level information.
839 void EmitImageInfo();
840
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000841public:
Owen Andersonae86c192009-07-13 04:10:07 +0000842 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000843 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000844
David Chisnall481e3a82010-01-23 02:40:42 +0000845 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000847 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
848 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000850 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000851
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000852 /// GetOrEmitProtocol - Get the protocol object for the given
853 /// declaration, emitting it if necessary. The return value has type
854 /// ProtocolPtrTy.
855 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000856
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000857 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
858 /// object for the given declaration, emitting it if needed. These
859 /// forward references will be filled in with empty bodies if no
860 /// definition is seen. The return value has type ProtocolPtrTy.
861 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000862 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
863 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000864
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000865};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000867class CGObjCMac : public CGObjCCommonMac {
868private:
869 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000870
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000871 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000873 void EmitModuleInfo();
874
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000875 /// EmitModuleSymols - Emit module symbols, the list of defined
876 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000877 llvm::Constant *EmitModuleSymbols();
878
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000879 /// FinishModule - Write out global data structures at the end of
880 /// processing a translation unit.
881 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000882
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000883 /// EmitClassExtension - Generate the class extension structure used
884 /// to store the weak ivar layout and properties. The return value
885 /// has type ClassExtensionPtrTy.
886 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
887
888 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
889 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000891 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000892
John McCall31168b02011-06-15 23:02:42 +0000893 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
894 IdentifierInfo *II);
895
896 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
897
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000898 /// EmitSuperClassRef - Emits reference to class's main metadata class.
899 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000900
901 /// EmitIvarList - Emit the ivar list for the given
902 /// implementation. If ForClass is true the list of class ivars
903 /// (i.e. metaclass ivars) is emitted, otherwise the list of
904 /// interface ivars will be emitted. The return value has type
905 /// IvarListPtrTy.
906 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000907 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000909 /// EmitMetaClass - Emit a forward reference to the class structure
910 /// for the metaclass of the given interface. The return value has
911 /// type ClassPtrTy.
912 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
913
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000914 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000915 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000916 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
917 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000918 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000920 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000921
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000922 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000923
924 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000925 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000926 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000927 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000928 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000929
930 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000931 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000932 /// - TypeName: The name for the type containing the methods.
933 /// - IsProtocol: True iff these methods are for a protocol.
934 /// - ClassMethds: True iff these are class methods.
935 /// - Required: When true, only "required" methods are
936 /// listed. Similarly, when false only "optional" methods are
937 /// listed. For classes this should always be true.
938 /// - begin, end: The method list to output.
939 ///
940 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000941 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000942 const char *Section,
943 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000944
Daniel Dunbarc475d422008-10-29 22:36:39 +0000945 /// GetOrEmitProtocol - Get the protocol object for the given
946 /// declaration, emitting it if necessary. The return value has type
947 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000948 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000949
950 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
951 /// object for the given declaration, emitting it if needed. These
952 /// forward references will be filled in with empty bodies if no
953 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000954 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000955
Daniel Dunbarb036db82008-08-13 03:21:16 +0000956 /// EmitProtocolExtension - Generate the protocol extension
957 /// structure used to store optional instance and class methods, and
958 /// protocol properties. The return value has type
959 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000960 llvm::Constant *
961 EmitProtocolExtension(const ObjCProtocolDecl *PD,
962 const ConstantVector &OptInstanceMethods,
963 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000964
965 /// EmitProtocolList - Generate the list of referenced
966 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000967 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +0000968 ObjCProtocolDecl::protocol_iterator begin,
969 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000970
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000971 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
972 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +0000973 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
974 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000975
976public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000977 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000978
Fariborz Jahanian71394042009-01-23 23:53:38 +0000979 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000980
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000981 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000982 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000983 QualType ResultType,
984 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000985 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000986 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +0000987 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +0000988 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000989
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000990 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000991 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000992 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000993 QualType ResultType,
994 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000995 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +0000996 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000997 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +0000998 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000999 const CallArgList &CallArgs,
1000 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001001
Daniel Dunbarcb463852008-11-01 01:53:16 +00001002 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001003 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001004
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001005 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1006 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001007
1008 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1009 /// untyped one.
1010 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1011 const ObjCMethodDecl *Method);
1012
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001013 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001014
Daniel Dunbar92992502008-08-15 22:20:32 +00001015 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001016
Daniel Dunbar92992502008-08-15 22:20:32 +00001017 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001018
Daniel Dunbarcb463852008-11-01 01:53:16 +00001019 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001020 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001021
Chris Lattnerd4808922009-03-22 21:03:39 +00001022 virtual llvm::Constant *GetPropertyGetFunction();
1023 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001024 virtual llvm::Constant *GetGetStructFunction();
1025 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001026 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001027
John McCallbd309292010-07-06 01:34:17 +00001028 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1029 const ObjCAtTryStmt &S);
1030 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1031 const ObjCAtSynchronizedStmt &S);
1032 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001033 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1034 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001035 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001036 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001037 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001038 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001039 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001040 llvm::Value *src, llvm::Value *dest,
1041 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001042 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001043 llvm::Value *src, llvm::Value *dest,
1044 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001045 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001047 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1048 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001049 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001050
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001051 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1052 QualType ObjectTy,
1053 llvm::Value *BaseValue,
1054 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001055 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001056 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001057 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001058 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001059
1060 /// GetClassGlobal - Return the global variable for the Objective-C
1061 /// class of the given name.
1062 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001063 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001064 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001065};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001066
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001067class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001068private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001069 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001070 llvm::GlobalVariable* ObjCEmptyCacheVar;
1071 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001072
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001073 /// SuperClassReferences - uniqued super class references.
1074 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001075
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001076 /// MetaClassReferences - uniqued meta class references.
1077 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001078
1079 /// EHTypeReferences - uniqued class ehtype references.
1080 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001081
John McCall9e8bb002011-05-14 03:10:52 +00001082 /// VTableDispatchMethods - List of methods for which we generate
1083 /// vtable-based message dispatch.
1084 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001085
Fariborz Jahanian67260552009-11-17 21:37:35 +00001086 /// DefinedMetaClasses - List of defined meta-classes.
1087 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1088
John McCall9e8bb002011-05-14 03:10:52 +00001089 /// isVTableDispatchedSelector - Returns true if SEL is a
1090 /// vtable-based selector.
1091 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001092
Fariborz Jahanian71394042009-01-23 23:53:38 +00001093 /// FinishNonFragileABIModule - Write out global data structures at the end of
1094 /// processing a translation unit.
1095 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001096
Daniel Dunbar19573e72009-05-15 21:48:48 +00001097 /// AddModuleClassList - Add the given list of class pointers to the
1098 /// module with the provided symbol and section names.
1099 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1100 const char *SymbolName,
1101 const char *SectionName);
1102
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001103 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1104 unsigned InstanceStart,
1105 unsigned InstanceSize,
1106 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001107 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001108 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001109 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001110 llvm::Constant *ClassRoGV,
1111 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001112
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001113 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001114
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001115 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001116
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001117 /// EmitMethodList - Emit the method list for the given
1118 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001119 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001120 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001121 const ConstantVector &Methods);
1122 /// EmitIvarList - Emit the ivar list for the given
1123 /// implementation. If ForClass is true the list of class ivars
1124 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1125 /// interface ivars will be emitted. The return value has type
1126 /// IvarListnfABIPtrTy.
1127 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001128
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001129 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001130 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001131 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001132
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001133 /// GetOrEmitProtocol - Get the protocol object for the given
1134 /// declaration, emitting it if necessary. The return value has type
1135 /// ProtocolPtrTy.
1136 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001137
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001138 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1139 /// object for the given declaration, emitting it if needed. These
1140 /// forward references will be filled in with empty bodies if no
1141 /// definition is seen. The return value has type ProtocolPtrTy.
1142 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001143
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001144 /// EmitProtocolList - Generate the list of referenced
1145 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001146 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001147 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001148 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001149
John McCall9e8bb002011-05-14 03:10:52 +00001150 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1151 ReturnValueSlot Return,
1152 QualType ResultType,
1153 Selector Sel,
1154 llvm::Value *Receiver,
1155 QualType Arg0Ty,
1156 bool IsSuper,
1157 const CallArgList &CallArgs,
1158 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001159
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001160 /// GetClassGlobal - Return the global variable for the Objective-C
1161 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001162 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001163
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001164 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001165 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001166 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001167 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001168
1169 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1170 IdentifierInfo *II);
1171
1172 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001173
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001174 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1175 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001176 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1177 const ObjCInterfaceDecl *ID);
1178
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001179 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1180 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001182 const ObjCInterfaceDecl *ID);
1183
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001184 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1185 /// the given ivar.
1186 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001187 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001188 const ObjCInterfaceDecl *ID,
1189 const ObjCIvarDecl *Ivar);
1190
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001191 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1192 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001193 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1194 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001195
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001196 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001197 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001198 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001199 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001200
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001201 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001202 return "OBJC_METACLASS_$_";
1203 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001204
Daniel Dunbar15894b72009-04-07 05:48:37 +00001205 const char *getClassSymbolPrefix() const {
1206 return "OBJC_CLASS_$_";
1207 }
1208
Daniel Dunbar961202372009-05-03 12:57:56 +00001209 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001210 uint32_t &InstanceStart,
1211 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001213 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001214 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001215 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1216 return CGM.getContext().Selectors.getSelector(0, &II);
1217 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001219 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001220 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1221 return CGM.getContext().Selectors.getSelector(1, &II);
1222 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001223
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001224 /// ImplementationIsNonLazy - Check whether the given category or
1225 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001226 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001227
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001228public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001229 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001230 // FIXME. All stubs for now!
1231 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001232
Fariborz Jahanian71394042009-01-23 23:53:38 +00001233 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001234 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001235 QualType ResultType,
1236 Selector Sel,
1237 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001238 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001239 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001240 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001241
1242 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001243 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001244 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001245 QualType ResultType,
1246 Selector Sel,
1247 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001248 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001249 llvm::Value *Receiver,
1250 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001251 const CallArgList &CallArgs,
1252 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001253
Fariborz Jahanian71394042009-01-23 23:53:38 +00001254 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001255 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001257 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1258 bool lvalue = false)
1259 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001260
1261 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1262 /// untyped one.
1263 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1264 const ObjCMethodDecl *Method)
1265 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001266
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001267 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001268
Fariborz Jahanian71394042009-01-23 23:53:38 +00001269 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001270 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001271 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001272
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001273 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001274
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001275 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001276 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001277 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001278 virtual llvm::Constant *GetPropertySetFunction() {
1279 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001280 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001281
David Chisnall168b80f2010-12-26 22:13:16 +00001282 virtual llvm::Constant *GetSetStructFunction() {
1283 return ObjCTypes.getCopyStructFn();
1284 }
1285 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001286 return ObjCTypes.getCopyStructFn();
1287 }
1288
Chris Lattnerd4808922009-03-22 21:03:39 +00001289 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001290 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001291 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292
John McCallbd309292010-07-06 01:34:17 +00001293 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1294 const ObjCAtTryStmt &S);
1295 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1296 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001297 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001298 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001299 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001300 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001301 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001302 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001303 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001304 llvm::Value *src, llvm::Value *dest,
1305 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001306 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001307 llvm::Value *src, llvm::Value *dest,
1308 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001309 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001310 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001311 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1312 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001313 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001314 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1315 QualType ObjectTy,
1316 llvm::Value *BaseValue,
1317 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001318 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001319 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001320 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001321 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001322};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323
John McCall5880fb82011-05-14 21:12:11 +00001324/// A helper class for performing the null-initialization of a return
1325/// value.
1326struct NullReturnState {
1327 llvm::BasicBlock *NullBB;
1328
1329 NullReturnState() : NullBB(0) {}
1330
1331 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1332 // Make blocks for the null-init and call edges.
1333 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1334 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1335
1336 // Check for a null receiver and, if there is one, jump to the
1337 // null-init test.
1338 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1339 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1340
1341 // Otherwise, start performing the call.
1342 CGF.EmitBlock(callBB);
1343 }
1344
1345 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1346 if (!NullBB) return result;
1347
1348 // Finish the call path.
1349 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1350 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1351
1352 // Emit the null-init block and perform the null-initialization there.
1353 CGF.EmitBlock(NullBB);
1354 assert(result.isAggregate() && "null init of non-aggregate result?");
1355 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1356
1357 // Jump to the continuation block.
1358 CGF.EmitBlock(contBB);
1359
1360 return result;
1361 }
1362};
1363
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001364} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001365
1366/* *** Helper Functions *** */
1367
1368/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001369static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001370 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001371 unsigned idx0,
1372 unsigned idx1) {
1373 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001374 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1375 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001376 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001377 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001378}
1379
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001380/// hasObjCExceptionAttribute - Return true if this class or any super
1381/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001382static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001383 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001384 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001385 return true;
1386 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001387 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001388 return false;
1389}
1390
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001391/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001392
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001393CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001394 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001395 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001396 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001397}
1398
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001399/// GetClass - Return a reference to the class for the given interface
1400/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001401llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001402 const ObjCInterfaceDecl *ID) {
1403 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001404}
1405
1406/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001407llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1408 bool lval) {
1409 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001410}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001411llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001412 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001413 return EmitSelector(Builder, Method->getSelector());
1414}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001415
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001416llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001417 if (T->isObjCIdType() ||
1418 T->isObjCQualifiedIdType()) {
1419 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001420 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001421 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001422 if (T->isObjCClassType() ||
1423 T->isObjCQualifiedClassType()) {
1424 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001425 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001426 }
1427 if (T->isObjCObjectPointerType())
1428 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1429
John McCall2ca705e2010-07-24 00:37:23 +00001430 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1431 return 0;
1432}
1433
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001434/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001435/*
1436 struct __builtin_CFString {
1437 const int *isa; // point to __CFConstantStringClassReference
1438 int flags;
1439 const char *str;
1440 long length;
1441 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001442*/
1443
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001444/// or Generate a constant NSString object.
1445/*
1446 struct __builtin_NSString {
1447 const int *isa; // point to __NSConstantStringClassReference
1448 const char *str;
1449 unsigned int length;
1450 };
1451*/
1452
Fariborz Jahanian71394042009-01-23 23:53:38 +00001453llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001454 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001455 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1456 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001457 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001458}
1459
1460/// Generates a message send where the super is the receiver. This is
1461/// a message send to self with special delivery semantics indicating
1462/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001463CodeGen::RValue
1464CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001465 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001466 QualType ResultType,
1467 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001468 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001469 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001470 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001471 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001472 const CodeGen::CallArgList &CallArgs,
1473 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001474 // Create and init a super structure; this is a (receiver, class)
1475 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001476 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001477 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001478 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001479 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001480 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001481 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001482
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001483 // If this is a class message the metaclass is passed as the target.
1484 llvm::Value *Target;
1485 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001486 if (isCategoryImpl) {
1487 // Message sent to 'super' in a class method defined in a category
1488 // implementation requires an odd treatment.
1489 // If we are in a class method, we must retrieve the
1490 // _metaclass_ for the current class, pointed at by
1491 // the class's "isa" pointer. The following assumes that
1492 // isa" is the first ivar in a class (which it must be).
1493 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1494 Target = CGF.Builder.CreateStructGEP(Target, 0);
1495 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001496 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001497 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1498 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1499 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1500 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001501 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001502 }
1503 else if (isCategoryImpl)
1504 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1505 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001506 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1507 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1508 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001509 }
Mike Stump18bb9282009-05-16 07:57:57 +00001510 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1511 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001512 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001513 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001514 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001515 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001516 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001517 return EmitMessageSend(CGF, Return, ResultType,
1518 EmitSelector(CGF.Builder, Sel),
1519 ObjCSuper, ObjCTypes.SuperPtrCTy,
1520 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001521}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001522
1523/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001524CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001525 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001526 QualType ResultType,
1527 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001528 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001529 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001530 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001531 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001532 return EmitMessageSend(CGF, Return, ResultType,
1533 EmitSelector(CGF.Builder, Sel),
1534 Receiver, CGF.getContext().getObjCIdType(),
1535 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001536}
1537
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001538CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001539CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1540 ReturnValueSlot Return,
1541 QualType ResultType,
1542 llvm::Value *Sel,
1543 llvm::Value *Arg0,
1544 QualType Arg0Ty,
1545 bool IsSuper,
1546 const CallArgList &CallArgs,
1547 const ObjCMethodDecl *Method,
1548 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001549 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001550 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001551 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001552 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1553 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001554 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001555
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001556 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001557 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001558 FunctionType::ExtInfo());
Chris Lattner2192fe52011-07-18 04:24:23 +00001559 llvm::FunctionType *FTy =
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001560 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001561
Anders Carlsson280e61f12010-06-21 20:59:55 +00001562 if (Method)
1563 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1564 CGM.getContext().getCanonicalType(ResultType) &&
1565 "Result type mismatch!");
1566
John McCall5880fb82011-05-14 21:12:11 +00001567 NullReturnState nullReturn;
1568
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001569 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001570 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCall5880fb82011-05-14 21:12:11 +00001571 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001572 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001573 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001574 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1575 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1576 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001577 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001578 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001579 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001580 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001581 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall5880fb82011-05-14 21:12:11 +00001582 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1583 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001584}
1585
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001586static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1587 if (FQT.isObjCGCStrong())
1588 return Qualifiers::Strong;
1589
John McCall31168b02011-06-15 23:02:42 +00001590 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001591 return Qualifiers::Weak;
1592
1593 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1594 return Qualifiers::Strong;
1595
1596 if (const PointerType *PT = FQT->getAs<PointerType>())
1597 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1598
1599 return Qualifiers::GCNone;
1600}
1601
John McCall351762c2011-02-07 10:33:21 +00001602llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1603 const CGBlockInfo &blockInfo) {
1604 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001605 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001606
Douglas Gregor79a91412011-09-13 17:21:33 +00001607 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001608 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001609 return nullPtr;
1610
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001611 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001612 SkipIvars.clear();
1613 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001614 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1615 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001616
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001617 // __isa is the first field in block descriptor and must assume by runtime's
1618 // convention that it is GC'able.
1619 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001620
1621 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1622
1623 // Calculate the basic layout of the block structure.
1624 const llvm::StructLayout *layout =
1625 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1626
1627 // Ignore the optional 'this' capture: C++ objects are not assumed
1628 // to be GC'ed.
1629
1630 // Walk the captured variables.
1631 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1632 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1633 const VarDecl *variable = ci->getVariable();
1634 QualType type = variable->getType();
1635
1636 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1637
1638 // Ignore constant captures.
1639 if (capture.isConstant()) continue;
1640
1641 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1642
1643 // __block variables are passed by their descriptor address.
1644 if (ci->isByRef()) {
1645 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001646 continue;
John McCall351762c2011-02-07 10:33:21 +00001647 }
1648
1649 assert(!type->isArrayType() && "array variable should not be caught");
1650 if (const RecordType *record = type->getAs<RecordType>()) {
1651 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001652 continue;
1653 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001654
John McCall351762c2011-02-07 10:33:21 +00001655 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1656 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1657
1658 if (GCAttr == Qualifiers::Strong)
1659 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1660 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001661 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001662 SkipIvars.push_back(GC_IVAR(fieldOffset,
1663 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001664 }
1665
1666 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001667 return nullPtr;
1668
1669 // Sort on byte position; captures might not be allocated in order,
1670 // and unions can do funny things.
1671 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1672 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001673
1674 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001675 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001676 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1677 printf("\n block variable layout for block: ");
1678 const unsigned char *s = (unsigned char*)BitMap.c_str();
1679 for (unsigned i = 0; i < BitMap.size(); i++)
1680 if (!(s[i] & 0xf0))
1681 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1682 else
1683 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1684 printf("\n");
1685 }
1686
1687 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001688}
1689
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001690llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001691 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001692 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001693 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001694 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1695
Owen Andersonade90fd2009-07-29 18:54:39 +00001696 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001697 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001698}
1699
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001700void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001701 // FIXME: We shouldn't need this, the protocol decl should contain enough
1702 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001703 DefinedProtocols.insert(PD->getIdentifier());
1704
1705 // If we have generated a forward reference to this protocol, emit
1706 // it now. Otherwise do nothing, the protocol objects are lazily
1707 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001708 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001709 GetOrEmitProtocol(PD);
1710}
1711
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001712llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001713 if (DefinedProtocols.count(PD->getIdentifier()))
1714 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001715
Daniel Dunbarc475d422008-10-29 22:36:39 +00001716 return GetOrEmitProtocolRef(PD);
1717}
1718
Daniel Dunbarb036db82008-08-13 03:21:16 +00001719/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001720// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1721struct _objc_protocol {
1722struct _objc_protocol_extension *isa;
1723char *protocol_name;
1724struct _objc_protocol_list *protocol_list;
1725struct _objc__method_prototype_list *instance_methods;
1726struct _objc__method_prototype_list *class_methods
1727};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001728
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001729See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001730*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001731llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1732 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1733
1734 // Early exit if a defining object has already been generated.
1735 if (Entry && Entry->hasInitializer())
1736 return Entry;
1737
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001738 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001739 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001740 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1741
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001742 // Construct method lists.
1743 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1744 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001745 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001746 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001747 ObjCMethodDecl *MD = *i;
1748 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001749 if (!C)
1750 return GetOrEmitProtocolRef(PD);
1751
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001752 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1753 OptInstanceMethods.push_back(C);
1754 } else {
1755 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001756 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001757 }
1758
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001759 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001760 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001761 ObjCMethodDecl *MD = *i;
1762 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001763 if (!C)
1764 return GetOrEmitProtocolRef(PD);
1765
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001766 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1767 OptClassMethods.push_back(C);
1768 } else {
1769 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001770 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001771 }
1772
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001773 llvm::Constant *Values[] = {
1774 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods),
1775 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001776 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001777 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001778 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001779 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001780 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001781 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001782 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001783 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001784 ClassMethods)
1785 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001786 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001787 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001788
Daniel Dunbarb036db82008-08-13 03:21:16 +00001789 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001790 // Already created, fix the linkage and update the initializer.
1791 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001792 Entry->setInitializer(Init);
1793 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001794 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001795 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001796 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001797 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001798 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001799 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001800 // FIXME: Is this necessary? Why only for protocol?
1801 Entry->setAlignment(4);
1802 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001803 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001804
1805 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001806}
1807
Daniel Dunbarc475d422008-10-29 22:36:39 +00001808llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001809 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1810
1811 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001812 // We use the initializer as a marker of whether this is a forward
1813 // reference or not. At module finalization we add the empty
1814 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001815 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001816 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001817 llvm::GlobalValue::ExternalLinkage,
1818 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001819 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001820 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001821 // FIXME: Is this necessary? Why only for protocol?
1822 Entry->setAlignment(4);
1823 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001824
Daniel Dunbarb036db82008-08-13 03:21:16 +00001825 return Entry;
1826}
1827
1828/*
1829 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001830 uint32_t size;
1831 struct objc_method_description_list *optional_instance_methods;
1832 struct objc_method_description_list *optional_class_methods;
1833 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001834 };
1835*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001836llvm::Constant *
1837CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1838 const ConstantVector &OptInstanceMethods,
1839 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001840 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001841 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001842 llvm::Constant *Values[] = {
1843 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001844 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001845 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001846 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001847 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001848 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001849 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001850 OptClassMethods),
1851 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
1852 ObjCTypes)
1853 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001855 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001856 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001858 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001859
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001860 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001861 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001862
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001863 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001864 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001865 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001866 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001867}
1868
1869/*
1870 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001871 struct objc_protocol_list *next;
1872 long count;
1873 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001874 };
1875*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001876llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001877CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001878 ObjCProtocolDecl::protocol_iterator begin,
1879 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001880 std::vector<llvm::Constant*> ProtocolRefs;
1881
Daniel Dunbardec75f82008-08-21 21:57:41 +00001882 for (; begin != end; ++begin)
1883 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001884
1885 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001886 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001887 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001888
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001889 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001890 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001891
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001892 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001893 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001894 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001895 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001896 ProtocolRefs.size() - 1);
1897 Values[2] =
1898 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1899 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001900 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001901
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001902 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001903 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001904 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001905 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001906 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001907}
1908
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001909void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1910 std::vector<llvm::Constant*> &Properties,
1911 const Decl *Container,
1912 const ObjCProtocolDecl *PROTO,
1913 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001914 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1915 E = PROTO->protocol_end(); P != E; ++P)
1916 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1917 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1918 E = PROTO->prop_end(); I != E; ++I) {
1919 const ObjCPropertyDecl *PD = *I;
1920 if (!PropertySet.insert(PD->getIdentifier()))
1921 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001922 llvm::Constant *Prop[] = {
1923 GetPropertyName(PD->getIdentifier()),
1924 GetPropertyTypeString(PD, Container)
1925 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001926 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1927 }
1928}
1929
Daniel Dunbarb036db82008-08-13 03:21:16 +00001930/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001931 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001932 const char * const name;
1933 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001934 };
1935
1936 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001937 uint32_t entsize; // sizeof (struct _objc_property)
1938 uint32_t prop_count;
1939 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001940 };
1941*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001942llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001943 const Decl *Container,
1944 const ObjCContainerDecl *OCD,
1945 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001946 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001947 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001948 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1949 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001950 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001951 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001952 llvm::Constant *Prop[] = {
1953 GetPropertyName(PD->getIdentifier()),
1954 GetPropertyTypeString(PD, Container)
1955 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001956 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001957 Prop));
1958 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001959 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001960 for (ObjCInterfaceDecl::all_protocol_iterator
1961 P = OID->all_referenced_protocol_begin(),
1962 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001963 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1964 ObjCTypes);
1965 }
1966 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1967 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1968 E = CD->protocol_end(); P != E; ++P)
1969 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1970 ObjCTypes);
1971 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001972
1973 // Return null for empty list.
1974 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001975 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001976
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001977 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001978 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001979 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001980 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1981 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001982 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001983 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001984 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001985 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001986
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001987 llvm::GlobalVariable *GV =
1988 CreateMetadataVar(Name, Init,
1989 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001990 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001991 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001992 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001993 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001994}
1995
1996/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001997 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001998 int count;
1999 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002000 };
2001*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002002llvm::Constant *
2003CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002004 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002005 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002006 ObjCTypes.SelectorPtrTy),
2007 GetMethodVarType(MD)
2008 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002009 if (!Desc[1])
2010 return 0;
2011
Owen Anderson0e0189d2009-07-27 22:29:56 +00002012 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002013 Desc);
2014}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002015
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002016llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002017 const char *Section,
2018 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002019 // Return null for empty list.
2020 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002021 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002022
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002023 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002024 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002025 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002026 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002027 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002028 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002029
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002030 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002031 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002032 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002033}
2034
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002035/*
2036 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002037 char *category_name;
2038 char *class_name;
2039 struct _objc_method_list *instance_methods;
2040 struct _objc_method_list *class_methods;
2041 struct _objc_protocol_list *protocols;
2042 uint32_t size; // <rdar://4585769>
2043 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002044 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002045*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002046void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002047 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002048
Mike Stump18bb9282009-05-16 07:57:57 +00002049 // FIXME: This is poor design, the OCD should have a pointer to the category
2050 // decl. Additionally, note that Category can be null for the @implementation
2051 // w/o an @interface case. Sema should just create one for us as it does for
2052 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002053 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002054 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002055 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002056
2057 llvm::SmallString<256> ExtName;
2058 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2059 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002060
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002061 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002062 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002063 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002064 // Instance methods should always be defined.
2065 InstanceMethods.push_back(GetMethodConstant(*i));
2066 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002067 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002068 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002069 // Class methods should always be defined.
2070 ClassMethods.push_back(GetMethodConstant(*i));
2071 }
2072
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002073 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002074 Values[0] = GetClassName(OCD->getIdentifier());
2075 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002076 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002077 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002078 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002079 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002080 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002081 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002082 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002083 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002084 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002085 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002086 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002087 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002088 Category->protocol_begin(),
2089 Category->protocol_end());
2090 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002091 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002092 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002093 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002094
2095 // If there is no category @interface then there can be no properties.
2096 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002097 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002098 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002099 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002100 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002101 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002102
Owen Anderson0e0189d2009-07-27 22:29:56 +00002103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002104 Values);
2105
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002106 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002107 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002108 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002109 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002110 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002111 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002112 // method definition entries must be clear for next implementation.
2113 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002114}
2115
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002116// FIXME: Get from somewhere?
2117enum ClassFlags {
2118 eClassFlags_Factory = 0x00001,
2119 eClassFlags_Meta = 0x00002,
2120 // <rdr://5142207>
2121 eClassFlags_HasCXXStructors = 0x02000,
2122 eClassFlags_Hidden = 0x20000,
2123 eClassFlags_ABI2_Hidden = 0x00010,
2124 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2125};
2126
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002127/*
2128 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002129 Class isa;
2130 Class super_class;
2131 const char *name;
2132 long version;
2133 long info;
2134 long instance_size;
2135 struct _objc_ivar_list *ivars;
2136 struct _objc_method_list *methods;
2137 struct _objc_cache *cache;
2138 struct _objc_protocol_list *protocols;
2139 // Objective-C 1.0 extensions (<rdr://4585769>)
2140 const char *ivar_layout;
2141 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002142 };
2143
2144 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002145*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002146void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002147 DefinedSymbols.insert(ID->getIdentifier());
2148
Chris Lattner86d7d912008-11-24 03:54:41 +00002149 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002150 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002151 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002152 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002153 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002154 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002155 Interface->all_referenced_protocol_begin(),
2156 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002157 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002158 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002159 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002160 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002161 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002162
2163 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002164 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002165 Flags |= eClassFlags_Hidden;
2166
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002167 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002168 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002169 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002170 // Instance methods should always be defined.
2171 InstanceMethods.push_back(GetMethodConstant(*i));
2172 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002173 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002174 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002175 // Class methods should always be defined.
2176 ClassMethods.push_back(GetMethodConstant(*i));
2177 }
2178
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002179 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002180 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002181 ObjCPropertyImplDecl *PID = *i;
2182
2183 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2184 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2185
2186 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2187 if (llvm::Constant *C = GetMethodConstant(MD))
2188 InstanceMethods.push_back(C);
2189 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2190 if (llvm::Constant *C = GetMethodConstant(MD))
2191 InstanceMethods.push_back(C);
2192 }
2193 }
2194
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002195 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002196 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002197 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002198 // Record a reference to the super class.
2199 LazySymbols.insert(Super->getIdentifier());
2200
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002201 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002202 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002203 ObjCTypes.ClassPtrTy);
2204 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002205 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002206 }
2207 Values[ 2] = GetClassName(ID->getIdentifier());
2208 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002209 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2210 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2211 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002212 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002213 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002214 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002215 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002216 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002217 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002218 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002219 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002220 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002221 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002222 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002223 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002224 std::string Name("\01L_OBJC_CLASS_");
2225 Name += ClassName;
2226 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2227 // Check for a forward reference.
2228 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2229 if (GV) {
2230 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2231 "Forward metaclass reference has incorrect type.");
2232 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2233 GV->setInitializer(Init);
2234 GV->setSection(Section);
2235 GV->setAlignment(4);
2236 CGM.AddUsedGlobal(GV);
2237 }
2238 else
2239 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002240 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002241 // method definition entries must be clear for next implementation.
2242 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002243}
2244
2245llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2246 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002247 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002248 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002249 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002250
John McCall457a04e2010-10-22 21:05:15 +00002251 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002252 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002253
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002254 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002255 // The isa for the metaclass is the root of the hierarchy.
2256 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2257 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2258 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002259 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002260 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002261 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002262 // The super class for the metaclass is emitted as the name of the
2263 // super class. The runtime fixes this up to point to the
2264 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002265 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002266 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002267 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002268 ObjCTypes.ClassPtrTy);
2269 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002270 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002271 }
2272 Values[ 2] = GetClassName(ID->getIdentifier());
2273 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002274 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2275 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2276 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002277 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002278 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002279 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002280 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002281 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002282 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002283 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002284 Values[ 9] = Protocols;
2285 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002286 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002287 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002288 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002289 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002290 Values);
2291
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002292 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002293 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002294
2295 // Check for a forward reference.
2296 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2297 if (GV) {
2298 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2299 "Forward metaclass reference has incorrect type.");
2300 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2301 GV->setInitializer(Init);
2302 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002303 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002304 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002305 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002306 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002307 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002308 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002309 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002310
2311 return GV;
2312}
2313
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002314llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002315 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002316
Mike Stump18bb9282009-05-16 07:57:57 +00002317 // FIXME: Should we look these up somewhere other than the module. Its a bit
2318 // silly since we only generate these while processing an implementation, so
2319 // exactly one pointer would work if know when we entered/exitted an
2320 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002321
2322 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002323 // Previously, metaclass with internal linkage may have been defined.
2324 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002325 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2326 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002327 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2328 "Forward metaclass reference has incorrect type.");
2329 return GV;
2330 } else {
2331 // Generate as an external reference to keep a consistent
2332 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002333 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002334 llvm::GlobalValue::ExternalLinkage,
2335 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002336 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002337 }
2338}
2339
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002340llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2341 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2342
2343 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2344 true)) {
2345 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2346 "Forward class metadata reference has incorrect type.");
2347 return GV;
2348 } else {
2349 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2350 llvm::GlobalValue::ExternalLinkage,
2351 0,
2352 Name);
2353 }
2354}
2355
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356/*
2357 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002358 uint32_t size;
2359 const char *weak_ivar_layout;
2360 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002361 };
2362*/
2363llvm::Constant *
2364CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002365 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002366 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002367
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002368 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002369 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002370 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002371 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002372 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002373
2374 // Return null if no extension bits are used.
2375 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002376 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002377
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002379 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002380 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002381 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002382 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002383}
2384
2385/*
2386 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002387 char *ivar_name;
2388 char *ivar_type;
2389 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002390 };
2391
2392 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002393 int ivar_count;
2394 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002395 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002396*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002397llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002398 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002399 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002400
2401 // When emitting the root class GCC emits ivar entries for the
2402 // actual class structure. It is not clear if we need to follow this
2403 // behavior; for now lets try and get away with not doing it. If so,
2404 // the cleanest solution would be to make up an ObjCInterfaceDecl
2405 // for the class.
2406 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002407 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002408
Jordy Rosea91768e2011-07-22 02:08:32 +00002409 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002410
Jordy Rosea91768e2011-07-22 02:08:32 +00002411 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002412 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002413 // Ignore unnamed bit-fields.
2414 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002415 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002416 llvm::Constant *Ivar[] = {
2417 GetMethodVarName(IVD->getIdentifier()),
2418 GetMethodVarType(IVD),
2419 llvm::ConstantInt::get(ObjCTypes.IntTy,
2420 ComputeIvarBaseOffset(CGM, OID, IVD))
2421 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002422 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002423 }
2424
2425 // Return null for empty list.
2426 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002427 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002428
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002429 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002430 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002431 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002432 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002433 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002434 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002435
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002436 llvm::GlobalVariable *GV;
2437 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002438 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002439 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002440 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002441 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002442 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002443 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002444 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002445 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002446}
2447
2448/*
2449 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002450 SEL method_name;
2451 char *method_types;
2452 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002453 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002454
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002455 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002456 struct objc_method_list *obsolete;
2457 int count;
2458 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002459 };
2460*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002461
2462/// GetMethodConstant - Return a struct objc_method constant for the
2463/// given method if it has been defined. The result is null if the
2464/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002465llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002466 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002467 if (!Fn)
2468 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002469
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002470 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002471 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002472 ObjCTypes.SelectorPtrTy),
2473 GetMethodVarType(MD),
2474 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2475 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002476 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002477}
2478
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002479llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002480 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002481 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002482 // Return null for empty list.
2483 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002484 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002485
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002486 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002487 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002488 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002489 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002491 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002492 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002493
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002494 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002495 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002496}
2497
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002498llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002499 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002500 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002501 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002502
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002503 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002504 llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002505 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002506 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002507 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002508 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002509 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002510 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002511 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002512
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002513 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002514}
2515
Daniel Dunbar30c65362009-03-09 20:09:19 +00002516llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002517CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002518 llvm::Constant *Init,
2519 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002520 unsigned Align,
2521 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002522 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002523 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002524 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002525 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002526 if (Section)
2527 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002528 if (Align)
2529 GV->setAlignment(Align);
2530 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002531 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002532 return GV;
2533}
2534
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002535llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002536 // Abuse this interface function as a place to finalize.
2537 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002538 return NULL;
2539}
2540
Chris Lattnerd4808922009-03-22 21:03:39 +00002541llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002542 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002543}
2544
Chris Lattnerd4808922009-03-22 21:03:39 +00002545llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002546 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002547}
2548
David Chisnall168b80f2010-12-26 22:13:16 +00002549llvm::Constant *CGObjCMac::GetGetStructFunction() {
2550 return ObjCTypes.getCopyStructFn();
2551}
2552llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002553 return ObjCTypes.getCopyStructFn();
2554}
2555
Chris Lattnerd4808922009-03-22 21:03:39 +00002556llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002557 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002558}
2559
John McCallbd309292010-07-06 01:34:17 +00002560void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2561 return EmitTryOrSynchronizedStmt(CGF, S);
2562}
2563
2564void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2565 const ObjCAtSynchronizedStmt &S) {
2566 return EmitTryOrSynchronizedStmt(CGF, S);
2567}
2568
John McCall65bea082010-07-21 06:59:36 +00002569namespace {
John McCallcda666c2010-07-21 07:22:38 +00002570 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002571 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002572 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002573 llvm::Value *CallTryExitVar;
2574 llvm::Value *ExceptionData;
2575 ObjCTypesHelper &ObjCTypes;
2576 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002577 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002578 llvm::Value *CallTryExitVar,
2579 llvm::Value *ExceptionData,
2580 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002581 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002582 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2583
John McCall30317fd2011-07-12 20:27:29 +00002584 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002585 // Check whether we need to call objc_exception_try_exit.
2586 // In optimized code, this branch will always be folded.
2587 llvm::BasicBlock *FinallyCallExit =
2588 CGF.createBasicBlock("finally.call_exit");
2589 llvm::BasicBlock *FinallyNoCallExit =
2590 CGF.createBasicBlock("finally.no_call_exit");
2591 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2592 FinallyCallExit, FinallyNoCallExit);
2593
2594 CGF.EmitBlock(FinallyCallExit);
2595 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2596 ->setDoesNotThrow();
2597
2598 CGF.EmitBlock(FinallyNoCallExit);
2599
2600 if (isa<ObjCAtTryStmt>(S)) {
2601 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002602 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2603 // Save the current cleanup destination in case there's
2604 // control flow inside the finally statement.
2605 llvm::Value *CurCleanupDest =
2606 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2607
John McCall65bea082010-07-21 06:59:36 +00002608 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2609
John McCallcebe0ca2010-08-11 00:16:14 +00002610 if (CGF.HaveInsertPoint()) {
2611 CGF.Builder.CreateStore(CurCleanupDest,
2612 CGF.getNormalCleanupDestSlot());
2613 } else {
2614 // Currently, the end of the cleanup must always exist.
2615 CGF.EnsureInsertPoint();
2616 }
2617 }
John McCall65bea082010-07-21 06:59:36 +00002618 } else {
2619 // Emit objc_sync_exit(expr); as finally's sole statement for
2620 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002621 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002622 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2623 ->setDoesNotThrow();
2624 }
2625 }
2626 };
John McCall42227ed2010-07-31 23:20:56 +00002627
2628 class FragileHazards {
2629 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002630 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002631 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2632
2633 llvm::InlineAsm *ReadHazard;
2634 llvm::InlineAsm *WriteHazard;
2635
2636 llvm::FunctionType *GetAsmFnType();
2637
2638 void collectLocals();
2639 void emitReadHazard(CGBuilderTy &Builder);
2640
2641 public:
2642 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002643
John McCall42227ed2010-07-31 23:20:56 +00002644 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002645 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002646 };
2647}
2648
2649/// Create the fragile-ABI read and write hazards based on the current
2650/// state of the function, which is presumed to be immediately prior
2651/// to a @try block. These hazards are used to maintain correct
2652/// semantics in the face of optimization and the fragile ABI's
2653/// cavalier use of setjmp/longjmp.
2654FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2655 collectLocals();
2656
2657 if (Locals.empty()) return;
2658
2659 // Collect all the blocks in the function.
2660 for (llvm::Function::iterator
2661 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2662 BlocksBeforeTry.insert(&*I);
2663
2664 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2665
2666 // Create a read hazard for the allocas. This inhibits dead-store
2667 // optimizations and forces the values to memory. This hazard is
2668 // inserted before any 'throwing' calls in the protected scope to
2669 // reflect the possibility that the variables might be read from the
2670 // catch block if the call throws.
2671 {
2672 std::string Constraint;
2673 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2674 if (I) Constraint += ',';
2675 Constraint += "*m";
2676 }
2677
2678 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2679 }
2680
2681 // Create a write hazard for the allocas. This inhibits folding
2682 // loads across the hazard. This hazard is inserted at the
2683 // beginning of the catch path to reflect the possibility that the
2684 // variables might have been written within the protected scope.
2685 {
2686 std::string Constraint;
2687 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2688 if (I) Constraint += ',';
2689 Constraint += "=*m";
2690 }
2691
2692 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2693 }
2694}
2695
2696/// Emit a write hazard at the current location.
2697void FragileHazards::emitWriteHazard() {
2698 if (Locals.empty()) return;
2699
Jay Foad5bd375a2011-07-15 08:37:34 +00002700 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002701}
2702
John McCall42227ed2010-07-31 23:20:56 +00002703void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2704 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002705 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002706}
2707
2708/// Emit read hazards in all the protected blocks, i.e. all the blocks
2709/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002710void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002711 if (Locals.empty()) return;
2712
2713 CGBuilderTy Builder(CGF.getLLVMContext());
2714
2715 // Iterate through all blocks, skipping those prior to the try.
2716 for (llvm::Function::iterator
2717 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2718 llvm::BasicBlock &BB = *FI;
2719 if (BlocksBeforeTry.count(&BB)) continue;
2720
2721 // Walk through all the calls in the block.
2722 for (llvm::BasicBlock::iterator
2723 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2724 llvm::Instruction &I = *BI;
2725
2726 // Ignore instructions that aren't non-intrinsic calls.
2727 // These are the only calls that can possibly call longjmp.
2728 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2729 if (isa<llvm::IntrinsicInst>(I))
2730 continue;
2731
2732 // Ignore call sites marked nounwind. This may be questionable,
2733 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2734 llvm::CallSite CS(&I);
2735 if (CS.doesNotThrow()) continue;
2736
John McCall2dd7d442010-08-04 05:59:32 +00002737 // Insert a read hazard before the call. This will ensure that
2738 // any writes to the locals are performed before making the
2739 // call. If the call throws, then this is sufficient to
2740 // guarantee correctness as long as it doesn't also write to any
2741 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002742 Builder.SetInsertPoint(&BB, BI);
2743 emitReadHazard(Builder);
2744 }
2745 }
2746}
2747
2748static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2749 if (V) S.insert(V);
2750}
2751
2752void FragileHazards::collectLocals() {
2753 // Compute a set of allocas to ignore.
2754 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2755 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2756 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002757
2758 // Collect all the allocas currently in the function. This is
2759 // probably way too aggressive.
2760 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2761 for (llvm::BasicBlock::iterator
2762 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2763 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2764 Locals.push_back(&*I);
2765}
2766
2767llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002768 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002769 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2770 tys[i] = Locals[i]->getType();
2771 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002772}
2773
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002774/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002775
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002776 Objective-C setjmp-longjmp (sjlj) Exception Handling
2777 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002778
John McCallbd309292010-07-06 01:34:17 +00002779 A catch buffer is a setjmp buffer plus:
2780 - a pointer to the exception that was caught
2781 - a pointer to the previous exception data buffer
2782 - two pointers of reserved storage
2783 Therefore catch buffers form a stack, with a pointer to the top
2784 of the stack kept in thread-local storage.
2785
2786 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2787 objc_exception_try_exit pops the given catch buffer, which is
2788 required to be the top of the EH stack.
2789 objc_exception_throw pops the top of the EH stack, writes the
2790 thrown exception into the appropriate field, and longjmps
2791 to the setjmp buffer. It crashes the process (with a printf
2792 and an abort()) if there are no catch buffers on the stack.
2793 objc_exception_extract just reads the exception pointer out of the
2794 catch buffer.
2795
2796 There's no reason an implementation couldn't use a light-weight
2797 setjmp here --- something like __builtin_setjmp, but API-compatible
2798 with the heavyweight setjmp. This will be more important if we ever
2799 want to implement correct ObjC/C++ exception interactions for the
2800 fragile ABI.
2801
2802 Note that for this use of setjmp/longjmp to be correct, we may need
2803 to mark some local variables volatile: if a non-volatile local
2804 variable is modified between the setjmp and the longjmp, it has
2805 indeterminate value. For the purposes of LLVM IR, it may be
2806 sufficient to make loads and stores within the @try (to variables
2807 declared outside the @try) volatile. This is necessary for
2808 optimized correctness, but is not currently being done; this is
2809 being tracked as rdar://problem/8160285
2810
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002811 The basic framework for a @try-catch-finally is as follows:
2812 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002813 objc_exception_data d;
2814 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002815 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002816
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002817 objc_exception_try_enter(&d);
2818 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002819 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002820 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002821 // exception path
2822 id _caught = objc_exception_extract(&d);
2823
2824 // enter new try scope for handlers
2825 if (!setjmp(d.jmp_buf)) {
2826 ... match exception and execute catch blocks ...
2827
2828 // fell off end, rethrow.
2829 _rethrow = _caught;
2830 ... jump-through-finally to finally_rethrow ...
2831 } else {
2832 // exception in catch block
2833 _rethrow = objc_exception_extract(&d);
2834 _call_try_exit = false;
2835 ... jump-through-finally to finally_rethrow ...
2836 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002837 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002838 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002839
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002840 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002841 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002842 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002843
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002844 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002845 ... dispatch to finally destination ...
2846
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002847 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002848 objc_exception_throw(_rethrow);
2849
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002850 finally_end:
2851 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002852
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002853 This framework differs slightly from the one gcc uses, in that gcc
2854 uses _rethrow to determine if objc_exception_try_exit should be called
2855 and if the object should be rethrown. This breaks in the face of
2856 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002857
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002858 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002859
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002860 - If there are no catch blocks, then we avoid emitting the second
2861 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002862
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002863 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2864 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002865
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002866 - FIXME: If there is no @finally block we can do a few more
2867 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002868
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869 Rethrows and Jumps-Through-Finally
2870 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002871
John McCallbd309292010-07-06 01:34:17 +00002872 '@throw;' is supported by pushing the currently-caught exception
2873 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002874
John McCallbd309292010-07-06 01:34:17 +00002875 Branches through the @finally block are handled with an ordinary
2876 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2877 exceptions are not compatible with C++ exceptions, and this is
2878 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002879
John McCallbd309292010-07-06 01:34:17 +00002880 @synchronized(expr) { stmt; } is emitted as if it were:
2881 id synch_value = expr;
2882 objc_sync_enter(synch_value);
2883 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002884*/
2885
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002886void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2887 const Stmt &S) {
2888 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002889
2890 // A destination for the fall-through edges of the catch handlers to
2891 // jump to.
2892 CodeGenFunction::JumpDest FinallyEnd =
2893 CGF.getJumpDestInCurrentScope("finally.end");
2894
2895 // A destination for the rethrow edge of the catch handlers to jump
2896 // to.
2897 CodeGenFunction::JumpDest FinallyRethrow =
2898 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002899
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002900 // For @synchronized, call objc_sync_enter(sync.expr). The
2901 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002902 // @synchronized. We can't avoid a temp here because we need the
2903 // value to be preserved. If the backend ever does liveness
2904 // correctly after setjmp, this will be unnecessary.
2905 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002906 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002907 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002908 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2909 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002910 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2911 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002912
2913 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2914 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002915 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002916
John McCall2dd7d442010-08-04 05:59:32 +00002917 // Allocate memory for the setjmp buffer. This needs to be kept
2918 // live throughout the try and catch blocks.
2919 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2920 "exceptiondata.ptr");
2921
John McCall42227ed2010-07-31 23:20:56 +00002922 // Create the fragile hazards. Note that this will not capture any
2923 // of the allocas required for exception processing, but will
2924 // capture the current basic block (which extends all the way to the
2925 // setjmp call) as "before the @try".
2926 FragileHazards Hazards(CGF);
2927
John McCallbd309292010-07-06 01:34:17 +00002928 // Create a flag indicating whether the cleanup needs to call
2929 // objc_exception_try_exit. This is true except when
2930 // - no catches match and we're branching through the cleanup
2931 // just to rethrow the exception, or
2932 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002933 // The setjmp-safety rule here is that we should always store to this
2934 // variable in a place that dominates the branch through the cleanup
2935 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002936 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002937 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002938
John McCall9916e3f2010-10-04 23:42:51 +00002939 // A slot containing the exception to rethrow. Only needed when we
2940 // have both a @catch and a @finally.
2941 llvm::Value *PropagatingExnVar = 0;
2942
John McCallbd309292010-07-06 01:34:17 +00002943 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002944 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002945 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002946 CallTryExitVar,
2947 ExceptionData,
2948 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002949
2950 // Enter a try block:
2951 // - Call objc_exception_try_enter to push ExceptionData on top of
2952 // the EH stack.
2953 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2954 ->setDoesNotThrow();
2955
2956 // - Call setjmp on the exception data buffer.
2957 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2958 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2959 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00002960 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00002961 llvm::CallInst *SetJmpResult =
2962 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2963 SetJmpResult->setDoesNotThrow();
2964
2965 // If setjmp returned 0, enter the protected block; otherwise,
2966 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002967 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2968 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002969 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002970 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2971 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002972
John McCallbd309292010-07-06 01:34:17 +00002973 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002974 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002975 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002976 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002977 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002978
2979 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002980
John McCallbd309292010-07-06 01:34:17 +00002981 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002982 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002983
John McCall42227ed2010-07-31 23:20:56 +00002984 // Don't optimize loads of the in-scope locals across this point.
2985 Hazards.emitWriteHazard();
2986
John McCallbd309292010-07-06 01:34:17 +00002987 // For a @synchronized (or a @try with no catches), just branch
2988 // through the cleanup to the rethrow block.
2989 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2990 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002991 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002992 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002993
2994 // Otherwise, we have to match against the caught exceptions.
2995 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002996 // Retrieve the exception object. We may emit multiple blocks but
2997 // nothing can cross this so the value is already in SSA form.
2998 llvm::CallInst *Caught =
2999 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3000 ExceptionData, "caught");
3001 Caught->setDoesNotThrow();
3002
John McCallbd309292010-07-06 01:34:17 +00003003 // Push the exception to rethrow onto the EH value stack for the
3004 // benefit of any @throws in the handlers.
3005 CGF.ObjCEHValueStack.push_back(Caught);
3006
Douglas Gregor96c79492010-04-23 22:50:49 +00003007 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008
John McCall2dd7d442010-08-04 05:59:32 +00003009 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003010
John McCall2dd7d442010-08-04 05:59:32 +00003011 llvm::BasicBlock *CatchBlock = 0;
3012 llvm::BasicBlock *CatchHandler = 0;
3013 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003014 // Save the currently-propagating exception before
3015 // objc_exception_try_enter clears the exception slot.
3016 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3017 "propagating_exception");
3018 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3019
John McCall2dd7d442010-08-04 05:59:32 +00003020 // Enter a new exception try block (in case a @catch block
3021 // throws an exception).
3022 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3023 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003024
John McCall2dd7d442010-08-04 05:59:32 +00003025 llvm::CallInst *SetJmpResult =
3026 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3027 "setjmp.result");
3028 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003029
John McCall2dd7d442010-08-04 05:59:32 +00003030 llvm::Value *Threw =
3031 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3032
3033 CatchBlock = CGF.createBasicBlock("catch");
3034 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3035 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3036
3037 CGF.EmitBlock(CatchBlock);
3038 }
3039
3040 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003041
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003042 // Handle catch list. As a special case we check if everything is
3043 // matched and avoid generating code for falling off the end if
3044 // so.
3045 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003046 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3047 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003048
Douglas Gregor46a572b2010-04-26 16:46:50 +00003049 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003050 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003051
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003052 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003053 if (!CatchParam) {
3054 AllMatched = true;
3055 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003056 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003057
John McCallbd309292010-07-06 01:34:17 +00003058 // catch(id e) always matches under this ABI, since only
3059 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003060 // FIXME: For the time being we also match id<X>; this should
3061 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003062 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003063 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003064 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003065
John McCallbd309292010-07-06 01:34:17 +00003066 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003067 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003068 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3069
Anders Carlsson9396a892008-09-11 09:15:33 +00003070 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003071 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003072 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003073
3074 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003075 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003076 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003077
Anders Carlsson9396a892008-09-11 09:15:33 +00003078 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003079
3080 // The scope of the catch variable ends right here.
3081 CatchVarCleanups.ForceCleanup();
3082
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003083 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003084 break;
3085 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003086
Steve Naroff7cae42b2009-07-10 23:34:53 +00003087 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003088 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003089
3090 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003091 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3092 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003093
3094 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003095 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003096
John McCallbd309292010-07-06 01:34:17 +00003097 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003098 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3099 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003100 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003101
John McCallbd309292010-07-06 01:34:17 +00003102 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3103 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003104
3105 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003106 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003107
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003108 // Emit the @catch block.
3109 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003110
3111 // Collect any cleanups for the catch variable. The scope lasts until
3112 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003113 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003114
John McCall1c9c3fd2010-10-15 04:57:14 +00003115 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003116 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003117
John McCallbd309292010-07-06 01:34:17 +00003118 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003119 llvm::Value *Tmp =
3120 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003121 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003122 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003123
Anders Carlsson9396a892008-09-11 09:15:33 +00003124 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003125
3126 // We're done with the catch variable.
3127 CatchVarCleanups.ForceCleanup();
3128
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003129 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003130
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003131 CGF.EmitBlock(NextCatchBlock);
3132 }
3133
John McCallbd309292010-07-06 01:34:17 +00003134 CGF.ObjCEHValueStack.pop_back();
3135
John McCall2dd7d442010-08-04 05:59:32 +00003136 // If nothing wanted anything to do with the caught exception,
3137 // kill the extract call.
3138 if (Caught->use_empty())
3139 Caught->eraseFromParent();
3140
3141 if (!AllMatched)
3142 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3143
3144 if (HasFinally) {
3145 // Emit the exception handler for the @catch blocks.
3146 CGF.EmitBlock(CatchHandler);
3147
3148 // In theory we might now need a write hazard, but actually it's
3149 // unnecessary because there's no local-accessing code between
3150 // the try's write hazard and here.
3151 //Hazards.emitWriteHazard();
3152
John McCall9916e3f2010-10-04 23:42:51 +00003153 // Extract the new exception and save it to the
3154 // propagating-exception slot.
3155 assert(PropagatingExnVar);
3156 llvm::CallInst *NewCaught =
3157 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3158 ExceptionData, "caught");
3159 NewCaught->setDoesNotThrow();
3160 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3161
John McCall2dd7d442010-08-04 05:59:32 +00003162 // Don't pop the catch handler; the throw already did.
3163 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003164 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003165 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003166 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
John McCall42227ed2010-07-31 23:20:56 +00003168 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003169 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003170
John McCallbd309292010-07-06 01:34:17 +00003171 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003172 CGF.Builder.restoreIP(TryFallthroughIP);
3173 if (CGF.HaveInsertPoint())
3174 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003175 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003176 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003177
John McCallbd309292010-07-06 01:34:17 +00003178 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003179 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003180 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003181 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003182 // If we have a propagating-exception variable, check it.
3183 llvm::Value *PropagatingExn;
3184 if (PropagatingExnVar) {
3185 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003186
John McCall9916e3f2010-10-04 23:42:51 +00003187 // Otherwise, just look in the buffer for the exception to throw.
3188 } else {
3189 llvm::CallInst *Caught =
3190 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3191 ExceptionData);
3192 Caught->setDoesNotThrow();
3193 PropagatingExn = Caught;
3194 }
3195
3196 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003197 ->setDoesNotThrow();
3198 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003199 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003200
John McCall42227ed2010-07-31 23:20:56 +00003201 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003202}
3203
3204void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003205 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003206 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003207
Anders Carlssone005aa12008-09-09 16:16:55 +00003208 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003209 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003210 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003211 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003212 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003213 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003214 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003215 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003216 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003217
John McCallbd309292010-07-06 01:34:17 +00003218 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3219 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003220 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003221
3222 // Clear the insertion point to indicate we are in unreachable code.
3223 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003224}
3225
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003226/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003227/// object: objc_read_weak (id *src)
3228///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003229llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003230 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003231 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003232 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3233 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3234 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003235 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003236 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003237 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003238 return read_weak;
3239}
3240
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003241/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3242/// objc_assign_weak (id src, id *dst)
3243///
3244void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003245 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003246 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003247 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003248 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003249 assert(Size <= 8 && "does not support size > 8");
3250 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003251 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003252 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3253 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003254 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3255 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003256 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003257 src, dst, "weakassign");
3258 return;
3259}
3260
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003261/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3262/// objc_assign_global (id src, id *dst)
3263///
3264void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003265 llvm::Value *src, llvm::Value *dst,
3266 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003267 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003268 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003269 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003270 assert(Size <= 8 && "does not support size > 8");
3271 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003272 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003273 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3274 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003275 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3276 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003277 if (!threadlocal)
3278 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3279 src, dst, "globalassign");
3280 else
3281 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3282 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003283 return;
3284}
3285
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003286/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003287/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003288///
3289void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003290 llvm::Value *src, llvm::Value *dst,
3291 llvm::Value *ivarOffset) {
3292 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003293 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003294 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003295 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003296 assert(Size <= 8 && "does not support size > 8");
3297 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003298 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003299 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3300 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003301 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3302 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003303 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3304 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003305 return;
3306}
3307
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003308/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3309/// objc_assign_strongCast (id src, id *dst)
3310///
3311void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003312 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003313 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003314 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003315 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003316 assert(Size <= 8 && "does not support size > 8");
3317 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003318 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003319 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3320 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003321 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3322 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003323 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003324 src, dst, "weakassign");
3325 return;
3326}
3327
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003328void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003329 llvm::Value *DestPtr,
3330 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003331 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003332 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3333 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003334 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003335 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003336 return;
3337}
3338
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003339/// EmitObjCValueForIvar - Code Gen for ivar reference.
3340///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003341LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3342 QualType ObjectTy,
3343 llvm::Value *BaseValue,
3344 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003345 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003346 const ObjCInterfaceDecl *ID =
3347 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003348 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3349 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003350}
3351
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003352llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003353 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003354 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003355 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003356 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003357 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3358 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003359}
3360
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003361/* *** Private Interface *** */
3362
3363/// EmitImageInfo - Emit the image info marker used to encode some module
3364/// level information.
3365///
3366/// See: <rdr://4810609&4810587&4810587>
3367/// struct IMAGE_INFO {
3368/// unsigned version;
3369/// unsigned flags;
3370/// };
3371enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003372 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003373 eImageInfo_GarbageCollected = (1 << 1),
3374 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003375 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3376
Daniel Dunbar5e639272010-04-25 20:39:01 +00003377 // A flag indicating that the module has no instances of a @synthesize of a
3378 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003379 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003380};
3381
Daniel Dunbar5e639272010-04-25 20:39:01 +00003382void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003383 unsigned version = 0; // Version is unused?
3384 unsigned flags = 0;
3385
3386 // FIXME: Fix and continue?
Douglas Gregor79a91412011-09-13 17:21:33 +00003387 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003388 flags |= eImageInfo_GarbageCollected;
Douglas Gregor79a91412011-09-13 17:21:33 +00003389 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003390 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003391
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003392 // We never allow @synthesize of a superclass property.
3393 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003394
Chris Lattner2192fe52011-07-18 04:24:23 +00003395 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner5e016ae2010-06-27 07:15:29 +00003396
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003397 // Emitted as int[2];
3398 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003399 llvm::ConstantInt::get(Int32Ty, version),
3400 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003401 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003402 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003403
3404 const char *Section;
3405 if (ObjCABI == 1)
3406 Section = "__OBJC, __image_info,regular";
3407 else
3408 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003409 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003410 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad83be3612011-06-22 09:24:39 +00003411 llvm::ConstantArray::get(AT, values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003412 Section,
3413 0,
3414 true);
3415 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003416}
3417
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003418
3419// struct objc_module {
3420// unsigned long version;
3421// unsigned long size;
3422// const char *name;
3423// Symtab symtab;
3424// };
3425
3426// FIXME: Get from somewhere
3427static const int ModuleVersion = 7;
3428
3429void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003430 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003431
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003432 llvm::Constant *Values[] = {
3433 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3434 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3435 // This used to be the filename, now it is unused. <rdr://4327263>
3436 GetClassName(&CGM.getContext().Idents.get("")),
3437 EmitModuleSymbols()
3438 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003439 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003440 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003441 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003442 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003443}
3444
3445llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003446 unsigned NumClasses = DefinedClasses.size();
3447 unsigned NumCategories = DefinedCategories.size();
3448
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003449 // Return null if no symbols were defined.
3450 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003451 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003452
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003453 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003454 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003455 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003456 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3457 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003458
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003459 // The runtime expects exactly the list of defined classes followed
3460 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003461 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003462 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003463 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003464 ObjCTypes.Int8PtrTy);
3465 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003466 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003467 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003468 ObjCTypes.Int8PtrTy);
3469
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003470 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003471 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003472 NumClasses + NumCategories),
3473 Symbols);
3474
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003475 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003476
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003477 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003478 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3479 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003480 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003481 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003482}
3483
John McCall31168b02011-06-15 23:02:42 +00003484llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3485 IdentifierInfo *II) {
3486 LazySymbols.insert(II);
3487
3488 llvm::GlobalVariable *&Entry = ClassReferences[II];
3489
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003490 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003491 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003492 llvm::ConstantExpr::getBitCast(GetClassName(II),
3493 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003494 Entry =
John McCall31168b02011-06-15 23:02:42 +00003495 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3496 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3497 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003498 }
John McCall31168b02011-06-15 23:02:42 +00003499
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003500 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003501}
3502
John McCall31168b02011-06-15 23:02:42 +00003503llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3504 const ObjCInterfaceDecl *ID) {
3505 return EmitClassRefFromId(Builder, ID->getIdentifier());
3506}
3507
3508llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3509 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3510 return EmitClassRefFromId(Builder, II);
3511}
3512
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003513llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3514 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003515 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003516
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003517 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003518 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003519 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003520 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003521 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003522 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3523 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003524 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003525 }
3526
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003527 if (lvalue)
3528 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003529 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003530}
3531
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003532llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003533 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003534
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003535 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003536 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003537 llvm::ConstantArray::get(VMContext,
3538 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003539 ((ObjCABI == 2) ?
3540 "__TEXT,__objc_classname,cstring_literals" :
3541 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003542 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003543
Owen Anderson170229f2009-07-14 23:10:40 +00003544 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003545}
3546
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003547llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3548 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3549 I = MethodDefinitions.find(MD);
3550 if (I != MethodDefinitions.end())
3551 return I->second;
3552
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003553 return NULL;
3554}
3555
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003556/// GetIvarLayoutName - Returns a unique constant for the given
3557/// ivar layout bitmap.
3558llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003559 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003560 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003561}
3562
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003563void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003564 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003565 bool ForStrongLayout,
3566 bool &HasUnion) {
3567 const RecordDecl *RD = RT->getDecl();
3568 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003569 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003570 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003571 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003572 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003573
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003574 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3575 ForStrongLayout, HasUnion);
3576}
3577
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003578void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003579 const llvm::StructLayout *Layout,
3580 const RecordDecl *RD,
Jordy Rosea91768e2011-07-22 02:08:32 +00003581 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 unsigned int BytePos, bool ForStrongLayout,
3583 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003584 bool IsUnion = (RD && RD->isUnion());
3585 uint64_t MaxUnionIvarSize = 0;
3586 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003587 const FieldDecl *MaxField = 0;
3588 const FieldDecl *MaxSkippedField = 0;
3589 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003590 uint64_t MaxFieldOffset = 0;
3591 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003592 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003593 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003594
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003595 if (RecFields.empty())
3596 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003597 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3598 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003599 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003600 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003601 FirstFieldDelta =
3602 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3603 }
3604
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003605 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003606 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003607 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003608 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003609 // Note that 'i' here is actually the field index inside RD of Field,
3610 // although this dependency is hidden.
3611 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003612 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003613 } else
John McCall31168b02011-06-15 23:02:42 +00003614 FieldOffset =
3615 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003616
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003617 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003618 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003619 LastFieldBitfieldOrUnnamed = Field;
3620 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003621 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003622 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003623
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003624 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003625 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003626 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003627 if (FQT->isUnionType())
3628 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003629
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003630 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003631 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003632 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003633 continue;
3634 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003635
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003636 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003638 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003639 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003640 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003641 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003642 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3643 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003644 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003645 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003646 FQT = CArray->getElementType();
3647 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003648
3649 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003650 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003651 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003652 int OldIndex = IvarsInfo.size() - 1;
3653 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003654
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003655 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003656 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003657 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003658
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003659 // Replicate layout information for each array element. Note that
3660 // one element is already done.
3661 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662 for (int FirstIndex = IvarsInfo.size() - 1,
3663 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003664 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003665 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3666 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3667 IvarsInfo[i].ivar_size));
3668 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3669 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3670 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003671 }
3672 continue;
3673 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003674 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003675 // At this point, we are done with Record/Union and array there of.
3676 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003677 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003678
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003679 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003680 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3681 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003682 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003683 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003684 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003685 MaxUnionIvarSize = UnionIvarSize;
3686 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003687 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003688 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003689 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003690 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003691 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003692 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003693 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003694 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3695 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003696 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003697 // FIXME: Why the asymmetry? We divide by word size in bits on other
3698 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003699 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003700 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003701 MaxSkippedUnionIvarSize = UnionIvarSize;
3702 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003703 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003704 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003705 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003706 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003707 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003708 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003709 }
3710 }
3711 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003712
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003713 if (LastFieldBitfieldOrUnnamed) {
3714 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3715 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003716 uint64_t BitFieldSize
3717 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003718 GC_IVAR skivar;
3719 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3720 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3721 + ((BitFieldSize % ByteSizeInBits) != 0);
3722 SkipIvars.push_back(skivar);
3723 } else {
3724 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3725 // Last field was unnamed. Must update skip info.
3726 unsigned FieldSize
3727 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3728 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3729 FieldSize / ByteSizeInBits));
3730 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003731 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003732
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003733 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003734 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003735 MaxUnionIvarSize));
3736 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003737 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003738 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003739}
3740
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003741/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3742/// the computations and returning the layout bitmap (for ivar or blocks) in
3743/// the given argument BitMap string container. Routine reads
3744/// two containers, IvarsInfo and SkipIvars which are assumed to be
3745/// filled already by the caller.
3746llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003747 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2192fe52011-07-18 04:24:23 +00003748 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003749
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003750 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003751 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003753 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003754 if (IvarsInfo[0].ivar_bytepos == 0) {
3755 WordsToSkip = 0;
3756 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003757 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003758 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3759 WordsToScan = IvarsInfo[0].ivar_size;
3760 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003761 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003762 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003763 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003764 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003765 // consecutive 'scanned' object pointers.
3766 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003767 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003768 // Skip over 'gc'able object pointer which lay over each other.
3769 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3770 continue;
3771 // Must skip over 1 or more words. We save current skip/scan values
3772 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003773 SKIP_SCAN SkScan;
3774 SkScan.skip = WordsToSkip;
3775 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003776 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003777
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003778 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003779 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3780 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003781 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003782 WordsToSkip = 0;
3783 WordsToScan = IvarsInfo[i].ivar_size;
3784 }
3785 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003786 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003787 SKIP_SCAN SkScan;
3788 SkScan.skip = WordsToSkip;
3789 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003790 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003791 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003792
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003793 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003794 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003795 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003796 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003797 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003798 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003799 IvarsInfo[LastIndex].ivar_bytepos +
3800 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003801 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003802 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003803 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003804 SKIP_SCAN SkScan;
3805 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3806 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003807 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003808 }
3809 }
3810 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3811 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003812 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003813 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003814 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3815 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3816 // 0xM0 followed by 0x0N detected.
3817 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3818 for (int j = i+1; j < SkipScan; j++)
3819 SkipScanIvars[j] = SkipScanIvars[j+1];
3820 --SkipScan;
3821 }
3822 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003823
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003824 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003825 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003826 unsigned char byte;
3827 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3828 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3829 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3830 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003831
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003832 // first skip big.
3833 for (unsigned int ix = 0; ix < skip_big; ix++)
3834 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003835
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003836 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003837 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003838 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003839 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003840 byte |= 0xf;
3841 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003842 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003843 byte |= scan_small;
3844 scan_small = 0;
3845 }
3846 BitMap += byte;
3847 }
3848 // next scan big
3849 for (unsigned int ix = 0; ix < scan_big; ix++)
3850 BitMap += (unsigned char)(0x0f);
3851 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003852 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003853 byte = scan_small;
3854 BitMap += byte;
3855 }
3856 }
3857 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003858 unsigned char zero = 0;
3859 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003860
3861 llvm::GlobalVariable * Entry =
3862 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3863 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003864 ((ObjCABI == 2) ?
3865 "__TEXT,__objc_classname,cstring_literals" :
3866 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003867 1, true);
3868 return getConstantGEP(VMContext, Entry, 0, 0);
3869}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003870
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003871/// BuildIvarLayout - Builds ivar layout bitmap for the class
3872/// implementation for the __strong or __weak case.
3873/// The layout map displays which words in ivar list must be skipped
3874/// and which must be scanned by GC (see below). String is built of bytes.
3875/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3876/// of words to skip and right nibble is count of words to scan. So, each
3877/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3878/// represented by a 0x00 byte which also ends the string.
3879/// 1. when ForStrongLayout is true, following ivars are scanned:
3880/// - id, Class
3881/// - object *
3882/// - __strong anything
3883///
3884/// 2. When ForStrongLayout is false, following ivars are scanned:
3885/// - __weak anything
3886///
3887llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3888 const ObjCImplementationDecl *OMD,
3889 bool ForStrongLayout) {
3890 bool hasUnion = false;
3891
Chris Lattner2192fe52011-07-18 04:24:23 +00003892 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregor79a91412011-09-13 17:21:33 +00003893 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00003894 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003895 return llvm::Constant::getNullValue(PtrTy);
3896
Jordy Rosea91768e2011-07-22 02:08:32 +00003897 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3898 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003899 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003900 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003901 IVD; IVD = IVD->getNextIvar())
3902 RecFields.push_back(cast<FieldDecl>(IVD));
3903 }
3904 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00003905 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00003906 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003907
Jordy Rosea91768e2011-07-22 02:08:32 +00003908 // FIXME: This is not ideal; we shouldn't have to do this copy.
3909 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00003910 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003911
3912 if (RecFields.empty())
3913 return llvm::Constant::getNullValue(PtrTy);
3914
3915 SkipIvars.clear();
3916 IvarsInfo.clear();
3917
3918 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3919 if (IvarsInfo.empty())
3920 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003921 // Sort on byte position in case we encounterred a union nested in
3922 // the ivar list.
3923 if (hasUnion && !IvarsInfo.empty())
3924 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3925 if (hasUnion && !SkipIvars.empty())
3926 std::sort(SkipIvars.begin(), SkipIvars.end());
3927
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003928 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003929 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003930
3931 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003932 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003933 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003934 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003935 const unsigned char *s = (unsigned char*)BitMap.c_str();
3936 for (unsigned i = 0; i < BitMap.size(); i++)
3937 if (!(s[i] & 0xf0))
3938 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3939 else
3940 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3941 printf("\n");
3942 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003943 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003944}
3945
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003946llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003947 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3948
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003949 // FIXME: Avoid std::string copying.
3950 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003951 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003952 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003953 ((ObjCABI == 2) ?
3954 "__TEXT,__objc_methname,cstring_literals" :
3955 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003956 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003957
Owen Anderson170229f2009-07-14 23:10:40 +00003958 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003959}
3960
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003961// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003962llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003963 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3964}
3965
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003966llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003967 std::string TypeStr;
3968 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3969
3970 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003971
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003972 if (!Entry)
3973 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003974 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003975 ((ObjCABI == 2) ?
3976 "__TEXT,__objc_methtype,cstring_literals" :
3977 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003978 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003979
Owen Anderson170229f2009-07-14 23:10:40 +00003980 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003981}
3982
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003983llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003984 std::string TypeStr;
Douglas Gregora9d84932011-05-27 01:19:52 +00003985 if (CGM.getContext().getObjCEncodingForMethodDecl(
3986 const_cast<ObjCMethodDecl*>(D),
3987 TypeStr))
3988 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003989
3990 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3991
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003992 if (!Entry)
3993 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003994 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003995 ((ObjCABI == 2) ?
3996 "__TEXT,__objc_methtype,cstring_literals" :
3997 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003998 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003999
Owen Anderson170229f2009-07-14 23:10:40 +00004000 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004001}
4002
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004003// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004004llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004005 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004006
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004007 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004008 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004009 llvm::ConstantArray::get(VMContext,
4010 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004011 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004012 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004013
Owen Anderson170229f2009-07-14 23:10:40 +00004014 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004015}
4016
4017// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004018// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004019llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004020CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4021 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004022 std::string TypeStr;
4023 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004024 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4025}
4026
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004027void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004028 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004029 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004030 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004031 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004032 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4033 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004034 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004035 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00004036 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004037 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004038}
4039
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004040void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004041 EmitModuleInfo();
4042
Daniel Dunbarc475d422008-10-29 22:36:39 +00004043 // Emit the dummy bodies for any protocols which were referenced but
4044 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004045 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004046 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4047 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004048 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004049
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004050 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004051 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004052 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004053 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004054 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004055 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004056 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004057 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004058 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004059 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004060 }
4061
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004062 // Add assembler directives to add lazy undefined symbol references
4063 // for classes which are referenced but not defined. This is
4064 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004065 //
4066 // FIXME: It would be nice if we had an LLVM construct for this.
4067 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4068 llvm::SmallString<256> Asm;
4069 Asm += CGM.getModule().getModuleInlineAsm();
4070 if (!Asm.empty() && Asm.back() != '\n')
4071 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004072
Daniel Dunbard027a922009-09-07 00:20:42 +00004073 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004074 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4075 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004076 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4077 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004078 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004079 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004080 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004081 }
4082
4083 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4084 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4085 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4086 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004087
Daniel Dunbard027a922009-09-07 00:20:42 +00004088 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004089 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004090}
4091
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004092CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004093 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004094 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004095 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004096 ObjCABI = 2;
4097}
4098
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004099/* *** */
4100
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004101ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004102 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004103 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4104 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004105
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004106 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004107 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004108 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004109 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004110 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004111
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004112 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004113 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004114 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004115
Mike Stump18bb9282009-05-16 07:57:57 +00004116 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4117 // comes out a bit cleaner.
Chris Lattner2192fe52011-07-18 04:24:23 +00004118 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004119 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004120
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004121 // I'm not sure I like this. The implicit coordination is a bit
4122 // gross. We should solve this in a reasonable fashion because this
4123 // is a pretty common task (match some runtime data structure with
4124 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004126 // FIXME: This is leaked.
4127 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004128
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004129 // struct _objc_super {
4130 // id self;
4131 // Class cls;
4132 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004133 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004134 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004135 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004136 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004137 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004138 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004139 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004140 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004141 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004142
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004143 SuperCTy = Ctx.getTagDeclType(RD);
4144 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004145
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004146 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004147 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4148
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004149 // struct _prop_t {
4150 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004151 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004152 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004153 PropertyTy = llvm::StructType::create("struct._prop_t",
4154 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004155
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004156 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004157 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004158 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004159 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004160 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004161 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004162 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4163 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004164 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004165 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004166
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004167 // struct _objc_method {
4168 // SEL _cmd;
4169 // char *method_type;
4170 // char *_imp;
4171 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004172 MethodTy = llvm::StructType::create("struct._objc_method",
4173 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4174 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004175
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004176 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004177 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004178 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004179
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004180}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004181
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004182ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004183 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004184 // struct _objc_method_description {
4185 // SEL name;
4186 // char *types;
4187 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004188 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004189 llvm::StructType::create("struct._objc_method_description",
4190 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004191
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004192 // struct _objc_method_description_list {
4193 // int count;
4194 // struct _objc_method_description[1];
4195 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004196 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004197 llvm::StructType::create("struct._objc_method_description_list",
4198 IntTy,
4199 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004200
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004201 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004202 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004203 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004204
Daniel Dunbarb036db82008-08-13 03:21:16 +00004205 // Protocol description structures
4206
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004207 // struct _objc_protocol_extension {
4208 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4209 // struct _objc_method_description_list *optional_instance_methods;
4210 // struct _objc_method_description_list *optional_class_methods;
4211 // struct _objc_property_list *instance_properties;
4212 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004213 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004214 llvm::StructType::create("struct._objc_protocol_extension",
4215 IntTy, MethodDescriptionListPtrTy,
4216 MethodDescriptionListPtrTy, PropertyListPtrTy,
4217 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004218
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004219 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004220 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004221
Daniel Dunbarc475d422008-10-29 22:36:39 +00004222 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004223
Chris Lattnera5f58b02011-07-09 17:41:47 +00004224 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004225 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004226
Chris Lattnera5f58b02011-07-09 17:41:47 +00004227 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004228 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004229 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004230 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004231 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004232 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004233
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004234 // struct _objc_protocol {
4235 // struct _objc_protocol_extension *isa;
4236 // char *protocol_name;
4237 // struct _objc_protocol **_objc_protocol_list;
4238 // struct _objc_method_description_list *instance_methods;
4239 // struct _objc_method_description_list *class_methods;
4240 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004241 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4242 llvm::PointerType::getUnqual(ProtocolListTy),
4243 MethodDescriptionListPtrTy,
4244 MethodDescriptionListPtrTy,
4245 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004246
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004247 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004248 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004249
Owen Anderson9793f0e2009-07-29 22:16:19 +00004250 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004251
4252 // Class description structures
4253
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004254 // struct _objc_ivar {
4255 // char *ivar_name;
4256 // char *ivar_type;
4257 // int ivar_offset;
4258 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004259 IvarTy = llvm::StructType::create("struct._objc_ivar",
4260 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004261
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004262 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004263 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004264 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004265 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004266
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004267 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004268 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004269 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004270 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004271
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004272 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004273 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004274 llvm::StructType::create("struct._objc_class_extension",
4275 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004276 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004277
Chris Lattner5ec04a52011-08-12 17:43:31 +00004278 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004279
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004280 // struct _objc_class {
4281 // Class isa;
4282 // Class super_class;
4283 // char *name;
4284 // long version;
4285 // long info;
4286 // long instance_size;
4287 // struct _objc_ivar_list *ivars;
4288 // struct _objc_method_list *methods;
4289 // struct _objc_cache *cache;
4290 // struct _objc_protocol_list *protocols;
4291 // char *ivar_layout;
4292 // struct _objc_class_ext *ext;
4293 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004294 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4295 llvm::PointerType::getUnqual(ClassTy),
4296 Int8PtrTy,
4297 LongTy,
4298 LongTy,
4299 LongTy,
4300 IvarListPtrTy,
4301 MethodListPtrTy,
4302 CachePtrTy,
4303 ProtocolListPtrTy,
4304 Int8PtrTy,
4305 ClassExtensionPtrTy,
4306 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004307
Owen Anderson9793f0e2009-07-29 22:16:19 +00004308 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004309
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004310 // struct _objc_category {
4311 // char *category_name;
4312 // char *class_name;
4313 // struct _objc_method_list *instance_method;
4314 // struct _objc_method_list *class_method;
4315 // uint32_t size; // sizeof(struct _objc_category)
4316 // struct _objc_property_list *instance_properties;// category's @property
4317 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004318 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004319 llvm::StructType::create("struct._objc_category",
4320 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4321 MethodListPtrTy, ProtocolListPtrTy,
4322 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004323
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004324 // Global metadata structures
4325
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004326 // struct _objc_symtab {
4327 // long sel_ref_cnt;
4328 // SEL *refs;
4329 // short cls_def_cnt;
4330 // short cat_def_cnt;
4331 // char *defs[cls_def_cnt + cat_def_cnt];
4332 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004333 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004334 llvm::StructType::create("struct._objc_symtab",
4335 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4336 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004337 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004338
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004339 // struct _objc_module {
4340 // long version;
4341 // long size; // sizeof(struct _objc_module)
4342 // char *name;
4343 // struct _objc_symtab* symtab;
4344 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004345 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004346 llvm::StructType::create("struct._objc_module",
4347 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004348
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004349
Mike Stump18bb9282009-05-16 07:57:57 +00004350 // FIXME: This is the size of the setjmp buffer and should be target
4351 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004352 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004353
Anders Carlsson9ff22482008-09-09 10:10:21 +00004354 // Exceptions
Chris Lattnera5f58b02011-07-09 17:41:47 +00004355 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004356 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004357
4358 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004359 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnera5f58b02011-07-09 17:41:47 +00004360 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4361 SetJmpBufferSize),
4362 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004363
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004364}
4365
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004366ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004367 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004368 // struct _method_list_t {
4369 // uint32_t entsize; // sizeof(struct _objc_method)
4370 // uint32_t method_count;
4371 // struct _objc_method method_list[method_count];
4372 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004373 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004374 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4375 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004376 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004377 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004378
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004379 // struct _protocol_t {
4380 // id isa; // NULL
4381 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004382 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004383 // const struct method_list_t * const instance_methods;
4384 // const struct method_list_t * const class_methods;
4385 // const struct method_list_t *optionalInstanceMethods;
4386 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004387 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004388 // const uint32_t size; // sizeof(struct _protocol_t)
4389 // const uint32_t flags; // = 0
4390 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004391
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004392 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004393 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004394 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004395
Chris Lattnera5f58b02011-07-09 17:41:47 +00004396 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004397 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4398 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4399 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4400 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4401 PropertyListPtrTy, IntTy, IntTy, NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004402
4403 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004404 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004405
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004406 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004407 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004408 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004409 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004410 ProtocolListnfABITy->setBody(LongTy,
4411 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4412 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004413
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004414 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004415 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004416
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004417 // struct _ivar_t {
4418 // unsigned long int *offset; // pointer to ivar offset location
4419 // char *name;
4420 // char *type;
4421 // uint32_t alignment;
4422 // uint32_t size;
4423 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004424 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004425 llvm::StructType::create("struct._ivar_t",
4426 llvm::PointerType::getUnqual(LongTy),
4427 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004428
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004429 // struct _ivar_list_t {
4430 // uint32 entsize; // sizeof(struct _ivar_t)
4431 // uint32 count;
4432 // struct _iver_t list[count];
4433 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004434 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004435 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4436 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004437
Owen Anderson9793f0e2009-07-29 22:16:19 +00004438 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004439
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004440 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004441 // uint32_t const flags;
4442 // uint32_t const instanceStart;
4443 // uint32_t const instanceSize;
4444 // uint32_t const reserved; // only when building for 64bit targets
4445 // const uint8_t * const ivarLayout;
4446 // const char *const name;
4447 // const struct _method_list_t * const baseMethods;
4448 // const struct _objc_protocol_list *const baseProtocols;
4449 // const struct _ivar_list_t *const ivars;
4450 // const uint8_t * const weakIvarLayout;
4451 // const struct _prop_list_t * const properties;
4452 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004453
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004454 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004455 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4456 IntTy, IntTy, IntTy, Int8PtrTy,
4457 Int8PtrTy, MethodListnfABIPtrTy,
4458 ProtocolListnfABIPtrTy,
4459 IvarListnfABIPtrTy,
4460 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004461
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004462 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004463 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004464 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4465 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004466
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004467 // struct _class_t {
4468 // struct _class_t *isa;
4469 // struct _class_t * const superclass;
4470 // void *cache;
4471 // IMP *vtable;
4472 // struct class_ro_t *ro;
4473 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004474
Chris Lattner5ec04a52011-08-12 17:43:31 +00004475 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004476 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4477 llvm::PointerType::getUnqual(ClassnfABITy),
4478 CachePtrTy,
4479 llvm::PointerType::getUnqual(ImpnfABITy),
4480 llvm::PointerType::getUnqual(ClassRonfABITy),
4481 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004482
Fariborz Jahanian71394042009-01-23 23:53:38 +00004483 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004484 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004485
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 // struct _category_t {
4487 // const char * const name;
4488 // struct _class_t *const cls;
4489 // const struct _method_list_t * const instance_methods;
4490 // const struct _method_list_t * const class_methods;
4491 // const struct _protocol_list_t * const protocols;
4492 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004493 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004494 CategorynfABITy = llvm::StructType::create("struct._category_t",
4495 Int8PtrTy, ClassnfABIPtrTy,
4496 MethodListnfABIPtrTy,
4497 MethodListnfABIPtrTy,
4498 ProtocolListnfABIPtrTy,
4499 PropertyListPtrTy,
4500 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004501
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004502 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004503 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4504 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004505
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004506 // MessageRefTy - LLVM for:
4507 // struct _message_ref_t {
4508 // IMP messenger;
4509 // SEL name;
4510 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004511
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004512 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004513 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004514 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004515 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004516 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004517 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004518 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004519 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004520 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004521 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004522
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004523 MessageRefCTy = Ctx.getTagDeclType(RD);
4524 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4525 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004526
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004527 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004528 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004529
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004530 // SuperMessageRefTy - LLVM for:
4531 // struct _super_message_ref_t {
4532 // SUPER_IMP messenger;
4533 // SEL name;
4534 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004535 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004536 llvm::StructType::create("struct._super_message_ref_t",
4537 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004538
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004539 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004540 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004541
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004542
4543 // struct objc_typeinfo {
4544 // const void** vtable; // objc_ehtype_vtable + 2
4545 // const char* name; // c++ typeinfo string
4546 // Class cls;
4547 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004548 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004549 llvm::StructType::create("struct._objc_typeinfo",
4550 llvm::PointerType::getUnqual(Int8PtrTy),
4551 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004552 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004553}
4554
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004555llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004556 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004557
Fariborz Jahanian71394042009-01-23 23:53:38 +00004558 return NULL;
4559}
4560
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004561void CGObjCNonFragileABIMac::AddModuleClassList(const
4562 std::vector<llvm::GlobalValue*>
4563 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004564 const char *SymbolName,
4565 const char *SectionName) {
4566 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004567
Daniel Dunbar19573e72009-05-15 21:48:48 +00004568 if (!NumClasses)
4569 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004570
Daniel Dunbar19573e72009-05-15 21:48:48 +00004571 std::vector<llvm::Constant*> Symbols(NumClasses);
4572 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004573 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004574 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004575 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004576 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004577 NumClasses),
4578 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004579
Daniel Dunbar19573e72009-05-15 21:48:48 +00004580 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004581 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004582 llvm::GlobalValue::InternalLinkage,
4583 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004584 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004585 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004586 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004587 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004588}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004589
Fariborz Jahanian71394042009-01-23 23:53:38 +00004590void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4591 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004592
Daniel Dunbar19573e72009-05-15 21:48:48 +00004593 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004594 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004595 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004596 "\01L_OBJC_LABEL_CLASS_$",
4597 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004598
Fariborz Jahanian67260552009-11-17 21:37:35 +00004599 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4600 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4601 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4602 continue;
4603 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004604 }
4605
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004606 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4607 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4608 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4609 continue;
4610 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4611 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004612
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004613 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004614 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4615 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004616
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004617 // Build list of all implemented category addresses in array
4618 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004619 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004620 "\01L_OBJC_LABEL_CATEGORY_$",
4621 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004622 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004623 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4624 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004625
Daniel Dunbar5e639272010-04-25 20:39:01 +00004626 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004627}
4628
John McCall9e8bb002011-05-14 03:10:52 +00004629/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4630/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004631/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004632/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004633bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4634 // At various points we've experimented with using vtable-based
4635 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004636 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4637 default:
John McCall9e8bb002011-05-14 03:10:52 +00004638 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004639 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004640 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004641 case CodeGenOptions::NonLegacy:
4642 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004643 case CodeGenOptions::Mixed:
4644 break;
4645 }
4646
4647 // If so, see whether this selector is in the white-list of things which must
4648 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004649 if (VTableDispatchMethods.empty()) {
4650 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4651 VTableDispatchMethods.insert(GetNullarySelector("class"));
4652 VTableDispatchMethods.insert(GetNullarySelector("self"));
4653 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4654 VTableDispatchMethods.insert(GetNullarySelector("length"));
4655 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004656
John McCall9e8bb002011-05-14 03:10:52 +00004657 // These are vtable-based if GC is disabled.
4658 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004659 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004660 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4661 VTableDispatchMethods.insert(GetNullarySelector("release"));
4662 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4663 }
4664
4665 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4666 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4667 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4668 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4669 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4670 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4671 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4672
4673 // These are vtable-based if GC is enabled.
4674 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004675 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004676 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4677 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4678
4679 // "countByEnumeratingWithState:objects:count"
4680 IdentifierInfo *KeyIdents[] = {
4681 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4682 &CGM.getContext().Idents.get("objects"),
4683 &CGM.getContext().Idents.get("count")
4684 };
4685 VTableDispatchMethods.insert(
4686 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4687 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004688 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004689
John McCall9e8bb002011-05-14 03:10:52 +00004690 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004691}
4692
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004693// Metadata flags
4694enum MetaDataDlags {
4695 CLS = 0x0,
4696 CLS_META = 0x1,
4697 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004698 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004699 CLS_EXCEPTION = 0x20,
4700
4701 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4702 CLS_HAS_IVAR_RELEASER = 0x40,
4703 /// class was compiled with -fobjc-arr
4704 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004705};
4706/// BuildClassRoTInitializer - generate meta-data for:
4707/// struct _class_ro_t {
4708/// uint32_t const flags;
4709/// uint32_t const instanceStart;
4710/// uint32_t const instanceSize;
4711/// uint32_t const reserved; // only when building for 64bit targets
4712/// const uint8_t * const ivarLayout;
4713/// const char *const name;
4714/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004715/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004716/// const struct _ivar_list_t *const ivars;
4717/// const uint8_t * const weakIvarLayout;
4718/// const struct _prop_list_t * const properties;
4719/// }
4720///
4721llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004722 unsigned flags,
4723 unsigned InstanceStart,
4724 unsigned InstanceSize,
4725 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004726 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004727 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004728
4729 if (CGM.getLangOptions().ObjCAutoRefCount)
4730 flags |= CLS_COMPILED_BY_ARC;
4731
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004732 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4733 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4734 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004735 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004736 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4737 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004738 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004739 // const struct _method_list_t * const baseMethods;
4740 std::vector<llvm::Constant*> Methods;
4741 std::string MethodListName("\01l_OBJC_$_");
4742 if (flags & CLS_META) {
4743 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004745 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004746 // Class methods should always be defined.
4747 Methods.push_back(GetMethodConstant(*i));
4748 }
4749 } else {
4750 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004751 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004752 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004753 // Instance methods should always be defined.
4754 Methods.push_back(GetMethodConstant(*i));
4755 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004756 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004757 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004758 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004760 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4761 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004762
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004763 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4764 if (llvm::Constant *C = GetMethodConstant(MD))
4765 Methods.push_back(C);
4766 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4767 if (llvm::Constant *C = GetMethodConstant(MD))
4768 Methods.push_back(C);
4769 }
4770 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004771 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004772 Values[ 5] = EmitMethodList(MethodListName,
4773 "__DATA, __objc_const", Methods);
4774
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004775 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4776 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004777 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004778 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004779 OID->all_referenced_protocol_begin(),
4780 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004781
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004782 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004783 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004784 else
4785 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004786 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4787 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004788 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004789 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004790 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004791 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4792 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004793 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004794 Values);
4795 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004796 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4797 llvm::GlobalValue::InternalLinkage,
4798 Init,
4799 (flags & CLS_META) ?
4800 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4801 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004802 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004803 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004804 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004805 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004806
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004807}
4808
4809/// BuildClassMetaData - This routine defines that to-level meta-data
4810/// for the given ClassName for:
4811/// struct _class_t {
4812/// struct _class_t *isa;
4813/// struct _class_t * const superclass;
4814/// void *cache;
4815/// IMP *vtable;
4816/// struct class_ro_t *ro;
4817/// }
4818///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004819llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004820 std::string &ClassName,
4821 llvm::Constant *IsAGV,
4822 llvm::Constant *SuperClassGV,
4823 llvm::Constant *ClassRoGV,
4824 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004825 llvm::Constant *Values[] = {
4826 IsAGV,
4827 SuperClassGV,
4828 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4829 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4830 ClassRoGV // &CLASS_RO_GV
4831 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832 if (!Values[1])
4833 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004834 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004835 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004836 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4837 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004838 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004839 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004840 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004841 if (HiddenVisibility)
4842 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004843 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004844}
4845
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004846bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004847CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004848 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004849}
4850
Daniel Dunbar961202372009-05-03 12:57:56 +00004851void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004852 uint32_t &InstanceStart,
4853 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004854 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004855 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004856
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004857 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004858 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004859
4860 // If there are no fields, the start is the same as the end.
4861 if (!RL.getFieldCount())
4862 InstanceStart = InstanceSize;
4863 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004864 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004865}
4866
Fariborz Jahanian71394042009-01-23 23:53:38 +00004867void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4868 std::string ClassName = ID->getNameAsString();
4869 if (!ObjCEmptyCacheVar) {
4870 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004871 CGM.getModule(),
4872 ObjCTypes.CacheTy,
4873 false,
4874 llvm::GlobalValue::ExternalLinkage,
4875 0,
4876 "_objc_empty_cache");
4877
Fariborz Jahanian71394042009-01-23 23:53:38 +00004878 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004879 CGM.getModule(),
4880 ObjCTypes.ImpnfABITy,
4881 false,
4882 llvm::GlobalValue::ExternalLinkage,
4883 0,
4884 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004885 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004886 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004887 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004888 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004889 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004890 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004891 uint32_t InstanceSize = InstanceStart;
4892 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004893 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4894 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004895
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004896 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004897
4898 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004899 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004900 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004901 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004902 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004903 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004904 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004905 // class is root
4906 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004907 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004908 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004909 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004910 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004911 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4912 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4913 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004914 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004915 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004916 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004917 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004918 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004919 ObjCMetaClassName +
4920 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004921 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004922 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004923 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004924 }
4925 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4926 InstanceStart,
4927 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004928 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004930 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4931 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004932 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004933
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004934 // Metadata for the class
4935 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004936 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004937 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00004938 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004939 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004940
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004941 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004942 flags |= CLS_EXCEPTION;
4943
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004944 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004945 flags |= CLS_ROOT;
4946 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004947 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004948 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004949 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004950 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004951 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004952 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004953 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004954 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004955 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004956 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004957 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004958 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004959 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004960
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004961 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004962 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004963 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4964 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004965 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004966
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004967 // Determine if this class is also "non-lazy".
4968 if (ImplementationIsNonLazy(ID))
4969 DefinedNonLazyClasses.push_back(ClassMD);
4970
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004971 // Force the definition of the EHType if necessary.
4972 if (flags & CLS_EXCEPTION)
4973 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00004974 // Make sure method definition entries are all clear for next implementation.
4975 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004976}
4977
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004978/// GenerateProtocolRef - This routine is called to generate code for
4979/// a protocol reference expression; as in:
4980/// @code
4981/// @protocol(Proto1);
4982/// @endcode
4983/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4984/// which will hold address of the protocol meta-data.
4985///
4986llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004987 const ObjCProtocolDecl *PD) {
4988
Fariborz Jahanian464423d2009-04-10 18:47:34 +00004989 // This routine is called for @protocol only. So, we must build definition
4990 // of protocol's meta-data (not a reference to it!)
4991 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004992 llvm::Constant *Init =
4993 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4994 ObjCTypes.ExternalProtocolPtrTy);
4995
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004996 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00004997 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004998
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004999 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5000 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005001 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005002 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005003 CGM.getModule(),
5004 Init->getType(), false,
5005 llvm::GlobalValue::WeakAnyLinkage,
5006 Init,
5007 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005008 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5009 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005010 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005011 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005012}
5013
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005014/// GenerateCategory - Build metadata for a category implementation.
5015/// struct _category_t {
5016/// const char * const name;
5017/// struct _class_t *const cls;
5018/// const struct _method_list_t * const instance_methods;
5019/// const struct _method_list_t * const class_methods;
5020/// const struct _protocol_list_t * const protocols;
5021/// const struct _prop_list_t * const properties;
5022/// }
5023///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005024void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005025 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005026 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005027 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5028 "_$_" + OCD->getNameAsString());
5029 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005030 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005031
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005032 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005033 Values[0] = GetClassName(OCD->getIdentifier());
5034 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005035 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005036 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005037 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5038
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005039 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005040 std::vector<llvm::Constant*> Methods;
5041 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005042 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005043 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005044
5045 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005046 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005047 // Instance methods should always be defined.
5048 Methods.push_back(GetMethodConstant(*i));
5049 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005050
5051 Values[2] = EmitMethodList(MethodListName,
5052 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005053 Methods);
5054
5055 MethodListName = Prefix;
5056 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5057 OCD->getNameAsString();
5058 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005059 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005060 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005061 // Class methods should always be defined.
5062 Methods.push_back(GetMethodConstant(*i));
5063 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064
5065 Values[3] = EmitMethodList(MethodListName,
5066 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005067 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005068 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005069 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005070 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005071 llvm::SmallString<256> ExtName;
5072 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5073 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005074 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005075 + Interface->getName() + "_$_"
5076 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005077 Category->protocol_begin(),
5078 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005079 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5080 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005081 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005082 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5083 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005084 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005085
5086 llvm::Constant *Init =
5087 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005088 Values);
5089 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005090 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005091 false,
5092 llvm::GlobalValue::InternalLinkage,
5093 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005094 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005095 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005096 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005097 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005098 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005099 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005100
5101 // Determine if this category is also "non-lazy".
5102 if (ImplementationIsNonLazy(OCD))
5103 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005104 // method definition entries must be clear for next implementation.
5105 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005106}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005107
5108/// GetMethodConstant - Return a struct objc_method constant for the
5109/// given method if it has been defined. The result is null if the
5110/// method has not been defined. The return value has type MethodPtrTy.
5111llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005112 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005113 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005114 if (!Fn)
5115 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005116
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005117 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005118 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005119 ObjCTypes.SelectorPtrTy),
5120 GetMethodVarType(MD),
5121 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5122 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005123 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005124}
5125
5126/// EmitMethodList - Build meta-data for method declarations
5127/// struct _method_list_t {
5128/// uint32_t entsize; // sizeof(struct _objc_method)
5129/// uint32_t method_count;
5130/// struct _objc_method method_list[method_count];
5131/// }
5132///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005133llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005134 const char *Section,
5135 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005136 // Return null for empty list.
5137 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005138 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005139
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005140 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005141 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005142 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005143 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005144 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005145 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005146 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005147 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005148 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005149 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005150
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005151 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005152 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005153 llvm::GlobalValue::InternalLinkage, Init, Name);
5154 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005155 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005156 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005157 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005158}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005159
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005160/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5161/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005162llvm::GlobalVariable *
5163CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5164 const ObjCIvarDecl *Ivar) {
5165 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005166 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005167 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005169 CGM.getModule().getGlobalVariable(Name);
5170 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005171 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005172 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005173 false,
5174 llvm::GlobalValue::ExternalLinkage,
5175 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005176 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005177 return IvarOffsetGV;
5178}
5179
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005180llvm::Constant *
5181CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5182 const ObjCIvarDecl *Ivar,
5183 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005184 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005185 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005186 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005187 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005188 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005189
Mike Stump18bb9282009-05-16 07:57:57 +00005190 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5191 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005192 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5193 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005194 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005195 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005196 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005197 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005198 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005199 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005200}
5201
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005202/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005203/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005204/// IvarListnfABIPtrTy.
5205/// struct _ivar_t {
5206/// unsigned long int *offset; // pointer to ivar offset location
5207/// char *name;
5208/// char *type;
5209/// uint32_t alignment;
5210/// uint32_t size;
5211/// }
5212/// struct _ivar_list_t {
5213/// uint32 entsize; // sizeof(struct _ivar_t)
5214/// uint32 count;
5215/// struct _iver_t list[count];
5216/// }
5217///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005218
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005219llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005220 const ObjCImplementationDecl *ID) {
5221
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005222 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005223
Jordy Rosea91768e2011-07-22 02:08:32 +00005224 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005225 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005227 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005228
Jordy Rosea91768e2011-07-22 02:08:32 +00005229 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005230 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005231 // Ignore unnamed bit-fields.
5232 if (!IVD->getDeclName())
5233 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005234 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005235 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005236 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005237 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5238 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005239 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005240 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005241 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005242 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005243 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005244 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005245 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005246 // NOTE. Size of a bitfield does not match gcc's, because of the
5247 // way bitfields are treated special in each. But I am told that
5248 // 'size' for bitfield ivars is ignored by the runtime so it does
5249 // not matter. If it matters, there is enough info to get the
5250 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005251 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005252 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005253 }
5254 // Return null for empty list.
5255 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005256 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005257
5258 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005259 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005260 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5261 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005262 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005263 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005264 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005265 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005266 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5267 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005268 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005269 llvm::GlobalValue::InternalLinkage,
5270 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005271 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005272 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005273 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005274 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005275
Chris Lattnerf56501c2009-07-17 23:57:13 +00005276 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005277 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005278}
5279
5280llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005281 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005282 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005283
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005284 if (!Entry) {
5285 // We use the initializer as a marker of whether this is a forward
5286 // reference or not. At module finalization we add the empty
5287 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288 Entry =
5289 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5290 llvm::GlobalValue::ExternalLinkage,
5291 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005292 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005293 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005294 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005295
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005296 return Entry;
5297}
5298
5299/// GetOrEmitProtocol - Generate the protocol meta-data:
5300/// @code
5301/// struct _protocol_t {
5302/// id isa; // NULL
5303/// const char * const protocol_name;
5304/// const struct _protocol_list_t * protocol_list; // super protocols
5305/// const struct method_list_t * const instance_methods;
5306/// const struct method_list_t * const class_methods;
5307/// const struct method_list_t *optionalInstanceMethods;
5308/// const struct method_list_t *optionalClassMethods;
5309/// const struct _prop_list_t * properties;
5310/// const uint32_t size; // sizeof(struct _protocol_t)
5311/// const uint32_t flags; // = 0
5312/// }
5313/// @endcode
5314///
5315
5316llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005317 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005318 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005319
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005320 // Early exit if a defining object has already been generated.
5321 if (Entry && Entry->hasInitializer())
5322 return Entry;
5323
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005324 // Construct method lists.
5325 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5326 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005327 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005328 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005329 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005330 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005331 if (!C)
5332 return GetOrEmitProtocolRef(PD);
5333
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005334 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5335 OptInstanceMethods.push_back(C);
5336 } else {
5337 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005338 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005339 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005340
5341 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005342 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005343 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005344 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005345 if (!C)
5346 return GetOrEmitProtocolRef(PD);
5347
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005348 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5349 OptClassMethods.push_back(C);
5350 } else {
5351 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005352 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005353 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005354
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005355 llvm::Constant *Values[10];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005356 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005357 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005358 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005359 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5360 PD->protocol_begin(),
5361 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005362
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005363 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005364 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005365 "__DATA, __objc_const",
5366 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005367 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005368 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005369 "__DATA, __objc_const",
5370 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005371 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005372 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005373 "__DATA, __objc_const",
5374 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005375 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005376 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005377 "__DATA, __objc_const",
5378 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005379 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005380 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005381 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005382 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005383 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005384 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005385 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005386 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005387
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005388 if (Entry) {
5389 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005390 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 Entry->setInitializer(Init);
5392 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005393 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005394 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5395 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5396 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005397 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005398 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005399 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005400 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005401 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005402 CGM.AddUsedGlobal(Entry);
5403
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005404 // Use this protocol meta-data to build protocol list table in section
5405 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005406 llvm::GlobalVariable *PTGV =
5407 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5408 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5409 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005410 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005411 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005412 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005413 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005414 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005415 return Entry;
5416}
5417
5418/// EmitProtocolList - Generate protocol list meta-data:
5419/// @code
5420/// struct _protocol_list_t {
5421/// long protocol_count; // Note, this is 32/64 bit
5422/// struct _protocol_t[protocol_count];
5423/// }
5424/// @endcode
5425///
5426llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005427CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005428 ObjCProtocolDecl::protocol_iterator begin,
5429 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005430 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005431
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005432 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005433 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005434 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005435
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005436 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005437 llvm::SmallString<256> TmpName;
5438 Name.toVector(TmpName);
5439 llvm::GlobalVariable *GV =
5440 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005441 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005442 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005443
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005444 for (; begin != end; ++begin)
5445 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5446
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005447 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005448 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005449 ObjCTypes.ProtocolnfABIPtrTy));
5450
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005451 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005452 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005453 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005454 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005455 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005456 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457 ProtocolRefs.size()),
5458 ProtocolRefs);
5459
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005460 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005461 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005462 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005463 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005464 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005465 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005466 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005467 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005469 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005470}
5471
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005472/// GetMethodDescriptionConstant - This routine build following meta-data:
5473/// struct _objc_method {
5474/// SEL _cmd;
5475/// char *method_type;
5476/// char *_imp;
5477/// }
5478
5479llvm::Constant *
5480CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005481 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005482 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5484 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005485 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005486 if (!Desc[1])
5487 return 0;
5488
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005489 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005490 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005491 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005492}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005493
5494/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5495/// This code gen. amounts to generating code for:
5496/// @code
5497/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5498/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005499///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005500LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005501 CodeGen::CodeGenFunction &CGF,
5502 QualType ObjectTy,
5503 llvm::Value *BaseValue,
5504 const ObjCIvarDecl *Ivar,
5505 unsigned CVRQualifiers) {
5506 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005507 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5508 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005509}
5510
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005511llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005512 CodeGen::CodeGenFunction &CGF,
5513 const ObjCInterfaceDecl *Interface,
5514 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005515 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005516}
5517
John McCall234eac82011-05-13 23:16:18 +00005518static void appendSelectorForMessageRefTable(std::string &buffer,
5519 Selector selector) {
5520 if (selector.isUnarySelector()) {
5521 buffer += selector.getNameForSlot(0);
5522 return;
5523 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005524
John McCall234eac82011-05-13 23:16:18 +00005525 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5526 buffer += selector.getNameForSlot(i);
5527 buffer += '_';
5528 }
5529}
5530
John McCall9e8bb002011-05-14 03:10:52 +00005531/// Emit a "v-table" message send. We emit a weak hidden-visibility
5532/// struct, initially containing the selector pointer and a pointer to
5533/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5534/// load and call the function pointer, passing the address of the
5535/// struct as the second parameter. The runtime determines whether
5536/// the selector is currently emitted using vtable dispatch; if so, it
5537/// substitutes a stub function which simply tail-calls through the
5538/// appropriate vtable slot, and if not, it substitues a stub function
5539/// which tail-calls objc_msgSend. Both stubs adjust the selector
5540/// argument to correctly point to the selector.
5541RValue
5542CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5543 ReturnValueSlot returnSlot,
5544 QualType resultType,
5545 Selector selector,
5546 llvm::Value *arg0,
5547 QualType arg0Type,
5548 bool isSuper,
5549 const CallArgList &formalArgs,
5550 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005551 // Compute the actual arguments.
5552 CallArgList args;
5553
John McCall9e8bb002011-05-14 03:10:52 +00005554 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005555 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005556 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5557 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005558
John McCall9e8bb002011-05-14 03:10:52 +00005559 // Second argument: a pointer to the message ref structure. Leave
5560 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005561 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5562
5563 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5564
5565 const CGFunctionInfo &fnInfo =
5566 CGM.getTypes().getFunctionInfo(resultType, args,
5567 FunctionType::ExtInfo());
5568
John McCall5880fb82011-05-14 21:12:11 +00005569 NullReturnState nullReturn;
5570
John McCall9e8bb002011-05-14 03:10:52 +00005571 // Find the function to call and the mangled name for the message
5572 // ref structure. Using a different mangled name wouldn't actually
5573 // be a problem; it would just be a waste.
5574 //
5575 // The runtime currently never uses vtable dispatch for anything
5576 // except normal, non-super message-sends.
5577 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005578 llvm::Constant *fn = 0;
5579 std::string messageRefName("\01l_");
5580 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005581 if (isSuper) {
5582 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5583 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005584 } else {
John McCall5880fb82011-05-14 21:12:11 +00005585 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005586 fn = ObjCTypes.getMessageSendStretFixupFn();
5587 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005588 }
John McCall234eac82011-05-13 23:16:18 +00005589 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5590 fn = ObjCTypes.getMessageSendFpretFixupFn();
5591 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005592 } else {
John McCall234eac82011-05-13 23:16:18 +00005593 if (isSuper) {
5594 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5595 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005596 } else {
John McCall234eac82011-05-13 23:16:18 +00005597 fn = ObjCTypes.getMessageSendFixupFn();
5598 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005599 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005600 }
John McCall234eac82011-05-13 23:16:18 +00005601 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5602 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005603
John McCall234eac82011-05-13 23:16:18 +00005604 // Append the selector name, except use underscores anywhere we
5605 // would have used colons.
5606 appendSelectorForMessageRefTable(messageRefName, selector);
5607
5608 llvm::GlobalVariable *messageRef
5609 = CGM.getModule().getGlobalVariable(messageRefName);
5610 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005611 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005612 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005613 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005614 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5615 init->getType(),
5616 /*constant*/ false,
5617 llvm::GlobalValue::WeakAnyLinkage,
5618 init,
5619 messageRefName);
5620 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5621 messageRef->setAlignment(16);
5622 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5623 }
5624 llvm::Value *mref =
5625 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5626
John McCall9e8bb002011-05-14 03:10:52 +00005627 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005628 args[1].RV = RValue::get(mref);
5629
5630 // Load the function to call from the message ref table.
5631 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5632 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5633
5634 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2192fe52011-07-18 04:24:23 +00005635 llvm::FunctionType *fnType =
John McCall234eac82011-05-13 23:16:18 +00005636 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5637 callee = CGF.Builder.CreateBitCast(callee,
5638 llvm::PointerType::getUnqual(fnType));
5639
John McCall5880fb82011-05-14 21:12:11 +00005640 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5641 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005642}
5643
5644/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005645CodeGen::RValue
5646CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005647 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005648 QualType ResultType,
5649 Selector Sel,
5650 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005651 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005652 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005653 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005654 return isVTableDispatchedSelector(Sel)
5655 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005656 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005657 false, CallArgs, Method)
5658 : EmitMessageSend(CGF, Return, ResultType,
5659 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005660 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005661 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005662}
5663
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005664llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005665CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005666 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5667
Daniel Dunbara6468342009-03-02 05:18:14 +00005668 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005669 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005670 false, llvm::GlobalValue::ExternalLinkage,
5671 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005672 }
5673
5674 return GV;
5675}
5676
John McCall31168b02011-06-15 23:02:42 +00005677llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5678 IdentifierInfo *II) {
5679 llvm::GlobalVariable *&Entry = ClassReferences[II];
5680
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005681 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005682 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005683 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005684 Entry =
John McCall31168b02011-06-15 23:02:42 +00005685 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5686 false, llvm::GlobalValue::InternalLinkage,
5687 ClassGV,
5688 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005689 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005690 CGM.getTargetData().getABITypeAlignment(
5691 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005692 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005693 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005694 }
John McCall31168b02011-06-15 23:02:42 +00005695
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005696 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005697}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005698
John McCall31168b02011-06-15 23:02:42 +00005699llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5700 const ObjCInterfaceDecl *ID) {
5701 return EmitClassRefFromId(Builder, ID->getIdentifier());
5702}
5703
5704llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5705 CGBuilderTy &Builder) {
5706 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5707 return EmitClassRefFromId(Builder, II);
5708}
5709
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005710llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005711CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005712 const ObjCInterfaceDecl *ID) {
5713 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005714
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005715 if (!Entry) {
5716 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5717 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005718 Entry =
5719 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005720 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005721 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005722 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005723 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005724 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005725 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005726 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005727 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005728 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005729
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005730 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005731}
5732
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005733/// EmitMetaClassRef - Return a Value * of the address of _class_t
5734/// meta-data
5735///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005736llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5737 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005738 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5739 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005740 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005741
Daniel Dunbar15894b72009-04-07 05:48:37 +00005742 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005743 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005744 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005745 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005746 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005747 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005748 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005749 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005750 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005751 ObjCTypes.ClassnfABIPtrTy));
5752
Daniel Dunbare60aa052009-04-15 19:03:14 +00005753 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005754 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005755
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005756 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005757}
5758
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005759/// GetClass - Return a reference to the class for the given interface
5760/// decl.
5761llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5762 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005763 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005764 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5765 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5766 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5767 }
5768
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005769 return EmitClassRef(Builder, ID);
5770}
5771
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005772/// Generates a message send where the super is the receiver. This is
5773/// a message send to self with special delivery semantics indicating
5774/// which class's method should be called.
5775CodeGen::RValue
5776CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005777 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005778 QualType ResultType,
5779 Selector Sel,
5780 const ObjCInterfaceDecl *Class,
5781 bool isCategoryImpl,
5782 llvm::Value *Receiver,
5783 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005784 const CodeGen::CallArgList &CallArgs,
5785 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005786 // ...
5787 // Create and init a super structure; this is a (receiver, class)
5788 // pair we will pass to objc_msgSendSuper.
5789 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005790 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005791
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005792 llvm::Value *ReceiverAsObject =
5793 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5794 CGF.Builder.CreateStore(ReceiverAsObject,
5795 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005796
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005797 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005798 llvm::Value *Target;
5799 if (IsClassMessage) {
5800 if (isCategoryImpl) {
5801 // Message sent to "super' in a class method defined in
5802 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005803 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005804 Target = CGF.Builder.CreateStructGEP(Target, 0);
5805 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005806 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005807 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005808 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005809 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005810
Mike Stump18bb9282009-05-16 07:57:57 +00005811 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5812 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00005813 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005814 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5815 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5816 CGF.Builder.CreateStore(Target,
5817 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005818
John McCall9e8bb002011-05-14 03:10:52 +00005819 return (isVTableDispatchedSelector(Sel))
5820 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005821 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005822 true, CallArgs, Method)
5823 : EmitMessageSend(CGF, Return, ResultType,
5824 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005825 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00005826 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005827}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005829llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005830 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005831 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005832
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005833 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005834 llvm::Constant *Casted =
5835 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5836 ObjCTypes.SelectorPtrTy);
5837 Entry =
5838 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5839 llvm::GlobalValue::InternalLinkage,
5840 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005841 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005842 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005843 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005844
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005845 if (lval)
5846 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005847 return Builder.CreateLoad(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005848}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005849/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005850/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005851///
5852void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005853 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005854 llvm::Value *dst,
5855 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005856 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005857 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005858 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005859 assert(Size <= 8 && "does not support size > 8");
5860 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5861 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005862 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5863 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005864 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5865 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005866 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5867 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005868 return;
5869}
5870
5871/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5872/// objc_assign_strongCast (id src, id *dst)
5873///
5874void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005876 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005877 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005878 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005879 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005880 assert(Size <= 8 && "does not support size > 8");
5881 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005882 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005883 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5884 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005885 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5886 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005887 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005888 src, dst, "weakassign");
5889 return;
5890}
5891
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005892void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005893 CodeGen::CodeGenFunction &CGF,
5894 llvm::Value *DestPtr,
5895 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005896 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005897 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5898 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005899 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005900 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005901 return;
5902}
5903
Fariborz Jahanian06292952009-02-16 22:52:32 +00005904/// EmitObjCWeakRead - Code gen for loading value of a __weak
5905/// object: objc_read_weak (id *src)
5906///
5907llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005908 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005909 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005910 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005911 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5912 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005913 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005914 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005915 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005916 return read_weak;
5917}
5918
5919/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5920/// objc_assign_weak (id src, id *dst)
5921///
5922void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005923 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005924 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005925 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005926 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005927 assert(Size <= 8 && "does not support size > 8");
5928 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5929 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005930 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5931 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005932 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5933 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005934 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005935 src, dst, "weakassign");
5936 return;
5937}
5938
5939/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5940/// objc_assign_global (id src, id *dst)
5941///
5942void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005943 llvm::Value *src, llvm::Value *dst,
5944 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00005945 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005946 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005947 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005948 assert(Size <= 8 && "does not support size > 8");
5949 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5950 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005951 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5952 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005953 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5954 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005955 if (!threadlocal)
5956 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5957 src, dst, "globalassign");
5958 else
5959 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5960 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005961 return;
5962}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005963
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005964void
John McCallbd309292010-07-06 01:34:17 +00005965CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5966 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005967 EmitAtSynchronizedStmt(CGF, S,
5968 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5969 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00005970}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005971
John McCall2ca705e2010-07-24 00:37:23 +00005972llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00005973CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00005974 // There's a particular fixed type info for 'id'.
5975 if (T->isObjCIdType() ||
5976 T->isObjCQualifiedIdType()) {
5977 llvm::Constant *IDEHType =
5978 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5979 if (!IDEHType)
5980 IDEHType =
5981 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5982 false,
5983 llvm::GlobalValue::ExternalLinkage,
5984 0, "OBJC_EHTYPE_id");
5985 return IDEHType;
5986 }
5987
5988 // All other types should be Objective-C interface pointer types.
5989 const ObjCObjectPointerType *PT =
5990 T->getAs<ObjCObjectPointerType>();
5991 assert(PT && "Invalid @catch type.");
5992 const ObjCInterfaceType *IT = PT->getInterfaceType();
5993 assert(IT && "Invalid @catch type.");
5994 return GetInterfaceEHType(IT->getDecl(), false);
5995}
5996
John McCallbd309292010-07-06 01:34:17 +00005997void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5998 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005999 EmitTryCatchStmt(CGF, S,
6000 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6001 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6002 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006003}
6004
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006005/// EmitThrowStmt - Generate code for a throw statement.
6006void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6007 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006008 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006009 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006010 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006011 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006012 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006013 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006014 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006015 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006016 }
6017
John McCall17afe452010-10-16 08:21:07 +00006018 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006019 CGF.Builder.ClearInsertionPoint();
6020}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006021
John McCall2ca705e2010-07-24 00:37:23 +00006022llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006023CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006024 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006025 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006026
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006027 // If we don't need a definition, return the entry if found or check
6028 // if we use an external reference.
6029 if (!ForDefinition) {
6030 if (Entry)
6031 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006032
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006033 // If this type (or a super class) has the __objc_exception__
6034 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006035 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006036 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006037 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006038 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006039 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006040 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006041 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006042 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006043
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006044 // Otherwise we need to either make a new entry or fill in the
6045 // initializer.
6046 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006047 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006048 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006049 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006050 CGM.getModule().getGlobalVariable(VTableName);
6051 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006052 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6053 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006054 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006055 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006056
Chris Lattner5e016ae2010-06-27 07:15:29 +00006057 llvm::Value *VTableIdx =
6058 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006059
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006060 llvm::Constant *Values[] = {
6061 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6062 GetClassName(ID->getIdentifier()),
6063 GetClassGlobal(ClassName)
6064 };
Owen Anderson170229f2009-07-14 23:10:40 +00006065 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006066 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006067
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006068 if (Entry) {
6069 Entry->setInitializer(Init);
6070 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006071 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006072 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006073 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006074 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006075 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006076 }
6077
John McCall457a04e2010-10-22 21:05:15 +00006078 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006079 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006080 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6081 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006082
6083 if (ForDefinition) {
6084 Entry->setSection("__DATA,__objc_const");
6085 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6086 } else {
6087 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6088 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006089
6090 return Entry;
6091}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006092
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006093/* *** */
6094
Daniel Dunbarb036db82008-08-13 03:21:16 +00006095CodeGen::CGObjCRuntime *
6096CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006097 if (CGM.getLangOptions().ObjCNonFragileABI)
6098 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006099 return new CGObjCMac(CGM);
6100}