blob: 9466c77ed37e54b0409e89c319fabfebe0d643d8 [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
John McCallc5799442011-02-26 09:48:59 +000046static void EmitNullReturnInitialization(CodeGenFunction &CGF,
47 ReturnValueSlot &returnSlot,
48 QualType resultType) {
49 // Force the return slot to exist.
50 if (!returnSlot.getValue())
51 returnSlot = ReturnValueSlot(CGF.CreateMemTemp(resultType), false);
52 CGF.EmitNullInitialization(returnSlot.getValue(), resultType);
53}
54
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000055
56///
57
Daniel Dunbar303e2c22008-08-11 02:45:11 +000058namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000059
Daniel Dunbar59e476b2009-08-03 17:06:42 +000060typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +000061
Daniel Dunbar59e476b2009-08-03 17:06:42 +000062// FIXME: We should find a nicer way to make the labels for metadata, string
63// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000064
Fariborz Jahanian279eda62009-01-21 22:04:16 +000065class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000066protected:
67 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000068
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000069private:
70 llvm::Constant *getMessageSendFn() const {
71 // id objc_msgSend (id, SEL, ...)
72 std::vector<const llvm::Type*> Params;
73 Params.push_back(ObjectPtrTy);
74 Params.push_back(SelectorPtrTy);
75 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +000076 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +000077 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +000078 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000079 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000080
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000081 llvm::Constant *getMessageSendStretFn() const {
82 // id objc_msgSend_stret (id, SEL, ...)
83 std::vector<const llvm::Type*> Params;
84 Params.push_back(ObjectPtrTy);
85 Params.push_back(SelectorPtrTy);
86 return
Owen Anderson41a75022009-08-13 21:57:51 +000087 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +000088 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +000089 "objc_msgSend_stret");
90
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000091 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000092
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
94 // FIXME: This should be long double on x86_64?
95 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
96 std::vector<const llvm::Type*> Params;
97 Params.push_back(ObjectPtrTy);
98 Params.push_back(SelectorPtrTy);
99 return
Owen Anderson41a75022009-08-13 21:57:51 +0000100 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
101 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000102 Params,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000103 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000104 "objc_msgSend_fpret");
105
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000106 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000107
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000108 llvm::Constant *getMessageSendSuperFn() const {
109 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
110 const char *SuperName = "objc_msgSendSuper";
111 std::vector<const llvm::Type*> Params;
112 Params.push_back(SuperPtrTy);
113 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000114 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000115 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000116 SuperName);
117 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000118
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn2() const {
120 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
121 const char *SuperName = "objc_msgSendSuper2";
122 std::vector<const llvm::Type*> Params;
123 Params.push_back(SuperPtrTy);
124 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000126 Params, true),
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000127 SuperName);
128 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000129
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperStretFn() const {
131 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
132 // SEL op, ...)
133 std::vector<const llvm::Type*> Params;
134 Params.push_back(Int8PtrTy);
135 Params.push_back(SuperPtrTy);
136 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000137 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000138 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000139 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000140 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000141 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000142
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000143 llvm::Constant *getMessageSendSuperStretFn2() const {
144 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
145 // SEL op, ...)
146 std::vector<const llvm::Type*> Params;
147 Params.push_back(Int8PtrTy);
148 Params.push_back(SuperPtrTy);
149 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000150 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000151 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000152 Params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000153 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000154 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000155
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperFpretFn() const {
157 // There is no objc_msgSendSuper_fpret? How can that work?
158 return getMessageSendSuperFn();
159 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000161 llvm::Constant *getMessageSendSuperFpretFn2() const {
162 // There is no objc_msgSendSuper_fpret? How can that work?
163 return getMessageSendSuperFn2();
164 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000165
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000166protected:
167 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000168
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000169public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000170 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000171 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Daniel Dunbar5d715592008-08-12 05:28:47 +0000173 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
174 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000176 /// PtrObjectPtrTy - LLVM type for id *
177 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000178
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000179 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000180 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000181 /// ProtocolPtrTy - LLVM type for external protocol handles
182 /// (typeof(Protocol))
183 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000184
Daniel Dunbarc722b852008-08-30 03:02:31 +0000185 // SuperCTy - clang type for struct objc_super.
186 QualType SuperCTy;
187 // SuperPtrCTy - clang type for struct objc_super *.
188 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000189
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000190 /// SuperTy - LLVM type for struct objc_super.
191 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000192 /// SuperPtrTy - LLVM type for struct objc_super *.
193 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000194
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000195 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
196 /// in GCC parlance).
197 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000198
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000199 /// PropertyListTy - LLVM type for struct objc_property_list
200 /// (_prop_list_t in GCC parlance).
201 const llvm::StructType *PropertyListTy;
202 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
203 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000204
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000205 // MethodTy - LLVM type for struct objc_method.
206 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000207
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000208 /// CacheTy - LLVM type for struct objc_cache.
209 const llvm::Type *CacheTy;
210 /// CachePtrTy - LLVM type for struct objc_cache *.
211 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000212
Chris Lattnerce8754e2009-04-22 02:44:54 +0000213 llvm::Constant *getGetPropertyFn() {
214 CodeGen::CodeGenTypes &Types = CGM.getTypes();
215 ASTContext &Ctx = CGM.getContext();
216 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000217 llvm::SmallVector<CanQualType,4> Params;
218 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
219 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000220 Params.push_back(IdType);
221 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000222 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000223 Params.push_back(Ctx.BoolTy);
224 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000225 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000226 FunctionType::ExtInfo()),
227 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000228 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
229 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000230
Chris Lattnerce8754e2009-04-22 02:44:54 +0000231 llvm::Constant *getSetPropertyFn() {
232 CodeGen::CodeGenTypes &Types = CGM.getTypes();
233 ASTContext &Ctx = CGM.getContext();
234 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000235 llvm::SmallVector<CanQualType,6> Params;
236 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
237 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000238 Params.push_back(IdType);
239 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000240 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000241 Params.push_back(IdType);
242 Params.push_back(Ctx.BoolTy);
243 Params.push_back(Ctx.BoolTy);
244 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000245 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000246 FunctionType::ExtInfo()),
247 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
249 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000250
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000251
252 llvm::Constant *getCopyStructFn() {
253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
254 ASTContext &Ctx = CGM.getContext();
255 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
256 llvm::SmallVector<CanQualType,5> Params;
257 Params.push_back(Ctx.VoidPtrTy);
258 Params.push_back(Ctx.VoidPtrTy);
259 Params.push_back(Ctx.LongTy);
260 Params.push_back(Ctx.BoolTy);
261 Params.push_back(Ctx.BoolTy);
262 const llvm::FunctionType *FTy =
263 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
264 FunctionType::ExtInfo()),
265 false);
266 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
267 }
268
Chris Lattnerce8754e2009-04-22 02:44:54 +0000269 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
271 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000272 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000273 llvm::SmallVector<CanQualType,1> Params;
274 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000275 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000276 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000277 FunctionType::ExtInfo()),
278 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000279 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
280 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000281
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000282 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000283 llvm::Constant *getGcReadWeakFn() {
284 // id objc_read_weak (id *)
285 std::vector<const llvm::Type*> Args;
286 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000287 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000288 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000289 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000290 }
291
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000292 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000293 llvm::Constant *getGcAssignWeakFn() {
294 // id objc_assign_weak (id, id *)
295 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
296 Args.push_back(ObjectPtrTy->getPointerTo());
297 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000298 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000299 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
300 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000301
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000302 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000303 llvm::Constant *getGcAssignGlobalFn() {
304 // id objc_assign_global(id, id *)
305 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
306 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000307 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000308 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000309 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
310 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000311
Fariborz Jahanian217af242010-07-20 20:30:03 +0000312 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
313 llvm::Constant *getGcAssignThreadLocalFn() {
314 // id objc_assign_threadlocal(id src, id * dest)
315 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
316 Args.push_back(ObjectPtrTy->getPointerTo());
317 llvm::FunctionType *FTy =
318 llvm::FunctionType::get(ObjectPtrTy, Args, false);
319 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
320 }
321
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000322 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000323 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000324 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000325 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
326 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000327 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000328 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000329 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000330 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
331 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000332
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000333 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
334 llvm::Constant *GcMemmoveCollectableFn() {
335 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
336 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
337 Args.push_back(Int8PtrTy);
338 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000339 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
341 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000342
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000343 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000344 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000345 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000346 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
347 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000348 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000349 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000350 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
351 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000352
353 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000354 llvm::Constant *getExceptionThrowFn() {
355 // void objc_exception_throw(id)
356 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
357 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000358 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
360 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000361
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000362 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
363 llvm::Constant *getExceptionRethrowFn() {
364 // void objc_exception_rethrow(void)
365 std::vector<const llvm::Type*> Args;
366 llvm::FunctionType *FTy =
John McCall17afe452010-10-16 08:21:07 +0000367 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
369 }
370
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000371 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000372 llvm::Constant *getSyncEnterFn() {
373 // void objc_sync_enter (id)
374 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
375 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000376 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000379
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000380 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000381 llvm::Constant *getSyncExitFn() {
382 // void objc_sync_exit (id)
383 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
384 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000385 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000386 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
387 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000388
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000389 llvm::Constant *getSendFn(bool IsSuper) const {
390 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
391 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000392
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000393 llvm::Constant *getSendFn2(bool IsSuper) const {
394 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
395 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000396
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000397 llvm::Constant *getSendStretFn(bool IsSuper) const {
398 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000400
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000401 llvm::Constant *getSendStretFn2(bool IsSuper) const {
402 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
403 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000404
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendFpretFn(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
407 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000408
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
411 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000412
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000413 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
414 ~ObjCCommonTypesHelper(){}
415};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000416
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000417/// ObjCTypesHelper - Helper class that encapsulates lazy
418/// construction of varies types used during ObjC generation.
419class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000420public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000421 /// SymtabTy - LLVM type for struct objc_symtab.
422 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000423 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
424 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000425 /// ModuleTy - LLVM type for struct objc_module.
426 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000427
Daniel Dunbarb036db82008-08-13 03:21:16 +0000428 /// ProtocolTy - LLVM type for struct objc_protocol.
429 const llvm::StructType *ProtocolTy;
430 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
431 const llvm::Type *ProtocolPtrTy;
432 /// ProtocolExtensionTy - LLVM type for struct
433 /// objc_protocol_extension.
434 const llvm::StructType *ProtocolExtensionTy;
435 /// ProtocolExtensionTy - LLVM type for struct
436 /// objc_protocol_extension *.
437 const llvm::Type *ProtocolExtensionPtrTy;
438 /// MethodDescriptionTy - LLVM type for struct
439 /// objc_method_description.
440 const llvm::StructType *MethodDescriptionTy;
441 /// MethodDescriptionListTy - LLVM type for struct
442 /// objc_method_description_list.
443 const llvm::StructType *MethodDescriptionListTy;
444 /// MethodDescriptionListPtrTy - LLVM type for struct
445 /// objc_method_description_list *.
446 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000447 /// ProtocolListTy - LLVM type for struct objc_property_list.
448 const llvm::Type *ProtocolListTy;
449 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
450 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000451 /// CategoryTy - LLVM type for struct objc_category.
452 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000453 /// ClassTy - LLVM type for struct objc_class.
454 const llvm::StructType *ClassTy;
455 /// ClassPtrTy - LLVM type for struct objc_class *.
456 const llvm::Type *ClassPtrTy;
457 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
458 const llvm::StructType *ClassExtensionTy;
459 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
460 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000461 // IvarTy - LLVM type for struct objc_ivar.
462 const llvm::StructType *IvarTy;
463 /// IvarListTy - LLVM type for struct objc_ivar_list.
464 const llvm::Type *IvarListTy;
465 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
466 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000467 /// MethodListTy - LLVM type for struct objc_method_list.
468 const llvm::Type *MethodListTy;
469 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
470 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000471
Anders Carlsson9ff22482008-09-09 10:10:21 +0000472 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
473 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000474
Anders Carlsson9ff22482008-09-09 10:10:21 +0000475 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000476 llvm::Constant *getExceptionTryEnterFn() {
477 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000478 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000479 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000480 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000481 Params, false),
482 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000483 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000484
485 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000486 llvm::Constant *getExceptionTryExitFn() {
487 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000488 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000489 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000490 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000491 Params, false),
492 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000493 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000494
495 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000496 llvm::Constant *getExceptionExtractFn() {
497 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000498 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
499 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000500 Params, false),
501 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000502
Chris Lattnerc6406db2009-04-22 02:26:14 +0000503 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000504
Anders Carlsson9ff22482008-09-09 10:10:21 +0000505 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000506 llvm::Constant *getExceptionMatchFn() {
507 std::vector<const llvm::Type*> Params;
508 Params.push_back(ClassPtrTy);
509 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000510 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000511 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000512 Params, false),
513 "objc_exception_match");
514
Chris Lattnerc6406db2009-04-22 02:26:14 +0000515 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000516
Anders Carlsson9ff22482008-09-09 10:10:21 +0000517 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000518 llvm::Constant *getSetJmpFn() {
519 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000520 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000521 return
Owen Anderson41a75022009-08-13 21:57:51 +0000522 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000523 Params, false),
524 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000525
Chris Lattnerc6406db2009-04-22 02:26:14 +0000526 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000527
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000528public:
529 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000530 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000531};
532
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000533/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000534/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000535class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000536public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000537
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000538 // MethodListnfABITy - LLVM for struct _method_list_t
539 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000541 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
542 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000544 // ProtocolnfABITy = LLVM for struct _protocol_t
545 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000546
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000547 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
548 const llvm::Type *ProtocolnfABIPtrTy;
549
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000550 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
551 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000552
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000553 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
554 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000555
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000556 // ClassnfABITy - LLVM for struct _class_t
557 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000558
Fariborz Jahanian71394042009-01-23 23:53:38 +0000559 // ClassnfABIPtrTy - LLVM for struct _class_t*
560 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000561
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000562 // IvarnfABITy - LLVM for struct _ivar_t
563 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000564
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000565 // IvarListnfABITy - LLVM for struct _ivar_list_t
566 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000567
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000568 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
569 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000570
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000571 // ClassRonfABITy - LLVM for struct _class_ro_t
572 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000574 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
575 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000576
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000577 // CategorynfABITy - LLVM for struct _category_t
578 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000579
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000580 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000581
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000582 // MessageRefTy - LLVM for:
583 // struct _message_ref_t {
584 // IMP messenger;
585 // SEL name;
586 // };
587 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000588 // MessageRefCTy - clang type for struct _message_ref_t
589 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000591 // MessageRefPtrTy - LLVM for struct _message_ref_t*
592 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000593 // MessageRefCPtrTy - clang type for struct _message_ref_t*
594 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000595
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000596 // MessengerTy - Type of the messenger (shown as IMP above)
597 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000599 // SuperMessageRefTy - LLVM for:
600 // struct _super_message_ref_t {
601 // SUPER_IMP messenger;
602 // SEL name;
603 // };
604 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000605
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000606 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
607 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000608
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000609 llvm::Constant *getMessageSendFixupFn() {
610 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
611 std::vector<const llvm::Type*> Params;
612 Params.push_back(ObjectPtrTy);
613 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000614 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000615 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000616 "objc_msgSend_fixup");
617 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000618
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000619 llvm::Constant *getMessageSendFpretFixupFn() {
620 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
621 std::vector<const llvm::Type*> Params;
622 Params.push_back(ObjectPtrTy);
623 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000624 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000625 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000626 "objc_msgSend_fpret_fixup");
627 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000628
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000629 llvm::Constant *getMessageSendStretFixupFn() {
630 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
631 std::vector<const llvm::Type*> Params;
632 Params.push_back(ObjectPtrTy);
633 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000635 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000636 "objc_msgSend_stret_fixup");
637 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000638
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000641 // struct _super_message_ref_t*, ...)
642 std::vector<const llvm::Type*> Params;
643 Params.push_back(SuperPtrTy);
644 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000645 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000646 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000647 "objc_msgSendSuper2_fixup");
648 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000650 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000651 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000652 // struct _super_message_ref_t*, ...)
653 std::vector<const llvm::Type*> Params;
654 Params.push_back(SuperPtrTy);
655 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000656 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian557c1ed2011-02-28 21:19:34 +0000657 Params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000658 "objc_msgSendSuper2_stret_fixup");
659 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000660
Chris Lattnera7c00b42009-04-22 02:15:23 +0000661 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000662 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000663 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000664 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000665
Chris Lattnera7c00b42009-04-22 02:15:23 +0000666 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Chris Lattnera7c00b42009-04-22 02:15:23 +0000668 llvm::Constant *getObjCBeginCatchFn() {
669 std::vector<const llvm::Type*> Params;
670 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000671 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000672 Params, false),
673 "objc_begin_catch");
674 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000675
676 const llvm::StructType *EHTypeTy;
677 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000678
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000679 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
680 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000681};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000682
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000683class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000684public:
685 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000686 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000687 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000688 unsigned ivar_bytepos;
689 unsigned ivar_size;
690 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000691 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000692
693 // Allow sorting based on byte pos.
694 bool operator<(const GC_IVAR &b) const {
695 return ivar_bytepos < b.ivar_bytepos;
696 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000697 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000698
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000699 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000700 public:
701 unsigned skip;
702 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000703 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000704 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000705 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000706
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000707protected:
708 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000709 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000710 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000711 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000713 // gc ivar layout bitmap calculation helper caches.
714 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
715 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000716
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000717 /// LazySymbols - Symbols to generate a lazy reference for. See
718 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000719 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000720
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000721 /// DefinedSymbols - External symbols which are defined by this
722 /// module. The symbols in this list and LazySymbols are used to add
723 /// special linker symbols which ensure that Objective-C modules are
724 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000725 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000726
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000727 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000728 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000729
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000730 /// MethodVarNames - uniqued method variable names.
731 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000732
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000733 /// DefinedCategoryNames - list of category names in form Class_Category.
734 llvm::SetVector<std::string> DefinedCategoryNames;
735
Daniel Dunbarb036db82008-08-13 03:21:16 +0000736 /// MethodVarTypes - uniqued method type signatures. We have to use
737 /// a StringMap here because have no other unique reference.
738 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000739
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000740 /// MethodDefinitions - map of methods which have been defined in
741 /// this translation unit.
742 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000743
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000744 /// PropertyNames - uniqued method variable names.
745 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000746
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000747 /// ClassReferences - uniqued class references.
748 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000749
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000750 /// SelectorReferences - uniqued selector references.
751 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752
Daniel Dunbarb036db82008-08-13 03:21:16 +0000753 /// Protocols - Protocols for which an objc_protocol structure has
754 /// been emitted. Forward declarations are handled by creating an
755 /// empty structure whose initializer is filled in when/if defined.
756 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000757
Daniel Dunbarc475d422008-10-29 22:36:39 +0000758 /// DefinedProtocols - Protocols which have actually been
759 /// defined. We should not need this, see FIXME in GenerateProtocol.
760 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000762 /// DefinedClasses - List of defined classes.
763 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000764
765 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
766 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000767
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000768 /// DefinedCategories - List of defined categories.
769 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000771 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
772 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000773
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000774 /// GetNameForMethod - Return a name for the given method.
775 /// \param[out] NameOut - The return value.
776 void GetNameForMethod(const ObjCMethodDecl *OMD,
777 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000778 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000780 /// GetMethodVarName - Return a unique constant for the given
781 /// selector's name. The return value has type char *.
782 llvm::Constant *GetMethodVarName(Selector Sel);
783 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000784
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000785 /// GetMethodVarType - Return a unique constant for the given
786 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000787
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000788 // FIXME: This is a horrible name.
789 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000790 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000792 /// GetPropertyName - Return a unique constant for the given
793 /// name. The return value has type char *.
794 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000795
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000796 // FIXME: This can be dropped once string functions are unified.
797 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
798 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000799
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000800 /// GetClassName - Return a unique constant for the given selector's
801 /// name. The return value has type char *.
802 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000803
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000804 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
805
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000806 /// BuildIvarLayout - Builds ivar layout bitmap for the class
807 /// implementation for the __strong or __weak case.
808 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000809 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
810 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000811
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000812 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000813
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000814 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000815 unsigned int BytePos, bool ForStrongLayout,
816 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000817 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000818 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000819 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000820 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000821 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000822 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000823
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000824 /// GetIvarLayoutName - Returns a unique constant for the given
825 /// ivar layout bitmap.
826 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
827 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000828
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000829 /// EmitPropertyList - Emit the given property list. The return
830 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000831 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000833 const ObjCContainerDecl *OCD,
834 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000836 /// PushProtocolProperties - Push protocol's property on the input stack.
837 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
838 std::vector<llvm::Constant*> &Properties,
839 const Decl *Container,
840 const ObjCProtocolDecl *PROTO,
841 const ObjCCommonTypesHelper &ObjCTypes);
842
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000843 /// GetProtocolRef - Return a reference to the internal protocol
844 /// description, creating an empty one if it has not been
845 /// defined. The return value has type ProtocolPtrTy.
846 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000847
Daniel Dunbar30c65362009-03-09 20:09:19 +0000848 /// CreateMetadataVar - Create a global variable with internal
849 /// linkage for use by the Objective-C runtime.
850 ///
851 /// This is a convenience wrapper which not only creates the
852 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000853 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000854 ///
855 /// \param Name - The variable name.
856 /// \param Init - The variable initializer; this is also used to
857 /// define the type of the variable.
858 /// \param Section - The section the variable should go into, or 0.
859 /// \param Align - The alignment for the variable, or 0.
860 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000861 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000862 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000863 llvm::Constant *Init,
864 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000865 unsigned Align,
866 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000867
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000868 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000869 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000870 QualType ResultType,
871 llvm::Value *Sel,
872 llvm::Value *Arg0,
873 QualType Arg0Ty,
874 bool IsSuper,
875 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000876 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000877 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000878
Daniel Dunbar5e639272010-04-25 20:39:01 +0000879 /// EmitImageInfo - Emit the image info marker used to encode some module
880 /// level information.
881 void EmitImageInfo();
882
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000883public:
Owen Andersonae86c192009-07-13 04:10:07 +0000884 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +0000885 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
David Chisnall481e3a82010-01-23 02:40:42 +0000887 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000888
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000889 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
890 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000891
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000892 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000894 /// GetOrEmitProtocol - Get the protocol object for the given
895 /// declaration, emitting it if necessary. The return value has type
896 /// ProtocolPtrTy.
897 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000898
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000899 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
900 /// object for the given declaration, emitting it if needed. These
901 /// forward references will be filled in with empty bodies if no
902 /// definition is seen. The return value has type ProtocolPtrTy.
903 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000904 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
905 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000906
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000907};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000909class CGObjCMac : public CGObjCCommonMac {
910private:
911 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000912
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000913 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000914 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000915 void EmitModuleInfo();
916
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000917 /// EmitModuleSymols - Emit module symbols, the list of defined
918 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000919 llvm::Constant *EmitModuleSymbols();
920
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000921 /// FinishModule - Write out global data structures at the end of
922 /// processing a translation unit.
923 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000924
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000925 /// EmitClassExtension - Generate the class extension structure used
926 /// to store the weak ivar layout and properties. The return value
927 /// has type ClassExtensionPtrTy.
928 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
929
930 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
931 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000932 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000933 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000934
935 /// EmitSuperClassRef - Emits reference to class's main metadata class.
936 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000937
938 /// EmitIvarList - Emit the ivar list for the given
939 /// implementation. If ForClass is true the list of class ivars
940 /// (i.e. metaclass ivars) is emitted, otherwise the list of
941 /// interface ivars will be emitted. The return value has type
942 /// IvarListPtrTy.
943 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000944 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000945
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000946 /// EmitMetaClass - Emit a forward reference to the class structure
947 /// for the metaclass of the given interface. The return value has
948 /// type ClassPtrTy.
949 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
950
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000951 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000952 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000953 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
954 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000955 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000956
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000957 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000958
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000959 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000960
961 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000962 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000963 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000964 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000965 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000966
967 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000968 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000969 /// - TypeName: The name for the type containing the methods.
970 /// - IsProtocol: True iff these methods are for a protocol.
971 /// - ClassMethds: True iff these are class methods.
972 /// - Required: When true, only "required" methods are
973 /// listed. Similarly, when false only "optional" methods are
974 /// listed. For classes this should always be true.
975 /// - begin, end: The method list to output.
976 ///
977 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000978 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000979 const char *Section,
980 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +0000981
Daniel Dunbarc475d422008-10-29 22:36:39 +0000982 /// GetOrEmitProtocol - Get the protocol object for the given
983 /// declaration, emitting it if necessary. The return value has type
984 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000985 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000986
987 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
988 /// object for the given declaration, emitting it if needed. These
989 /// forward references will be filled in with empty bodies if no
990 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000991 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +0000992
Daniel Dunbarb036db82008-08-13 03:21:16 +0000993 /// EmitProtocolExtension - Generate the protocol extension
994 /// structure used to store optional instance and class methods, and
995 /// protocol properties. The return value has type
996 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000997 llvm::Constant *
998 EmitProtocolExtension(const ObjCProtocolDecl *PD,
999 const ConstantVector &OptInstanceMethods,
1000 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001001
1002 /// EmitProtocolList - Generate the list of referenced
1003 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001004 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001005 ObjCProtocolDecl::protocol_iterator begin,
1006 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001007
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001008 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1009 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001010 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1011 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001012
1013public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001014 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001015
Fariborz Jahanian71394042009-01-23 23:53:38 +00001016 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001017
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001018 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001019 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001020 QualType ResultType,
1021 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001022 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001023 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001024 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001025 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001026
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001027 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001028 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001029 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001030 QualType ResultType,
1031 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001032 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001033 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001034 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001035 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001036 const CallArgList &CallArgs,
1037 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001038
Daniel Dunbarcb463852008-11-01 01:53:16 +00001039 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001040 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001041
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001042 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1043 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001044
1045 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1046 /// untyped one.
1047 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1048 const ObjCMethodDecl *Method);
1049
John McCall2ca705e2010-07-24 00:37:23 +00001050 virtual llvm::Constant *GetEHType(QualType T);
1051
Daniel Dunbar92992502008-08-15 22:20:32 +00001052 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001053
Daniel Dunbar92992502008-08-15 22:20:32 +00001054 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001055
Daniel Dunbarcb463852008-11-01 01:53:16 +00001056 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001057 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001058
Chris Lattnerd4808922009-03-22 21:03:39 +00001059 virtual llvm::Constant *GetPropertyGetFunction();
1060 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001061 virtual llvm::Constant *GetGetStructFunction();
1062 virtual llvm::Constant *GetSetStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001063 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001064
John McCallbd309292010-07-06 01:34:17 +00001065 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1066 const ObjCAtTryStmt &S);
1067 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1068 const ObjCAtSynchronizedStmt &S);
1069 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001070 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1071 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001072 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001073 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001074 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001075 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001076 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001077 llvm::Value *src, llvm::Value *dest,
1078 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001079 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001080 llvm::Value *src, llvm::Value *dest,
1081 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001082 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1083 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001084 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1085 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001086 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001087
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001088 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1089 QualType ObjectTy,
1090 llvm::Value *BaseValue,
1091 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001092 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001093 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001094 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001095 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001096};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001097
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001098class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001099private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001100 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001101 llvm::GlobalVariable* ObjCEmptyCacheVar;
1102 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001103
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001104 /// SuperClassReferences - uniqued super class references.
1105 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001106
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001107 /// MetaClassReferences - uniqued meta class references.
1108 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001109
1110 /// EHTypeReferences - uniqued class ehtype references.
1111 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001112
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001113 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001114 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001115 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001116
Fariborz Jahanian67260552009-11-17 21:37:35 +00001117 /// DefinedMetaClasses - List of defined meta-classes.
1118 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1119
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001120 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001121 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001122 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001123
Fariborz Jahanian71394042009-01-23 23:53:38 +00001124 /// FinishNonFragileABIModule - Write out global data structures at the end of
1125 /// processing a translation unit.
1126 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001127
Daniel Dunbar19573e72009-05-15 21:48:48 +00001128 /// AddModuleClassList - Add the given list of class pointers to the
1129 /// module with the provided symbol and section names.
1130 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1131 const char *SymbolName,
1132 const char *SectionName);
1133
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001134 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1135 unsigned InstanceStart,
1136 unsigned InstanceSize,
1137 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001138 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001139 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001140 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001141 llvm::Constant *ClassRoGV,
1142 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001143
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001144 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001145
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001146 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001147
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001148 /// EmitMethodList - Emit the method list for the given
1149 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001150 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001151 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001152 const ConstantVector &Methods);
1153 /// EmitIvarList - Emit the ivar list for the given
1154 /// implementation. If ForClass is true the list of class ivars
1155 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1156 /// interface ivars will be emitted. The return value has type
1157 /// IvarListnfABIPtrTy.
1158 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001159
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001160 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001161 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001162 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001163
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001164 /// GetOrEmitProtocol - Get the protocol object for the given
1165 /// declaration, emitting it if necessary. The return value has type
1166 /// ProtocolPtrTy.
1167 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001168
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001169 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1170 /// object for the given declaration, emitting it if needed. These
1171 /// forward references will be filled in with empty bodies if no
1172 /// definition is seen. The return value has type ProtocolPtrTy.
1173 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001174
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001175 /// EmitProtocolList - Generate the list of referenced
1176 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001177 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001178 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001179 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001180
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001181 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001182 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001183 QualType ResultType,
1184 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001185 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001186 QualType Arg0Ty,
1187 bool IsSuper,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00001188 const CallArgList &CallArgs,
1189 const ObjCMethodDecl *Method);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001190
1191 /// GetClassGlobal - Return the global variable for the Objective-C
1192 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001193 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001194
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001195 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001196 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001197 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001198 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001200 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1201 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001202 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1203 const ObjCInterfaceDecl *ID);
1204
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001205 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1206 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001207 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001208 const ObjCInterfaceDecl *ID);
1209
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001210 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1211 /// the given ivar.
1212 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001213 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001214 const ObjCInterfaceDecl *ID,
1215 const ObjCIvarDecl *Ivar);
1216
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001217 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1218 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001219 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1220 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001221
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001222 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001223 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001224 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001225 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001226
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001227 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001228 return "OBJC_METACLASS_$_";
1229 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001230
Daniel Dunbar15894b72009-04-07 05:48:37 +00001231 const char *getClassSymbolPrefix() const {
1232 return "OBJC_CLASS_$_";
1233 }
1234
Daniel Dunbar961202372009-05-03 12:57:56 +00001235 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001236 uint32_t &InstanceStart,
1237 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001238
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001239 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001240 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001241 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1242 return CGM.getContext().Selectors.getSelector(0, &II);
1243 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001244
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001245 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001246 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1247 return CGM.getContext().Selectors.getSelector(1, &II);
1248 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001249
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001250 /// ImplementationIsNonLazy - Check whether the given category or
1251 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001252 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001253
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001254public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001255 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001256 // FIXME. All stubs for now!
1257 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001258
Fariborz Jahanian71394042009-01-23 23:53:38 +00001259 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001260 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001261 QualType ResultType,
1262 Selector Sel,
1263 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001264 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001265 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001266 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
1268 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001269 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001270 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001271 QualType ResultType,
1272 Selector Sel,
1273 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001274 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001275 llvm::Value *Receiver,
1276 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001277 const CallArgList &CallArgs,
1278 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Fariborz Jahanian71394042009-01-23 23:53:38 +00001280 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001281 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001282
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001283 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1284 bool lvalue = false)
1285 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001286
1287 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1288 /// untyped one.
1289 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1290 const ObjCMethodDecl *Method)
1291 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001293 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001294
Fariborz Jahanian71394042009-01-23 23:53:38 +00001295 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001296 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001297 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001298
John McCall2ca705e2010-07-24 00:37:23 +00001299 virtual llvm::Constant *GetEHType(QualType T);
1300
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001301 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001302 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001303 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001304 virtual llvm::Constant *GetPropertySetFunction() {
1305 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001306 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001307
David Chisnall168b80f2010-12-26 22:13:16 +00001308 virtual llvm::Constant *GetSetStructFunction() {
1309 return ObjCTypes.getCopyStructFn();
1310 }
1311 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001312 return ObjCTypes.getCopyStructFn();
1313 }
1314
Chris Lattnerd4808922009-03-22 21:03:39 +00001315 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001316 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001317 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001318
John McCallbd309292010-07-06 01:34:17 +00001319 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1320 const ObjCAtTryStmt &S);
1321 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1322 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001323 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001324 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001325 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001326 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001327 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001328 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001329 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001330 llvm::Value *src, llvm::Value *dest,
1331 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001332 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001333 llvm::Value *src, llvm::Value *dest,
1334 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001335 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001336 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001337 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1338 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001339 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001340 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1341 QualType ObjectTy,
1342 llvm::Value *BaseValue,
1343 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001344 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001345 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001346 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001347 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001348};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001349
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001350} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001351
1352/* *** Helper Functions *** */
1353
1354/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001355static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001356 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001357 unsigned idx0,
1358 unsigned idx1) {
1359 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001360 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1361 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001362 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001363 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001364}
1365
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001366/// hasObjCExceptionAttribute - Return true if this class or any super
1367/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001368static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001369 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001370 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001371 return true;
1372 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001373 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001374 return false;
1375}
1376
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001377/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001378
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001379CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001380 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001381 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001382 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001383}
1384
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001385/// GetClass - Return a reference to the class for the given interface
1386/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001387llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001388 const ObjCInterfaceDecl *ID) {
1389 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001390}
1391
1392/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001393llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1394 bool lval) {
1395 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001396}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001397llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001398 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001399 return EmitSelector(Builder, Method->getSelector());
1400}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001401
John McCall2ca705e2010-07-24 00:37:23 +00001402llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1403 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1404 return 0;
1405}
1406
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001407/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001408/*
1409 struct __builtin_CFString {
1410 const int *isa; // point to __CFConstantStringClassReference
1411 int flags;
1412 const char *str;
1413 long length;
1414 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001415*/
1416
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001417/// or Generate a constant NSString object.
1418/*
1419 struct __builtin_NSString {
1420 const int *isa; // point to __NSConstantStringClassReference
1421 const char *str;
1422 unsigned int length;
1423 };
1424*/
1425
Fariborz Jahanian71394042009-01-23 23:53:38 +00001426llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001427 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001428 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1429 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001430 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001431}
1432
1433/// Generates a message send where the super is the receiver. This is
1434/// a message send to self with special delivery semantics indicating
1435/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001436CodeGen::RValue
1437CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001438 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001439 QualType ResultType,
1440 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001441 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001442 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001443 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001444 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001445 const CodeGen::CallArgList &CallArgs,
1446 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001447 // Create and init a super structure; this is a (receiver, class)
1448 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001449 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001450 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001451 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001452 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001453 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001454 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001455
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001456 // If this is a class message the metaclass is passed as the target.
1457 llvm::Value *Target;
1458 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001459 if (isCategoryImpl) {
1460 // Message sent to 'super' in a class method defined in a category
1461 // implementation requires an odd treatment.
1462 // If we are in a class method, we must retrieve the
1463 // _metaclass_ for the current class, pointed at by
1464 // the class's "isa" pointer. The following assumes that
1465 // isa" is the first ivar in a class (which it must be).
1466 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1467 Target = CGF.Builder.CreateStructGEP(Target, 0);
1468 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001469 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001470 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1471 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1472 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1473 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001474 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001475 }
1476 else if (isCategoryImpl)
1477 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1478 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001479 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1480 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1481 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001482 }
Mike Stump18bb9282009-05-16 07:57:57 +00001483 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1484 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001485 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001486 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001487 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001488 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001489 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001490 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001491 EmitSelector(CGF.Builder, Sel),
1492 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001493 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001494}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001495
1496/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001497CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001498 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001499 QualType ResultType,
1500 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001501 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001502 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001503 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001504 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001505 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001506 EmitSelector(CGF.Builder, Sel),
1507 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001508 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001509}
1510
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001511CodeGen::RValue
1512CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001513 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001514 QualType ResultType,
1515 llvm::Value *Sel,
1516 llvm::Value *Arg0,
1517 QualType Arg0Ty,
1518 bool IsSuper,
1519 const CallArgList &CallArgs,
1520 const ObjCMethodDecl *Method,
1521 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001522 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001523 if (!IsSuper)
1524 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001525 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001526 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001527 CGF.getContext().getObjCSelType()));
1528 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001529
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001530 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001531 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001532 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001533 const llvm::FunctionType *FTy =
1534 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001535
Anders Carlsson280e61f12010-06-21 20:59:55 +00001536 if (Method)
1537 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1538 CGM.getContext().getCanonicalType(ResultType) &&
1539 "Result type mismatch!");
1540
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001541 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001542 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00001543 EmitNullReturnInitialization(CGF, Return, ResultType);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001544 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001545 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001546 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1547 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1548 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001549 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001550 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001551 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001552 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001553 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001554 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001555}
1556
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001557static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1558 if (FQT.isObjCGCStrong())
1559 return Qualifiers::Strong;
1560
1561 if (FQT.isObjCGCWeak())
1562 return Qualifiers::Weak;
1563
1564 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1565 return Qualifiers::Strong;
1566
1567 if (const PointerType *PT = FQT->getAs<PointerType>())
1568 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1569
1570 return Qualifiers::GCNone;
1571}
1572
John McCall351762c2011-02-07 10:33:21 +00001573llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1574 const CGBlockInfo &blockInfo) {
1575 llvm::Constant *nullPtr =
Fariborz Jahanianafa3c0a2010-08-04 18:44:59 +00001576 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall351762c2011-02-07 10:33:21 +00001577
Fariborz Jahanian0aa35b92010-09-13 16:09:44 +00001578 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
John McCall351762c2011-02-07 10:33:21 +00001579 return nullPtr;
1580
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001581 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001582 SkipIvars.clear();
1583 IvarsInfo.clear();
1584 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1585 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1586
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001587 // __isa is the first field in block descriptor and must assume by runtime's
1588 // convention that it is GC'able.
1589 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001590
1591 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1592
1593 // Calculate the basic layout of the block structure.
1594 const llvm::StructLayout *layout =
1595 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1596
1597 // Ignore the optional 'this' capture: C++ objects are not assumed
1598 // to be GC'ed.
1599
1600 // Walk the captured variables.
1601 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1602 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1603 const VarDecl *variable = ci->getVariable();
1604 QualType type = variable->getType();
1605
1606 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1607
1608 // Ignore constant captures.
1609 if (capture.isConstant()) continue;
1610
1611 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1612
1613 // __block variables are passed by their descriptor address.
1614 if (ci->isByRef()) {
1615 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001616 continue;
John McCall351762c2011-02-07 10:33:21 +00001617 }
1618
1619 assert(!type->isArrayType() && "array variable should not be caught");
1620 if (const RecordType *record = type->getAs<RecordType>()) {
1621 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001622 continue;
1623 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001624
John McCall351762c2011-02-07 10:33:21 +00001625 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1626 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1627
1628 if (GCAttr == Qualifiers::Strong)
1629 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1630 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001631 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001632 SkipIvars.push_back(GC_IVAR(fieldOffset,
1633 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001634 }
1635
1636 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001637 return nullPtr;
1638
1639 // Sort on byte position; captures might not be allocated in order,
1640 // and unions can do funny things.
1641 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1642 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001643
1644 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001645 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001646 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1647 printf("\n block variable layout for block: ");
1648 const unsigned char *s = (unsigned char*)BitMap.c_str();
1649 for (unsigned i = 0; i < BitMap.size(); i++)
1650 if (!(s[i] & 0xf0))
1651 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1652 else
1653 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1654 printf("\n");
1655 }
1656
1657 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001658}
1659
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001660llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001661 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001662 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001663 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001664 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1665
Owen Andersonade90fd2009-07-29 18:54:39 +00001666 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001667 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001668}
1669
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001670void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001671 // FIXME: We shouldn't need this, the protocol decl should contain enough
1672 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001673 DefinedProtocols.insert(PD->getIdentifier());
1674
1675 // If we have generated a forward reference to this protocol, emit
1676 // it now. Otherwise do nothing, the protocol objects are lazily
1677 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001678 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001679 GetOrEmitProtocol(PD);
1680}
1681
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001682llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001683 if (DefinedProtocols.count(PD->getIdentifier()))
1684 return GetOrEmitProtocol(PD);
1685 return GetOrEmitProtocolRef(PD);
1686}
1687
Daniel Dunbarb036db82008-08-13 03:21:16 +00001688/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001689// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1690struct _objc_protocol {
1691struct _objc_protocol_extension *isa;
1692char *protocol_name;
1693struct _objc_protocol_list *protocol_list;
1694struct _objc__method_prototype_list *instance_methods;
1695struct _objc__method_prototype_list *class_methods
1696};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001697
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001698See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001699*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001700llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1701 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1702
1703 // Early exit if a defining object has already been generated.
1704 if (Entry && Entry->hasInitializer())
1705 return Entry;
1706
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001707 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001708 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001709 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1710
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001711 // Construct method lists.
1712 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1713 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001714 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001715 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001716 ObjCMethodDecl *MD = *i;
1717 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1718 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1719 OptInstanceMethods.push_back(C);
1720 } else {
1721 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001722 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001723 }
1724
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001725 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001726 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001727 ObjCMethodDecl *MD = *i;
1728 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1729 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1730 OptClassMethods.push_back(C);
1731 } else {
1732 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001733 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001734 }
1735
Daniel Dunbarb036db82008-08-13 03:21:16 +00001736 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001737 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001738 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001739 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001740 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001741 PD->protocol_begin(),
1742 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001743 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001744 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001745 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1746 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001747 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001748 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001749 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1750 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001751 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001752 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001753
Daniel Dunbarb036db82008-08-13 03:21:16 +00001754 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001755 // Already created, fix the linkage and update the initializer.
1756 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001757 Entry->setInitializer(Init);
1758 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001759 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001760 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001761 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001762 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001763 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001764 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001765 // FIXME: Is this necessary? Why only for protocol?
1766 Entry->setAlignment(4);
1767 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001768 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001769
1770 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001771}
1772
Daniel Dunbarc475d422008-10-29 22:36:39 +00001773llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001774 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1775
1776 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001777 // We use the initializer as a marker of whether this is a forward
1778 // reference or not. At module finalization we add the empty
1779 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001780 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001781 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001782 llvm::GlobalValue::ExternalLinkage,
1783 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001784 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001785 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001786 // FIXME: Is this necessary? Why only for protocol?
1787 Entry->setAlignment(4);
1788 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001789
Daniel Dunbarb036db82008-08-13 03:21:16 +00001790 return Entry;
1791}
1792
1793/*
1794 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001795 uint32_t size;
1796 struct objc_method_description_list *optional_instance_methods;
1797 struct objc_method_description_list *optional_class_methods;
1798 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001799 };
1800*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001801llvm::Constant *
1802CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1803 const ConstantVector &OptInstanceMethods,
1804 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001805 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001806 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001807 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001808 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001809 Values[1] =
1810 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001811 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001812 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1813 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001814 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001815 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001816 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1817 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001818 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001819 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001820
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001821 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001822 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001823 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001824 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001825
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001826 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001827 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001828
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001829 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001830 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001831 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001832 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001833}
1834
1835/*
1836 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001837 struct objc_protocol_list *next;
1838 long count;
1839 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001840 };
1841*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001842llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001843CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001844 ObjCProtocolDecl::protocol_iterator begin,
1845 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001846 std::vector<llvm::Constant*> ProtocolRefs;
1847
Daniel Dunbardec75f82008-08-21 21:57:41 +00001848 for (; begin != end; ++begin)
1849 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001850
1851 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001852 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001853 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001854
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001855 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001856 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001857
1858 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001859 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001860 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001861 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001862 ProtocolRefs.size() - 1);
1863 Values[2] =
1864 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1865 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001866 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001867
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001868 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001869 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001870 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001871 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001872 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001873}
1874
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001875void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1876 std::vector<llvm::Constant*> &Properties,
1877 const Decl *Container,
1878 const ObjCProtocolDecl *PROTO,
1879 const ObjCCommonTypesHelper &ObjCTypes) {
1880 std::vector<llvm::Constant*> Prop(2);
1881 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1882 E = PROTO->protocol_end(); P != E; ++P)
1883 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1884 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1885 E = PROTO->prop_end(); I != E; ++I) {
1886 const ObjCPropertyDecl *PD = *I;
1887 if (!PropertySet.insert(PD->getIdentifier()))
1888 continue;
1889 Prop[0] = GetPropertyName(PD->getIdentifier());
1890 Prop[1] = GetPropertyTypeString(PD, Container);
1891 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1892 }
1893}
1894
Daniel Dunbarb036db82008-08-13 03:21:16 +00001895/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001896 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001897 const char * const name;
1898 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001899 };
1900
1901 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001902 uint32_t entsize; // sizeof (struct _objc_property)
1903 uint32_t prop_count;
1904 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001905 };
1906*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001907llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001908 const Decl *Container,
1909 const ObjCContainerDecl *OCD,
1910 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001911 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001912 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001913 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1914 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001915 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001916 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001917 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001918 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001919 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001920 Prop));
1921 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001922 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001923 for (ObjCInterfaceDecl::all_protocol_iterator
1924 P = OID->all_referenced_protocol_begin(),
1925 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001926 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1927 ObjCTypes);
1928 }
1929 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1930 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1931 E = CD->protocol_end(); P != E; ++P)
1932 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1933 ObjCTypes);
1934 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001935
1936 // Return null for empty list.
1937 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001938 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001939
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001941 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001942 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001943 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1944 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001945 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001946 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001947 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001948 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001949
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001950 llvm::GlobalVariable *GV =
1951 CreateMetadataVar(Name, Init,
1952 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001953 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001954 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001955 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001956 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001957}
1958
1959/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001960 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001961 int count;
1962 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001963 };
1964*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001965llvm::Constant *
1966CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1967 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001968 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001969 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1970 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001971 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001972 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001973 Desc);
1974}
Daniel Dunbarb036db82008-08-13 03:21:16 +00001975
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001976llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001977 const char *Section,
1978 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001979 // Return null for empty list.
1980 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001981 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001982
1983 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001984 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001985 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001986 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001987 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001988 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001989
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001990 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001991 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001993}
1994
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001995/*
1996 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001997 char *category_name;
1998 char *class_name;
1999 struct _objc_method_list *instance_methods;
2000 struct _objc_method_list *class_methods;
2001 struct _objc_protocol_list *protocols;
2002 uint32_t size; // <rdar://4585769>
2003 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002004 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002005*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002006void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002007 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002008
Mike Stump18bb9282009-05-16 07:57:57 +00002009 // FIXME: This is poor design, the OCD should have a pointer to the category
2010 // decl. Additionally, note that Category can be null for the @implementation
2011 // w/o an @interface case. Sema should just create one for us as it does for
2012 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002013 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002014 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002015 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002016
2017 llvm::SmallString<256> ExtName;
2018 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2019 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002020
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002021 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002022 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002023 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002024 // Instance methods should always be defined.
2025 InstanceMethods.push_back(GetMethodConstant(*i));
2026 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002027 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002028 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002029 // Class methods should always be defined.
2030 ClassMethods.push_back(GetMethodConstant(*i));
2031 }
2032
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002033 std::vector<llvm::Constant*> Values(7);
2034 Values[0] = GetClassName(OCD->getIdentifier());
2035 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002036 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002037 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002038 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002039 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002040 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002041 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002042 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002043 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002044 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002045 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002046 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002047 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002048 Category->protocol_begin(),
2049 Category->protocol_end());
2050 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002051 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002052 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002053 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002054
2055 // If there is no category @interface then there can be no properties.
2056 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002057 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002058 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002059 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002060 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002061 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002062
Owen Anderson0e0189d2009-07-27 22:29:56 +00002063 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002064 Values);
2065
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002066 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002067 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002068 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002069 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002070 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002071 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002072}
2073
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002074// FIXME: Get from somewhere?
2075enum ClassFlags {
2076 eClassFlags_Factory = 0x00001,
2077 eClassFlags_Meta = 0x00002,
2078 // <rdr://5142207>
2079 eClassFlags_HasCXXStructors = 0x02000,
2080 eClassFlags_Hidden = 0x20000,
2081 eClassFlags_ABI2_Hidden = 0x00010,
2082 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2083};
2084
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002085/*
2086 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002087 Class isa;
2088 Class super_class;
2089 const char *name;
2090 long version;
2091 long info;
2092 long instance_size;
2093 struct _objc_ivar_list *ivars;
2094 struct _objc_method_list *methods;
2095 struct _objc_cache *cache;
2096 struct _objc_protocol_list *protocols;
2097 // Objective-C 1.0 extensions (<rdr://4585769>)
2098 const char *ivar_layout;
2099 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002100 };
2101
2102 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002103*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002104void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002105 DefinedSymbols.insert(ID->getIdentifier());
2106
Chris Lattner86d7d912008-11-24 03:54:41 +00002107 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002108 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002109 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002110 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002111 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002112 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002113 Interface->all_referenced_protocol_begin(),
2114 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002115 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002116 if (ID->getNumIvarInitializers())
2117 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002118 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002119 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002120
2121 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002122 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002123 Flags |= eClassFlags_Hidden;
2124
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002126 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002127 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002128 // Instance methods should always be defined.
2129 InstanceMethods.push_back(GetMethodConstant(*i));
2130 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002131 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002132 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002133 // Class methods should always be defined.
2134 ClassMethods.push_back(GetMethodConstant(*i));
2135 }
2136
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002137 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002138 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002139 ObjCPropertyImplDecl *PID = *i;
2140
2141 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2142 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2143
2144 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2145 if (llvm::Constant *C = GetMethodConstant(MD))
2146 InstanceMethods.push_back(C);
2147 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2148 if (llvm::Constant *C = GetMethodConstant(MD))
2149 InstanceMethods.push_back(C);
2150 }
2151 }
2152
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002153 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002154 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002155 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002156 // Record a reference to the super class.
2157 LazySymbols.insert(Super->getIdentifier());
2158
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002159 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002160 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002161 ObjCTypes.ClassPtrTy);
2162 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002163 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002164 }
2165 Values[ 2] = GetClassName(ID->getIdentifier());
2166 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002167 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2168 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2169 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002170 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002171 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002172 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002173 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002174 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002175 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002176 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002177 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002178 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002179 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002180 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002181 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002182 std::string Name("\01L_OBJC_CLASS_");
2183 Name += ClassName;
2184 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2185 // Check for a forward reference.
2186 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2187 if (GV) {
2188 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2189 "Forward metaclass reference has incorrect type.");
2190 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2191 GV->setInitializer(Init);
2192 GV->setSection(Section);
2193 GV->setAlignment(4);
2194 CGM.AddUsedGlobal(GV);
2195 }
2196 else
2197 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002198 DefinedClasses.push_back(GV);
2199}
2200
2201llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2202 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002203 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002204 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002205 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002206
John McCall457a04e2010-10-22 21:05:15 +00002207 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002209
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002210 std::vector<llvm::Constant*> Values(12);
2211 // The isa for the metaclass is the root of the hierarchy.
2212 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2213 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2214 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002215 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002216 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002217 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002218 // The super class for the metaclass is emitted as the name of the
2219 // super class. The runtime fixes this up to point to the
2220 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002221 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002222 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002223 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002224 ObjCTypes.ClassPtrTy);
2225 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002226 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002227 }
2228 Values[ 2] = GetClassName(ID->getIdentifier());
2229 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002230 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2231 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2232 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002233 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002234 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002235 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002236 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002237 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002239 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002240 Values[ 9] = Protocols;
2241 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002242 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002243 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002244 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002245 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002246 Values);
2247
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002248 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002249 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002250
2251 // Check for a forward reference.
2252 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2253 if (GV) {
2254 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2255 "Forward metaclass reference has incorrect type.");
2256 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2257 GV->setInitializer(Init);
2258 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002259 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002260 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002261 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002262 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002263 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002264 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002265 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002266
2267 return GV;
2268}
2269
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002270llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002271 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002272
Mike Stump18bb9282009-05-16 07:57:57 +00002273 // FIXME: Should we look these up somewhere other than the module. Its a bit
2274 // silly since we only generate these while processing an implementation, so
2275 // exactly one pointer would work if know when we entered/exitted an
2276 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002277
2278 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002279 // Previously, metaclass with internal linkage may have been defined.
2280 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002281 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2282 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002283 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2284 "Forward metaclass reference has incorrect type.");
2285 return GV;
2286 } else {
2287 // Generate as an external reference to keep a consistent
2288 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002289 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002290 llvm::GlobalValue::ExternalLinkage,
2291 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002292 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002293 }
2294}
2295
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002296llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2297 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2298
2299 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2300 true)) {
2301 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2302 "Forward class metadata reference has incorrect type.");
2303 return GV;
2304 } else {
2305 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2306 llvm::GlobalValue::ExternalLinkage,
2307 0,
2308 Name);
2309 }
2310}
2311
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002312/*
2313 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002314 uint32_t size;
2315 const char *weak_ivar_layout;
2316 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002317 };
2318*/
2319llvm::Constant *
2320CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002321 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002322 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002323
2324 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002325 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002326 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002327 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002328 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002329
2330 // Return null if no extension bits are used.
2331 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002332 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002333
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002334 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002335 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002336 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002337 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002338 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002339}
2340
2341/*
2342 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002343 char *ivar_name;
2344 char *ivar_type;
2345 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002346 };
2347
2348 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002349 int ivar_count;
2350 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002351 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002352*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002353llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002354 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002355 std::vector<llvm::Constant*> Ivars, Ivar(3);
2356
2357 // When emitting the root class GCC emits ivar entries for the
2358 // actual class structure. It is not clear if we need to follow this
2359 // behavior; for now lets try and get away with not doing it. If so,
2360 // the cleanest solution would be to make up an ObjCInterfaceDecl
2361 // for the class.
2362 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002363 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002364
2365 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002367
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002368 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002369 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002370
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002371 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2372 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002373 // Ignore unnamed bit-fields.
2374 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002375 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002376 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2377 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002379 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002380 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002381 }
2382
2383 // Return null for empty list.
2384 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002385 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002386
2387 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002388 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002389 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002390 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002391 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002392 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002393
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002394 llvm::GlobalVariable *GV;
2395 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002396 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002397 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002398 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002399 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002400 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002401 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002402 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002403 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002404}
2405
2406/*
2407 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002408 SEL method_name;
2409 char *method_types;
2410 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002411 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002412
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002413 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002414 struct objc_method_list *obsolete;
2415 int count;
2416 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002417 };
2418*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002419
2420/// GetMethodConstant - Return a struct objc_method constant for the
2421/// given method if it has been defined. The result is null if the
2422/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002423llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002424 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002425 if (!Fn)
2426 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002427
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002428 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002429 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002430 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002431 ObjCTypes.SelectorPtrTy);
2432 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002433 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002434 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002435}
2436
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002437llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002438 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002439 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002440 // Return null for empty list.
2441 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002442 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002443
2444 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002445 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002446 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002447 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002448 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002449 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002450 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002451
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002452 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002453 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002454 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002455}
2456
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002457llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002458 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002459 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002460 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002461
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002462 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002463 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002464 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002465 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002466 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002467 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002468 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002469 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002470 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002471
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002472 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002473}
2474
Daniel Dunbar30c65362009-03-09 20:09:19 +00002475llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002476CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002477 llvm::Constant *Init,
2478 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002479 unsigned Align,
2480 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002481 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002482 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002483 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002484 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002485 if (Section)
2486 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002487 if (Align)
2488 GV->setAlignment(Align);
2489 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002490 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002491 return GV;
2492}
2493
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002494llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002495 // Abuse this interface function as a place to finalize.
2496 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002497 return NULL;
2498}
2499
Chris Lattnerd4808922009-03-22 21:03:39 +00002500llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002501 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002502}
2503
Chris Lattnerd4808922009-03-22 21:03:39 +00002504llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002505 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002506}
2507
David Chisnall168b80f2010-12-26 22:13:16 +00002508llvm::Constant *CGObjCMac::GetGetStructFunction() {
2509 return ObjCTypes.getCopyStructFn();
2510}
2511llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002512 return ObjCTypes.getCopyStructFn();
2513}
2514
Chris Lattnerd4808922009-03-22 21:03:39 +00002515llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002516 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002517}
2518
John McCallbd309292010-07-06 01:34:17 +00002519void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2520 return EmitTryOrSynchronizedStmt(CGF, S);
2521}
2522
2523void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2524 const ObjCAtSynchronizedStmt &S) {
2525 return EmitTryOrSynchronizedStmt(CGF, S);
2526}
2527
John McCall65bea082010-07-21 06:59:36 +00002528namespace {
John McCallcda666c2010-07-21 07:22:38 +00002529 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002530 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002531 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002532 llvm::Value *CallTryExitVar;
2533 llvm::Value *ExceptionData;
2534 ObjCTypesHelper &ObjCTypes;
2535 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002536 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002537 llvm::Value *CallTryExitVar,
2538 llvm::Value *ExceptionData,
2539 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002540 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002541 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2542
2543 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2544 // Check whether we need to call objc_exception_try_exit.
2545 // In optimized code, this branch will always be folded.
2546 llvm::BasicBlock *FinallyCallExit =
2547 CGF.createBasicBlock("finally.call_exit");
2548 llvm::BasicBlock *FinallyNoCallExit =
2549 CGF.createBasicBlock("finally.no_call_exit");
2550 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2551 FinallyCallExit, FinallyNoCallExit);
2552
2553 CGF.EmitBlock(FinallyCallExit);
2554 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2555 ->setDoesNotThrow();
2556
2557 CGF.EmitBlock(FinallyNoCallExit);
2558
2559 if (isa<ObjCAtTryStmt>(S)) {
2560 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002561 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2562 // Save the current cleanup destination in case there's
2563 // control flow inside the finally statement.
2564 llvm::Value *CurCleanupDest =
2565 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2566
John McCall65bea082010-07-21 06:59:36 +00002567 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2568
John McCallcebe0ca2010-08-11 00:16:14 +00002569 if (CGF.HaveInsertPoint()) {
2570 CGF.Builder.CreateStore(CurCleanupDest,
2571 CGF.getNormalCleanupDestSlot());
2572 } else {
2573 // Currently, the end of the cleanup must always exist.
2574 CGF.EnsureInsertPoint();
2575 }
2576 }
John McCall65bea082010-07-21 06:59:36 +00002577 } else {
2578 // Emit objc_sync_exit(expr); as finally's sole statement for
2579 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002580 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002581 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2582 ->setDoesNotThrow();
2583 }
2584 }
2585 };
John McCall42227ed2010-07-31 23:20:56 +00002586
2587 class FragileHazards {
2588 CodeGenFunction &CGF;
2589 llvm::SmallVector<llvm::Value*, 20> Locals;
2590 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2591
2592 llvm::InlineAsm *ReadHazard;
2593 llvm::InlineAsm *WriteHazard;
2594
2595 llvm::FunctionType *GetAsmFnType();
2596
2597 void collectLocals();
2598 void emitReadHazard(CGBuilderTy &Builder);
2599
2600 public:
2601 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002602
John McCall42227ed2010-07-31 23:20:56 +00002603 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002604 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002605 };
2606}
2607
2608/// Create the fragile-ABI read and write hazards based on the current
2609/// state of the function, which is presumed to be immediately prior
2610/// to a @try block. These hazards are used to maintain correct
2611/// semantics in the face of optimization and the fragile ABI's
2612/// cavalier use of setjmp/longjmp.
2613FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2614 collectLocals();
2615
2616 if (Locals.empty()) return;
2617
2618 // Collect all the blocks in the function.
2619 for (llvm::Function::iterator
2620 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2621 BlocksBeforeTry.insert(&*I);
2622
2623 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2624
2625 // Create a read hazard for the allocas. This inhibits dead-store
2626 // optimizations and forces the values to memory. This hazard is
2627 // inserted before any 'throwing' calls in the protected scope to
2628 // reflect the possibility that the variables might be read from the
2629 // catch block if the call throws.
2630 {
2631 std::string Constraint;
2632 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2633 if (I) Constraint += ',';
2634 Constraint += "*m";
2635 }
2636
2637 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2638 }
2639
2640 // Create a write hazard for the allocas. This inhibits folding
2641 // loads across the hazard. This hazard is inserted at the
2642 // beginning of the catch path to reflect the possibility that the
2643 // variables might have been written within the protected scope.
2644 {
2645 std::string Constraint;
2646 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2647 if (I) Constraint += ',';
2648 Constraint += "=*m";
2649 }
2650
2651 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2652 }
2653}
2654
2655/// Emit a write hazard at the current location.
2656void FragileHazards::emitWriteHazard() {
2657 if (Locals.empty()) return;
2658
2659 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2660 ->setDoesNotThrow();
2661}
2662
John McCall42227ed2010-07-31 23:20:56 +00002663void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2664 assert(!Locals.empty());
2665 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2666 ->setDoesNotThrow();
2667}
2668
2669/// Emit read hazards in all the protected blocks, i.e. all the blocks
2670/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002671void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002672 if (Locals.empty()) return;
2673
2674 CGBuilderTy Builder(CGF.getLLVMContext());
2675
2676 // Iterate through all blocks, skipping those prior to the try.
2677 for (llvm::Function::iterator
2678 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2679 llvm::BasicBlock &BB = *FI;
2680 if (BlocksBeforeTry.count(&BB)) continue;
2681
2682 // Walk through all the calls in the block.
2683 for (llvm::BasicBlock::iterator
2684 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2685 llvm::Instruction &I = *BI;
2686
2687 // Ignore instructions that aren't non-intrinsic calls.
2688 // These are the only calls that can possibly call longjmp.
2689 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2690 if (isa<llvm::IntrinsicInst>(I))
2691 continue;
2692
2693 // Ignore call sites marked nounwind. This may be questionable,
2694 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2695 llvm::CallSite CS(&I);
2696 if (CS.doesNotThrow()) continue;
2697
John McCall2dd7d442010-08-04 05:59:32 +00002698 // Insert a read hazard before the call. This will ensure that
2699 // any writes to the locals are performed before making the
2700 // call. If the call throws, then this is sufficient to
2701 // guarantee correctness as long as it doesn't also write to any
2702 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002703 Builder.SetInsertPoint(&BB, BI);
2704 emitReadHazard(Builder);
2705 }
2706 }
2707}
2708
2709static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2710 if (V) S.insert(V);
2711}
2712
2713void FragileHazards::collectLocals() {
2714 // Compute a set of allocas to ignore.
2715 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2716 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2717 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2718 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2719
2720 // Collect all the allocas currently in the function. This is
2721 // probably way too aggressive.
2722 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2723 for (llvm::BasicBlock::iterator
2724 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2725 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2726 Locals.push_back(&*I);
2727}
2728
2729llvm::FunctionType *FragileHazards::GetAsmFnType() {
2730 std::vector<const llvm::Type *> Tys(Locals.size());
2731 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2732 Tys[I] = Locals[I]->getType();
2733 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCall65bea082010-07-21 06:59:36 +00002734}
2735
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002736/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002737
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002738 Objective-C setjmp-longjmp (sjlj) Exception Handling
2739 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002740
John McCallbd309292010-07-06 01:34:17 +00002741 A catch buffer is a setjmp buffer plus:
2742 - a pointer to the exception that was caught
2743 - a pointer to the previous exception data buffer
2744 - two pointers of reserved storage
2745 Therefore catch buffers form a stack, with a pointer to the top
2746 of the stack kept in thread-local storage.
2747
2748 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2749 objc_exception_try_exit pops the given catch buffer, which is
2750 required to be the top of the EH stack.
2751 objc_exception_throw pops the top of the EH stack, writes the
2752 thrown exception into the appropriate field, and longjmps
2753 to the setjmp buffer. It crashes the process (with a printf
2754 and an abort()) if there are no catch buffers on the stack.
2755 objc_exception_extract just reads the exception pointer out of the
2756 catch buffer.
2757
2758 There's no reason an implementation couldn't use a light-weight
2759 setjmp here --- something like __builtin_setjmp, but API-compatible
2760 with the heavyweight setjmp. This will be more important if we ever
2761 want to implement correct ObjC/C++ exception interactions for the
2762 fragile ABI.
2763
2764 Note that for this use of setjmp/longjmp to be correct, we may need
2765 to mark some local variables volatile: if a non-volatile local
2766 variable is modified between the setjmp and the longjmp, it has
2767 indeterminate value. For the purposes of LLVM IR, it may be
2768 sufficient to make loads and stores within the @try (to variables
2769 declared outside the @try) volatile. This is necessary for
2770 optimized correctness, but is not currently being done; this is
2771 being tracked as rdar://problem/8160285
2772
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002773 The basic framework for a @try-catch-finally is as follows:
2774 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002775 objc_exception_data d;
2776 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002777 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002778
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002779 objc_exception_try_enter(&d);
2780 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002781 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002782 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002783 // exception path
2784 id _caught = objc_exception_extract(&d);
2785
2786 // enter new try scope for handlers
2787 if (!setjmp(d.jmp_buf)) {
2788 ... match exception and execute catch blocks ...
2789
2790 // fell off end, rethrow.
2791 _rethrow = _caught;
2792 ... jump-through-finally to finally_rethrow ...
2793 } else {
2794 // exception in catch block
2795 _rethrow = objc_exception_extract(&d);
2796 _call_try_exit = false;
2797 ... jump-through-finally to finally_rethrow ...
2798 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002799 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002800 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002801
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002802 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002803 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002804 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002805
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002806 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002807 ... dispatch to finally destination ...
2808
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002809 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002810 objc_exception_throw(_rethrow);
2811
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002812 finally_end:
2813 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002814
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002815 This framework differs slightly from the one gcc uses, in that gcc
2816 uses _rethrow to determine if objc_exception_try_exit should be called
2817 and if the object should be rethrown. This breaks in the face of
2818 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002819
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002820 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002821
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002822 - If there are no catch blocks, then we avoid emitting the second
2823 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002824
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002825 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2826 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002827
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002828 - FIXME: If there is no @finally block we can do a few more
2829 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002830
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002831 Rethrows and Jumps-Through-Finally
2832 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002833
John McCallbd309292010-07-06 01:34:17 +00002834 '@throw;' is supported by pushing the currently-caught exception
2835 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002836
John McCallbd309292010-07-06 01:34:17 +00002837 Branches through the @finally block are handled with an ordinary
2838 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2839 exceptions are not compatible with C++ exceptions, and this is
2840 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002841
John McCallbd309292010-07-06 01:34:17 +00002842 @synchronized(expr) { stmt; } is emitted as if it were:
2843 id synch_value = expr;
2844 objc_sync_enter(synch_value);
2845 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002846*/
2847
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002848void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2849 const Stmt &S) {
2850 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002851
2852 // A destination for the fall-through edges of the catch handlers to
2853 // jump to.
2854 CodeGenFunction::JumpDest FinallyEnd =
2855 CGF.getJumpDestInCurrentScope("finally.end");
2856
2857 // A destination for the rethrow edge of the catch handlers to jump
2858 // to.
2859 CodeGenFunction::JumpDest FinallyRethrow =
2860 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002861
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002862 // For @synchronized, call objc_sync_enter(sync.expr). The
2863 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00002864 // @synchronized. We can't avoid a temp here because we need the
2865 // value to be preserved. If the backend ever does liveness
2866 // correctly after setjmp, this will be unnecessary.
2867 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002868 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00002869 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002870 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2871 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002872 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2873 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00002874
2875 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2876 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002877 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002878
John McCall2dd7d442010-08-04 05:59:32 +00002879 // Allocate memory for the setjmp buffer. This needs to be kept
2880 // live throughout the try and catch blocks.
2881 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2882 "exceptiondata.ptr");
2883
John McCall42227ed2010-07-31 23:20:56 +00002884 // Create the fragile hazards. Note that this will not capture any
2885 // of the allocas required for exception processing, but will
2886 // capture the current basic block (which extends all the way to the
2887 // setjmp call) as "before the @try".
2888 FragileHazards Hazards(CGF);
2889
John McCallbd309292010-07-06 01:34:17 +00002890 // Create a flag indicating whether the cleanup needs to call
2891 // objc_exception_try_exit. This is true except when
2892 // - no catches match and we're branching through the cleanup
2893 // just to rethrow the exception, or
2894 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00002895 // The setjmp-safety rule here is that we should always store to this
2896 // variable in a place that dominates the branch through the cleanup
2897 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00002898 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002899 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002900
John McCall9916e3f2010-10-04 23:42:51 +00002901 // A slot containing the exception to rethrow. Only needed when we
2902 // have both a @catch and a @finally.
2903 llvm::Value *PropagatingExnVar = 0;
2904
John McCallbd309292010-07-06 01:34:17 +00002905 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002906 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00002907 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00002908 CallTryExitVar,
2909 ExceptionData,
2910 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002911
2912 // Enter a try block:
2913 // - Call objc_exception_try_enter to push ExceptionData on top of
2914 // the EH stack.
2915 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2916 ->setDoesNotThrow();
2917
2918 // - Call setjmp on the exception data buffer.
2919 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2920 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2921 llvm::Value *SetJmpBuffer =
2922 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2923 llvm::CallInst *SetJmpResult =
2924 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2925 SetJmpResult->setDoesNotThrow();
2926
2927 // If setjmp returned 0, enter the protected block; otherwise,
2928 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002929 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2930 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002931 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00002932 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2933 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002934
John McCallbd309292010-07-06 01:34:17 +00002935 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002936 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00002937 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002938 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002939 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00002940
2941 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002942
John McCallbd309292010-07-06 01:34:17 +00002943 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002944 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002945
John McCall42227ed2010-07-31 23:20:56 +00002946 // Don't optimize loads of the in-scope locals across this point.
2947 Hazards.emitWriteHazard();
2948
John McCallbd309292010-07-06 01:34:17 +00002949 // For a @synchronized (or a @try with no catches), just branch
2950 // through the cleanup to the rethrow block.
2951 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2952 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00002953 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002954 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002955
2956 // Otherwise, we have to match against the caught exceptions.
2957 } else {
John McCall2dd7d442010-08-04 05:59:32 +00002958 // Retrieve the exception object. We may emit multiple blocks but
2959 // nothing can cross this so the value is already in SSA form.
2960 llvm::CallInst *Caught =
2961 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2962 ExceptionData, "caught");
2963 Caught->setDoesNotThrow();
2964
John McCallbd309292010-07-06 01:34:17 +00002965 // Push the exception to rethrow onto the EH value stack for the
2966 // benefit of any @throws in the handlers.
2967 CGF.ObjCEHValueStack.push_back(Caught);
2968
Douglas Gregor96c79492010-04-23 22:50:49 +00002969 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002970
John McCall2dd7d442010-08-04 05:59:32 +00002971 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00002972
John McCall2dd7d442010-08-04 05:59:32 +00002973 llvm::BasicBlock *CatchBlock = 0;
2974 llvm::BasicBlock *CatchHandler = 0;
2975 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00002976 // Save the currently-propagating exception before
2977 // objc_exception_try_enter clears the exception slot.
2978 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
2979 "propagating_exception");
2980 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
2981
John McCall2dd7d442010-08-04 05:59:32 +00002982 // Enter a new exception try block (in case a @catch block
2983 // throws an exception).
2984 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2985 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002986
John McCall2dd7d442010-08-04 05:59:32 +00002987 llvm::CallInst *SetJmpResult =
2988 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2989 "setjmp.result");
2990 SetJmpResult->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002991
John McCall2dd7d442010-08-04 05:59:32 +00002992 llvm::Value *Threw =
2993 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2994
2995 CatchBlock = CGF.createBasicBlock("catch");
2996 CatchHandler = CGF.createBasicBlock("catch_for_catch");
2997 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
2998
2999 CGF.EmitBlock(CatchBlock);
3000 }
3001
3002 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003003
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003004 // Handle catch list. As a special case we check if everything is
3005 // matched and avoid generating code for falling off the end if
3006 // so.
3007 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003008 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3009 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003010
Douglas Gregor46a572b2010-04-26 16:46:50 +00003011 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003012 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003013
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003014 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003015 if (!CatchParam) {
3016 AllMatched = true;
3017 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003018 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003019
John McCallbd309292010-07-06 01:34:17 +00003020 // catch(id e) always matches under this ABI, since only
3021 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003022 // FIXME: For the time being we also match id<X>; this should
3023 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003024 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003025 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003026 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003027
John McCallbd309292010-07-06 01:34:17 +00003028 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003029 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003030 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3031
Anders Carlsson9396a892008-09-11 09:15:33 +00003032 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003033 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003034 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003035
3036 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003037 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003038 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003039
Anders Carlsson9396a892008-09-11 09:15:33 +00003040 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003041
3042 // The scope of the catch variable ends right here.
3043 CatchVarCleanups.ForceCleanup();
3044
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003045 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003046 break;
3047 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003048
Steve Naroff7cae42b2009-07-10 23:34:53 +00003049 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003050 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003051
3052 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003053 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3054 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003055
3056 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003057 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003058
John McCallbd309292010-07-06 01:34:17 +00003059 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003060 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3061 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003062 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003063
John McCallbd309292010-07-06 01:34:17 +00003064 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3065 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003066
3067 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003068 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003069
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003070 // Emit the @catch block.
3071 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003072
3073 // Collect any cleanups for the catch variable. The scope lasts until
3074 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003075 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003076
John McCall1c9c3fd2010-10-15 04:57:14 +00003077 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003078 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003079
John McCallbd309292010-07-06 01:34:17 +00003080 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003081 llvm::Value *Tmp =
3082 CGF.Builder.CreateBitCast(Caught,
3083 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003084 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00003085 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003086
Anders Carlsson9396a892008-09-11 09:15:33 +00003087 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003088
3089 // We're done with the catch variable.
3090 CatchVarCleanups.ForceCleanup();
3091
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003092 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003093
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003094 CGF.EmitBlock(NextCatchBlock);
3095 }
3096
John McCallbd309292010-07-06 01:34:17 +00003097 CGF.ObjCEHValueStack.pop_back();
3098
John McCall2dd7d442010-08-04 05:59:32 +00003099 // If nothing wanted anything to do with the caught exception,
3100 // kill the extract call.
3101 if (Caught->use_empty())
3102 Caught->eraseFromParent();
3103
3104 if (!AllMatched)
3105 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3106
3107 if (HasFinally) {
3108 // Emit the exception handler for the @catch blocks.
3109 CGF.EmitBlock(CatchHandler);
3110
3111 // In theory we might now need a write hazard, but actually it's
3112 // unnecessary because there's no local-accessing code between
3113 // the try's write hazard and here.
3114 //Hazards.emitWriteHazard();
3115
John McCall9916e3f2010-10-04 23:42:51 +00003116 // Extract the new exception and save it to the
3117 // propagating-exception slot.
3118 assert(PropagatingExnVar);
3119 llvm::CallInst *NewCaught =
3120 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3121 ExceptionData, "caught");
3122 NewCaught->setDoesNotThrow();
3123 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3124
John McCall2dd7d442010-08-04 05:59:32 +00003125 // Don't pop the catch handler; the throw already did.
3126 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003127 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003128 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003129 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003130
John McCall42227ed2010-07-31 23:20:56 +00003131 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003132 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003133
John McCallbd309292010-07-06 01:34:17 +00003134 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003135 CGF.Builder.restoreIP(TryFallthroughIP);
3136 if (CGF.HaveInsertPoint())
3137 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003138 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003139 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003140
John McCallbd309292010-07-06 01:34:17 +00003141 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003142 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003143 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003144 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003145 // If we have a propagating-exception variable, check it.
3146 llvm::Value *PropagatingExn;
3147 if (PropagatingExnVar) {
3148 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003149
John McCall9916e3f2010-10-04 23:42:51 +00003150 // Otherwise, just look in the buffer for the exception to throw.
3151 } else {
3152 llvm::CallInst *Caught =
3153 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3154 ExceptionData);
3155 Caught->setDoesNotThrow();
3156 PropagatingExn = Caught;
3157 }
3158
3159 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003160 ->setDoesNotThrow();
3161 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003162 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003163
John McCall42227ed2010-07-31 23:20:56 +00003164 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003165}
3166
3167void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003168 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003169 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003170
Anders Carlssone005aa12008-09-09 16:16:55 +00003171 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3172 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003173 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00003174 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3175 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003176 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003177 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003178 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003179 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003180
John McCallbd309292010-07-06 01:34:17 +00003181 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3182 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003183 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003184
3185 // Clear the insertion point to indicate we are in unreachable code.
3186 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003187}
3188
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003189/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003190/// object: objc_read_weak (id *src)
3191///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003192llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003193 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003194 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003195 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3196 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3197 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003198 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003199 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003200 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003201 return read_weak;
3202}
3203
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003204/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3205/// objc_assign_weak (id src, id *dst)
3206///
3207void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003208 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003209 const llvm::Type * SrcTy = src->getType();
3210 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003211 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003212 assert(Size <= 8 && "does not support size > 8");
3213 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003215 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3216 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003217 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3218 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003219 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003220 src, dst, "weakassign");
3221 return;
3222}
3223
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003224/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3225/// objc_assign_global (id src, id *dst)
3226///
3227void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003228 llvm::Value *src, llvm::Value *dst,
3229 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003230 const llvm::Type * SrcTy = src->getType();
3231 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003232 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003233 assert(Size <= 8 && "does not support size > 8");
3234 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003235 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003236 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3237 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003238 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3239 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003240 if (!threadlocal)
3241 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3242 src, dst, "globalassign");
3243 else
3244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3245 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003246 return;
3247}
3248
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003249/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003250/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003251///
3252void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003253 llvm::Value *src, llvm::Value *dst,
3254 llvm::Value *ivarOffset) {
3255 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003256 const llvm::Type * SrcTy = src->getType();
3257 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003258 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003259 assert(Size <= 8 && "does not support size > 8");
3260 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003261 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003262 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3263 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003264 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3265 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003266 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3267 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003268 return;
3269}
3270
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003271/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3272/// objc_assign_strongCast (id src, id *dst)
3273///
3274void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003275 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003276 const llvm::Type * SrcTy = src->getType();
3277 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003278 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003279 assert(Size <= 8 && "does not support size > 8");
3280 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003281 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003282 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3283 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003284 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3285 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003286 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003287 src, dst, "weakassign");
3288 return;
3289}
3290
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003291void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003292 llvm::Value *DestPtr,
3293 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003294 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003295 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3296 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003297 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003298 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003299 return;
3300}
3301
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003302/// EmitObjCValueForIvar - Code Gen for ivar reference.
3303///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003304LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3305 QualType ObjectTy,
3306 llvm::Value *BaseValue,
3307 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003308 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003309 const ObjCInterfaceDecl *ID =
3310 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003311 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3312 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003313}
3314
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003315llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003316 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003317 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003318 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003319 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003320 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3321 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003322}
3323
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003324/* *** Private Interface *** */
3325
3326/// EmitImageInfo - Emit the image info marker used to encode some module
3327/// level information.
3328///
3329/// See: <rdr://4810609&4810587&4810587>
3330/// struct IMAGE_INFO {
3331/// unsigned version;
3332/// unsigned flags;
3333/// };
3334enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003335 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003336 eImageInfo_GarbageCollected = (1 << 1),
3337 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003338 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3339
Daniel Dunbar5e639272010-04-25 20:39:01 +00003340 // A flag indicating that the module has no instances of a @synthesize of a
3341 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003342 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003343};
3344
Daniel Dunbar5e639272010-04-25 20:39:01 +00003345void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003346 unsigned version = 0; // Version is unused?
3347 unsigned flags = 0;
3348
3349 // FIXME: Fix and continue?
3350 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3351 flags |= eImageInfo_GarbageCollected;
3352 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3353 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003354
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003355 // We never allow @synthesize of a superclass property.
3356 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003357
Chris Lattner5e016ae2010-06-27 07:15:29 +00003358 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3359
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003360 // Emitted as int[2];
3361 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003362 llvm::ConstantInt::get(Int32Ty, version),
3363 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003364 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003365 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003366
3367 const char *Section;
3368 if (ObjCABI == 1)
3369 Section = "__OBJC, __image_info,regular";
3370 else
3371 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003372 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003373 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003374 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003375 Section,
3376 0,
3377 true);
3378 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003379}
3380
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003381
3382// struct objc_module {
3383// unsigned long version;
3384// unsigned long size;
3385// const char *name;
3386// Symtab symtab;
3387// };
3388
3389// FIXME: Get from somewhere
3390static const int ModuleVersion = 7;
3391
3392void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003393 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003394
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003395 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003396 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3397 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003398 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003399 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003400 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003401 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003402 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003403 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003404 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003405}
3406
3407llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003408 unsigned NumClasses = DefinedClasses.size();
3409 unsigned NumCategories = DefinedCategories.size();
3410
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003411 // Return null if no symbols were defined.
3412 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003413 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003414
3415 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003416 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003417 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003418 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3419 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003420
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003421 // The runtime expects exactly the list of defined classes followed
3422 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003423 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003424 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003425 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003426 ObjCTypes.Int8PtrTy);
3427 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003428 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003429 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003430 ObjCTypes.Int8PtrTy);
3431
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003432 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003433 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003434 NumClasses + NumCategories),
3435 Symbols);
3436
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003437 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003438
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003439 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003440 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3441 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003442 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003443 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003444}
3445
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003446llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003447 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003448 LazySymbols.insert(ID->getIdentifier());
3449
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003450 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003451
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003452 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003453 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003454 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003455 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003456 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003457 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3458 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003459 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003460 }
3461
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003462 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003463}
3464
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003465llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3466 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003467 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003468
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003469 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003470 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003471 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003472 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003473 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003474 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3475 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003476 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003477 }
3478
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003479 if (lvalue)
3480 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003481 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003482}
3483
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003484llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003485 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003486
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003487 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003488 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003489 llvm::ConstantArray::get(VMContext,
3490 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003491 ((ObjCABI == 2) ?
3492 "__TEXT,__objc_classname,cstring_literals" :
3493 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003494 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003495
Owen Anderson170229f2009-07-14 23:10:40 +00003496 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003497}
3498
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003499llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3500 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3501 I = MethodDefinitions.find(MD);
3502 if (I != MethodDefinitions.end())
3503 return I->second;
3504
3505 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3506 // MD isn't emitted yet because it comes from PCH.
3507 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3508 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3509 return MethodDefinitions[MD];
3510 }
3511
3512 return NULL;
3513}
3514
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003515/// GetIvarLayoutName - Returns a unique constant for the given
3516/// ivar layout bitmap.
3517llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003518 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003519 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003520}
3521
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003522void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003523 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003524 bool ForStrongLayout,
3525 bool &HasUnion) {
3526 const RecordDecl *RD = RT->getDecl();
3527 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003528 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003529 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003530 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003531 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003532
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003533 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3534 ForStrongLayout, HasUnion);
3535}
3536
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003537void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003538 const llvm::StructLayout *Layout,
3539 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003540 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003541 unsigned int BytePos, bool ForStrongLayout,
3542 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003543 bool IsUnion = (RD && RD->isUnion());
3544 uint64_t MaxUnionIvarSize = 0;
3545 uint64_t MaxSkippedUnionIvarSize = 0;
3546 FieldDecl *MaxField = 0;
3547 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003548 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003549 uint64_t MaxFieldOffset = 0;
3550 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003551 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003552
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003553 if (RecFields.empty())
3554 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003555 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3556 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3557
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003558 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003559 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003560 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003561 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003562 // Note that 'i' here is actually the field index inside RD of Field,
3563 // although this dependency is hidden.
3564 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckc5ca8762011-04-14 00:43:09 +00003565 FieldOffset = RL.getFieldOffset(i) / ByteSizeInBits;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003566 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003567 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003568
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003569 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003570 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003571 LastFieldBitfieldOrUnnamed = Field;
3572 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003573 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003574 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003575
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003576 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003577 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003578 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003579 if (FQT->isUnionType())
3580 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003581
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003582 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003583 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003584 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003585 continue;
3586 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003587
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003588 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003589 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003590 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003591 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003592 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003593 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003594 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3595 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003596 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003597 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003598 FQT = CArray->getElementType();
3599 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003600
3601 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003602 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003603 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003604 int OldIndex = IvarsInfo.size() - 1;
3605 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003606
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003607 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003608 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003609 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003610
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003611 // Replicate layout information for each array element. Note that
3612 // one element is already done.
3613 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003614 for (int FirstIndex = IvarsInfo.size() - 1,
3615 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003616 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003617 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3618 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3619 IvarsInfo[i].ivar_size));
3620 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3621 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3622 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003623 }
3624 continue;
3625 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003626 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003627 // At this point, we are done with Record/Union and array there of.
3628 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003629 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003630
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003631 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003632 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3633 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003634 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003635 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003636 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003637 MaxUnionIvarSize = UnionIvarSize;
3638 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003639 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003640 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003641 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003642 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003643 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003644 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003645 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003646 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3647 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003648 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003649 // FIXME: Why the asymmetry? We divide by word size in bits on other
3650 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003651 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003652 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003653 MaxSkippedUnionIvarSize = UnionIvarSize;
3654 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003655 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003656 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003657 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003658 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003659 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003660 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003661 }
3662 }
3663 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003664
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003665 if (LastFieldBitfieldOrUnnamed) {
3666 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3667 // Last field was a bitfield. Must update skip info.
3668 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3669 uint64_t BitFieldSize =
3670 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3671 GC_IVAR skivar;
3672 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3673 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3674 + ((BitFieldSize % ByteSizeInBits) != 0);
3675 SkipIvars.push_back(skivar);
3676 } else {
3677 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3678 // Last field was unnamed. Must update skip info.
3679 unsigned FieldSize
3680 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3681 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3682 FieldSize / ByteSizeInBits));
3683 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003684 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003685
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003686 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003687 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003688 MaxUnionIvarSize));
3689 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003690 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003691 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003692}
3693
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003694/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3695/// the computations and returning the layout bitmap (for ivar or blocks) in
3696/// the given argument BitMap string container. Routine reads
3697/// two containers, IvarsInfo and SkipIvars which are assumed to be
3698/// filled already by the caller.
3699llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003700 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003701 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003702
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003703 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003704 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003705 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003706 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003707 if (IvarsInfo[0].ivar_bytepos == 0) {
3708 WordsToSkip = 0;
3709 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003710 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003711 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3712 WordsToScan = IvarsInfo[0].ivar_size;
3713 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003714 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003715 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003716 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003717 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003718 // consecutive 'scanned' object pointers.
3719 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003720 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003721 // Skip over 'gc'able object pointer which lay over each other.
3722 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3723 continue;
3724 // Must skip over 1 or more words. We save current skip/scan values
3725 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003726 SKIP_SCAN SkScan;
3727 SkScan.skip = WordsToSkip;
3728 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003729 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003730
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003731 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003732 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3733 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003734 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003735 WordsToSkip = 0;
3736 WordsToScan = IvarsInfo[i].ivar_size;
3737 }
3738 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003739 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003740 SKIP_SCAN SkScan;
3741 SkScan.skip = WordsToSkip;
3742 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003743 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003744 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003745
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003746 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003747 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003748 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003749 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003750 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003751 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003752 IvarsInfo[LastIndex].ivar_bytepos +
3753 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003754 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003755 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003756 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003757 SKIP_SCAN SkScan;
3758 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3759 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003760 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003761 }
3762 }
3763 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3764 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003765 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003766 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003767 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3768 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3769 // 0xM0 followed by 0x0N detected.
3770 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3771 for (int j = i+1; j < SkipScan; j++)
3772 SkipScanIvars[j] = SkipScanIvars[j+1];
3773 --SkipScan;
3774 }
3775 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003776
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003777 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003778 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003779 unsigned char byte;
3780 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3781 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3782 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3783 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003784
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003785 // first skip big.
3786 for (unsigned int ix = 0; ix < skip_big; ix++)
3787 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003788
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003789 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003790 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003791 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003792 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003793 byte |= 0xf;
3794 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003795 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003796 byte |= scan_small;
3797 scan_small = 0;
3798 }
3799 BitMap += byte;
3800 }
3801 // next scan big
3802 for (unsigned int ix = 0; ix < scan_big; ix++)
3803 BitMap += (unsigned char)(0x0f);
3804 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003805 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003806 byte = scan_small;
3807 BitMap += byte;
3808 }
3809 }
3810 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003811 unsigned char zero = 0;
3812 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003813
3814 llvm::GlobalVariable * Entry =
3815 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3816 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003817 ((ObjCABI == 2) ?
3818 "__TEXT,__objc_classname,cstring_literals" :
3819 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003820 1, true);
3821 return getConstantGEP(VMContext, Entry, 0, 0);
3822}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003823
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003824/// BuildIvarLayout - Builds ivar layout bitmap for the class
3825/// implementation for the __strong or __weak case.
3826/// The layout map displays which words in ivar list must be skipped
3827/// and which must be scanned by GC (see below). String is built of bytes.
3828/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3829/// of words to skip and right nibble is count of words to scan. So, each
3830/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3831/// represented by a 0x00 byte which also ends the string.
3832/// 1. when ForStrongLayout is true, following ivars are scanned:
3833/// - id, Class
3834/// - object *
3835/// - __strong anything
3836///
3837/// 2. When ForStrongLayout is false, following ivars are scanned:
3838/// - __weak anything
3839///
3840llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3841 const ObjCImplementationDecl *OMD,
3842 bool ForStrongLayout) {
3843 bool hasUnion = false;
3844
3845 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3846 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3847 return llvm::Constant::getNullValue(PtrTy);
3848
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003849 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003850 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003851 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003852
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003853 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003854 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3855 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3856
3857 if (RecFields.empty())
3858 return llvm::Constant::getNullValue(PtrTy);
3859
3860 SkipIvars.clear();
3861 IvarsInfo.clear();
3862
3863 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3864 if (IvarsInfo.empty())
3865 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003866 // Sort on byte position in case we encounterred a union nested in
3867 // the ivar list.
3868 if (hasUnion && !IvarsInfo.empty())
3869 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3870 if (hasUnion && !SkipIvars.empty())
3871 std::sort(SkipIvars.begin(), SkipIvars.end());
3872
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003873 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003874 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003875
3876 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003877 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003878 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00003879 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003880 const unsigned char *s = (unsigned char*)BitMap.c_str();
3881 for (unsigned i = 0; i < BitMap.size(); i++)
3882 if (!(s[i] & 0xf0))
3883 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3884 else
3885 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3886 printf("\n");
3887 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003888 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003889}
3890
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003891llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003892 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3893
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003894 // FIXME: Avoid std::string copying.
3895 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003896 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003897 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003898 ((ObjCABI == 2) ?
3899 "__TEXT,__objc_methname,cstring_literals" :
3900 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003901 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003902
Owen Anderson170229f2009-07-14 23:10:40 +00003903 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003904}
3905
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003906// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003907llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003908 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3909}
3910
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003911llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003912 std::string TypeStr;
3913 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3914
3915 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003916
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003917 if (!Entry)
3918 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003919 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003920 ((ObjCABI == 2) ?
3921 "__TEXT,__objc_methtype,cstring_literals" :
3922 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003923 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003924
Owen Anderson170229f2009-07-14 23:10:40 +00003925 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003926}
3927
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003928llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003929 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003930 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3931 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003932
3933 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3934
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003935 if (!Entry)
3936 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003937 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003938 ((ObjCABI == 2) ?
3939 "__TEXT,__objc_methtype,cstring_literals" :
3940 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003941 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003942
Owen Anderson170229f2009-07-14 23:10:40 +00003943 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003944}
3945
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003946// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003947llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003948 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003949
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003950 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003951 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003952 llvm::ConstantArray::get(VMContext,
3953 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003954 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003955 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003956
Owen Anderson170229f2009-07-14 23:10:40 +00003957 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003958}
3959
3960// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003961// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003962llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003963CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3964 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003965 std::string TypeStr;
3966 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003967 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3968}
3969
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003970void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003971 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003972 llvm::SmallVectorImpl<char> &Name) {
3973 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003974 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003975 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3976 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003977 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003978 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003979 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003980 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003981}
3982
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003983void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003984 EmitModuleInfo();
3985
Daniel Dunbarc475d422008-10-29 22:36:39 +00003986 // Emit the dummy bodies for any protocols which were referenced but
3987 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003988 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003989 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3990 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003991 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003992
Daniel Dunbarc475d422008-10-29 22:36:39 +00003993 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00003994 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003995 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00003996 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003997 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00003998 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003999 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004000 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004001 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004002 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004003 }
4004
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004005 // Add assembler directives to add lazy undefined symbol references
4006 // for classes which are referenced but not defined. This is
4007 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004008 //
4009 // FIXME: It would be nice if we had an LLVM construct for this.
4010 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4011 llvm::SmallString<256> Asm;
4012 Asm += CGM.getModule().getModuleInlineAsm();
4013 if (!Asm.empty() && Asm.back() != '\n')
4014 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004015
Daniel Dunbard027a922009-09-07 00:20:42 +00004016 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004017 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4018 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004019 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4020 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004021 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004022 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004023 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004024 }
4025
4026 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4027 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4028 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4029 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004030
Daniel Dunbard027a922009-09-07 00:20:42 +00004031 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004032 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004033}
4034
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004035CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004036 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004037 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004038 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004039 ObjCABI = 2;
4040}
4041
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004042/* *** */
4043
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004044ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004045 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004046 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4047 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004048
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004049 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004050 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004051 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004052 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004053 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004054
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004055 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004056 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004057 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004058
Mike Stump18bb9282009-05-16 07:57:57 +00004059 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4060 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00004061 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004062 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004063
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004064 // I'm not sure I like this. The implicit coordination is a bit
4065 // gross. We should solve this in a reasonable fashion because this
4066 // is a pretty common task (match some runtime data structure with
4067 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004068
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004069 // FIXME: This is leaked.
4070 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004071
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004072 // struct _objc_super {
4073 // id self;
4074 // Class cls;
4075 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004076 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004077 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004078 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004079 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004080 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004081 Ctx.getObjCIdType(), 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004082 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004083 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004084 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004085
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004086 SuperCTy = Ctx.getTagDeclType(RD);
4087 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004088
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004089 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004090 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4091
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004092 // struct _prop_t {
4093 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004094 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004095 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004096 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004097 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004098 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004099
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004100 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004101 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004102 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004103 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004104 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004105 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004106 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004107 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004108 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004109 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004110 PropertyListTy);
4111 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004112 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004113
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004114 // struct _objc_method {
4115 // SEL _cmd;
4116 // char *method_type;
4117 // char *_imp;
4118 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004119 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004120 Int8PtrTy,
4121 Int8PtrTy,
4122 NULL);
4123 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004124
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004125 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004126 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004127 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004128 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004129}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004130
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004131ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004132 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004133 // struct _objc_method_description {
4134 // SEL name;
4135 // char *types;
4136 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004137 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004138 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004139 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004140 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004141 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004142 MethodDescriptionTy);
4143
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004144 // struct _objc_method_description_list {
4145 // int count;
4146 // struct _objc_method_description[1];
4147 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004148 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004149 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004150 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004151 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004152 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004153 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004154
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004155 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004157 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004158
Daniel Dunbarb036db82008-08-13 03:21:16 +00004159 // Protocol description structures
4160
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004161 // struct _objc_protocol_extension {
4162 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4163 // struct _objc_method_description_list *optional_instance_methods;
4164 // struct _objc_method_description_list *optional_class_methods;
4165 // struct _objc_property_list *instance_properties;
4166 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004167 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004168 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004169 MethodDescriptionListPtrTy,
4170 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00004171 PropertyListPtrTy,
4172 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004173 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004174 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004175
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004176 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004177 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004178
Daniel Dunbarc475d422008-10-29 22:36:39 +00004179 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004180
Owen Andersonc36edfe2009-08-13 23:27:53 +00004181 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4182 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004183
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004184 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00004185 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004186 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004187 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004188 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004189 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004190 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4191
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004192 // struct _objc_protocol {
4193 // struct _objc_protocol_extension *isa;
4194 // char *protocol_name;
4195 // struct _objc_protocol **_objc_protocol_list;
4196 // struct _objc_method_description_list *instance_methods;
4197 // struct _objc_method_description_list *class_methods;
4198 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004199 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004200 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004201 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00004202 MethodDescriptionListPtrTy,
4203 MethodDescriptionListPtrTy,
4204 NULL);
4205 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4206
4207 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00004209 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004210 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004211 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004212
4213 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004214 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004215 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004216
4217 // Class description structures
4218
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004219 // struct _objc_ivar {
4220 // char *ivar_name;
4221 // char *ivar_type;
4222 // int ivar_offset;
4223 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004224 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004225 Int8PtrTy,
4226 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004227 NULL);
4228 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4229
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004230 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004231 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004232 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004233 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004234
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004235 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004236 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004237 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004238 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004239
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004240 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004241 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004242 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004243 Int8PtrTy,
4244 PropertyListPtrTy,
4245 NULL);
4246 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004247 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004248
Owen Andersonc36edfe2009-08-13 23:27:53 +00004249 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004250
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004251 // struct _objc_class {
4252 // Class isa;
4253 // Class super_class;
4254 // char *name;
4255 // long version;
4256 // long info;
4257 // long instance_size;
4258 // struct _objc_ivar_list *ivars;
4259 // struct _objc_method_list *methods;
4260 // struct _objc_cache *cache;
4261 // struct _objc_protocol_list *protocols;
4262 // char *ivar_layout;
4263 // struct _objc_class_ext *ext;
4264 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004265 T = llvm::StructType::get(VMContext,
4266 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004267 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004268 Int8PtrTy,
4269 LongTy,
4270 LongTy,
4271 LongTy,
4272 IvarListPtrTy,
4273 MethodListPtrTy,
4274 CachePtrTy,
4275 ProtocolListPtrTy,
4276 Int8PtrTy,
4277 ClassExtensionPtrTy,
4278 NULL);
4279 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004280
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004281 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4282 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004283 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004284
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004285 // struct _objc_category {
4286 // char *category_name;
4287 // char *class_name;
4288 // struct _objc_method_list *instance_method;
4289 // struct _objc_method_list *class_method;
4290 // uint32_t size; // sizeof(struct _objc_category)
4291 // struct _objc_property_list *instance_properties;// category's @property
4292 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004293 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004294 Int8PtrTy,
4295 MethodListPtrTy,
4296 MethodListPtrTy,
4297 ProtocolListPtrTy,
4298 IntTy,
4299 PropertyListPtrTy,
4300 NULL);
4301 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4302
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004303 // Global metadata structures
4304
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004305 // struct _objc_symtab {
4306 // long sel_ref_cnt;
4307 // SEL *refs;
4308 // short cls_def_cnt;
4309 // short cat_def_cnt;
4310 // char *defs[cls_def_cnt + cat_def_cnt];
4311 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004312 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004313 SelectorPtrTy,
4314 ShortTy,
4315 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004316 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004317 NULL);
4318 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004319 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004320
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004321 // struct _objc_module {
4322 // long version;
4323 // long size; // sizeof(struct _objc_module)
4324 // char *name;
4325 // struct _objc_symtab* symtab;
4326 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004327 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004328 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004329 LongTy,
4330 Int8PtrTy,
4331 SymtabPtrTy,
4332 NULL);
4333 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004334
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004335
Mike Stump18bb9282009-05-16 07:57:57 +00004336 // FIXME: This is the size of the setjmp buffer and should be target
4337 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004338 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004339
Anders Carlsson9ff22482008-09-09 10:10:21 +00004340 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004341 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004342 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004343
4344 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004345 llvm::StructType::get(VMContext,
4346 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4347 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004348 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004349 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004350 ExceptionDataTy);
4351
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004352}
4353
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004354ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004355 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004356 // struct _method_list_t {
4357 // uint32_t entsize; // sizeof(struct _objc_method)
4358 // uint32_t method_count;
4359 // struct _objc_method method_list[method_count];
4360 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004361 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004362 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004363 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004364 NULL);
4365 CGM.getModule().addTypeName("struct.__method_list_t",
4366 MethodListnfABITy);
4367 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004368 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004369
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004370 // struct _protocol_t {
4371 // id isa; // NULL
4372 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004373 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004374 // const struct method_list_t * const instance_methods;
4375 // const struct method_list_t * const class_methods;
4376 // const struct method_list_t *optionalInstanceMethods;
4377 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004378 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004379 // const uint32_t size; // sizeof(struct _protocol_t)
4380 // const uint32_t flags; // = 0
4381 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004382
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004383 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004384 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004385
Owen Anderson758428f2009-08-05 23:18:46 +00004386 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004387 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004388 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004389 ProtocolListTyHolder),
4390 MethodListnfABIPtrTy,
4391 MethodListnfABIPtrTy,
4392 MethodListnfABIPtrTy,
4393 MethodListnfABIPtrTy,
4394 PropertyListPtrTy,
4395 IntTy,
4396 IntTy,
4397 NULL);
4398 CGM.getModule().addTypeName("struct._protocol_t",
4399 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004400
4401 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004402 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004403
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004404 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004405 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004406 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004407 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004408 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004409 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004410 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004411 NULL);
4412 CGM.getModule().addTypeName("struct._objc_protocol_list",
4413 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004414 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004415 ProtocolListnfABITy);
4416
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004417 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004418 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004419
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004420 // struct _ivar_t {
4421 // unsigned long int *offset; // pointer to ivar offset location
4422 // char *name;
4423 // char *type;
4424 // uint32_t alignment;
4425 // uint32_t size;
4426 // }
Mike Stump11289f42009-09-09 15:08:12 +00004427 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004428 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004429 Int8PtrTy,
4430 Int8PtrTy,
4431 IntTy,
4432 IntTy,
4433 NULL);
4434 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004435
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004436 // struct _ivar_list_t {
4437 // uint32 entsize; // sizeof(struct _ivar_t)
4438 // uint32 count;
4439 // struct _iver_t list[count];
4440 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004441 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004442 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004443 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004444 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004445 NULL);
4446 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004447
Owen Anderson9793f0e2009-07-29 22:16:19 +00004448 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004449
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004450 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004451 // uint32_t const flags;
4452 // uint32_t const instanceStart;
4453 // uint32_t const instanceSize;
4454 // uint32_t const reserved; // only when building for 64bit targets
4455 // const uint8_t * const ivarLayout;
4456 // const char *const name;
4457 // const struct _method_list_t * const baseMethods;
4458 // const struct _objc_protocol_list *const baseProtocols;
4459 // const struct _ivar_list_t *const ivars;
4460 // const uint8_t * const weakIvarLayout;
4461 // const struct _prop_list_t * const properties;
4462 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004463
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004464 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004465 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004466 IntTy,
4467 IntTy,
4468 Int8PtrTy,
4469 Int8PtrTy,
4470 MethodListnfABIPtrTy,
4471 ProtocolListnfABIPtrTy,
4472 IvarListnfABIPtrTy,
4473 Int8PtrTy,
4474 PropertyListPtrTy,
4475 NULL);
4476 CGM.getModule().addTypeName("struct._class_ro_t",
4477 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004478
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004479 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4480 std::vector<const llvm::Type*> Params;
4481 Params.push_back(ObjectPtrTy);
4482 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004483 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004484 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4485
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004486 // struct _class_t {
4487 // struct _class_t *isa;
4488 // struct _class_t * const superclass;
4489 // void *cache;
4490 // IMP *vtable;
4491 // struct class_ro_t *ro;
4492 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004493
Owen Andersonc36edfe2009-08-13 23:27:53 +00004494 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004495 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004496 llvm::StructType::get(VMContext,
4497 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004498 llvm::PointerType::getUnqual(ClassTyHolder),
4499 CachePtrTy,
4500 llvm::PointerType::getUnqual(ImpnfABITy),
4501 llvm::PointerType::getUnqual(ClassRonfABITy),
4502 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004503 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4504
4505 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004506 ClassnfABITy);
4507
Fariborz Jahanian71394042009-01-23 23:53:38 +00004508 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004509 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004510
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004511 // struct _category_t {
4512 // const char * const name;
4513 // struct _class_t *const cls;
4514 // const struct _method_list_t * const instance_methods;
4515 // const struct _method_list_t * const class_methods;
4516 // const struct _protocol_list_t * const protocols;
4517 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004518 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004519 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004520 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004521 MethodListnfABIPtrTy,
4522 MethodListnfABIPtrTy,
4523 ProtocolListnfABIPtrTy,
4524 PropertyListPtrTy,
4525 NULL);
4526 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004527
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004528 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004529 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4530 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004531
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004532 // MessageRefTy - LLVM for:
4533 // struct _message_ref_t {
4534 // IMP messenger;
4535 // SEL name;
4536 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004537
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004538 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004539 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004540 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004541 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004542 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004543 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004544 Ctx.VoidPtrTy, 0, 0, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004545 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004546 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004547 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004549 MessageRefCTy = Ctx.getTagDeclType(RD);
4550 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4551 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004552
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004553 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004554 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004555
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004556 // SuperMessageRefTy - LLVM for:
4557 // struct _super_message_ref_t {
4558 // SUPER_IMP messenger;
4559 // SEL name;
4560 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004561 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004562 SelectorPtrTy,
4563 NULL);
4564 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004565
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004566 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004567 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4568
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004569
4570 // struct objc_typeinfo {
4571 // const void** vtable; // objc_ehtype_vtable + 2
4572 // const char* name; // c++ typeinfo string
4573 // Class cls;
4574 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004575 EHTypeTy = llvm::StructType::get(VMContext,
4576 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004577 Int8PtrTy,
4578 ClassnfABIPtrTy,
4579 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004580 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004581 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004582}
4583
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004584llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004585 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004586
Fariborz Jahanian71394042009-01-23 23:53:38 +00004587 return NULL;
4588}
4589
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004590void CGObjCNonFragileABIMac::AddModuleClassList(const
4591 std::vector<llvm::GlobalValue*>
4592 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004593 const char *SymbolName,
4594 const char *SectionName) {
4595 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004596
Daniel Dunbar19573e72009-05-15 21:48:48 +00004597 if (!NumClasses)
4598 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004599
Daniel Dunbar19573e72009-05-15 21:48:48 +00004600 std::vector<llvm::Constant*> Symbols(NumClasses);
4601 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004602 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004603 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004604 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004605 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004606 NumClasses),
4607 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004608
Daniel Dunbar19573e72009-05-15 21:48:48 +00004609 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004610 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004611 llvm::GlobalValue::InternalLinkage,
4612 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004613 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004614 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004615 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004616 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004617}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004618
Fariborz Jahanian71394042009-01-23 23:53:38 +00004619void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4620 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004621
Daniel Dunbar19573e72009-05-15 21:48:48 +00004622 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004623 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004625 "\01L_OBJC_LABEL_CLASS_$",
4626 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004627
Fariborz Jahanian67260552009-11-17 21:37:35 +00004628 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4629 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4630 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4631 continue;
4632 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004633 }
4634
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004635 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4636 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4637 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4638 continue;
4639 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4640 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004641
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004642 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004643 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4644 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004645
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004646 // Build list of all implemented category addresses in array
4647 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004648 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004649 "\01L_OBJC_LABEL_CATEGORY_$",
4650 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004651 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004652 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4653 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004654
Daniel Dunbar5e639272010-04-25 20:39:01 +00004655 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004656}
4657
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004658/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004659/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004660/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004661/// message dispatch call for all the rest.
4662///
4663bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004664 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4665 default:
4666 assert(0 && "Invalid dispatch method!");
4667 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004668 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004669 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004670 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004671 case CodeGenOptions::Mixed:
4672 break;
4673 }
4674
4675 // If so, see whether this selector is in the white-list of things which must
4676 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004677 if (NonLegacyDispatchMethods.empty()) {
4678 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4679 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4680 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4681 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4682 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4683 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4684 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4685 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4686 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4687 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004688
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004689 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4690 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4691 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4692 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4693 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4694 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4695 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4696 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004697 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004698 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004699 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4700 &CGM.getContext().Idents.get("objects"),
4701 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004702 };
4703 NonLegacyDispatchMethods.insert(
4704 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004705 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004706
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004707 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004708}
4709
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004710// Metadata flags
4711enum MetaDataDlags {
4712 CLS = 0x0,
4713 CLS_META = 0x1,
4714 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004715 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004716 CLS_EXCEPTION = 0x20
4717};
4718/// BuildClassRoTInitializer - generate meta-data for:
4719/// struct _class_ro_t {
4720/// uint32_t const flags;
4721/// uint32_t const instanceStart;
4722/// uint32_t const instanceSize;
4723/// uint32_t const reserved; // only when building for 64bit targets
4724/// const uint8_t * const ivarLayout;
4725/// const char *const name;
4726/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004727/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004728/// const struct _ivar_list_t *const ivars;
4729/// const uint8_t * const weakIvarLayout;
4730/// const struct _prop_list_t * const properties;
4731/// }
4732///
4733llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004734 unsigned flags,
4735 unsigned InstanceStart,
4736 unsigned InstanceSize,
4737 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004738 std::string ClassName = ID->getNameAsString();
4739 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004740 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4741 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4742 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004743 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4745 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004746 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004747 // const struct _method_list_t * const baseMethods;
4748 std::vector<llvm::Constant*> Methods;
4749 std::string MethodListName("\01l_OBJC_$_");
4750 if (flags & CLS_META) {
4751 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004752 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004753 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004754 // Class methods should always be defined.
4755 Methods.push_back(GetMethodConstant(*i));
4756 }
4757 } else {
4758 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004759 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004760 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004761 // Instance methods should always be defined.
4762 Methods.push_back(GetMethodConstant(*i));
4763 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004764 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004765 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004766 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004767
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004768 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4769 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004770
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004771 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4772 if (llvm::Constant *C = GetMethodConstant(MD))
4773 Methods.push_back(C);
4774 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4775 if (llvm::Constant *C = GetMethodConstant(MD))
4776 Methods.push_back(C);
4777 }
4778 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004779 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004780 Values[ 5] = EmitMethodList(MethodListName,
4781 "__DATA, __objc_const", Methods);
4782
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004783 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4784 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004785 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004786 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004787 OID->all_referenced_protocol_begin(),
4788 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004789
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004790 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004791 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004792 else
4793 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4795 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004796 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004797 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004798 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004799 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4800 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004801 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004802 Values);
4803 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004804 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4805 llvm::GlobalValue::InternalLinkage,
4806 Init,
4807 (flags & CLS_META) ?
4808 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4809 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004810 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004811 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004812 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004813 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004814
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004815}
4816
4817/// BuildClassMetaData - This routine defines that to-level meta-data
4818/// for the given ClassName for:
4819/// struct _class_t {
4820/// struct _class_t *isa;
4821/// struct _class_t * const superclass;
4822/// void *cache;
4823/// IMP *vtable;
4824/// struct class_ro_t *ro;
4825/// }
4826///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004827llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004828 std::string &ClassName,
4829 llvm::Constant *IsAGV,
4830 llvm::Constant *SuperClassGV,
4831 llvm::Constant *ClassRoGV,
4832 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004833 std::vector<llvm::Constant*> Values(5);
4834 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004835 Values[1] = SuperClassGV;
4836 if (!Values[1])
4837 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004838 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4839 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4840 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004841 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004842 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004843 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4844 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004845 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004846 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004847 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004848 if (HiddenVisibility)
4849 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004850 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004851}
4852
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004853bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004854CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004855 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004856}
4857
Daniel Dunbar961202372009-05-03 12:57:56 +00004858void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004859 uint32_t &InstanceStart,
4860 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004861 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004862 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004863
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004864 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00004865 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004866
4867 // If there are no fields, the start is the same as the end.
4868 if (!RL.getFieldCount())
4869 InstanceStart = InstanceSize;
4870 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00004871 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00004872}
4873
Fariborz Jahanian71394042009-01-23 23:53:38 +00004874void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4875 std::string ClassName = ID->getNameAsString();
4876 if (!ObjCEmptyCacheVar) {
4877 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004878 CGM.getModule(),
4879 ObjCTypes.CacheTy,
4880 false,
4881 llvm::GlobalValue::ExternalLinkage,
4882 0,
4883 "_objc_empty_cache");
4884
Fariborz Jahanian71394042009-01-23 23:53:38 +00004885 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004886 CGM.getModule(),
4887 ObjCTypes.ImpnfABITy,
4888 false,
4889 llvm::GlobalValue::ExternalLinkage,
4890 0,
4891 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004892 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004893 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004894 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004895 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004896 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004897 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004898 uint32_t InstanceSize = InstanceStart;
4899 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004900 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4901 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004902
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004903 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004904
4905 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00004906 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004907 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004908 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004909 if (ID->getNumIvarInitializers())
4910 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004911 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004912 // class is root
4913 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004914 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004915 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004916 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004917 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004918 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4919 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4920 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004921 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004922 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004923 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004924 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004925 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004926 ObjCMetaClassName +
4927 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004928 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004929 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004930 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004931 }
4932 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4933 InstanceStart,
4934 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004935 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004936 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004937 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4938 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004939 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004940
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004941 // Metadata for the class
4942 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004943 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004944 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004945 if (ID->getNumIvarInitializers())
4946 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004947
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004948 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004949 flags |= CLS_EXCEPTION;
4950
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004951 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004952 flags |= CLS_ROOT;
4953 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004954 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004955 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004956 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004957 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004958 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004959 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00004960 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004961 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004962 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004963 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004964 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004966 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004967
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004968 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004969 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004970 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4971 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004972 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004973
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004974 // Determine if this class is also "non-lazy".
4975 if (ImplementationIsNonLazy(ID))
4976 DefinedNonLazyClasses.push_back(ClassMD);
4977
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004978 // Force the definition of the EHType if necessary.
4979 if (flags & CLS_EXCEPTION)
4980 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00004981}
4982
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004983/// GenerateProtocolRef - This routine is called to generate code for
4984/// a protocol reference expression; as in:
4985/// @code
4986/// @protocol(Proto1);
4987/// @endcode
4988/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4989/// which will hold address of the protocol meta-data.
4990///
4991llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004992 const ObjCProtocolDecl *PD) {
4993
Fariborz Jahanian464423d2009-04-10 18:47:34 +00004994 // This routine is called for @protocol only. So, we must build definition
4995 // of protocol's meta-data (not a reference to it!)
4996 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004997 llvm::Constant *Init =
4998 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4999 ObjCTypes.ExternalProtocolPtrTy);
5000
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005001 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005002 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005003
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005004 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5005 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005006 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005007 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005008 CGM.getModule(),
5009 Init->getType(), false,
5010 llvm::GlobalValue::WeakAnyLinkage,
5011 Init,
5012 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005013 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5014 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005015 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005016 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005017}
5018
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005019/// GenerateCategory - Build metadata for a category implementation.
5020/// struct _category_t {
5021/// const char * const name;
5022/// struct _class_t *const cls;
5023/// const struct _method_list_t * const instance_methods;
5024/// const struct _method_list_t * const class_methods;
5025/// const struct _protocol_list_t * const protocols;
5026/// const struct _prop_list_t * const properties;
5027/// }
5028///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005029void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005030 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005031 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005032 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5033 "_$_" + OCD->getNameAsString());
5034 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005035 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005036
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005037 std::vector<llvm::Constant*> Values(6);
5038 Values[0] = GetClassName(OCD->getIdentifier());
5039 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005040 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005041 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005042 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5043
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005044 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005045 std::vector<llvm::Constant*> Methods;
5046 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005047 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005048 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005049
5050 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005051 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005052 // Instance methods should always be defined.
5053 Methods.push_back(GetMethodConstant(*i));
5054 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055
5056 Values[2] = EmitMethodList(MethodListName,
5057 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005058 Methods);
5059
5060 MethodListName = Prefix;
5061 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5062 OCD->getNameAsString();
5063 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005064 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005065 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005066 // Class methods should always be defined.
5067 Methods.push_back(GetMethodConstant(*i));
5068 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005069
5070 Values[3] = EmitMethodList(MethodListName,
5071 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005072 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005074 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005075 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005076 llvm::SmallString<256> ExtName;
5077 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5078 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005079 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005080 + Interface->getName() + "_$_"
5081 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005082 Category->protocol_begin(),
5083 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005084 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5085 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005086 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005087 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5088 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005089 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005090
5091 llvm::Constant *Init =
5092 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005093 Values);
5094 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005095 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005096 false,
5097 llvm::GlobalValue::InternalLinkage,
5098 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005099 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005100 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005101 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005102 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005103 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005104 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005105
5106 // Determine if this category is also "non-lazy".
5107 if (ImplementationIsNonLazy(OCD))
5108 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005109}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005110
5111/// GetMethodConstant - Return a struct objc_method constant for the
5112/// given method if it has been defined. The result is null if the
5113/// method has not been defined. The return value has type MethodPtrTy.
5114llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005115 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005116 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005117 if (!Fn)
5118 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005119
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005120 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005121 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00005122 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005123 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005124 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00005125 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005126 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005127}
5128
5129/// EmitMethodList - Build meta-data for method declarations
5130/// struct _method_list_t {
5131/// uint32_t entsize; // sizeof(struct _objc_method)
5132/// uint32_t method_count;
5133/// struct _objc_method method_list[method_count];
5134/// }
5135///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005136llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5137 const char *Section,
5138 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005139 // Return null for empty list.
5140 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005141 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005143 std::vector<llvm::Constant*> Values(3);
5144 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005145 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005146 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005147 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005148 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005149 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005150 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005151 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005152 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005153
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005154 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005155 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005156 llvm::GlobalValue::InternalLinkage,
5157 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005158 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005159 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005160 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005161 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005162 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005163 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005164 ObjCTypes.MethodListnfABIPtrTy);
5165}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005166
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005167/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5168/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005169llvm::GlobalVariable *
5170CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5171 const ObjCIvarDecl *Ivar) {
5172 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005173 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005174 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005175 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005176 CGM.getModule().getGlobalVariable(Name);
5177 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005178 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005179 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005180 false,
5181 llvm::GlobalValue::ExternalLinkage,
5182 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005183 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005184 return IvarOffsetGV;
5185}
5186
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005187llvm::Constant *
5188CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5189 const ObjCIvarDecl *Ivar,
5190 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005191 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005192 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005193 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005194 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005195 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005196
Mike Stump18bb9282009-05-16 07:57:57 +00005197 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5198 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005199 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5200 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005201 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005202 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005203 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005204 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005205 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005206 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005207}
5208
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005209/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005210/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005211/// IvarListnfABIPtrTy.
5212/// struct _ivar_t {
5213/// unsigned long int *offset; // pointer to ivar offset location
5214/// char *name;
5215/// char *type;
5216/// uint32_t alignment;
5217/// uint32_t size;
5218/// }
5219/// struct _ivar_list_t {
5220/// uint32 entsize; // sizeof(struct _ivar_t)
5221/// uint32 count;
5222/// struct _iver_t list[count];
5223/// }
5224///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005225
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005226llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005227 const ObjCImplementationDecl *ID) {
5228
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005229 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005230
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005231 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5232 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005233
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005234 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005235
Daniel Dunbarae032262009-04-20 00:33:43 +00005236 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005237 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005238 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005239
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005240 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5241 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005242 // Ignore unnamed bit-fields.
5243 if (!IVD->getDeclName())
5244 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005245 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005246 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005247 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5248 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005249 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005250 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005251 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005252 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005253 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005254 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005255 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005256 // NOTE. Size of a bitfield does not match gcc's, because of the
5257 // way bitfields are treated special in each. But I am told that
5258 // 'size' for bitfield ivars is ignored by the runtime so it does
5259 // not matter. If it matters, there is enough info to get the
5260 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005261 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005262 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005263 }
5264 // Return null for empty list.
5265 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005266 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005267 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005268 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005269 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5270 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005271 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005272 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005273 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005274 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005275 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5276 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005277 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005278 llvm::GlobalValue::InternalLinkage,
5279 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005280 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005281 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005282 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005283 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005284
Chris Lattnerf56501c2009-07-17 23:57:13 +00005285 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005286 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005287}
5288
5289llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005290 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005291 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005292
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005293 if (!Entry) {
5294 // We use the initializer as a marker of whether this is a forward
5295 // reference or not. At module finalization we add the empty
5296 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005297 Entry =
5298 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5299 llvm::GlobalValue::ExternalLinkage,
5300 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005301 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005302 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005303 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005304
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005305 return Entry;
5306}
5307
5308/// GetOrEmitProtocol - Generate the protocol meta-data:
5309/// @code
5310/// struct _protocol_t {
5311/// id isa; // NULL
5312/// const char * const protocol_name;
5313/// const struct _protocol_list_t * protocol_list; // super protocols
5314/// const struct method_list_t * const instance_methods;
5315/// const struct method_list_t * const class_methods;
5316/// const struct method_list_t *optionalInstanceMethods;
5317/// const struct method_list_t *optionalClassMethods;
5318/// const struct _prop_list_t * properties;
5319/// const uint32_t size; // sizeof(struct _protocol_t)
5320/// const uint32_t flags; // = 0
5321/// }
5322/// @endcode
5323///
5324
5325llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005326 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005327 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005328
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005329 // Early exit if a defining object has already been generated.
5330 if (Entry && Entry->hasInitializer())
5331 return Entry;
5332
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005333 // Construct method lists.
5334 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5335 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005336 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005337 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005338 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005339 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005340 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5341 OptInstanceMethods.push_back(C);
5342 } else {
5343 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005344 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005345 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005346
5347 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005348 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005349 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005350 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005351 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5352 OptClassMethods.push_back(C);
5353 } else {
5354 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005355 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005356 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005357
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005358 std::vector<llvm::Constant*> Values(10);
5359 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005360 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005361 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005362 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5363 PD->protocol_begin(),
5364 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005365
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005366 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005367 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005368 "__DATA, __objc_const",
5369 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005371 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005372 "__DATA, __objc_const",
5373 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005374 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005375 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005376 "__DATA, __objc_const",
5377 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005378 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005379 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005380 "__DATA, __objc_const",
5381 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005382 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005383 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005384 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005385 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005386 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005387 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005388 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005389 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005390
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005391 if (Entry) {
5392 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005393 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005394 Entry->setInitializer(Init);
5395 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005396 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005397 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5398 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5399 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005400 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005401 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005402 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005403 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005404 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005405 CGM.AddUsedGlobal(Entry);
5406
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005407 // Use this protocol meta-data to build protocol list table in section
5408 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005409 llvm::GlobalVariable *PTGV =
5410 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5411 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5412 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005413 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005414 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005415 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005416 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005417 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005418 return Entry;
5419}
5420
5421/// EmitProtocolList - Generate protocol list meta-data:
5422/// @code
5423/// struct _protocol_list_t {
5424/// long protocol_count; // Note, this is 32/64 bit
5425/// struct _protocol_t[protocol_count];
5426/// }
5427/// @endcode
5428///
5429llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005430CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5431 ObjCProtocolDecl::protocol_iterator begin,
5432 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005433 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005435 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005436 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005437 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005438
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005439 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005440 llvm::SmallString<256> TmpName;
5441 Name.toVector(TmpName);
5442 llvm::GlobalVariable *GV =
5443 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005444 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005445 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005446
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005447 for (; begin != end; ++begin)
5448 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5449
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005450 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005451 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452 ObjCTypes.ProtocolnfABIPtrTy));
5453
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005454 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005455 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005456 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005457 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005458 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005459 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005460 ProtocolRefs.size()),
5461 ProtocolRefs);
5462
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005463 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005464 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005465 llvm::GlobalValue::InternalLinkage,
5466 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005467 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005468 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005469 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005470 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005471 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005472 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005473 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005474}
5475
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005476/// GetMethodDescriptionConstant - This routine build following meta-data:
5477/// struct _objc_method {
5478/// SEL _cmd;
5479/// char *method_type;
5480/// char *_imp;
5481/// }
5482
5483llvm::Constant *
5484CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5485 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005486 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005487 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5488 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005489 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005490 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005491 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005492 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005493}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005494
5495/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5496/// This code gen. amounts to generating code for:
5497/// @code
5498/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5499/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005500///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005501LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005502 CodeGen::CodeGenFunction &CGF,
5503 QualType ObjectTy,
5504 llvm::Value *BaseValue,
5505 const ObjCIvarDecl *Ivar,
5506 unsigned CVRQualifiers) {
5507 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005508 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5509 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005510}
5511
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005512llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005513 CodeGen::CodeGenFunction &CGF,
5514 const ObjCInterfaceDecl *Interface,
5515 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005516 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005517}
5518
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005519CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005520 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005521 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005522 QualType ResultType,
5523 Selector Sel,
5524 llvm::Value *Receiver,
5525 QualType Arg0Ty,
5526 bool IsSuper,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005527 const CallArgList &CallArgs,
5528 const ObjCMethodDecl *Method) {
Mike Stump18bb9282009-05-16 07:57:57 +00005529 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5530 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005531 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005532 llvm::Value *Arg0 = Receiver;
5533 if (!IsSuper)
5534 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005535
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005536 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005537 // FIXME. This is too much work to get the ABI-specific result type needed to
5538 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005539 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005540 = Types.getFunctionInfo(ResultType, CallArgList(),
5541 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005542 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005543 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005544 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallc5799442011-02-26 09:48:59 +00005545 EmitNullReturnInitialization(CGF, Return, ResultType);
Chris Lattner396639d2010-08-18 16:09:06 +00005546 if (IsSuper) {
5547 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5548 Name += "objc_msgSendSuper2_stret_fixup";
5549 } else {
5550 Fn = ObjCTypes.getMessageSendStretFixupFn();
5551 Name += "objc_msgSend_stret_fixup";
5552 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005553 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5554 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5555 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005556 } else {
Chris Lattner396639d2010-08-18 16:09:06 +00005557 if (IsSuper) {
5558 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5559 Name += "objc_msgSendSuper2_fixup";
5560 } else {
5561 Fn = ObjCTypes.getMessageSendFixupFn();
5562 Name += "objc_msgSend_fixup";
5563 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005564 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005565 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005566 Name += '_';
5567 std::string SelName(Sel.getAsString());
5568 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005569 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005570 if (SelName[i] == ':')
5571 SelName[i] = '_';
5572 Name += SelName;
5573 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5574 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005575 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005576 std::vector<llvm::Constant*> Values(2);
5577 Values[0] = Fn;
5578 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005579 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005580 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005581 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005582 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005583 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005584 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005585 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005586 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005587 }
5588 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005589
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005590 CallArgList ActualArgs;
5591 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005592 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005593 ObjCTypes.MessageRefCPtrTy));
5594 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005595 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005596 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005597 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5598 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005599 const llvm::FunctionType *FTy =
5600 Types.GetFunctionType(FnInfo1, Method ? Method->isVariadic() : false);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005601 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005602 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005603 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005604}
5605
5606/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005607CodeGen::RValue
5608CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005609 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005610 QualType ResultType,
5611 Selector Sel,
5612 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005613 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005614 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005615 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005616 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005617 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5618 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005619 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005620 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005621 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005622 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005623 false, CallArgs, Method);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005624}
5625
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005626llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005627CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005628 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5629
Daniel Dunbara6468342009-03-02 05:18:14 +00005630 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005631 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005632 false, llvm::GlobalValue::ExternalLinkage,
5633 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005634 }
5635
5636 return GV;
5637}
5638
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005639llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5640 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005641 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005642
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005643 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005644 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005645 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005646 Entry =
5647 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005648 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005649 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005650 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005651 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005652 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005653 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005654 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005655 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005656 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005657
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005658 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005659}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005660
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005661llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005662CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005663 const ObjCInterfaceDecl *ID) {
5664 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005665
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005666 if (!Entry) {
5667 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5668 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005669 Entry =
5670 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005671 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005672 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005673 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005674 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005675 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005676 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005677 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005678 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005679 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005680
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005681 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005682}
5683
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005684/// EmitMetaClassRef - Return a Value * of the address of _class_t
5685/// meta-data
5686///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005687llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5688 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005689 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5690 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005691 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005692
Daniel Dunbar15894b72009-04-07 05:48:37 +00005693 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005694 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005695 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005696 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005697 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005698 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005699 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005700 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005701 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005702 ObjCTypes.ClassnfABIPtrTy));
5703
Daniel Dunbare60aa052009-04-15 19:03:14 +00005704 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005705 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005706
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005707 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005708}
5709
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005710/// GetClass - Return a reference to the class for the given interface
5711/// decl.
5712llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5713 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005714 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005715 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5716 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5717 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5718 }
5719
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005720 return EmitClassRef(Builder, ID);
5721}
5722
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005723/// Generates a message send where the super is the receiver. This is
5724/// a message send to self with special delivery semantics indicating
5725/// which class's method should be called.
5726CodeGen::RValue
5727CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005728 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005729 QualType ResultType,
5730 Selector Sel,
5731 const ObjCInterfaceDecl *Class,
5732 bool isCategoryImpl,
5733 llvm::Value *Receiver,
5734 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005735 const CodeGen::CallArgList &CallArgs,
5736 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005737 // ...
5738 // Create and init a super structure; this is a (receiver, class)
5739 // pair we will pass to objc_msgSendSuper.
5740 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00005741 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005742
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005743 llvm::Value *ReceiverAsObject =
5744 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5745 CGF.Builder.CreateStore(ReceiverAsObject,
5746 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005747
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005748 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005749 llvm::Value *Target;
5750 if (IsClassMessage) {
5751 if (isCategoryImpl) {
5752 // Message sent to "super' in a class method defined in
5753 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005754 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005755 Target = CGF.Builder.CreateStructGEP(Target, 0);
5756 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005757 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005758 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005759 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005760 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005761
Mike Stump18bb9282009-05-16 07:57:57 +00005762 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5763 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005764 const llvm::Type *ClassTy =
5765 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5766 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5767 CGF.Builder.CreateStore(Target,
5768 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005769
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005770 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005771 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5772 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005773 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005774 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005775 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005776 ObjCSuper, ObjCTypes.SuperPtrCTy,
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00005777 true, CallArgs, Method);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005778}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005779
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005780llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005781 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005782 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005783
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005784 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005785 llvm::Constant *Casted =
5786 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5787 ObjCTypes.SelectorPtrTy);
5788 Entry =
5789 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5790 llvm::GlobalValue::InternalLinkage,
5791 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005792 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005793 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005794 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005795
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005796 if (lval)
5797 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005798 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005799}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005800/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005801/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005802///
5803void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005804 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005805 llvm::Value *dst,
5806 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005807 const llvm::Type * SrcTy = src->getType();
5808 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005809 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005810 assert(Size <= 8 && "does not support size > 8");
5811 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5812 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005813 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5814 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005815 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5816 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005817 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5818 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005819 return;
5820}
5821
5822/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5823/// objc_assign_strongCast (id src, id *dst)
5824///
5825void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005826 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005827 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005828 const llvm::Type * SrcTy = src->getType();
5829 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005830 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005831 assert(Size <= 8 && "does not support size > 8");
5832 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005833 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005834 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5835 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005836 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5837 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005838 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005839 src, dst, "weakassign");
5840 return;
5841}
5842
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005843void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005844 CodeGen::CodeGenFunction &CGF,
5845 llvm::Value *DestPtr,
5846 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005847 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005848 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5849 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005850 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005851 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005852 return;
5853}
5854
Fariborz Jahanian06292952009-02-16 22:52:32 +00005855/// EmitObjCWeakRead - Code gen for loading value of a __weak
5856/// object: objc_read_weak (id *src)
5857///
5858llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005859 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005860 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005861 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005862 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5863 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005864 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005865 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005866 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005867 return read_weak;
5868}
5869
5870/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5871/// objc_assign_weak (id src, id *dst)
5872///
5873void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005874 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005875 const llvm::Type * SrcTy = src->getType();
5876 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005877 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005878 assert(Size <= 8 && "does not support size > 8");
5879 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5880 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005881 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5882 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005883 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5884 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005885 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005886 src, dst, "weakassign");
5887 return;
5888}
5889
5890/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5891/// objc_assign_global (id src, id *dst)
5892///
5893void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005894 llvm::Value *src, llvm::Value *dst,
5895 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005896 const llvm::Type * SrcTy = src->getType();
5897 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005898 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005899 assert(Size <= 8 && "does not support size > 8");
5900 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5901 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005902 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5903 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005904 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5905 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005906 if (!threadlocal)
5907 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5908 src, dst, "globalassign");
5909 else
5910 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5911 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005912 return;
5913}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005914
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005915void
John McCallbd309292010-07-06 01:34:17 +00005916CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5917 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005918 EmitAtSynchronizedStmt(CGF, S,
5919 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
5920 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00005921}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005922
John McCall2ca705e2010-07-24 00:37:23 +00005923llvm::Constant *
5924CGObjCNonFragileABIMac::GetEHType(QualType T) {
5925 // There's a particular fixed type info for 'id'.
5926 if (T->isObjCIdType() ||
5927 T->isObjCQualifiedIdType()) {
5928 llvm::Constant *IDEHType =
5929 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5930 if (!IDEHType)
5931 IDEHType =
5932 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5933 false,
5934 llvm::GlobalValue::ExternalLinkage,
5935 0, "OBJC_EHTYPE_id");
5936 return IDEHType;
5937 }
5938
5939 // All other types should be Objective-C interface pointer types.
5940 const ObjCObjectPointerType *PT =
5941 T->getAs<ObjCObjectPointerType>();
5942 assert(PT && "Invalid @catch type.");
5943 const ObjCInterfaceType *IT = PT->getInterfaceType();
5944 assert(IT && "Invalid @catch type.");
5945 return GetInterfaceEHType(IT->getDecl(), false);
5946}
5947
John McCallbd309292010-07-06 01:34:17 +00005948void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5949 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00005950 EmitTryCatchStmt(CGF, S,
5951 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
5952 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
5953 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005954}
5955
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005956/// EmitThrowStmt - Generate code for a throw statement.
5957void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5958 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005959 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall17afe452010-10-16 08:21:07 +00005960 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCalld5091822010-10-16 16:34:08 +00005961 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
5962 "tmp");
John McCall17afe452010-10-16 08:21:07 +00005963 llvm::Value *Args[] = { Exception };
5964 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
5965 Args, Args+1)
5966 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005967 } else {
John McCall17afe452010-10-16 08:21:07 +00005968 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
5969 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005970 }
5971
John McCall17afe452010-10-16 08:21:07 +00005972 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005973 CGF.Builder.ClearInsertionPoint();
5974}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005975
John McCall2ca705e2010-07-24 00:37:23 +00005976llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005977CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005978 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005979 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005980
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005981 // If we don't need a definition, return the entry if found or check
5982 // if we use an external reference.
5983 if (!ForDefinition) {
5984 if (Entry)
5985 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00005986
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005987 // If this type (or a super class) has the __objc_exception__
5988 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005989 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005990 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005991 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005992 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005993 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005994 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00005995 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005996 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005997
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005998 // Otherwise we need to either make a new entry or fill in the
5999 // initializer.
6000 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006001 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006002 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006003 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006004 CGM.getModule().getGlobalVariable(VTableName);
6005 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006006 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6007 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006008 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006009 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006010
Chris Lattner5e016ae2010-06-27 07:15:29 +00006011 llvm::Value *VTableIdx =
6012 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006013
6014 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00006015 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006016 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006017 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00006018 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006019 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006020
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006021 if (Entry) {
6022 Entry->setInitializer(Init);
6023 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006024 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006025 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006026 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006027 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006028 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006029 }
6030
John McCall457a04e2010-10-22 21:05:15 +00006031 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006032 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006033 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6034 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006035
6036 if (ForDefinition) {
6037 Entry->setSection("__DATA,__objc_const");
6038 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6039 } else {
6040 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6041 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006042
6043 return Entry;
6044}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006045
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006046/* *** */
6047
Daniel Dunbarb036db82008-08-13 03:21:16 +00006048CodeGen::CGObjCRuntime *
6049CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006050 if (CGM.getLangOptions().ObjCNonFragileABI)
6051 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006052 return new CGObjCMac(CGM);
6053}